summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/GameLoop.cpp396
1 files changed, 157 insertions, 239 deletions
diff --git a/src/GameLoop.cpp b/src/GameLoop.cpp
index 3a9e8a0..ab8d545 100644
--- a/src/GameLoop.cpp
+++ b/src/GameLoop.cpp
@@ -1,178 +1,119 @@
+// Event loop handler
+// Copyright (C) 2002  David Rosen
+// Copyright (C) 2003  Zachary Jack Slater
+// Copyright (C) 2003  Steven Fuller
+//
+// This file is part of Black Shades.
+//
+// Black Shades 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 3 of the License, or
+// (at your option) any later version.
+//
+// Black Shades 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 Black Shades.  If not, see <https://www.gnu.org/licenses/>.
+
 #include "Game.h"
 
 extern double multiplier;
-
 extern int visions;
-
 extern unsigned int gSourceID[100];
-
 extern unsigned int gSampleSet[100];
-
 extern Camera camera;
-
 extern float rad2deg;
-
 extern Fog fog;
-
 extern int environment;
-
 extern int slomo;
 
-/********************> HandleKeyDown() <*****/
-
-void	Game::HandleKeyDown( char theChar )
-
+void Game::HandleKeyDown(char key)
 {
-	XYZ facing;
-
-	if(!mainmenu){
-
-	switch( theChar )
+	if (mainmenu)
+		return;
 
-	{
-
-		case 'l':
-		        if(!lasersight==1){lasersight=1;}else{lasersight=0;}
-
-			break;
-
-		case 'k':
-
-			if(debug)timeremaining=0;
+	ALfloat pitch;
+	XYZ facing;
 
+	switch(key) {
+	case 'l':
+		lasersight ^= 1;
+		break;
+	case 'k':
+		if (debug)
+			timeremaining = 0;
+		break;
+	case 'b':
+		if (!debug)
 			break;
-
-		case 'b':
-
-			if(debug){
-
-			alSourcePlay(gSourceID[soulinsound]);
-
-			if(!slomo)slomo=1;
-
-			else slomo=0;}
-
-			if(slomo){
-
-				alSourcef(gSourceID[knifesong], AL_PITCH, (ALfloat)(.5));
-
-				alSourcef(gSourceID[shootsong], AL_PITCH, (ALfloat)(.5));
-
-				alSourcef(gSourceID[zombiesong], AL_PITCH, (ALfloat)(.5));
-			}
-
-			if(!slomo){
-
-				alSourcef(gSourceID[knifesong], AL_PITCH, (ALfloat)(1));
-
-				alSourcef(gSourceID[shootsong], AL_PITCH, (ALfloat)(1));
-
-				alSourcef(gSourceID[zombiesong], AL_PITCH, (ALfloat)(1));
-
-			}
-
+		alSourcePlay(gSourceID[soulinsound]);
+		pitch = (slomo ^= 1) ? 0.5f : 1.0f;
+		alSourcef(gSourceID[knifesong], AL_PITCH, pitch);
+		alSourcef(gSourceID[shootsong], AL_PITCH, pitch);
+		alSourcef(gSourceID[zombiesong], AL_PITCH, pitch);
+		break;
+	case 'B':
+		if (!debug)
 			break;
-
-		case 'B':
-
-			if(debug){
-
-			alSourcePlay(gSourceID[soulinsound]);
-
-			paused=1-paused;}
-
+		alSourcePlay(gSourceID[soulinsound]);
+		paused = 1 - paused;
+		break;
+	case 'f':
+		if (!debug)
 			break;
-
-		case 'f':
-
-			if(debug){
-
-			alSourcePlay(gSourceID[souloutsound]);
-
-			//Facing
-
-			facing=0;
-
-			facing.z=-1;
-
-			facing=DoRotation(facing,-camera.rotation2,0,0);
-
-			facing=DoRotation(facing,0,0-camera.rotation,0);
-
-			for(int i=1;i<numpeople;i++){
-
-				if(person[i].skeleton.free!=1){
-
-				if(findDistancefast(person[i].playercoords,person[0].playercoords)<1000){
-
-				person[i].skeleton.free=1;
-
-				person[i].longdead=1;
-
-				for(int j=0;j<person[i].skeleton.num_joints;j++){
-
-					person[i].skeleton.joints[j].position=DoRotation(person[i].skeleton.joints[j].position,0,person[i].playerrotation,0);
-
-					person[i].skeleton.joints[j].position+=person[i].playercoords;
-
-					person[i].skeleton.joints[j].realoldposition=person[i].skeleton.joints[j].position;
-
-					person[i].skeleton.joints[j].velocity=DoRotation(person[i].skeleton.joints[j].velocity,0,person[i].playerrotation,0);
-
-					person[i].skeleton.joints[j].velocity+=person[i].velocity;
-
-					person[i].skeleton.joints[j].velocity+=facing*50;
-
-					person[i].skeleton.joints[j].velocity.x+=abs(Random()%20)-10;
-
-					person[i].skeleton.joints[j].velocity.y+=abs(Random()%20)-10;
-
-					person[i].skeleton.joints[j].velocity.z+=abs(Random()%20)-10;
-
-				}}}
-
-			}}
-
-			break;
-
-		case 'X':
-
-			if(debug){
-
-			if(person[0].grenphase==0){
-
-				person[0].ammo=-1;
-
-				person[0].whichgun++;
-
-				person[0].grenphase=0;
-
-				person[0].reloads[person[0].whichgun]=3;
-
-				if(person[0].whichgun>7)person[0].whichgun=0;
-
-			}}
-
+		alSourcePlay(gSourceID[souloutsound]);
+
+		facing = 0;
+		facing.z = -1;
+		facing = DoRotation(facing, -camera.rotation2, 0, 0);
+		facing = DoRotation(facing, 0, 0 - camera.rotation, 0);
+
+		for(int i = 1; i < numpeople; i++) {
+			if (person[i].skeleton.free == 1
+			    || findDistancefast(person[i].playercoords,
+			                        person[0].playercoords) > 1000)
+				continue;
+			person[i].skeleton.free = 1;
+			person[i].longdead = 1;
+
+			for (auto& joint : person[i].skeleton.joints) {
+				joint.position = DoRotation(joint.position, 0, person[i].playerrotation, 0);
+				joint.position += person[i].playercoords;
+				joint.realoldposition = joint.position;
+				joint.velocity = DoRotation(joint.velocity, 0, person[i].playerrotation, 0);
+				joint.velocity += person[i].velocity;
+				joint.velocity += facing * 50;
+				joint.velocity.x += abs(Random() % 20) - 10;
+				joint.velocity.y += abs(Random() % 20) - 10;
+				joint.velocity.z += abs(Random() % 20) - 10;
+			}
+		}
+		break;
+	case 'X':
+		if (!debug || person[0].grenphase)
 			break;
-
-	}
-
+		person[0].ammo = -1;
+		person[0].whichgun++;
+		person[0].grenphase = 0;
+		person[0].reloads[person[0].whichgun] = 3;
+		if (person[0].whichgun > 7)
+			person[0].whichgun = 0;
+		break;
 	}
-
 }
 
 static int mapinit = 0;
 static int sdlkeymap[SDLK_LAST];
-
 static unsigned char ourkeys[16];
 
 static void init_sdlkeymap()
 {
 	int i;
-
-	for (i = 0; i < SDLK_LAST; i++) {
+	for (i = 0; i < SDLK_LAST; i++)
 		sdlkeymap[i] = -1;
-	}
 
 	sdlkeymap[SDLK_1] = MAC_1_KEY;
 	sdlkeymap[SDLK_2] = MAC_2_KEY;
@@ -269,125 +210,103 @@ void GetKeys(unsigned long *keys)
 
 static void DoSDLKey(Game *g, SDL_Event *event)
 {
-	int press = (event->type == SDL_KEYDOWN) ? 1 : 0;
-	int mackey;
-	int index;
-	int mask;
-
-	if (mapinit == 0) {
+	if (mapinit == 0)
 		init_sdlkeymap();
-	}
-
-	mackey = sdlkeymap[event->key.keysym.sym];
 
+	int mackey = sdlkeymap[event->key.keysym.sym];
 	if (mackey != -1) {
-		index = mackey / 8;
-		mask = 1 << (mackey % 8);
+		int index = mackey / 8;
+		int mask = 1 << (mackey % 8);
 
-		if (press) {
+		if (event->type == SDL_KEYDOWN)
 			ourkeys[index] |= mask;
-		} else {
+		else
 			ourkeys[index] &= ~mask;
-		}
 	}
 
-	if (event->key.keysym.unicode &&
-		!(event->key.keysym.unicode & 0xFF80)) {
-
+	if (event->key.keysym.unicode
+	    && !(event->key.keysym.unicode & 0xFF80))
 		/* hey, at least it was aleady public */
 		g->HandleKeyDown(event->key.keysym.unicode);
-	}
-
 }
 
-static void ProcessSDLEvents(Game *g)
+static void ProcessSDLEvents(Game* g)
 {
 	SDL_Event event;
-
-	if (SDL_PollEvent(&event)) {
-		do {
-			switch(event.type) {
-				case SDL_KEYDOWN:
-				if (event.key.keysym.sym == SDLK_RETURN &&
-					event.key.keysym.mod & KMOD_ALT)
-				{
-					SDL_WM_ToggleFullScreen (SDL_GetVideoSurface ());
-					break;
-				}
-				if (event.key.keysym.sym == SDLK_g &&
-					event.key.keysym.mod & KMOD_CTRL)
-				{
-					if (SDL_WM_GrabInput (SDL_GRAB_QUERY) == SDL_GRAB_OFF)
-					{
-						SDL_WM_GrabInput (SDL_GRAB_ON);
-						SDL_ShowCursor (SDL_DISABLE);
-					}
-					else
-					{
-						SDL_WM_GrabInput (SDL_GRAB_OFF);
-						SDL_ShowCursor (SDL_ENABLE);
-					}
-					break;
+	while (SDL_PollEvent(&event)) {
+		switch (event.type) {
+		case SDL_KEYDOWN:
+			if (event.key.keysym.sym == SDLK_RETURN
+			    && event.key.keysym.mod & KMOD_ALT) {
+				SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
+				break;
+			} else if (event.key.keysym.sym == SDLK_g
+			           && event.key.keysym.mod & KMOD_CTRL) {
+				if (SDL_WM_GrabInput(SDL_GRAB_QUERY)
+				    == SDL_GRAB_OFF) {
+					SDL_WM_GrabInput(SDL_GRAB_ON);
+					SDL_ShowCursor(SDL_DISABLE);
+				} else {
+					SDL_WM_GrabInput(SDL_GRAB_OFF);
+					SDL_ShowCursor(SDL_ENABLE);
 				}
-				case SDL_KEYUP:
-					DoSDLKey(g, &event);
-					break;
-				case SDL_QUIT:
-					exit(0);
+				break;
 			}
-		} while (SDL_PollEvent(&event));
+		case SDL_KEYUP:
+			DoSDLKey(g, &event);
+			break;
+		case SDL_QUIT:
+			exit(0);
+		}
 	}
 }
 
-/********************> EventLoop() <*****/
-void	Game::EventLoop( void )
+void Game::EventLoop()
 {
-	unsigned char	theKeyMap[16];
-	int colaccuracy,i;
-	GLfloat oldmult;
+	unsigned char theKeyMap[16];
 	gQuit = false;
 	framespersecond = 60;
-	while ( gQuit == false ) {
+
+	while (!gQuit) {
 		ProcessSDLEvents(this);
-		start=TimerGetTime(&theTimer);
-		colaccuracy=sps/framespersecond+1;
-		if(colaccuracy>sps){colaccuracy=sps;}
+		start = TimerGetTime(&theTimer);
 
-		oldmult=multiplier;
-		multiplier/=colaccuracy;
-		for(i = 0; i < (int) (colaccuracy+.5); i++)
+		GLfloat oldmult = multiplier;
+		int colaccuracy = min(sps, sps / (float) framespersecond + 1);
+		multiplier /= colaccuracy;
+		for (int i = 0; i < (int) (colaccuracy + 0.5); i++)
 			Tick();
-		multiplier=oldmult;
-		if ( DrawGLScene())
+		if (DrawGLScene())
 			SDL_GL_SwapBuffers();
 		else
 			gQuit = true;
-		oldmult=multiplier;
-
-		end=TimerGetTime(&theTimer);
-		timetaken=end-start;
-		framespersecond=600000000/timetaken;
-		while(framespersecond>maxfps){
-			end=TimerGetTime(&theTimer);
-			timetaken=end-start;
-			framespersecond=600000000/timetaken;
-		}
-
-		multiplier5=multiplier4;
-		multiplier4=multiplier3;
-		multiplier3=multiplier2;
-		multiplier2=1/framespersecond;
-		multiplier=(multiplier2+multiplier3+multiplier4+multiplier5)/4;
-		if(multiplier>1)multiplier=1;
-		if(multiplier<.00001)multiplier=.00001;
-		if(visions==1&&mainmenu==0)multiplier/=3;
-		if(slomo)multiplier*=.2;
-		if(paused)multiplier=0;
+		multiplier = oldmult;
 
-		GetKeys( ( unsigned long * )theKeyMap );
-		if ( IsKeyDown( theKeyMap, MAC_COMMAND_KEY )&&IsKeyDown( theKeyMap, MAC_Q_KEY )){
+		do {
+			end = TimerGetTime(&theTimer);
+			timetaken = end - start;
+			framespersecond = 6e8 / timetaken;
+		} while (framespersecond > 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)
+			multiplier /= 3;
+		if (slomo)
+			multiplier /= 5;
+		if (paused)
+			multiplier = 0;
+
+		GetKeys((unsigned long*) theKeyMap);
+		if (IsKeyDown(theKeyMap, MAC_COMMAND_KEY)
+		    &&IsKeyDown(theKeyMap, MAC_Q_KEY)) {
 			gQuit = true;
-			if(score>highscore){
+			if (score > highscore) {
 				highscore=score;
 				/* TODO */
 				ofstream opstream("Data/Highscore");
@@ -398,14 +317,13 @@ void	Game::EventLoop( void )
 			}
 		}
 
-		if ( IsKeyDown( theKeyMap, MAC_ESCAPE_KEY )){
+		if (IsKeyDown(theKeyMap, MAC_ESCAPE_KEY)) {
 			alSourcePause(gSourceID[rainsound]);
-			mainmenu=1;
+			mainmenu = 1;
 			alSourcePlay(gSourceID[souloutsound]);
-			flashamount=1;
-			flashr=1;flashg=1;flashb=1;
+			flashamount = flashr = flashg = flashb = 1;
 			alSourceStop(gSourceID[visionsound]);
-			whichsong=mainmenusong;
+			whichsong = mainmenusong;
 			alSourceStop(gSourceID[knifesong]);
 			alSourceStop(gSourceID[shootsong]);
 			alSourceStop(gSourceID[zombiesong]);