about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNguyễn Gia Phong <cnx@loang.net>2023-11-19 05:08:01 +0900
committerNguyễn Gia Phong <cnx@loang.net>2023-11-19 05:08:01 +0900
commite68d3ee2380dba98a72f2668a2c2664fc993bb4a (patch)
tree8aaabd3bc11b16b31c67fe2667ad8a6542327ef1 /src
parentdb730f01951231196cac36630d043c7a3676d446 (diff)
downloadblackshades-e68d3ee2380dba98a72f2668a2c2664fc993bb4a.tar.gz
Fix off-by-one music selection
Fixes: https://todo.sr.ht/~cnx/blackshades/25
Diffstat (limited to 'src')
-rw-r--r--src/Game.h2
-rw-r--r--src/GameInitDispose.cpp21
-rw-r--r--src/GameLoop.cpp23
-rw-r--r--src/GameTick.cpp57
4 files changed, 33 insertions, 70 deletions
diff --git a/src/Game.h b/src/Game.h
index 0639532..6110aaa 100644
--- a/src/Game.h
+++ b/src/Game.h
@@ -65,7 +65,7 @@ struct Game {
 	bool cubetest;
 	bool disttest;
 
-	bool initialized;
+	bool initialized = false;
 
 	float flashamount;
 	float flashr,flashg,flashb;
diff --git a/src/GameInitDispose.cpp b/src/GameInitDispose.cpp
index 76721ee..23d3e67 100644
--- a/src/GameInitDispose.cpp
+++ b/src/GameInitDispose.cpp
@@ -134,6 +134,7 @@ void loadSounds(bool musictoggle)
 	alSourcei(gSourceID[explosionsound], AL_BUFFER, gSampleSet[explosionsound]);
 	alSourcei(gSourceID[bodywhacksound], AL_BUFFER, gSampleSet[bodywhacksound]);
 	alSourcei(gSourceID[rainsound], AL_BUFFER, gSampleSet[rainsound]);
+	alSourcei(gSourceID[rainsound], AL_LOOPING, 1);
 	alSourcei(gSourceID[losesound], AL_BUFFER, gSampleSet[losesound]);
 	alSourcei(gSourceID[disguisekillsound], AL_BUFFER, gSampleSet[disguisekillsound]);
 	alSourcei(gSourceID[knifeslashsound], AL_BUFFER, gSampleSet[knifeslashsound]);
@@ -307,24 +308,8 @@ void initGame(Game* game)
 	if (!game->initialized) {
 		soundscalefactor = 10;
 		loadSounds(game->musictoggle);
-
-		//Play correct song
-		if (environment == rainy_environment)
-			alSourcePlay(gSourceID[rainsound]);
-		else
-			alSourcePause(gSourceID[rainsound]);
-
-		ALuint sound_source = gSourceID[game->whichsong];
-		alSourceStop(sound_source);
-		alSourcef(sound_source, AL_MIN_GAIN, 0);
-		alSourcef(sound_source, AL_MAX_GAIN, 0);
-
-		sound_source = gSourceID[game->whichsong = mainmenusong];
-		alSourceStop(sound_source);
-		alSourcef(sound_source, AL_PITCH, 1);
-		alSourcePlay(sound_source);
-		alSourcef(sound_source, AL_MIN_GAIN, 1);
-		alSourcef(sound_source, AL_MAX_GAIN, 1);
+		if (game->musictoggle)
+			alSourcePlay(gSourceID[game->whichsong = mainmenusong]);
 	}
 
 	auto& vip = game->person[game->numpeople = 1];
diff --git a/src/GameLoop.cpp b/src/GameLoop.cpp
index 66b7395..da531e1 100644
--- a/src/GameLoop.cpp
+++ b/src/GameLoop.cpp
@@ -41,23 +41,18 @@ void handleKey(Game* game, int key, int action, int mods)
 		return;
 	auto& player = game->person[0];
 	if (key == GLFW_KEY_ESCAPE) {
-		alSourcePause(gSourceID[rainsound]);
-		setMenu(game, true);
+		alSourceStop(gSourceID[rainsound]);
+		alSourceStop(gSourceID[visionsound]);
+		if (game->musictoggle) {
+			alSourceStop(gSourceID[game->whichsong]);
+			game->whichsong = mainmenusong;
+			alSourcef(gSourceID[game->whichsong], AL_PITCH, 1);
+			alSourcePlay(gSourceID[game->whichsong]);
+		}
 		alSourcePlay(gSourceID[souloutsound]);
+		setMenu(game, true);
 		game->flashamount = 1.0f;
 		game->flashr = game->flashg = game->flashb = 1.0f;
-		alSourceStop(gSourceID[visionsound]);
-		game->whichsong = mainmenusong;
-		alSourceStop(gSourceID[knifesong]);
-		alSourceStop(gSourceID[shootsong]);
-		alSourceStop(gSourceID[zombiesong]);
-		alSourceStop(gSourceID[mainmenusong]);
-		alSourcef(gSourceID[knifesong], AL_MIN_GAIN, 0);
-		alSourcef(gSourceID[shootsong], AL_MIN_GAIN, 0);
-		alSourcef(gSourceID[zombiesong], AL_MIN_GAIN, 0);
-		alSourcef(gSourceID[mainmenusong], AL_MIN_GAIN, 0);
-		alSourcePlay(gSourceID[game->whichsong]);
-		alSourcef(gSourceID[game->whichsong], AL_MIN_GAIN, 1);
 	} else if (key == keymap.dive) {
 		if (player.playerrotation == player.playerlowrotation
 		    && player.targetanimation == joganim
diff --git a/src/GameTick.cpp b/src/GameTick.cpp
index 75bcc9d..a449ae2 100644
--- a/src/GameTick.cpp
+++ b/src/GameTick.cpp
@@ -90,22 +90,18 @@ void updateSong(Game* game)
 	if (environment == rainy_environment)
 		alSourcePlay(gSourceID[rainsound]);
 	else
-		alSourcePause(gSourceID[rainsound]);
+		alSourceStop(gSourceID[rainsound]);
 
+	if (!game->musictoggle)
+		return;
 	alSourceStop(gSourceID[game->whichsong]);
-	alSourcef(gSourceID[game->whichsong], AL_MIN_GAIN, 0);
-	alSourcef(gSourceID[game->whichsong], AL_MAX_GAIN, 0);
-
 	if (game->type == zombie_type)
 		game->whichsong = zombiesong;
 	else if (game->person[0].whichgun == knife)
 		game->whichsong = knifesong;
 	else
 		game->whichsong = shootsong;
-
 	alSourcef(gSourceID[game->whichsong], AL_PITCH, 1);
-	alSourcef(gSourceID[game->whichsong], AL_MIN_GAIN, 1);
-	alSourcef(gSourceID[game->whichsong], AL_MAX_GAIN, 1);
 	alSourcePlay(gSourceID[game->whichsong]);
 }
 
@@ -136,7 +132,6 @@ void click(Game* game, int button, int action, int mods)
 
 		switch (game->mouseoverbutton) {
 		case 1:
-			updateSong(game);
 			game->flashr = game->flashg = game->flashb = 1.0f;
 			game->flashamount = 1.0f;
 			alSourcePlay(gSourceID[soulinsound]);
@@ -146,6 +141,7 @@ void click(Game* game, int button, int action, int mods)
 				initGame(game);
 			}
 
+			updateSong(game);
 			if (visions)
 				alSourcePlay(gSourceID[visionsound]);
 
@@ -443,23 +439,19 @@ void nextLevel(Game* game)
 	alSourcePlay(gSourceID[souloutsound]);
 
 	if (game->mission >= game->nummissions) {
-		alSourcePause(gSourceID[rainsound]);
+		alSourceStop(gSourceID[rainsound]);
 		alSourceStop(gSourceID[visionsound]);
-		alSourceStop(gSourceID[game->whichsong]);
-		alSourcef(gSourceID[game->whichsong], AL_MIN_GAIN, 0);
-		alSourcef(gSourceID[game->whichsong], AL_MAX_GAIN, 0);
-
-		game->whichsong = mainmenusong;
-		alSourcef(gSourceID[game->whichsong], AL_PITCH, 1);
-		alSourcef(gSourceID[game->whichsong], AL_MIN_GAIN, 1);
-		alSourcef(gSourceID[game->whichsong], AL_MAX_GAIN, 1);
-		alSourcePlay(gSourceID[game->whichsong]);
-
+		if (game->musictoggle) {
+			alSourceStop(gSourceID[game->whichsong]);
+			game->whichsong = mainmenusong;
+			alSourcef(gSourceID[game->whichsong], AL_PITCH, 1);
+			alSourcePlay(gSourceID[game->whichsong]);
+		}
 		setMenu(game, game->beatgame = true);
 		game->gameinprogress = 0;
 	} else {
-		updateSong(game);
 		initGame(game);
+		updateSong(game);
 	}
 }
 
@@ -994,8 +986,8 @@ void Game::Tick()
 		flashr = flashg = flashb = 0;
 		alSourcePlay(gSourceID[soulinsound]);
 
-		updateSong(this);
 		initGame(this);
+		updateSong(this);
 	}
 
 	spawnNpc(this);
