summary refs log tree commit diff
path: root/src/GameLoop.cpp
blob: 9e42b8868a8ecc7af48cad2c1813f9720210826d (plain) (blame)
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
// Event loop handler
// 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.
//
// 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 float 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;

void keyCallback(Game* game, int key, int action, int mods)
{
	if (game->mainmenu || action != GLFW_PRESS)
		return;

	auto shift = mods & GLFW_MOD_SHIFT;
	auto player = game->person[0];
	XYZ facing;

	switch(key) {
	case GLFW_KEY_L:
		game->lasersight ^= 1;
		break;
	case GLFW_KEY_K:
		if (game->debug)
			game->timeremaining = 0;
		break;
	case GLFW_KEY_B:
		if (!game->debug)
			break;
		alSourcePlay(gSourceID[soulinsound]);
		if (shift) {
			game->paused = 1 - game->paused;
		} else {
			auto 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 GLFW_KEY_F:
		if (!game->debug)
			break;
		alSourcePlay(gSourceID[souloutsound]);

		facing = {0, 0, -1};
		facing = DoRotation(facing, -camera.rotation2, 0, 0);
		facing = DoRotation(facing, 0, 0 - camera.rotation, 0);

		for (int i = 1; i < game->numpeople; i++) {
			auto& person = game->person[i];
			if (person.skeleton.free == 1
			    || findDistancefast(person.playercoords,
						player.playercoords) > 1000)
				continue;
			person.skeleton.free = 1;
			person.longdead = 1;

			for (auto& joint : person.skeleton.joints) {
				joint.position = DoRotation(joint.position,
					0, person.playerrotation, 0);
				joint.position += person.playercoords;
				joint.realoldposition = joint.position;
				joint.velocity = DoRotation(joint.velocity,
					0, person.playerrotation, 0);
				joint.velocity += person.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 GLFW_KEY_X:
		if (!game->debug || !shift || player.grenphase)
			break;
		player.ammo = -1;
		player.whichgun++;
		player.grenphase = 0;
		player.reloads[player.whichgun] = 3;
		if (player.whichgun > 7)
			player.whichgun = 0;
		break;
	}
}

void eventLoop(Game* game)
{
	auto start = glfwGetTime();
	GLfloat oldmult = multiplier;
	int colaccuracy = min(game->sps,
		game->sps / game->framespersecond + 1.0f);
	multiplier /= colaccuracy;
	for (int i = 0; i < (int) (colaccuracy + 0.5f); i++)
		game->Tick();
	auto window = glfwGetCurrentContext();
	if (game->DrawGLScene())
		glfwSwapBuffers(window);
	else
		glfwSetWindowShouldClose(window, GLFW_TRUE);
	multiplier = oldmult;

	do game->framespersecond = 1.0 / (glfwGetTime() - start);
	while (game->framespersecond > game->maxfps);

	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 (game->paused)
		multiplier = 0;

	if (IsKeyDown(GLFW_KEY_ESCAPE)) {
		alSourcePause(gSourceID[rainsound]);
		game->mainmenu = 1;
		alSourcePlay(gSourceID[souloutsound]);
		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);
	}
}