summary refs log tree commit diff
path: root/src/GameLoop.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/GameLoop.cpp')
-rw-r--r--src/GameLoop.cpp73
1 files changed, 38 insertions, 35 deletions
diff --git a/src/GameLoop.cpp b/src/GameLoop.cpp
index 2fa7fc1..a4b4a60 100644
--- a/src/GameLoop.cpp
+++ b/src/GameLoop.cpp
@@ -2,6 +2,7 @@
 // Copyright (C) 2002  David Rosen
 // Copyright (C) 2003  Zachary Jack Slater
 // Copyright (C) 2003  Steven Fuller
+// Copyright (C) 2021  Nguyễn Gia Phong
 //
 // This file is part of Black Shades.
 //
@@ -20,7 +21,7 @@
 
 #include "Game.h"
 
-extern double multiplier;
+extern float multiplier;
 extern int visions;
 extern unsigned int gSourceID[100];
 extern unsigned int gSampleSet[100];
@@ -261,69 +262,71 @@ static void ProcessSDLEvents(Game* g)
 	}
 }
 
-void Game::EventLoop()
+void eventLoop(Game* game)
 {
 	unsigned char theKeyMap[16];
-	gQuit = false;
-	framespersecond = 60;
+	game->gQuit = false;
+	game->framespersecond = 60.0f;
 
-	while (!gQuit) {
-		ProcessSDLEvents(this);
-		start = TimerGetTime(&theTimer);
+	while (!game->gQuit) {
+		ProcessSDLEvents(game);
+		game->start = TimerGetTime(&game->theTimer);
 
 		GLfloat oldmult = multiplier;
-		int colaccuracy = min(sps, sps / (float) framespersecond + 1);
+		int colaccuracy = min(game->sps,
+			game->sps / game->framespersecond + 1.0f);
 		multiplier /= colaccuracy;
-		for (int i = 0; i < (int) (colaccuracy + 0.5); i++)
-			Tick();
-		if (DrawGLScene())
+		for (int i = 0; i < (int) (colaccuracy + 0.5f); i++)
+			game->Tick();
+		if (game->DrawGLScene())
 			SDL_GL_SwapBuffers();
 		else
-			gQuit = true;
+			game->gQuit = true;
 		multiplier = oldmult;
 
 		do {
-			end = TimerGetTime(&theTimer);
-			timetaken = end - start;
-			framespersecond = 6e8 / timetaken;
-		} while (framespersecond > maxfps);
+			game->end = TimerGetTime(&game->theTimer);
+			game->timetaken = game->end - game->start;
+			game->framespersecond = 6e8f / game->timetaken;
+		} while (game->framespersecond > game->maxfps);
 
-		multiplier5 = multiplier4;
-		multiplier4 = multiplier3;
-		multiplier3 = multiplier2;
-		multiplier2 = 1 / framespersecond;
-		multiplier = (multiplier2 + multiplier3
-		              + multiplier4 + multiplier5) / 4;
-		multiplier = min(max(multiplier, 0.00001), 1.0);
-		if (visions == 1 && !mainmenu)
+		game->multiplier5 = game->multiplier4;
+		game->multiplier4 = game->multiplier3;
+		game->multiplier3 = game->multiplier2;
+		game->multiplier2 = 1 / game->framespersecond;
+		multiplier = (game->multiplier2 + game->multiplier3
+			+ game->multiplier4 + game->multiplier5) / 4.0f;
+		multiplier = min(max(multiplier, 0.00001f), 1.0f);
+		if (visions == 1 && !game->mainmenu)
 			multiplier /= 3;
 		if (slomo)
 			multiplier /= 5;
-		if (paused)
+		if (game->paused)
 			multiplier = 0;
 
 		GetKeys((unsigned long*) theKeyMap);
 		if (IsKeyDown(theKeyMap, MAC_COMMAND_KEY)
 		    &&IsKeyDown(theKeyMap, MAC_Q_KEY)) {
-			gQuit = true;
-			if (score > highscore) {
-				highscore=score;
+			game->gQuit = true;
+			if (game->score > game->highscore) {
+				game->highscore = game->score;
 				/* TODO */
 				ofstream opstream("highscore.txt");
-				opstream << highscore;
+				opstream << game->highscore;
 				opstream << "\n";
-				opstream << beatgame;
+				opstream << game->beatgame;
 				opstream.close();
 			}
 		}
 
 		if (IsKeyDown(theKeyMap, MAC_ESCAPE_KEY)) {
 			alSourcePause(gSourceID[rainsound]);
-			mainmenu = 1;
+			game->mainmenu = 1;
 			alSourcePlay(gSourceID[souloutsound]);
-			flashamount = flashr = flashg = flashb = 1;
+			game->flashamount = 1.0f;
+			game->flashr = game->flashg = game->flashb = 1.0f;
 			alSourceStop(gSourceID[visionsound]);
-			whichsong = mainmenusong;
+			game->whichsong = mainmenusong;
 			alSourceStop(gSourceID[knifesong]);
 			alSourceStop(gSourceID[shootsong]);
 			alSourceStop(gSourceID[zombiesong]);
@@ -332,8 +335,8 @@ void Game::EventLoop()
 			alSourcef(gSourceID[shootsong], AL_MIN_GAIN, 0);
 			alSourcef(gSourceID[zombiesong], AL_MIN_GAIN, 0);
 			alSourcef(gSourceID[mainmenusong], AL_MIN_GAIN, 0);
-			alSourcePlay(gSourceID[whichsong]);
-			alSourcef(gSourceID[whichsong], AL_MIN_GAIN, 1);
+			alSourcePlay(gSourceID[game->whichsong]);
+			alSourcef(gSourceID[game->whichsong], AL_MIN_GAIN, 1);
 		}
 	}
 }