-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameEngine.cpp
195 lines (184 loc) · 4.78 KB
/
GameEngine.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "GameEngine.h"
void GameEngine::init(unsigned int gridSize, unsigned int numWorkers) {
totalFrames = 0;
FPS = 5;
if (gridSize < 4 || gridSize > 1000000) {
gridSize = 10;
}
int pixelSize = 4;
if (gridSize > 150) {
pixelSize--;
if (gridSize > 300) {
pixelSize--;
}
}
if (gridSize < 75) {
pixelSize += 2;
if (gridSize < 50) {
pixelSize += 2;
}
}
SCR_HEIGHT = SCR_WIDTH = (pixelSize) * gridSize;
grid = new Grid(gridSize, pixelSize);
gWeb = new GridWeb(gridSize, numWorkers);
sTime = lUpdate = lDraw = mClock::now();
isRunning = true;
gameState = MAINMENU;
g_mode = G_PLAY;
}
void GameEngine::initSDL() {
if ((SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)) {
printf("Could not initialize SDL: %s.\n", SDL_GetError());
exit(-1);
}
gWindow = SDL_CreateWindow("Conway's Game of Life", 50, 50, SCR_WIDTH, SCR_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == nullptr) {
printf("Could not create window!");
}
ren = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == NULL) {
SDL_DestroyWindow(gWindow);
printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
SDL_Quit();
}
}
void GameEngine::update() {
totalFrames++;
if (g_mode == G_PLAY) {
grid->update();
}
}
int GameEngine::updateGame(void* self) {
GameEngine *g = (GameEngine*)self;
while (g->isRunning) {
if (g->updateRequired()) {
g->update();
}
}
return 0;
}
int GameEngine::renderGame(void* self) {
GameEngine *g = (GameEngine*)self;
while (g->isRunning) {
g->draw();
}
return 0;
}
int GameEngine::searchPatterns(void* self) {
GameEngine *g = (GameEngine*)self;
//g->gPlayer->start();
g->gWeb->startSearching();
return 0;
}
bool GameEngine::updateRequired() {
long long difference = (long long)(MILLIS_PER_SECOND / (FPS + 1)) - std::chrono::duration_cast<std::chrono::milliseconds>(mClock::now() - lUpdate).count();
if (difference <= 0) {
lUpdate = mClock::now();
return true;
}
return false;
}
bool GameEngine::drawRequired() {
long long difference = (long long)(MILLIS_PER_SECOND / (FPS + 1)) - std::chrono::duration_cast<std::chrono::milliseconds>(mClock::now() - lDraw).count();
if (difference <= 0) {
lDraw = mClock::now();
return true;
}
return false;
}
void GameEngine::handleEvent(SDL_Event e) {
static int mouseX;
static int mouseY;
if (e.type == SDL_MOUSEBUTTONDOWN) {
SDL_GetMouseState(&mouseX, &mouseY);
if (g_mode == G_PLAY) {
if (e.button.button == SDL_BUTTON_LEFT) {
grid->clear();
//grid->setState(gPlayer->previousFound());
grid->setState(gWeb->previousGrid());
}
if (e.button.button == SDL_BUTTON_RIGHT) {
grid->clear();
//grid->setState(gPlayer->nextFound());
grid->setState(gWeb->nextGrid());
//grid.drawGlider(mouseX, mouseY);
}
}
}
else if (e.type == SDL_KEYDOWN) {
int FPS_Map[10] = {0, 2, 5, 10, 20, 40,80, 160,320, 640};
SDL_Keycode KEY_Map[10] = {SDLK_0, SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5, SDLK_6, SDLK_7, SDLK_8, SDLK_9};
for (int i = 0; i < 9; i++) {
if (e.key.keysym.sym == KEY_Map[i]) {
FPS = FPS_Map[i];
i == 0 ? g_mode = G_FREEZE : g_mode = G_PLAY;
}
}
switch (e.key.keysym.sym) {
case SDLK_s:
if (this->gWeb->isSearching()) {
this->gWeb->stopSearching();
} else {
this->gWeb->startSearching();
}
}
}
}
void GameEngine::draw() {
SDL_RenderClear(ren);
grid->draw(ren);
SDL_RenderPresent(ren);
}
GameEngine::GameEngine() {
init();
initSDL();
}
GameEngine::GameEngine(unsigned int GridSize, unsigned int gridWork) {
init(GridSize, gridWork);
initSDL();
}
GameEngine::~GameEngine() {
//delete gPlayer;
delete gWeb;
}
void GameEngine::run() {
int updateResult, drawResult, searchResult;
updateThread = SDL_CreateThread(updateGame, "Update", this);
drawThread = SDL_CreateThread(renderGame, "Render", this);
searchThread = SDL_CreateThread(searchPatterns, "Search", this);
this->gWeb->stopSearching();
while (isRunning) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_CLOSE)
isRunning = false;
}
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_ESCAPE)
isRunning = false;
}
if (isRunning) handleEvent(event);
}
}
//if (gPlayer != nullptr) {
// gPlayer->stop();
//}
if (gWeb != nullptr) {
gWeb->stopSearching();
}
printf("FRAMES: %d FPS: %lld", totalFrames, (totalFrames*MILLIS_PER_SECOND) / ((std::chrono::duration_cast<std::chrono::milliseconds>(mClock::now() - sTime).count())));
SDL_WaitThread(updateThread, &updateResult);
SDL_WaitThread(drawThread, &drawResult);
SDL_WaitThread(searchThread, &searchResult);
quit();
std::cin.get();
}
void GameEngine::quit() {
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(gWindow);
// Mix_Quit();
// TTF_Quit();
// IMG_Quit();
SDL_Quit();
}