-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioMod.cpp
79 lines (66 loc) · 2.22 KB
/
ioMod.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 <SDL_image.h>
#include "ioMod.h"
#include "gameData.h"
#include "renderContext.h"
IoMod& IoMod::getInstance() {
static IoMod instance;
return instance;
}
IoMod::~IoMod() {
TTF_CloseFont(font);
TTF_Quit();
}
IoMod::IoMod() :
init(TTF_Init()),
renderer( (RenderContext::getInstance()).getRenderer() ),
font(TTF_OpenFont(Gamedata::getInstance().getXmlStr("font/file").c_str(),
Gamedata::getInstance().getXmlInt("font/size"))),
textColor({0xff, 0, 0, 0})
{
if ( init == -1 ) {
throw std::string("error: Couldn't init font");
}
if (font == NULL) {
throw std::string("error: font not found");
}
textColor.r = Gamedata::getInstance().getXmlInt("font/red");
textColor.g = Gamedata::getInstance().getXmlInt("font/green");
textColor.b = Gamedata::getInstance().getXmlInt("font/blue");
textColor.a = Gamedata::getInstance().getXmlInt("font/alpha");
}
SDL_Texture* IoMod::readTexture(const std::string& filename) {
SDL_Texture *texture = IMG_LoadTexture(renderer, filename.c_str());
if ( texture == NULL ) {
throw std::string("Couldn't load ") + filename;
}
return texture;
}
SDL_Surface* IoMod::readSurface(const std::string& filename) {
SDL_Surface *surface = IMG_Load(filename.c_str());
if ( !surface ) {
throw std::string("Couldn't load ") + filename;
}
return surface;
}
void IoMod::writeText(const std::string& msg, int x, int y) const {
SDL_Surface* surface =
TTF_RenderText_Solid(font, msg.c_str(), textColor);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
int textWidth = surface->w;
int textHeight = surface->h;
SDL_FreeSurface(surface);
SDL_Rect dst = {x, y, textWidth, textHeight};
SDL_RenderCopy(renderer, texture, NULL, &dst);
SDL_DestroyTexture(texture);
}
void IoMod::writeText(const std::string& msg, int x, int y, SDL_Color color) const {
SDL_Surface* surface =
TTF_RenderText_Solid(font, msg.c_str(), color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
int textWidth = surface->w;
int textHeight = surface->h;
SDL_FreeSurface(surface);
SDL_Rect dst = {x, y, textWidth, textHeight};
SDL_RenderCopy(renderer, texture, NULL, &dst);
SDL_DestroyTexture(texture);
}