-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathioManager.cpp
More file actions
164 lines (149 loc) · 4.79 KB
/
ioManager.cpp
File metadata and controls
164 lines (149 loc) · 4.79 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <iomanip>
#include "ioManager.h"
using std::string;
IOManager& IOManager::getInstance() {
static IOManager io;
return io;
}
IOManager::IOManager( ) :
gdata( Gamedata::getInstance() ),
viewWidth( gdata.getXmlInt("view/Width") ),
viewHeight( gdata.getXmlInt("view/Height") ),
MAX_STRING_SIZE( gdata.getXmlInt("maxStringSize") ),
// The 3rd and 4th parameters are just as important as the first 2!
screen(SDL_SetVideoMode(viewWidth, viewHeight, 32, SDL_DOUBLEBUF)),
font( NULL ),
color(),
inputString("")
{
if (screen == NULL) {
throw string("Unable to set video mode; screen is NULL in IOMAnager");
}
if ( TTF_Init() == -1 ) {
throw string("TTF_Init failed: ") + TTF_GetError();
}
font = TTF_OpenFont(
gdata.getXmlStr("font/File").c_str(),
gdata.getXmlInt("font/Size")
);
if ( !font ) {
throw string("TTF_OpenFont failed: ") + TTF_GetError();
}
color.r = gdata.getXmlInt("font/Red");
color.g = gdata.getXmlInt("font/Green");
color.b = gdata.getXmlInt("font/Blue");
color.unused = gdata.getXmlInt("font/Unused");
SDL_EnableUNICODE( SDL_ENABLE );
atexit(TTF_Quit);
}
SDL_Surface* IOManager::loadAndSet(const string& filename, bool setcolorkey) const {
SDL_Surface *tmp = IMG_Load(filename.c_str());
if (tmp == NULL) {
throw string("Unable to load bitmap ")+filename;
}
if ( setcolorkey ) {
Uint32 colorkey = SDL_MapRGB(tmp->format, 255, 0, 255);
SDL_SetColorKey(tmp, SDL_SRCCOLORKEY|SDL_RLEACCEL, colorkey);
}
// Optimize the strip for fast display
SDL_Surface *image = SDL_DisplayFormatAlpha(tmp);
if (image == NULL) {
image = tmp;
}
else {
SDL_FreeSurface(tmp);
}
return image;
}
void IOManager::printMessageAt(const string& msg, Uint32 x, Uint32 y) const {
SDL_Rect dest = {x,y,0,0};
SDL_Surface * stext = TTF_RenderText_Blended(font, msg.c_str(), color);
if (stext) {
SDL_BlitSurface( stext, NULL, screen, &dest );
SDL_FreeSurface(stext);
}
else {
throw
string("Couldn't allocate text sureface in printMessageAt");
}
}
void IOManager::printMessageAt(const string& msg, Uint32 x, Uint32 y, const string& fontname) const {
TTF_Font * deadfont = TTF_OpenFont(
gdata.getXmlStr(fontname+"/File").c_str(),
gdata.getXmlInt(fontname+"/Size")
);
SDL_Color deadcolor;
deadcolor.r = gdata.getXmlInt(fontname+"/Red");
deadcolor.g = gdata.getXmlInt(fontname+"/Green");
deadcolor.b = gdata.getXmlInt(fontname+"/Blue");
SDL_Rect dest = {x,y,0,0};
SDL_Surface * stext = TTF_RenderText_Blended(deadfont, msg.c_str(), deadcolor);
TTF_CloseFont(deadfont);
if (stext) {
SDL_BlitSurface( stext, NULL, screen, &dest );
SDL_FreeSurface(stext);
}
else {
throw
string("Couldn't allocate text sureface in printMessageAt");
}
}
void IOManager::printMessageCenteredAt( const string& msg, Uint32 y) const {
SDL_Surface *stext = TTF_RenderText_Blended(font, msg.c_str(), color);
if (stext) {
Uint32 x = ( viewWidth - stext->w ) / 2;
SDL_Rect dest = {x,y,0,0};
SDL_BlitSurface( stext, NULL, screen, &dest );
SDL_FreeSurface(stext);
}
else {
throw
string("Couldn't allocate text sureface in printMessageCenteredAt");
}
}
template <typename T>
void IOManager::printMessageValueAt(const string& msg, T value,
Uint32 x, Uint32 y) const {
std::stringstream strm;
std::string message = msg;
strm << message << value << "\0";
message = strm.str();
SDL_Rect dest = {x,y,0,0};
SDL_Surface *stext =
TTF_RenderText_Blended(font, message.c_str(), color);
if (stext) {
SDL_BlitSurface( stext, NULL, screen, &dest );
SDL_FreeSurface(stext);
}
else {
throw
string("Couldn't allocate text sureface in printMessageValueAt");
}
}
void IOManager::printStringAfterMessage( const string& msg,
Uint32 x, Uint32 y ) const {
printMessageAt(msg+inputString, x, y);
}
void IOManager::buildString(SDL_Event event) {
if( inputString.size() <= MAX_STRING_SIZE) {
unsigned ch = event.key.keysym.sym;
if ( isalpha(ch) || isdigit(ch) || ch == ' ') {
inputString += char(event.key.keysym.unicode);
}
}
if( event.key.keysym.sym == SDLK_BACKSPACE
&& inputString.length() > 0 ) {
// remove a character from the end
int length = inputString.size();
inputString.erase( length - 1 );
}
}
template void IOManager::
printMessageValueAt(const string& msg, unsigned long, Uint32, Uint32) const;
template void IOManager::
printMessageValueAt(const string& msg, float, Uint32, Uint32) const;
template void IOManager::
printMessageValueAt(const string& msg, unsigned, Uint32, Uint32) const;
template void IOManager::
printMessageValueAt(const string& msg, int, Uint32, Uint32) const;