-
Notifications
You must be signed in to change notification settings - Fork 1
/
ioManager.cpp
160 lines (147 loc) · 4.69 KB
/
ioManager.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
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
#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("viewWidth") ),
viewHeight( gdata->getXmlInt("viewHeight") ),
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 ),
damageFont( NULL ),
color(),
title( gdata->getXmlStr("screenTitle") ),
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(
Gamedata::getInstance()->getXmlStr("fontFile").c_str(),
Gamedata::getInstance()->getXmlInt("fontSize")
);
damageFont = TTF_OpenFont(
Gamedata::getInstance()->getXmlStr("fontFile").c_str(),
36
);
if ( !font ) {
throw string("TTF_OpenFont failed: ") + TTF_GetError();
}
color.r = Gamedata::getInstance()->getXmlInt("fontRed");
color.g = Gamedata::getInstance()->getXmlInt("fontGreen");
color.b = Gamedata::getInstance()->getXmlInt("fontBlue");
color.unused = Gamedata::getInstance()->getXmlInt("fontUnused");
SDL_EnableUNICODE( SDL_ENABLE );
SDL_WM_SetCaption(title.c_str(), NULL);
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::printDamage(const double& msg, Uint32 x, Uint32 y) const {
std::ostringstream s;
s << msg << "%";
SDL_Rect dest = {x,y,0,0};
SDL_Surface * stext = TTF_RenderText_Blended(damageFont, s.str().c_str(), color);
if (stext) {
SDL_BlitSurface( stext, NULL, screen, &dest );
SDL_FreeSurface(stext);
}
else {
throw
string("Couldn't allocate text sureface in printDamage");
}
}
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, 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;