-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
57 lines (50 loc) · 1.4 KB
/
Game.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
#include "Game.h"
Game::Game() : mWindow(nullptr), mIsRunning(true) {}
bool Game::Initialize() {
// Initialize SDL
int sdlResult = SDL_Init(SDL_INIT_VIDEO);
if (sdlResult != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return false;
}
// Create an SDL Window
mWindow = SDL_CreateWindow("Game Programming in C++ (Chapter 1)",
100, // Top left x-coordinate of window
100, // Top left y-coordinate of window
1024, // Width of window
768, // Height of window
0 // Flags
);
if (!mWindow) {
SDL_Log("Failed to create window: %s", SDL_GetError());
return false;
}
return true;
}
void Game::Shutdown() {
SDL_DestroyWindow(mWindow);
SDL_Quit();
}
void Game::RunLoop() {
while (mIsRunning) {
ProcessInput();
UpdateGame();
GenerateOutput();
}
}
void Game::ProcessInput() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
mIsRunning = false;
break;
}
}
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_ESCAPE]) {
mIsRunning = false;
}
}
void Game::UpdateGame() {}
void Game::GenerateOutput() {}