-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
77 lines (65 loc) · 1.69 KB
/
Copy pathGame.cpp
File metadata and controls
77 lines (65 loc) · 1.69 KB
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
#include "Game.h"
bool Game::Initialize()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return false;
}
window = SDL_CreateWindow(WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
if (!window) {
SDL_Log("Failed to create window: %s", SDL_GetError());
return false;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
SDL_Log("Failed to create renderer: %s", SDL_GetError());
return false;
}
isRunning = true;
ticksCount = SDL_GetTicks();
return true;
}
void Game::RunLoop()
{
while (isRunning) {
ProcessInput();
UpdateGame();
GenerateOutput();
}
}
void Game::ProcessInput()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
isRunning = false;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
isRunning = false;
} else {
ProcessKeydown(event.key.keysym.sym);
}
break;
}
}
}
void Game::UpdateGame()
{
while (!SDL_TICKS_PASSED(SDL_GetTicks(), ticksCount + 16)) {
}
float deltaTime = (SDL_GetTicks() - ticksCount) / 1000.0f;
if (deltaTime > 0.05f) {
deltaTime = 0.05f;
}
ticksCount = SDL_GetTicks();
UpdateGame(deltaTime);
}
void Game::Shutdown()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}