forked from MetaCipher/the-sdl-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cpp
123 lines (91 loc) · 2.92 KB
/
App.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
//==============================================================================
#include "App.h"
#include "Log.h"
App App::Instance;
//==============================================================================
App::App() {
}
//------------------------------------------------------------------------------
void App::OnEvent(SDL_Event* Event) {
}
//------------------------------------------------------------------------------
bool App::Init() {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
Log("Unable to Init SDL: %s", SDL_GetError());
return false;
}
if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
Log("Unable to Init hinting: %s", SDL_GetError());
}
if((Window = SDL_CreateWindow(
"My SDL Game",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WindowWidth, WindowHeight, SDL_WINDOW_SHOWN)
) == NULL) {
Log("Unable to create SDL Window: %s", SDL_GetError());
return false;
}
PrimarySurface = SDL_GetWindowSurface(Window);
if((Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED)) == NULL) {
Log("Unable to create renderer");
return false;
}
SDL_SetRenderDrawColor(Renderer, 0x00, 0x00, 0x00, 0xFF);
// Initialize image loading for PNGs
if(!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
Log("Unable to init SDL_image: %s", IMG_GetError());
return false;
}
// Load all of our Textures (see TextureBank class for expected folder)
if(TextureBank::Init() == false) {
Log("Unable to init TextureBank");
return false;
}
return true;
}
//------------------------------------------------------------------------------
void App::Loop() {
}
//------------------------------------------------------------------------------
void App::Render() {
SDL_RenderClear(Renderer);
TextureBank::Get("tex1")->Render(0, 0); // You should really check your pointers
SDL_RenderPresent(Renderer);
}
//------------------------------------------------------------------------------
void App::Cleanup() {
TextureBank::Cleanup();
if(Renderer) {
SDL_DestroyRenderer(Renderer);
Renderer = NULL;
}
if(Window) {
SDL_DestroyWindow(Window);
Window = NULL;
}
IMG_Quit();
SDL_Quit();
}
//------------------------------------------------------------------------------
int App::Execute(int argc, char* argv[]) {
if(!Init()) return 0;
SDL_Event Event;
while(Running) {
while(SDL_PollEvent(&Event) != 0) {
OnEvent(&Event);
if(Event.type == SDL_QUIT) Running = false;
}
Loop();
Render();
SDL_Delay(1); // Breath
}
Cleanup();
return 1;
}
//==============================================================================
SDL_Renderer* App::GetRenderer() { return Renderer; }
//==============================================================================
App* App::GetInstance() { return &App::Instance; }
int App::GetWindowWidth() { return WindowWidth; }
int App::GetWindowHeight() { return WindowHeight; }
//==============================================================================