-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
111 lines (83 loc) · 2.39 KB
/
Game.cpp
File metadata and controls
111 lines (83 loc) · 2.39 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
107
108
109
110
111
#include "Game.h"
//#include "ObjectDataBase.h"
//important global variables declared and insantiated.
ObjectDataBase allObjects;
SDL_Renderer* Game::renderer = nullptr;
SDL_Event event;
std::vector<Event> eventHandle = {};
//Game class consturctor and destructor
Game::Game()
{
}
Game::~Game()
{
}
//initializes the game, only mess with this if you have to
void Game::init(const char* title, int width, int height, bool fullscreen)
{
//renderer = rendererStorage::renderer;
int flags = 0;
mouseClick = false;
if (fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
}
isRunning = true;
}
//
TTF_Init();
//allObjects.addObject(new TextObject("title", "arial.ttf", 40, 178, 34, 34, 650, 0, "Viking Raids!"));
rendererStorage::renderer = renderer;
eventHandles = &eventHandle;
//set all startup items after this
}
//event handler for SDL events and user created button events
void Game::handleEvents()
{
// this section should be used when you need to access variables within the game class from button actions
while (eventHandles->size() > 0) {
//this goes first
std::string action = eventHandles->at(0).ID;
//define events in here sent by buttons
//this goes last
eventHandles->erase(eventHandles->begin() + 0);
}
//this is SDL's event handling section
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
isRunning = false;
break;
default:
break;
}
}
//runs every frame, where most of your code in this file should take place aside from buttons and event handles
void Game::update()
{
allObjects.updateObjects();
}
//this is where objects are generated on the screen, there is little work you should be doing here unless you are declaring new objects that are not part of the object database
void Game::render()
{
SDL_RenderClear(renderer);
allObjects.renderObjects();
SDL_RenderPresent(renderer); //make sure this goes last
}
//this is run when the game closes, leave this as is.
void Game::clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
TTF_Quit();
SDL_Quit();
}