Fixes for datatype size on amd64.
[crack-attack.git] / src / Music.cxx
blob3f0d8cef0cb6bc0ba6ac07ca684fb91097271e4f
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 "Music.h"
30 #include "TextureLoader.h"
31 #include "Random.h"
32 #include "Sound.h"
34 Mix_Music *music = NULL;
35 int music_available = 0;
36 int keep_playing = 0;
37 int current_track = 0;
39 vector<string> music_filelist;
40 char *music_filename;
41 string music_dirname;
42 dirent *music_dirent;
43 DIR *music_dir;
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;
57 return;
59 #ifndef NDEBUG
60 cout << "Music dir: " << music_dirname.c_str() << endl;
61 #endif
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 ) );
67 #ifndef NDEBUG
68 cout << "Added music file: " << music_filename << endl;
69 #endif
70 break;
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 ) ) )
77 continue;
78 music_filelist.push_back( music_dirname + string( music_filename ) );
79 #ifndef NDEBUG
80 cout << "Added music file: " << music_filename << endl;
81 #endif
83 closedir( music_dir );
84 music_available = music_filelist.size();
87 void Music::play( )
89 if ( !music_available ) return;
90 current_track = Random::number( music_filelist.size() );
91 #ifndef NDEBUG
92 cout << "Playing " << music_filelist[current_track].c_str() << endl;
93 #endif
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);
98 keep_playing = 1;
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;
126 #ifndef NDEBUG
127 cout << "Playing " << Track.c_str() << endl;
128 #endif
129 music = Mix_LoadMUS( Track.c_str() );
130 Mix_VolumeMusic( MIX_MAX_VOLUME / 4 );
131 Mix_PlayMusic( music , 0 );
132 keep_playing = 0;
136 void Music::finished( )
138 Mix_HaltMusic();
139 if ( keep_playing ) Music::play();
142 void Music::pause( )
144 if ( !Mix_PlayingMusic() ) return;
145 Mix_PauseMusic();
148 void Music::resume( )
150 if ( !Mix_PlayingMusic() ) return;
151 Mix_ResumeMusic();
154 void Music::stop( )
156 if ( !Mix_PlayingMusic() ) return;
157 keep_playing = 0;
158 Mix_HaltMusic();
161 void Music::fadeout( int ms )
163 if ( !Mix_PlayingMusic() ) return;
164 keep_playing = 0;
165 Mix_FadeOutMusic(ms);
168 void Music::cleanup ( )
170 Mix_FreeMusic( music );
171 music = NULL;