-
Notifications
You must be signed in to change notification settings - Fork 2
/
sdl_wrap.cc
135 lines (116 loc) · 3.48 KB
/
sdl_wrap.cc
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
#include "sdl_wrap.h"
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int SDL_Runner::x = 0;
Text::~Text() {}
Screen::Screen(int w, int h, std::string screenName) :
window{SDL_CreateWindow(screenName.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN)},
render{SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)},
w{w}, h{h}, screenName{screenName}, rects{} {
fonts["FUTURAM28"] = TTF_OpenFont("16020_FUTURAM.ttf", 28);
if (render == NULL) {
cerr << "Couldn't load renderer " << SDL_GetError() << endl;
}
}
Screen::~Screen() {
for (auto &p: fonts) {
TTF_CloseFont(p.second);
}
for (auto &t : msgs) {
SDL_DestroyTexture(t->texture);
}
for (auto &i : imgs) {
SDL_DestroyTexture(i.second);
}
SDL_DestroyRenderer(render);
SDL_DestroyWindow(window);
}
void Screen::draw_rect(int xloc, int yloc, int w, int h, Colour c) {
// No longer capped at 10 rects, we have a vector now.
rects.emplace_back(Rect{SDL_Rect{xloc, yloc, w, h}, c});
}
void Screen::add_font(string key, string path, unsigned int point) {
fonts[key] = TTF_OpenFont(path.c_str(), point);
if(fonts[key] == NULL)
{
cerr << "Failed to load font: " << path << endl;
}
}
void Screen::draw_string(string msg, int x, int y, Colour c, string fontKey) {
SDL_Surface *textSurface = TTF_RenderText_Solid(fonts[fontKey], msg.c_str(), c);
SDL_Texture *texture = SDL_CreateTextureFromSurface(render, textSurface);
if (texture == NULL) {
cerr << "Error loading string texture: " << SDL_GetError() << endl;
}
msgs.emplace_back(new OwningText{x, y, textSurface->w, textSurface->h, texture});
SDL_FreeSurface(textSurface);
}
void Screen::add_img(string key, string path) {
SDL_Surface *imgSurface = IMG_Load(path.c_str());
if (imgSurface == NULL) {
cerr << "Error loading image at path: " << path << " - " << IMG_GetError() << endl;
return;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(render, imgSurface);
if (texture == NULL) {
cerr << "Unable to create texture from image with path " << path << " - " << SDL_GetError();
return;
}
SDL_FreeSurface(imgSurface);
if (imgs.find(key) != imgs.end()) SDL_DestroyTexture(imgs[key]);
imgs[key] = texture;
}
void Screen::draw_img(std::string key, int x, int y) {
int w, h;
SDL_QueryTexture(imgs[key], NULL, NULL, &w, &h);
msgs.emplace_back(new NonOwningText{x, y, w, h, imgs[key]});
}
void Screen::update() {
SDL_RenderClear(render);
for (auto &r : rects) {
SDL_SetRenderDrawColor(render, r.c.r, r.c.g, r.c.b, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(render, &r.rect);
}
for (auto &m: msgs) {
SDL_Rect r{m->x, m->y, m->w, m->h};
SDL_RenderCopy(render, m->texture, nullptr, &r);
}
SDL_RenderPresent(render);
for (auto &m: msgs) {
delete m;
}
msgs.clear();
rects.clear();
// All rects are erased each update.
}
void Clock::start() {
started = true;
initTick = SDL_GetTicks();
}
int Clock::getTicks() {
if (started) {
return SDL_GetTicks() - initTick;
}
return 0;
}
SDL_Runner::SDL_Runner() {
if (!x) {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
cerr << "Error initializing SDL!" << endl;
}
if (TTF_Init() == -1) {
cerr << "Error initializing SDL Fonts" << endl;
}
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG )) {
cerr << "Error initializing SDL image" << endl;
}
++x;
}
}
SDL_Runner::~SDL_Runner() {
TTF_Quit();
IMG_Quit();
SDL_Quit();
}