-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
165 lines (138 loc) · 3.58 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include "SDL_ttf.h"
#include "Game.hpp"
#include "Colors.hpp"
#include "Constants.hpp"
SDL_Renderer* Game::renderer = nullptr;
TTF_Font* Game::defaultFont = nullptr;
Game::Game()
{
init("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, false);
// non-sdl initialization
map = std::make_unique<LevelMap>();
}
Game::~Game()
{
clean();
}
const bool Game::isRunning() const
{
return running;
}
void Game::init(const char* title, int xpos, int ypos, bool fullScreen)
{
// check return code of SDL_Init() to get
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
SDL_DisplayMode displayMode;
SDL_GetCurrentDisplayMode(0, &displayMode);
std::cout << "Subsystems initialized..." << std::endl;
window = SDL_CreateWindow(title, xpos, ypos, fullScreen ? displayMode.w : 1280, fullScreen ? displayMode.h : 720,
fullScreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
if (window)
{
std::cout << "Window initialized..." << std::endl;
}
else
{
std::cout << "Error initializing main window. Exiting..." << std::endl;
return;
}
// Default is hardware accelerated, but specifying it here for clarity
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer)
{
SDL_SetRenderDrawColor(renderer, Colors::black.r, Colors::black.g, Colors::black.b, Colors::black.a);
std::cout << "Renderer initialized..." << std::endl;
}
else
{
std::cout << "Error initializing renderer. Exiting..." << std::endl;
return;
}
// TTF_Init() returns 0 on success for some reason
if (TTF_Init() == 0)
{
std::cout << "TrueType fonts library and default font (SDL_ttf) initialized..." << std::endl;
}
else
{
std::cout << "Error with message '" << TTF_GetError() <<
"' while initializing TrueType fonts library(SDL_ttf)" << std::endl;
return;
}
//Game::defaultFont = TTF_OpenFont("Fonts\\OpenSans-Regular.ttf", Game::fontSize);
Game::defaultFont = TTF_OpenFont("Fonts\\Cascadia.ttf", Game::fontSize);
if (defaultFont)
{
std::cout << "Default font initialized..." << std::endl;
}
else
{
std::cout << "Error with message '" << TTF_GetError() << "' while initializing default font" << std::endl;
return;
}
//map = std::make_unique<LevelMap>();
std::cout << "Map Initialized..." << std::endl;
std::cout << "Game Elements initialized..." << std::endl;
std::cout << "Initialization finished. Running game..." << std::endl;
running = true;
}
}
void Game::handleEvents()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN: {
auto state = SDL_GetKeyboardState(nullptr);
if (state[SDL_SCANCODE_UP])
{
map->movePlayable(Directions::UP);
}
else if (state[SDL_SCANCODE_DOWN])
{
map->movePlayable(Directions::DOWN);
}
else if (state[SDL_SCANCODE_LEFT])
{
map->movePlayable(Directions::LEFT);
}
else if (state[SDL_SCANCODE_RIGHT])
{
map->movePlayable(Directions::RIGHT);
}
else if (state[SDL_SCANCODE_ESCAPE])
{
running = false;
}
}
default:
break;
}
}
}
void Game::update()
{
map->update();
}
void Game::render()
{
// clear buffer and screen back to default color specified in SDL_SetRenderDrawColor
SDL_RenderClear(renderer);
map->render();
// render to the screen
SDL_RenderPresent(renderer);
}
void Game::clean()
{
TTF_CloseFont(Game::defaultFont);
TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
std::cout << "Game Cleaned." << std::endl;
}