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
30 #include "TextureLoader.h"
34 Mix_Music
*music
= NULL
;
35 int music_available
= 0;
37 int current_track
= 0;
39 vector
<string
> music_filelist
;
45 void Music::initialize ( )
47 if (!Sound::audio_available()) return;
48 char tmp_music_dirname
[256];
49 TextureLoader::buildLocalDataFileName("music/", tmp_music_dirname
);
50 music_dirname
= string( tmp_music_dirname
);
51 if ( (music_dir
= opendir( music_dirname
.c_str() ) ) == NULL
) {
52 char *another_dir
= GC_DATA_DIRECTORY("music/");
53 music_dirname
= string( another_dir
);
55 if ( (music_dir
= opendir( music_dirname
.c_str() ) ) == NULL
) {
56 cout
<< "WARNING *** Unable to open music directory!" << endl
;
60 cout
<< "Music dir: " << music_dirname
.c_str() << endl
;
62 while ( music_dirent
= readdir ( music_dir
) ) {
63 music_filename
= music_dirent
->d_name
;
64 if ( ( music_filename
== string( GC_MUSIC_GAME_TRACK
) ) ) {
65 music_filelist
.clear();
66 music_filelist
.push_back( music_dirname
+ string( music_filename
) );
68 cout
<< "Added music file: " << music_filename
<< endl
;
72 if ( ( music_filename
== string( "." ) )
73 || ( music_filename
== string( ".." ) )
74 || ( music_filename
== string( GC_MUSIC_PRELUDE_TRACK
) )
75 || ( music_filename
== string( GC_MUSIC_GAMEOVER_TRACK
) )
76 || ( music_filename
== string( GC_MUSIC_YOUWIN_TRACK
) ) )
78 music_filelist
.push_back( music_dirname
+ string( music_filename
) );
80 cout
<< "Added music file: " << music_filename
<< endl
;
83 closedir( music_dir
);
84 music_available
= music_filelist
.size();
89 if ( !music_available
) return;
90 current_track
= Random::number( music_filelist
.size() );
92 cout
<< "Playing " << music_filelist
[current_track
].c_str() << endl
;
94 music
= Mix_LoadMUS( music_filelist
[current_track
].c_str() );
95 Mix_VolumeMusic( MIX_MAX_VOLUME
/ 4 );
96 Mix_PlayMusic( music
, 0 );
97 Mix_HookMusicFinished(Music::finished
);
101 void Music::play_prelude( )
103 Music::play_track( GC_MUSIC_PRELUDE_TRACK
);
106 void Music::play_game( )
108 Music::play_track( GC_MUSIC_GAME_TRACK
);
111 void Music::play_gameover( )
113 Music::play_track( GC_MUSIC_GAMEOVER_TRACK
);
116 void Music::play_youwin( )
118 Music::play_track( GC_MUSIC_YOUWIN_TRACK
);
121 void Music::play_track( char *track
)
123 string Track
= string( music_dirname
.c_str() );
124 Track
.append( string( track
) );
125 if ( !music_available
) return;
127 cout
<< "Playing " << Track
.c_str() << endl
;
129 music
= Mix_LoadMUS( Track
.c_str() );
130 Mix_VolumeMusic( MIX_MAX_VOLUME
/ 4 );
131 Mix_PlayMusic( music
, 0 );
136 void Music::finished( )
139 if ( keep_playing
) Music::play();
144 if ( !Mix_PlayingMusic() ) return;
148 void Music::resume( )
150 if ( !Mix_PlayingMusic() ) return;
156 if ( !Mix_PlayingMusic() ) return;
161 void Music::fadeout( int ms
)
163 if ( !Mix_PlayingMusic() ) return;
165 Mix_FadeOutMusic(ms
);
168 void Music::cleanup ( )
170 Mix_FreeMusic( music
);