Updated to work with freeglut 2.4.0
[crack-attack.git] / src / Music.cxx
blob7e4baa7ce82d010d1ee30fef995f059a8c93837a
1 /*
2 * Music.cxx
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"
29 #include "Random.h"
30 #include "Sound.h"
31 #include "Music.h"
33 Mix_Music *music = NULL;
34 int music_available = 0;
35 int keep_playing = 0;
36 int current_track = 0;
38 vector<string> music_filelist;
39 char *music_filename;
40 string music_dirname;
41 dirent *music_dirent;
42 DIR *music_dir;
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;
56 return;
58 #ifndef NDEBUG
59 cout << "Music dir: " << music_dirname.c_str() << endl;
60 #endif
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 ) );
66 #ifndef NDEBUG
67 cout << "Added music file: " << music_filename << endl;
68 #endif
69 break;
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 ) ) )
76 continue;
77 music_filelist.push_back( music_dirname + string( music_filename ) );
78 #ifndef NDEBUG
79 cout << "Added music file: " << music_filename << endl;
80 #endif
82 closedir( music_dir );
83 music_available = music_filelist.size();
86 void Music::play( )
88 if ( !music_available ) return;
89 current_track = Random::number( music_filelist.size() );
90 #ifndef NDEBUG
91 cout << "Playing " << music_filelist[current_track].c_str() << endl;
92 #endif
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);
97 keep_playing = 1;
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;
125 #ifndef NDEBUG
126 cout << "Playing " << Track.c_str() << endl;
127 #endif
128 music = Mix_LoadMUS( Track.c_str() );
129 Mix_VolumeMusic( MIX_MAX_VOLUME / 4 );
130 Mix_PlayMusic( music , 0 );
131 keep_playing = 0;
135 void Music::finished( )
137 Mix_HaltMusic();
138 if ( keep_playing ) Music::play();
141 void Music::pause( )
143 if ( !Mix_PlayingMusic() ) return;
144 Mix_PauseMusic();
147 void Music::resume( )
149 if ( !Mix_PlayingMusic() ) return;
150 Mix_ResumeMusic();
153 void Music::stop( )
155 if ( !Mix_PlayingMusic() ) return;
156 keep_playing = 0;
157 Mix_HaltMusic();
160 void Music::fadeout( int ms )
162 if ( !Mix_PlayingMusic() ) return;
163 keep_playing = 0;
164 Mix_FadeOutMusic(ms);
167 void Music::cleanup ( )
169 Mix_FreeMusic( music );
170 music = NULL;