-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
106 lines (89 loc) · 2.46 KB
/
Copy pathGame.cpp
File metadata and controls
106 lines (89 loc) · 2.46 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
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
#include "Game.h"
#include "Cities.h"
#include <stdlib.h>
#include <time.h>
Game::~Game()
{
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
}
bool Game::Initialize()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return false;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
window = SDL_CreateWindow(WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL);
if (!window) {
SDL_Log("Failed to create window: %s", SDL_GetError());
return false;
}
context = SDL_GL_CreateContext(window);
if (!context) {
SDL_Log("Failed to create context: %s", SDL_GetError());
return false;
}
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
SDL_Log("Failed to initialize glew");
return false;
}
glGetError();
isRunning = true;
ticksCount = SDL_GetTicks();
srand(time(NULL));
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_SPACE) {
ResetDestination();
}
break;
}
}
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_ESCAPE]) {
isRunning = false;
}
ProcessKeyboard(state);
}
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);
}