From 6da2f0d84e983303aa863d25fb8b16c06a5bf270 Mon Sep 17 00:00:00 2001 From: pelya Date: Tue, 8 Dec 2009 19:30:20 +0200 Subject: [PATCH] Audio working, yay! --- .../project/jni/alienblaster/src/enemy.cc | 24 +++++----- .../project/jni/alienblaster/src/explosion.cc | 4 +- .../project/jni/alienblaster/src/game.cc | 26 +++++------ .../jni/alienblaster/src/infoscreen.cc | 14 +++--- .../project/jni/alienblaster/src/intro.cc | 22 ++++----- .../jni/alienblaster/src/menuArcadeMode.cc | 18 ++++---- .../project/jni/alienblaster/src/mixer.cc | 9 +++- .../project/jni/alienblaster/src/mixer.h | 4 +- .../project/jni/alienblaster/src/racer.cc | 46 +++++++++---------- .../jni/alienblaster/src/setDifficulty.cc | 14 +++--- .../project/jni/alienblaster/src/settings.cc | 4 +- .../sdl/src/audio/android/SDL_androidaudio.c | 32 ++++++++++++- alienblaster/project/src/DemoActivity.java | 20 +++++++- alienblaster/readme.txt | 3 +- 14 files changed, 145 insertions(+), 95 deletions(-) diff --git a/alienblaster/project/jni/alienblaster/src/enemy.cc b/alienblaster/project/jni/alienblaster/src/enemy.cc index 7a75a819f..fea64969e 100644 --- a/alienblaster/project/jni/alienblaster/src/enemy.cc +++ b/alienblaster/project/jni/alienblaster/src/enemy.cc @@ -131,8 +131,8 @@ Enemy::Enemy( Vector2D pos, Vector2D vel, EnemyTypes whichEnemyType, nextShotPrimary = rand() % (ENEMY_RAND_WAIT_PRIMARY[ enemyType ]+1); nextShotSecondary = rand() % (ENEMY_RAND_WAIT_SECONDARY[ enemyType ]+1); - sndShotPrimary = mixer.loadSample( FN_SOUND_SHOT_PRIMARY ); - sndShotSecondary = mixer.loadSample( FN_SOUND_SHOT_SECONDARY ); + sndShotPrimary = Mixer::mixer().loadSample( FN_SOUND_SHOT_PRIMARY ); + sndShotSecondary = Mixer::mixer().loadSample( FN_SOUND_SHOT_SECONDARY ); } Enemy::~Enemy() { @@ -258,7 +258,7 @@ void Enemy::firePrimary() { pos + Vector2D( 0, spriteEnemy->h / 2 ), 90 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); break; } case BOMBER: @@ -273,7 +273,7 @@ void Enemy::firePrimary() { pos + Vector2D( +7, spriteEnemy->h / 2 ), 80 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); break; } case TANK: @@ -283,7 +283,7 @@ void Enemy::firePrimary() { pos, (rand() % 360) - 180 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); break; } case BOSS_1_MAIN_GUN: @@ -293,7 +293,7 @@ void Enemy::firePrimary() { pos, (rand() % 20) + 80 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); break; } case BOSS_1_SHOT_BATTERY_LEFT: @@ -303,7 +303,7 @@ void Enemy::firePrimary() { pos, (rand() % 120) + 30 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); break; } case BOSS_1_SHOT_BATTERY_RIGHT: @@ -313,7 +313,7 @@ void Enemy::firePrimary() { pos, (rand() % 120) + 30 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); break; } case BOSS_1_ROCKET_LAUNCHER: @@ -323,7 +323,7 @@ void Enemy::firePrimary() { Shot *shot = new Shot( ENEMY_SHOT_TANK_ROCKET, 666, pos, angle ); shots->addShot( shot ); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); break; } case BOSS_2: @@ -332,7 +332,7 @@ void Enemy::firePrimary() { float angle = (racers->getRacer( racerIdx )->getPos() - pos).getDirection(); Shot *shot = new Shot( ENEMY_SHOT_NORMAL, 666, pos, angle ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); break; } default: @@ -363,7 +363,7 @@ void Enemy::fireSecondary() { Shot *shot = new Shot( ENEMY_SHOT_TANK_ROCKET, 666, pos, angle ); shots->addShot( shot ); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); break; } case BOSS_2: @@ -374,7 +374,7 @@ void Enemy::fireSecondary() { shots->addShot( shot ); shot = new Shot( ENEMY_SHOT_TANK_ROCKET, 666, pos - Vector2D(+80,0), angle ); shots->addShot( shot ); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); break; } default: diff --git a/alienblaster/project/jni/alienblaster/src/explosion.cc b/alienblaster/project/jni/alienblaster/src/explosion.cc index 7814de017..14b6750d0 100644 --- a/alienblaster/project/jni/alienblaster/src/explosion.cc +++ b/alienblaster/project/jni/alienblaster/src/explosion.cc @@ -31,8 +31,8 @@ Explosion::Explosion(string fn, const Vector2D &position, nrAnimStages = sprite->w / sprite->h; expired = false; - sndExplosion = mixer.loadSample( FN_SOUND_EXPLOSION_NORMAL ); - mixer.playSample( sndExplosion, 0 ); + sndExplosion = Mixer::mixer().loadSample( FN_SOUND_EXPLOSION_NORMAL ); + Mixer::mixer().playSample( sndExplosion, 0 ); this->explosionType = explosionType; pos = position; diff --git a/alienblaster/project/jni/alienblaster/src/game.cc b/alienblaster/project/jni/alienblaster/src/game.cc index a75a531b9..54c72b129 100644 --- a/alienblaster/project/jni/alienblaster/src/game.cc +++ b/alienblaster/project/jni/alienblaster/src/game.cc @@ -23,7 +23,7 @@ using namespace std; #include #include #include "SDL.h" -#include "SDL_mixer.h" +#include "mixer.h" #include "game.h" #include "surfaceDB.h" #include "racers.h" @@ -88,7 +88,7 @@ Game::Game() { nukeEffectSurface = surfaceDB.loadSurface( FN_NUKE_EFFECT ); - bossAlarm = mixer.loadSample( FN_SOUND_BOSS_ALARM, 60 ); + bossAlarm = Mixer::mixer().loadSample( FN_SOUND_BOSS_ALARM, 60 ); fontTime = new Font( FN_FONT_NUMBERS_TIME ); fontSizeTime = fontTime->getCharWidth(); @@ -234,7 +234,7 @@ void Game::initNewGame() { paused = true; bossTime = false; bossNukeEffect = false; - bossExplosion = mixer.loadSample( FN_SOUND_EXPLOSION_BOSS ); + bossExplosion = Mixer::mixer().loadSample( FN_SOUND_EXPLOSION_BOSS ); minibossAlreadyKilled = false; minibossTime = false; @@ -300,7 +300,7 @@ void Game::run(){ case GS_PLAYON: { initNewGame(); - if ( playMusicOn ) mixer.playMusic( MUSIC_PLAYON, -1, 1000 ); + if ( playMusicOn ) Mixer::mixer().playMusic( MUSIC_PLAYON, -1, 1000 ); playOn(); break; } @@ -339,8 +339,8 @@ void Game::playOn() { (unsigned int)GAME_LENGTH < gameActRuntime) { enemys->bossTime(1); // generates the boss bossTime = true; - mixer.playSample( bossAlarm , 0, true ); - if ( playMusicOn ) mixer.playMusic( MUSIC_BOSS1, -1, 0 ); + Mixer::mixer().playSample( bossAlarm , 0, true ); + if ( playMusicOn ) Mixer::mixer().playMusic( MUSIC_BOSS1, -1, 0 ); } if ( bossTime && enemys->bossDead() ) gameState = GS_BOSS_KILLED; if ( racers->bothPlayersLost() ) gameState = GS_ROUNDFINISHED; @@ -405,11 +405,11 @@ void Game::handleEventsPlayOn() { case SDLK_F7: { if ( playMusicOn ) { playMusicOn = false; - mixer.stopMusic(); + Mixer::mixer().stopMusic(); } else { playMusicOn = true; - if ( bossTime ) mixer.playMusic( MUSIC_BOSS1, -1, 1000 ); - else mixer.playMusic( MUSIC_PLAYON, -1, 1000 ); + if ( bossTime ) Mixer::mixer().playMusic( MUSIC_BOSS1, -1, 1000 ); + else Mixer::mixer().playMusic( MUSIC_PLAYON, -1, 1000 ); } break; } @@ -781,14 +781,14 @@ void Game::generateMiniboss() { scrollingOn = false; minibossTime = true; enemys->bossTime(2); // generates the miniboss - mixer.playSample( bossAlarm , 0, true ); - if ( playMusicOn ) mixer.playMusic( MUSIC_BOSS1, -1, 0 ); + Mixer::mixer().playSample( bossAlarm , 0, true ); + if ( playMusicOn ) Mixer::mixer().playMusic( MUSIC_BOSS1, -1, 0 ); } void Game::minibossKilled() { scrollingOn = true; minibossTime = false; - if ( playMusicOn ) mixer.playMusic( MUSIC_PLAYON, -1, 0 ); + if ( playMusicOn ) Mixer::mixer().playMusic( MUSIC_PLAYON, -1, 0 ); } @@ -818,7 +818,7 @@ void Game::bossKilled() { else if ( (actualTime - startOfBossExplosion) < (BOSS_EXPLOSION_DURATION + NUKE_EFFECT_DURATION) ) { // nuke effect if ( !bossExplosionSound ) { - mixer.playSample( bossExplosion, 0, true ); + Mixer::mixer().playSample( bossExplosion, 0, true ); bossExplosionSound = false; enemys->doNukeDamage(); enemys->deleteExpiredEnemys(); diff --git a/alienblaster/project/jni/alienblaster/src/infoscreen.cc b/alienblaster/project/jni/alienblaster/src/infoscreen.cc index d37e6bf3b..9ef3c081b 100644 --- a/alienblaster/project/jni/alienblaster/src/infoscreen.cc +++ b/alienblaster/project/jni/alienblaster/src/infoscreen.cc @@ -39,8 +39,8 @@ Infoscreen::Infoscreen( SDL_Surface *scr ) { lightFighterIcon2 = surfaceDB.loadSurface( FN_LIGHT_FIGHTER_2_ICON ); heavyFighterIcon1 = surfaceDB.loadSurface( FN_HEAVY_FIGHTER_1_ICON ); heavyFighterIcon2 = surfaceDB.loadSurface( FN_HEAVY_FIGHTER_2_ICON ); - choose = mixer.loadSample( FN_SOUND_INTRO_CHOOSE, 100 ); - confirm = mixer.loadSample( FN_SOUND_INTRO_CONFIRM, 100 ); + choose = Mixer::mixer().loadSample( FN_SOUND_INTRO_CHOOSE, 100 ); + confirm = Mixer::mixer().loadSample( FN_SOUND_INTRO_CONFIRM, 100 ); if (infoscreenItems) delete infoscreenItems; infoscreenItems = new Items(); activeChoice = 0; @@ -344,22 +344,22 @@ void Infoscreen::handleEvents() { case SDLK_F7: { if ( playMusicOn ) { playMusicOn = false; - mixer.stopMusic(); + Mixer::mixer().stopMusic(); } else { playMusicOn = true; - mixer.playMusic( MUSIC_INTRO, -1, 1000 ); + Mixer::mixer().playMusic( MUSIC_INTRO, -1, 1000 ); } break; } case SDLK_UP: { - mixer.playSample( choose, 0 ); + Mixer::mixer().playSample( choose, 0 ); infoscreenItems->deleteAllItems(); activeChoice--; if ( activeChoice < 0 ) activeChoice = NR_INFOSCREEN_CHOICES - 1; break; } case SDLK_DOWN: { - mixer.playSample( choose, 0 ); + Mixer::mixer().playSample( choose, 0 ); infoscreenItems->deleteAllItems(); activeChoice = (activeChoice + 1) % NR_INFOSCREEN_CHOICES; break; @@ -369,7 +369,7 @@ void Infoscreen::handleEvents() { break; } case SDLK_RETURN: { - mixer.playSample( confirm, 0 ); + Mixer::mixer().playSample( confirm, 0 ); switch (activeChoice) { case INFO_BACK_TO_MAIN_MENU: { quitInfoscreen = true; diff --git a/alienblaster/project/jni/alienblaster/src/intro.cc b/alienblaster/project/jni/alienblaster/src/intro.cc index 542034aa1..bd9467030 100644 --- a/alienblaster/project/jni/alienblaster/src/intro.cc +++ b/alienblaster/project/jni/alienblaster/src/intro.cc @@ -35,8 +35,8 @@ Intro::Intro( SDL_Surface *scr ) { font = new Font( FN_FONT_INTRO ); fontHighlighted = new Font( FN_FONT_INTRO_HIGHLIGHTED ); activeChoice = 0; - choose = mixer.loadSample( FN_SOUND_INTRO_CHOOSE, 100 ); - confirm = mixer.loadSample( FN_SOUND_INTRO_CONFIRM, 100 ); + choose = Mixer::mixer().loadSample( FN_SOUND_INTRO_CHOOSE, 100 ); + confirm = Mixer::mixer().loadSample( FN_SOUND_INTRO_CONFIRM, 100 ); infoscreen = new Infoscreen( screen ); } @@ -44,8 +44,8 @@ Intro::~Intro() {} void Intro::run( GameStates &gameState ) { - if ( playMusicOn && mixer.whichMusicPlaying() != MUSIC_INTRO ) { - mixer.playMusic( MUSIC_INTRO, -1, 1000 ); + if ( playMusicOn && Mixer::mixer().whichMusicPlaying() != MUSIC_INTRO ) { + Mixer::mixer().playMusic( MUSIC_INTRO, -1, 1000 ); } draw(); @@ -94,21 +94,21 @@ void Intro::handleEvents( GameStates &gameState ) { case SDLK_F7: { if ( playMusicOn ) { playMusicOn = false; - mixer.stopMusic(); + Mixer::mixer().stopMusic(); } else { playMusicOn = true; - mixer.playMusic( MUSIC_INTRO, -1, 1000 ); + Mixer::mixer().playMusic( MUSIC_INTRO, -1, 1000 ); } break; } case SDLK_UP: { - mixer.playSample( choose, 0 ); + Mixer::mixer().playSample( choose, 0 ); activeChoice--; if ( activeChoice < 0 ) activeChoice = NR_INTRO_CHOICES - 1; break; } case SDLK_DOWN: { - mixer.playSample( choose, 0 ); + Mixer::mixer().playSample( choose, 0 ); activeChoice = (activeChoice + 1) % NR_INTRO_CHOICES; break; } @@ -117,7 +117,7 @@ void Intro::handleEvents( GameStates &gameState ) { break; } case SDLK_RETURN: { - mixer.playSample( confirm, 0 ); + Mixer::mixer().playSample( confirm, 0 ); switch (activeChoice) { case ONE_PLAYER_GAME: { onePlayerGame = true; @@ -168,8 +168,8 @@ void Intro::handleEvents( GameStates &gameState ) { void Intro::showScreenshots() { - if ( playMusicOn && mixer.whichMusicPlaying() != MUSIC_INTRO ) { - mixer.playMusic( MUSIC_INTRO, -1, 1000 ); + if ( playMusicOn && Mixer::mixer().whichMusicPlaying() != MUSIC_INTRO ) { + Mixer::mixer().playMusic( MUSIC_INTRO, -1, 1000 ); } SDL_Surface *surfS = SDL_LoadBMP( FN_ALIENBLASTER_INTRO.c_str() ); diff --git a/alienblaster/project/jni/alienblaster/src/menuArcadeMode.cc b/alienblaster/project/jni/alienblaster/src/menuArcadeMode.cc index f139dd972..cdbd7d894 100644 --- a/alienblaster/project/jni/alienblaster/src/menuArcadeMode.cc +++ b/alienblaster/project/jni/alienblaster/src/menuArcadeMode.cc @@ -41,8 +41,8 @@ MenuArcadeMode::MenuArcadeMode( SDL_Surface *scr ) { fontHighlighted = new Font( FN_FONT_INTRO_HIGHLIGHTED ); lightFighterIcon1 = surfaceDB.loadSurface( FN_LIGHT_FIGHTER_1_ICON ); heavyFighterIcon1 = surfaceDB.loadSurface( FN_HEAVY_FIGHTER_1_ICON ); - choose = mixer.loadSample( FN_SOUND_ARCADE_CHOOSE, 100 ); - confirm = mixer.loadSample( FN_SOUND_ARCADE_CONFIRM, 60 ); + choose = Mixer::mixer().loadSample( FN_SOUND_ARCADE_CHOOSE, 100 ); + confirm = Mixer::mixer().loadSample( FN_SOUND_ARCADE_CONFIRM, 60 ); activeChoice = 0; playerOneLightFighter = true; @@ -175,21 +175,21 @@ void MenuArcadeMode::handleEvents( GameStates &gameState ) { case SDLK_F7: { if ( playMusicOn ) { playMusicOn = false; - mixer.stopMusic(); + Mixer::mixer().stopMusic(); } else { playMusicOn = true; - mixer.playMusic( MUSIC_INTRO, -1, 1000 ); + Mixer::mixer().playMusic( MUSIC_INTRO, -1, 1000 ); } break; } case SDLK_UP: { - mixer.playSample( choose, 0 ); + Mixer::mixer().playSample( choose, 0 ); activeChoice--; if ( activeChoice < 0 ) activeChoice = NR_MENU_ARCADE_CHOICES - 1; break; } case SDLK_DOWN: { - mixer.playSample( choose, 0 ); + Mixer::mixer().playSample( choose, 0 ); activeChoice = (activeChoice + 1) % NR_MENU_ARCADE_CHOICES; break; } @@ -200,7 +200,7 @@ void MenuArcadeMode::handleEvents( GameStates &gameState ) { case SDLK_RETURN: { switch (activeChoice) { case ARCADE_FIGHT: { - mixer.playSample( confirm, 0 ); + Mixer::mixer().playSample( confirm, 0 ); difficultyLevel = ARCADE_DIFFICULTY_LEVEL; gameState = GS_PLAYON; break; @@ -312,10 +312,10 @@ bool MenuArcadeMode::handleEventsReadName( string &newName ) { { if ( playMusicOn ) { playMusicOn = false; - mixer.stopMusic(); + Mixer::mixer().stopMusic(); } else { playMusicOn = true; - mixer.playMusic( MUSIC_INTRO, -1, 1000 ); + Mixer::mixer().playMusic( MUSIC_INTRO, -1, 1000 ); } break; } diff --git a/alienblaster/project/jni/alienblaster/src/mixer.cc b/alienblaster/project/jni/alienblaster/src/mixer.cc index f1cd6015c..8f205dcdd 100644 --- a/alienblaster/project/jni/alienblaster/src/mixer.cc +++ b/alienblaster/project/jni/alienblaster/src/mixer.cc @@ -25,7 +25,14 @@ using namespace std; #include #include -Mixer mixer; +Mixer * mixerInstance = NULL; + +Mixer & Mixer::mixer() +{ + if( mixerInstance == NULL ) + mixerInstance = new Mixer(); + return *mixerInstance; +} Mixer::Mixer() { if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { diff --git a/alienblaster/project/jni/alienblaster/src/mixer.h b/alienblaster/project/jni/alienblaster/src/mixer.h index 4970dfa5d..61a52a737 100644 --- a/alienblaster/project/jni/alienblaster/src/mixer.h +++ b/alienblaster/project/jni/alienblaster/src/mixer.h @@ -32,8 +32,6 @@ typedef vector Musics; class Mixer; -extern Mixer mixer; - const int MIXER_NUMBER_CHANNELS = 32; // reserved for samples, which should play with priority const int MIXER_RESERVED_CHANNELS = 8; @@ -100,7 +98,7 @@ public: MusicTracks whichMusicPlaying(); - static const Mixer mixer(); + static Mixer & mixer(); }; #endif //#define MIXER_HH diff --git a/alienblaster/project/jni/alienblaster/src/racer.cc b/alienblaster/project/jni/alienblaster/src/racer.cc index c94f29303..76cf19dc7 100644 --- a/alienblaster/project/jni/alienblaster/src/racer.cc +++ b/alienblaster/project/jni/alienblaster/src/racer.cc @@ -141,8 +141,8 @@ Racer::Racer( string fnSprite, int whichPlayer, Vector2D startpos , int newShipT } fontSize = font->getCharWidth(); - sndShotPrimary = mixer.loadSample( FN_SOUND_SHOT_PRIMARY ); - sndShotSecondary = mixer.loadSample( FN_SOUND_SHOT_SECONDARY ); + sndShotPrimary = Mixer::mixer().loadSample( FN_SOUND_SHOT_PRIMARY ); + sndShotSecondary = Mixer::mixer().loadSample( FN_SOUND_SHOT_SECONDARY ); spriteSpecials = surfaceDB.loadSurface( FN_ICONS_SPECIALS ); spriteSecondaryWeapons = surfaceDB.loadSurface( FN_ICONS_SECONDARY_WEAPONS ); @@ -426,7 +426,7 @@ void Racer::shootPrimary() { -90 + (rand() % SPREAD_ANGLE_SHOT_NORMAL) - SPREAD_ANGLE_SHOT_NORMAL / 2 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -438,7 +438,7 @@ void Racer::shootPrimary() { new Shot( SHOT_NORMAL_HEAVY, playerNr, pos + Vector2D(0, -spriteRacerBase->h / 2), -90 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -452,7 +452,7 @@ void Racer::shootPrimary() { new Shot( SHOT_DOUBLE, playerNr, pos + Vector2D(8,-2), -85 ); shots->addShot(shot1); shots->addShot(shot2); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -466,7 +466,7 @@ void Racer::shootPrimary() { new Shot( SHOT_DOUBLE_HEAVY, playerNr, pos + Vector2D(8,-2), -85 ); shots->addShot(shot1); shots->addShot(shot2); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -483,7 +483,7 @@ void Racer::shootPrimary() { shots->addShot(shot1); shots->addShot(shot2); shots->addShot(shot3); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -497,7 +497,7 @@ void Racer::shootPrimary() { new Shot( SHOT_HF_NORMAL, playerNr, pos + Vector2D(0, -spriteRacerBase->h / 2), -90 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -509,7 +509,7 @@ void Racer::shootPrimary() { shots->addShot( shot ); shot = new Shot( SHOT_HF_DOUBLE, playerNr, pos + Vector2D(12, -12), -90 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -524,7 +524,7 @@ void Racer::shootPrimary() { shots->addShot( shot ); shot = new Shot( SHOT_HF_TRIPLE, playerNr, pos + Vector2D(12, -12), -77 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -540,7 +540,7 @@ void Racer::shootPrimary() { shots->addShot( shot ); shot = new Shot( SHOT_HF_QUATTRO, playerNr, pos + Vector2D(30, -15), -80 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -559,7 +559,7 @@ void Racer::shootPrimary() { shots->addShot( shot ); shot = new Shot( SHOT_HF_QUINTO, playerNr, pos + Vector2D(30, -15), -70 ); shots->addShot( shot ); - mixer.playSample( sndShotPrimary, 0 ); + Mixer::mixer().playSample( sndShotPrimary, 0 ); timeLastShotPrimary = SDL_GetTicks(); } break; @@ -587,7 +587,7 @@ void Racer::shootSecondary() { Shot *shot = new Shot( SHOT_DUMBFIRE, playerNr, pos + Vector2D(0, -5), -90 ); shots->addShot(shot); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -603,7 +603,7 @@ void Racer::shootSecondary() { } lastDumbfireWasLeft = !lastDumbfireWasLeft; shots->addShot(shot); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -614,7 +614,7 @@ void Racer::shootSecondary() { Shot *shot = new Shot( SHOT_KICK_ASS_ROCKET, playerNr, pos, -90 ); shots->addShot(shot); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -628,7 +628,7 @@ void Racer::shootSecondary() { new Shot( SHOT_HELLFIRE, playerNr, pos + Vector2D(15, -3), 0 ); shots->addShot(shot1); shots->addShot(shot2); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -674,7 +674,7 @@ void Racer::shootSecondary() { pos + Vector2D(+3, -spriteRacerBase->h/2), angle2 ); shots->addShot(shot1); shots->addShot(shot2); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -685,7 +685,7 @@ void Racer::shootSecondary() { Shot *shot = new Shot( SHOT_ENERGY_BEAM, playerNr, pos + Vector2D(-1, -10), -90 ); shots->addShot(shot); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -701,7 +701,7 @@ void Racer::shootSecondary() { shot = new Shot( SHOT_HF_DUMBFIRE, playerNr, pos + Vector2D(30, -5), -90 ); shots->addShot(shot); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -715,7 +715,7 @@ void Racer::shootSecondary() { shot = new Shot( SHOT_HF_DUMBFIRE_DOUBLE, playerNr, pos + Vector2D(30, -5), -90 ); shots->addShot(shot); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -729,7 +729,7 @@ void Racer::shootSecondary() { Shot *shot2 = new Shot( SHOT_HF_KICK_ASS_ROCKET, playerNr, pos + Vector2D(30,0), -90 ); shots->addShot(shot2); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -743,7 +743,7 @@ void Racer::shootSecondary() { Shot *shot2 = new Shot( SHOT_HF_LASER, playerNr, pos + Vector2D(30,-10), -90 ); shots->addShot(shot2); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastShotSecondary = SDL_GetTicks(); } break; @@ -778,7 +778,7 @@ void Racer::shootSpecial() { } lastHeatseekerWasLeft = !lastHeatseekerWasLeft; shots->addShot(shot); - mixer.playSample( sndShotSecondary, 0 ); + Mixer::mixer().playSample( sndShotSecondary, 0 ); timeLastHeatseekerUsed = SDL_GetTicks(); specialsAvailability[ SPECIAL_HEATSEEKER ]--; } diff --git a/alienblaster/project/jni/alienblaster/src/setDifficulty.cc b/alienblaster/project/jni/alienblaster/src/setDifficulty.cc index 37cf4cac8..a538ae0ac 100644 --- a/alienblaster/project/jni/alienblaster/src/setDifficulty.cc +++ b/alienblaster/project/jni/alienblaster/src/setDifficulty.cc @@ -40,8 +40,8 @@ SetDifficulty::SetDifficulty( SDL_Surface *scr ) { lightFighterIcon2 = surfaceDB.loadSurface( FN_LIGHT_FIGHTER_2_ICON ); heavyFighterIcon1 = surfaceDB.loadSurface( FN_HEAVY_FIGHTER_1_ICON ); heavyFighterIcon2 = surfaceDB.loadSurface( FN_HEAVY_FIGHTER_2_ICON ); - choose = mixer.loadSample( FN_SOUND_INTRO_CHOOSE, 100 ); - confirm = mixer.loadSample( FN_SOUND_INTRO_CONFIRM, 100 ); + choose = Mixer::mixer().loadSample( FN_SOUND_INTRO_CHOOSE, 100 ); + confirm = Mixer::mixer().loadSample( FN_SOUND_INTRO_CONFIRM, 100 ); activeChoice = 0; playerOneLightFighter = true; @@ -169,21 +169,21 @@ void SetDifficulty::handleEvents( GameStates &gameState ) { case SDLK_F7: { if ( playMusicOn ) { playMusicOn = false; - mixer.stopMusic(); + Mixer::mixer().stopMusic(); } else { playMusicOn = true; - mixer.playMusic( MUSIC_INTRO, -1, 1000 ); + Mixer::mixer().playMusic( MUSIC_INTRO, -1, 1000 ); } break; } case SDLK_UP: { - mixer.playSample( choose, 0 ); + Mixer::mixer().playSample( choose, 0 ); activeChoice--; if ( activeChoice < 0 ) activeChoice = NR_DIFFICULTY_CHOICES - 1; break; } case SDLK_DOWN: { - mixer.playSample( choose, 0 ); + Mixer::mixer().playSample( choose, 0 ); activeChoice = (activeChoice + 1) % NR_DIFFICULTY_CHOICES; break; } @@ -192,7 +192,7 @@ void SetDifficulty::handleEvents( GameStates &gameState ) { break; } case SDLK_RETURN: { - mixer.playSample( confirm, 0 ); + Mixer::mixer().playSample( confirm, 0 ); switch (activeChoice) { case DIFFICULTY_0: { difficultyLevel = 0; diff --git a/alienblaster/project/jni/alienblaster/src/settings.cc b/alienblaster/project/jni/alienblaster/src/settings.cc index 5a0527ce2..a5fa8d94a 100644 --- a/alienblaster/project/jni/alienblaster/src/settings.cc +++ b/alienblaster/project/jni/alienblaster/src/settings.cc @@ -346,10 +346,10 @@ void Settings::settingsDialog( SDL_Surface *screen ) { case SDLK_F7: { if ( playMusicOn ) { playMusicOn = false; - mixer.stopMusic(); + Mixer::mixer().stopMusic(); } else { playMusicOn = true; - mixer.playMusic( MUSIC_INTRO, -1, 1000 ); + Mixer::mixer().playMusic( MUSIC_INTRO, -1, 1000 ); } break; } diff --git a/alienblaster/project/jni/sdl/src/audio/android/SDL_androidaudio.c b/alienblaster/project/jni/sdl/src/audio/android/SDL_androidaudio.c index b6a6a948f..d30358906 100644 --- a/alienblaster/project/jni/sdl/src/audio/android/SDL_androidaudio.c +++ b/alienblaster/project/jni/sdl/src/audio/android/SDL_androidaudio.c @@ -35,6 +35,7 @@ #include "SDL_mutex.h" #include "SDL_thread.h" #include +#include #define ANDROIDAUD_DRIVER_NAME "android" @@ -48,7 +49,7 @@ static void ANDROIDAUD_CloseAudio(_THIS); /* Audio driver bootstrap functions */ static int ANDROIDAUD_Available(void) { - return(0); + return(1); } static void ANDROIDAUD_DeleteDevice(SDL_AudioDevice *device) @@ -140,6 +141,7 @@ static void ANDROIDAUD_CloseAudio(_THIS) static int ANDROIDAUD_OpenAudio(_THIS, SDL_AudioSpec *spec) { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio 1"); if( ! (spec->format == AUDIO_S8 || spec->format == AUDIO_S16) ) return (-1); // TODO: enable format conversion? Don't know how to do that in SDL @@ -160,21 +162,36 @@ static int ANDROIDAUD_OpenAudio(_THIS, SDL_AudioSpec *spec) audioMutex = SDL_CreateMutex(); audioCond = SDL_CreateCond(); } + + __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio 2"); SDL_mutexP(audioMutex); while( !audioInitialized ) - SDL_CondWait( audioCond, audioMutex ); + { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio 3"); + if( SDL_CondWaitTimeout( audioCond, audioMutex, 500 ) != 0 ) + { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio() failed! timeout when waiting callback"); + SDL_mutexV(audioMutex); + ANDROIDAUD_CloseAudio(this); + return(-1); + } + } + __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio 4"); audioFormat = NULL; SDL_mutexV(audioMutex); + __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_OpenAudio 5"); + return(0); } static void ANDROIDAUD_PlayAudio(_THIS) { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio 1"); SDL_mutexP(audioMutex); audioBuffer = this->hidden->mixbuf; @@ -184,6 +201,7 @@ static void ANDROIDAUD_PlayAudio(_THIS) SDL_CondWait( audioCond, audioMutex ); SDL_mutexV(audioMutex); + __android_log_print(ANDROID_LOG_INFO, "libSDL", "ANDROIDAUD_PlayAudio 2"); } #ifndef SDL_JAVA_PACKAGE_PATH @@ -195,16 +213,20 @@ static void ANDROIDAUD_PlayAudio(_THIS) extern jintArray JAVA_EXPORT_NAME(AudioThread_nativeAudioInit) (JNIEnv * env, jobject jobj) { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "AudioThread_nativeAudioInit 1"); jintArray ret = NULL; int initData[4] = { 0, 0, 0, 0 }; // { rate, channels, encoding, bufsize }; if( audioMutex == NULL ) return; + + __android_log_print(ANDROID_LOG_INFO, "libSDL", "AudioThread_nativeAudioInit 2"); SDL_mutexP(audioMutex); if( audioInitialized == 0 ) { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "AudioThread_nativeAudioInit 3"); initData[0] = audioFormat->freq; initData[1] = audioFormat->channels; initData[2] = ( audioFormat->format == AUDIO_S16 ) ? 1 : 0; @@ -213,15 +235,19 @@ extern jintArray JAVA_EXPORT_NAME(AudioThread_nativeAudioInit) (JNIEnv * env, jo (*env)->SetIntArrayRegion(env, ret, 0, 4, (jint *)initData); audioInitialized = 1; SDL_CondSignal(audioCond); + __android_log_print(ANDROID_LOG_INFO, "libSDL", "AudioThread_nativeAudioInit 4"); } SDL_mutexV(audioMutex); + + __android_log_print(ANDROID_LOG_INFO, "libSDL", "AudioThread_nativeAudioInit 5"); return (ret); }; extern jint JAVA_EXPORT_NAME(AudioThread_nativeAudioBuffer) ( JNIEnv * env, jobject jobj, jbyteArray data ) { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "AudioThread_nativeAudioBuffer 1"); int ret = 0; if( audioMutex == NULL ) @@ -238,6 +264,7 @@ extern jint JAVA_EXPORT_NAME(AudioThread_nativeAudioBuffer) ( JNIEnv * env, jobj } else { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "AudioThread_nativeAudioBuffer 2"); (*env)->SetByteArrayRegion(env, data, 0, audioBufferSize, (jbyte *)audioBuffer); ret = audioBufferSize; audioBuffer = NULL; @@ -247,6 +274,7 @@ extern jint JAVA_EXPORT_NAME(AudioThread_nativeAudioBuffer) ( JNIEnv * env, jobj SDL_mutexV(audioMutex); + __android_log_print(ANDROID_LOG_INFO, "libSDL", "AudioThread_nativeAudioBuffer 3"); return ret; }; diff --git a/alienblaster/project/src/DemoActivity.java b/alienblaster/project/src/DemoActivity.java index ed5112613..cc7ef60c8 100644 --- a/alienblaster/project/src/DemoActivity.java +++ b/alienblaster/project/src/DemoActivity.java @@ -148,22 +148,32 @@ class AudioThread extends Thread { private Activity mParent; private AudioTrack mAudio; private byte[] mAudioBuffer; + public int mLibraryLoaded; public AudioThread(Activity parent) { + android.util.Log.i("SDL Java", "AudioThread created"); mParent = parent; mAudio = null; mAudioBuffer = null; + mLibraryLoaded = 0; } @Override public void run() { + android.util.Log.i("SDL Java", "AudioThread::Run(): enter"); while( !isInterrupted() ) { + android.util.Log.i("SDL Java", "AudioThread::Run(): loop"); if( mAudio == null ) { - int[] initParams = nativeAudioInit(); + int[] initParams = null; + if( mLibraryLoaded != 0 ) + { + android.util.Log.i("SDL Java", "AudioThread::Run(): call nativeAudioInit()"); + initParams = nativeAudioInit(); + } if( initParams == null ) { try { @@ -172,6 +182,7 @@ class AudioThread extends Thread { } else { + android.util.Log.i("SDL Java", "AudioThread::Run(): !!!INIT!!!"); int rate = initParams[0]; int channels = initParams[1]; channels = ( channels == 1 ) ? AudioFormat.CHANNEL_CONFIGURATION_MONO : @@ -194,6 +205,7 @@ class AudioThread extends Thread { } else { + android.util.Log.i("SDL Java", "AudioThread::Run(): playing"); int len = nativeAudioBuffer( mAudioBuffer ); if( len > 0 ) mAudio.write( mAudioBuffer, 0, len ); @@ -213,9 +225,13 @@ public class DemoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mLoadLibraryStub = new LoadLibrary(); + // Wicked - we have to create audio thread before loading library + // because audio is initialized even before main() (how's that even possible?) + mLoadLibraryStub = null; mAudioThread = new AudioThread(this); mAudioThread.start(); + mLoadLibraryStub = new LoadLibrary(); + mAudioThread.mLibraryLoaded = 1; mGLView = new DemoGLSurfaceView(this); setContentView(mGLView); // Receive keyboard events diff --git a/alienblaster/readme.txt b/alienblaster/readme.txt index 9c0eba8e2..713b0a1e3 100644 --- a/alienblaster/readme.txt +++ b/alienblaster/readme.txt @@ -1,6 +1,7 @@ This is Alien Blaster game ported to Google Android. I did not change anything in Alien Blaster sources, except for SCREEN_WIDTH, -SCREEN_HEIGHT and BIT_DEPTH constants in global.h, to support 320x430x16bpp video mode. +SCREEN_HEIGHT and BIT_DEPTH constants in global.h, to support 320x430x16bpp video mode, +and also made audio initialize after main() has been called, not inside static initializers (ugh) This should be compiled with Android 1.6 SDK and NDK - google for them and install them as described in their docs. You'll need to install Eclipse or Ant too