-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
139 lines (107 loc) · 2.64 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
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
#include <iostream>
#include "Game.h"
using namespace std;
// constructor
Game::Game()
{
// set the SDL Window and Renderer to null in case it has memory
SdlWindow = nullptr;
SdlRenderer = nullptr;
// Initialise the subsytem in the SDL2 Framework
if (SDL_InitSubSystem(SDL_INIT_EVERYTHING) != 0) {
bIsGameOver = true;
// if failed to initialise subsystem, tell us on the console
cout << "Initialise SDL - failed" << endl;
}
else {
// enable the game loop
bIsGameOver = false;
// if the subsytem succesfully initialises
cout << "Initialise SDL - success" << endl;
}
}
// deconstructor
Game::~Game()
{
}
bool Game::Start()
{
// create the sdl renderer and define it
SdlRenderer = SDL_CreateRenderer(SdlWindow, 0, -1);
// make sure the renderer worked
if (SdlRenderer != nullptr) {
cout << "Create Renderer - success" << endl;
return true;
}
cout << "Create Renderer - failed" << endl;
return false;
}
void Game::ProcessInput()
{
// @ TODO: Process player inputs
}
void Game::Update()
{
// @ TODO: Add and changes to the game each frame
// get how many seconds it's been
int Seconds = SDL_GetTicks() / 1000;
// after 10 seconds kill the program
if (Seconds > 9) {
bIsGameOver = true;
}
}
void Game::Draw()
{
// set the draw colour
SDL_SetRenderDrawColor(SdlRenderer, 15, 15, 15, 255);
// clear the renderer
SDL_RenderClear(SdlRenderer);
// @ TODO: Draw stuff here
SDL_RenderPresent(SdlRenderer);
}
void Game::Run(const char* title, int width, int height, bool fullscreen)
{
// define the creation flag
int CreationFlag = 0;
// if fullscreen is set to false then set windowed mode
if (!fullscreen) {
CreationFlag = SDL_WINDOW_SHOWN;
}
else {
CreationFlag = SDL_WINDOW_FULLSCREEN;
}
// create the SDL Window
SdlWindow = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height, CreationFlag);
// check if the SDL window worked
// create the renderer and start the game loop
if (SdlWindow != nullptr && Start()) {
cout << "Create Window - success" << endl;
// run the game loop
while (!bIsGameOver) {
// check for player input
ProcessInput();
// process any changes that were made either by input or game code
Update();
// draw everything to the screen
Draw();
}
}
// debug if we failed or exited
cout << "Create Window - failed or was exited" << endl;
Shutdown();
Destroy();
}
void Game::Shutdown()
{
// @ TODO: deallocate all of the stuff we add in start
}
void Game::Destroy()
{
// deallocate the window
SDL_DestroyWindow(SdlWindow);
// deallocate the renderer
SDL_DestroyRenderer(SdlRenderer);
// shut down the SDL framework
SDL_Quit();
}