-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioManager.cpp
110 lines (102 loc) · 3.13 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
#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, 255, 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, Sint16 x, Sint16 y) const {
Uint16 zero = 0;
SDL_Rect dest = {x,y,zero,zero};
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::printMessageCenteredAt( const string& msg, Sint16 y) const {
SDL_Surface *stext = TTF_RenderText_Blended(font, msg.c_str(), color);
if (stext) {
Sint16 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");
}
}
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 );
}
}