-
Notifications
You must be signed in to change notification settings - Fork 0
/
Display.cpp
79 lines (60 loc) · 1.47 KB
/
Display.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
#include "Display.h"
#include "Renderable.h" // including this here to avoid circular dependancies
Display::Display(int w, int h, const char* windowTitle, SDL_Color backgroundColor)
{
title = windowTitle;
this->backgroundColor = backgroundColor;
w_ = w;
h_ = h;
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, 0);
if (!window)
SDL_LogError(0, "failed to create window");
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer)
SDL_LogError(0, "failed to create renderer");
}
Display::~Display()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
exit();
}
void Display::init()
{
SDL_Init(SDL_INIT_VIDEO < 0);
IMG_Init(IMG_INIT_PNG);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
TTF_Init();
}
void Display::exit()
{
TTF_Quit();
}
void Display::RenderAll()
{
renderList.sort([](Renderable* item1, Renderable* item2)
{
return item1->zOrder < item2->zOrder;
});
for (Renderable* item : renderList)
{
item->RenderCall(renderer);
}
}
void Display::AddRenderable(Renderable* toAdd)
{
renderList.push_back(toAdd);
}
void Display::ProcessRender()
{
SDL_RenderPresent(renderer);
}
void Display::PrepareRender()
{
SDL_SetRenderDrawColor(renderer, backgroundColor.r, backgroundColor.g, backgroundColor.b, backgroundColor.a);
SDL_RenderClear(renderer);
}
void Display::ClearRenderables()
{
renderList.clear();
}