This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SdlGame.cpp
101 lines (85 loc) · 2.72 KB
/
SdlGame.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
#include "SdlGame.h"
#include "utils/SdlTimer.h"
#include "utils/SdlGraphicsDevice.h"
namespace gamebase { namespace sdl
{
SdlGame::SdlGame() : SdlGame("Gamebase SDL", 640, 480)
{
}
SdlGame::SdlGame(std::string title, unsigned int windowWidth, unsigned int windowHeight)
: Gamebase(SdlTimer::Create()),
window(NULL),
renderer(NULL),
title("Hey there!"),
windowWidth(windowWidth),
windowHeight(windowHeight)
{
backgroundColor.r = backgroundColor.g = backgroundColor.b = 0x00;
backgroundColor.a = 0x0;
}
bool SdlGame::Initialize()
{
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return false;
}
//Set texture filtering to linear
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
}
//Create the window
int flags = SDL_WINDOW_SHOWN;
window = SDL_CreateWindow(title.c_str(),
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
windowWidth, windowHeight, flags);
if (window == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
return false;
}
//Create renderer for window
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
return false;
}
std::unique_ptr<IGraphicsDevice> device(new SdlGraphicsDevice (renderer, backgroundColor));
this->registerGraphicsDevice(std::move(device));
return true;
}
void SdlGame::Uninitialize()
{
// Free resources & zero pointers
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window = NULL;
renderer = NULL;
// Quit
SDL_Quit();
}
void SdlGame::Update()
{
SDL_Event e;
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
Quit();
// Quit if the user presses ESC when the window is fullscreen
if (isFullscreen && e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) {
Quit();
}
}
}
void SdlGame::Draw()
{
}
void SdlGame::Load() { }
void SdlGame::Unload() { }
}}