1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/* $Id: Sound.cpp,v 1.29 2003/08/03 10:11:56 nan Exp $ */
// Copyright (C) 2000-2003 ศน(Kanna Yoshihiro)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "ttinc.h"
#include "Sound.h"
#include "Control.h"
#ifdef WIN32
#include <io.h>
#endif
extern long mode;
extern SDL_mutex *loadMutex;
Sound* Sound::m_theSound = NULL;
Sound::Sound() {
#ifdef HAVE_LIBSDL_MIXER
m_queuehead = m_queuetail = 0;
#else
m_soundMode = SOUND_NONE;
#endif
}
Sound::~Sound() {
#ifdef HAVE_LIBSDL_MIXER
for ( int i = 0 ; i < 16 ; i++ ) {
if ( m_sound[i] != 0 ) {
Mix_FreeChunk( m_sound[i] );
m_sound[i] = 0;
}
}
if ( m_opening != 0 ) {
Mix_FreeMusic( m_opening );
m_opening = 0;
}
Mix_CloseAudio();
#endif
}
Sound*
Sound::TheSound() {
if ( Sound::m_theSound )
return Sound::m_theSound;
else
return (Sound::m_theSound = new Sound());
}
bool
Sound::Init( long sndMode ) {
m_soundMode = sndMode;
#ifdef HAVE_LIBSDL_MIXER
for ( int i = 0 ; i < 16 ; i++ ) {
m_sound[i] = 0;
}
m_opening = 0;
#endif
if ( m_soundMode == SOUND_NONE )
return true;
#ifdef HAVE_LIBSDL_MIXER
#ifdef WIN32
if ( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 4096 ) < 0 ) {
#else
if ( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 128 ) < 0 ) {
#endif
perror( _("SDL Mix_OpenAudio failed\n") );
}
m_sound[SOUND_RACKET] = Mix_LoadWAV( "wav/racket.wav" );
m_sound[SOUND_TABLE] = Mix_LoadWAV( "wav/table.wav" );
m_sound[SOUND_CLICK] = Mix_LoadWAV( "wav/click.wav" );
m_sound[SOUND_LOVEALL] = Mix_LoadWAV( "wav/loveall.wav" );
m_sound[SOUND_ALL] = Mix_LoadWAV( "wav/all.wav" );
m_score[0] = Mix_LoadWAV( "wav/zero.wav" );
m_score[1] = Mix_LoadWAV( "wav/one.wav" );
m_score[2] = Mix_LoadWAV( "wav/two.wav" );
m_score[3] = Mix_LoadWAV( "wav/three.wav" );
m_score[4] = Mix_LoadWAV( "wav/four.wav" );
m_score[5] = Mix_LoadWAV( "wav/five.wav" );
m_score[6] = Mix_LoadWAV( "wav/six.wav" );
m_score[7] = Mix_LoadWAV( "wav/seven.wav" );
m_score[8] = Mix_LoadWAV( "wav/eight.wav" );
m_score[9] = Mix_LoadWAV( "wav/nine.wav" );
m_score[10] = Mix_LoadWAV( "wav/ten.wav" );
m_score[11] = Mix_LoadWAV( "wav/eleven.wav" );
m_score[12] = Mix_LoadWAV( "wav/twelve.wav" );
m_score[13] = Mix_LoadWAV( "wav/thirteen.wav" );
m_score[14] = Mix_LoadWAV( "wav/fourteen.wav" );
m_score[15] = Mix_LoadWAV( "wav/fifteen.wav" );
m_score[16] = Mix_LoadWAV( "wav/sixteen.wav" );
m_score[17] = Mix_LoadWAV( "wav/seventeen.wav" );
m_score[18] = Mix_LoadWAV( "wav/eighteen.wav" );
m_score[19] = Mix_LoadWAV( "wav/nineteen.wav" );
m_score[20] = Mix_LoadWAV( "wav/twenty.wav" );
m_score[30] = Mix_LoadWAV( "wav/thirty.wav" );
Mix_ChannelFinished( Sound::PlayFinishHandler );
#endif
return true;
}
void
Sound::Clear() {
#ifdef HAVE_LIBSDL_MIXER
for ( int i = 0 ; i < 16 ; i++ ) {
if ( m_sound[i] != 0 ) {
Mix_FreeChunk( m_sound[i] );
m_sound[i] = 0;
}
}
if ( m_opening != 0 ) {
Mix_FreeMusic( m_opening );
m_opening = 0;
}
Mix_ChannelFinished( NULL );
Mix_CloseAudio();
#endif
}
bool
Sound::Play( long soundID, double x, double y ) {
if ( m_soundMode == SOUND_NONE )
return true;
#ifdef HAVE_LIBSDL_MIXER
double srcX, srcY, srcZ;
double destX, destY, destZ;
double angle, destAngle;
Control::TheControl()->LookAt( srcX, srcY, srcZ, destX, destY, destZ );
destX -= srcX; destY -= srcY;
x -= srcX; y -= srcY;
destAngle = acos( destX/hypot(destX, destY) );
if ( destY < 0 )
destAngle += 3.14159265;
angle = acos( x/hypot(x, y) );
if ( y < 0 )
angle += 3.14159265;
angle -= destAngle;
Mix_SetPosition( 0, (int)(angle*180.0/3.14159265), hypot(x, y)*8 );
Mix_PlayChannel( 0, m_sound[soundID], 0 );
#endif
return true;
}
bool
Sound::PlayScore( long score1, long score2 ) {
if ( m_soundMode == SOUND_NONE )
return true;
#ifdef HAVE_LIBSDL_MIXER
double x = -TABLEWIDTH/2-0.5;
double y = 0;
double srcX, srcY, srcZ;
double destX, destY, destZ;
double angle, destAngle;
Control::TheControl()->LookAt( srcX, srcY, srcZ, destX, destY, destZ );
destX -= srcX; destY -= srcY;
x -= srcX; y -= srcY;
destAngle = acos( destX/hypot(destX, destY) );
if ( destY < 0 )
destAngle += 3.14159265;
angle = acos( x/hypot(x, y) );
if ( y < 0 )
angle += 3.14159265;
angle -= destAngle;
Mix_SetPosition( 0, (int)(angle*180.0/3.14159265), hypot(x, y)*8 );
PlayNumber( score1 );
if ( score1 == score2 )
PlayBlocking( 1, m_sound[SOUND_ALL] );
else
PlayNumber( score2 );
#endif
return true;
}
bool
Sound::PlayNumber( long number ) {
if ( m_soundMode == SOUND_NONE )
return true;
#ifdef HAVE_LIBSDL_MIXER
number %= 40;
if ( number > 30 ) {
PlayBlocking( 1, m_score[30] );
PlayBlocking( 1, m_score[number-30] );
} else if ( number > 20 ) {
PlayBlocking( 1, m_score[20] );
PlayBlocking( 1, m_score[number-20] );
} else
PlayBlocking( 1, m_score[number] );
#endif
return true;
}
/* Play the sound on blocking mode */
bool
Sound::PlayBlocking( int channel, Mix_Chunk *chunk ) {
if ( !Mix_Playing( channel ) ) {
Mix_PlayChannel( channel, chunk, 0 );
} else {
m_chunkqueue[m_queuetail%SOUND_QUEUESIZE] = chunk;
m_queuetail++;
}
return true;
}
void
Sound::PlayFinishHandler( int channel ) {
if ( channel != 1 )
return;
if ( Sound::TheSound()->m_queuehead != Sound::TheSound()->m_queuetail ) {
Mix_PlayChannel( channel,
Sound::TheSound()->
m_chunkqueue[Sound::TheSound()->
m_queuehead%SOUND_QUEUESIZE],
0 );
Sound::TheSound()->m_queuehead++;
}
}
long
Sound::GetSoundMode() {
return m_soundMode;
}
long
Sound::InitBGM( char *filename ) {
if ( m_soundMode == SOUND_NONE )
return true;
#ifdef HAVE_LIBSDL_MIXER
m_opening = Mix_LoadMUS( OPENINGFILENAME );
if ( m_opening == 0 )
printf( "%s\n", SDL_GetError() );
#endif
return 0;
}
// It is better to move it to other thread
long
Sound::PlayBGM() {
if ( m_soundMode == SOUND_NONE )
return true;
#ifdef HAVE_LIBSDL_MIXER
Mix_PlayMusic( m_opening, 1 );
#endif
return 0;
}
long
Sound::StopBGM() {
if ( m_soundMode == SOUND_NONE )
return true;
#ifdef HAVE_LIBSDL_MIXER
Mix_FadeOutMusic( 2000 );
#endif
return 0;
}
|