-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioManager.cpp
170 lines (158 loc) · 5.45 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
160
161
162
163
164
165
166
167
168
169
#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, 0, SDL_HWSURFACE)),
fmap(), inputString("") {
if (screen == NULL) {
throw string("Unable to set video mode");
}
if ( TTF_Init() == -1 ) {
throw string("TTF_Init failed: ") + TTF_GetError();
}
fmap["smaller"] = TTF_OpenFont(Gamedata::getInstance()->getXmlStr("fontSmaller").c_str(),
Gamedata::getInstance()->getXmlInt("fontSmallerSize"));
if ( !fmap["smaller"] ) {
throw string("TTF_OpenFont failed: ") + TTF_GetError();
}
fmap["small"] = TTF_OpenFont(Gamedata::getInstance()->getXmlStr("fontSmall").c_str(),
Gamedata::getInstance()->getXmlInt("fontSmallSize"));
if ( !fmap["small"] ) {
throw string("TTF_OpenFont failed: ") + TTF_GetError();
}
fmap["normal"] = TTF_OpenFont(Gamedata::getInstance()->getXmlStr("fontNormal").c_str(),
Gamedata::getInstance()->getXmlInt("fontNormalSize"));
if ( !fmap["normal"] ) {
throw string("TTF_OpenFont failed: ") + TTF_GetError();
}
fmap["large"] = TTF_OpenFont(Gamedata::getInstance()->getXmlStr("fontLarge").c_str(),
Gamedata::getInstance()->getXmlInt("fontLargeSize"));
if ( !fmap["large"] ) {
throw string("TTF_OpenFont failed: ") + TTF_GetError();
}
fmap["larger"] = TTF_OpenFont(Gamedata::getInstance()->getXmlStr("fontLarger").c_str(),
Gamedata::getInstance()->getXmlInt("fontLargerSize"));
if ( !fmap["larger"] ) {
throw string("TTF_OpenFont failed: ") + TTF_GetError();
}
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_DisplayFormat(tmp);
if (image == NULL) {
image = tmp;
}
else {
SDL_FreeSurface(tmp);
}
return image;
}
void IOManager::printMessageAt(const string& msg, Uint32 x, Uint32 y, std::string font) const {
SDL_Rect dest = {x,y,0,0};
SDL_Color color = {0, 0, 0, 0};
std::map<std::string, TTF_Font*>::const_iterator it = fmap.find(font);
if(it!=fmap.end()){
SDL_Surface * stext = TTF_RenderText_Blended(it->second, 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");
}
}
else{
std::cout<<"Please use correct font size"<<std::endl;
}
}
void IOManager::printMessageCenteredAt( const string& msg, Uint32 y, std::string font) const {
SDL_Color color = {0, 0, 0, 0};
std::map<std::string, TTF_Font*>::const_iterator it = fmap.find(font);
if(it!=fmap.end()){
SDL_Surface *stext = TTF_RenderText_Blended(it->second, 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");
}
}
else{
std::cout<<"Please use correct font size"<<std::endl;
}
}
template <typename T>
void IOManager::printMessageValueAt(const string& msg, T value,
Uint32 x, Uint32 y, std::string font) const {
std::stringstream strm;
std::string message = msg;
strm << message << value << "\0";
message = strm.str();
SDL_Rect dest = {x,y,0,0};
SDL_Color color = {0, 0, 0, 0};
std::map<std::string, TTF_Font*>::const_iterator it = fmap.find(font);
if(it!=fmap.end()){
SDL_Surface *stext =
TTF_RenderText_Blended(it->second, 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");
}
}
else{
std::cout<<"Please use correct font size"<<std::endl;
}
}
void IOManager::printStringAfterMessage( const string& msg,
Uint32 x, Uint32 y, std::string font ) const {
printMessageAt(msg+inputString, x, y, font);
}
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, std::string) const;
template void IOManager::
printMessageValueAt(const string& msg, unsigned, Uint32, Uint32, std::string) const;
template void IOManager::
printMessageValueAt(const string& msg, int, Uint32, Uint32, std::string) const;