3 * Miguel Ángel Vilela García - 8/29/03
5 * Copyright (C) 2003 Miguel Ángel Vilela García
6 * Copyright (C) 2005 See COPYRIGHT
7 * Crack Attack! is the legal property of its developers, whose names
8 * are too numerous to list here. Please refer to the COPYRIGHT file
9 * distributed with this source distribution for a full listing.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Miguel Ángel Vilela García - www.miguev.net
28 #include "TextureLoader.h"
33 Mix_Music
*music
= NULL
;
34 int music_available
= 0;
36 int current_track
= 0;
38 vector
<string
> music_filelist
;
44 void Music::initialize ( )
46 if (!Sound::audio_available()) return;
47 char tmp_music_dirname
[256];
48 TextureLoader::buildLocalDataFileName("music/", tmp_music_dirname
);
49 music_dirname
= string( tmp_music_dirname
);
50 if ( (music_dir
= opendir( music_dirname
.c_str() ) ) == NULL
) {
51 char *another_dir
= GC_DATA_DIRECTORY("music/");
52 music_dirname
= string( another_dir
);
54 if ( (music_dir
= opendir( music_dirname
.c_str() ) ) == NULL
) {
55 cout
<< "WARNING *** Unable to open music directory!" << endl
;
59 cout
<< "Music dir: " << music_dirname
.c_str() << endl
;
61 while ( music_dirent
= readdir ( music_dir
) ) {
62 music_filename
= music_dirent
->d_name
;
63 if ( ( music_filename
== string( GC_MUSIC_GAME_TRACK
) ) ) {
64 music_filelist
.clear();
65 music_filelist
.push_back( music_dirname
+ string( music_filename
) );
67 cout
<< "Added music file: " << music_filename
<< endl
;
71 if ( ( music_filename
== string( "." ) )
72 || ( music_filename
== string( ".." ) )
73 || ( music_filename
== string( GC_MUSIC_PRELUDE_TRACK
) )
74 || ( music_filename
== string( GC_MUSIC_GAMEOVER_TRACK
) )
75 || ( music_filename
== string( GC_MUSIC_YOUWIN_TRACK
) ) )
77 music_filelist
.push_back( music_dirname
+ string( music_filename
) );
79 cout
<< "Added music file: " << music_filename
<< endl
;
82 closedir( music_dir
);
83 music_available
= music_filelist
.size();
88 if ( !music_available
) return;
89 current_track
= Random::number( music_filelist
.size() );
91 cout
<< "Playing " << music_filelist
[current_track
].c_str() << endl
;
93 music
= Mix_LoadMUS( music_filelist
[current_track
].c_str() );
94 Mix_VolumeMusic( MIX_MAX_VOLUME
/ 4 );
95 Mix_PlayMusic( music
, 0 );
96 Mix_HookMusicFinished(Music::finished
);
100 void Music::play_prelude( )
102 Music::play_track( GC_MUSIC_PRELUDE_TRACK
);
105 void Music::play_game( )
107 Music::play_track( GC_MUSIC_GAME_TRACK
);
110 void Music::play_gameover( )
112 Music::play_track( GC_MUSIC_GAMEOVER_TRACK
);
115 void Music::play_youwin( )
117 Music::play_track( GC_MUSIC_YOUWIN_TRACK
);
120 void Music::play_track( char *track
)
122 string Track
= string( music_dirname
.c_str() );
123 Track
.append( string( track
) );
124 if ( !music_available
) return;
126 cout
<< "Playing " << Track
.c_str() << endl
;
128 music
= Mix_LoadMUS( Track
.c_str() );
129 Mix_VolumeMusic( MIX_MAX_VOLUME
/ 4 );
130 Mix_PlayMusic( music
, 0 );
135 void Music::finished( )
138 if ( keep_playing
) Music::play();
143 if ( !Mix_PlayingMusic() ) return;
147 void Music::resume( )
149 if ( !Mix_PlayingMusic() ) return;
155 if ( !Mix_PlayingMusic() ) return;
160 void Music::fadeout( int ms
)
162 if ( !Mix_PlayingMusic() ) return;
164 Mix_FadeOutMusic(ms
);
167 void Music::cleanup ( )
169 Mix_FreeMusic( music
);