@@ -2404,24 +2396,15 @@ hit_terrain:
 		person[i].oldhealth=person[i].health;
 	}
 
-	if(slomo==2){
-		psychicpower-=multiplier*15;
-		if(psychicpower<0){
-			alSourceStop(gSourceID[whichsong]);
-			alSourcef(gSourceID[whichsong], AL_MIN_GAIN, 0);
-			alSourcef(gSourceID[whichsong], AL_MAX_GAIN, 0);
-			if(person[0].whichgun==knife)whichsong=knifesong;
-			if(person[0].whichgun!=knife)whichsong=shootsong;
-			if(type==zombie_type)whichsong=zombiesong;
+	if (slomo == 2) {
+		psychicpower -= multiplier*15;
+		if (psychicpower < 0) {
 			alSourcef(gSourceID[whichsong], AL_PITCH, 1);
-			alSourcePlay(gSourceID[whichsong]);
-			alSourcef(gSourceID[whichsong], AL_MIN_GAIN, 1);
-			alSourcef(gSourceID[whichsong], AL_MAX_GAIN, 1);
-			slomo=0;
+			slomo = 0;
 			alSourcePlay(gSourceID[soulinsound]);
-			psychicpower=0;
-			flashamount=.5;
-			flashr=1;flashg=1;flashb=1;
+			psychicpower = 0;
+			flashamount = 0.5f;
+			flashr = flashg = flashb = 1.0f;
 		}
 	}