-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextObject.cpp
238 lines (205 loc) · 5.5 KB
/
TextObject.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Greyout - a colourful platformer about love
Greyout is Copyright (c)2011-2014 Janek Schäfer
This file is part of Greyout.
Greyout is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Greyout is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Please direct any feedback, questions or comments to
Janek Schäfer (foxblock), foxblock_at_gmail_dot_com
*/
#include "TextObject.h"
#include "StringUtility.h"
#include "Level.h"
#include "MusicCache.h"
#include "MyGame.h"
TextObject::TextObject(Level* newParent) : BaseUnit(newParent)
{
stringToProp["font"] = tpFont;
stringToProp["fontsize"] =tpFontSize;
stringToProp["text"] = tpText;
flags.addFlag(ufNoMapCollision);
flags.addFlag(ufNoGravity);
unitCollisionMode = 0;
col = BLACK;
line = "";
currentText = NULL;
fontSize = 24;
size = Vector2di(-1,-1);
}
TextObject::~TextObject()
{
delete currentText;
}
///---public---
bool TextObject::load(list<PARAMETER_TYPE >& params)
{
bool result = BaseUnit::load(params);
if (line[0] == 0)
{
printf("ERROR: No text specified for TextObject: %s\n",id.c_str());
return false;
}
if (currentText == NULL)
{
printf("ERROR: Failed to load or missing font on TextObject: %s\n",id.c_str());
return false;
}
currentText->setColour(col);
currentText->setWrapping(false);
// calculate automatically if not passed in level file (often inaccurate!)
if (size.x == -1 && size.y == -1)
{
SDL_Surface *dummy = SDL_CreateRGBSurface(SDL_SWSURFACE,1,1,
GFX::getVideoSurface()->format->BitsPerPixel,0,0,0,0);
// this also calculates the size of a Penjin::Text object
render(dummy);
size = currentText->getDimensions();
SDL_FreeSurface(dummy);
}
return result;
}
bool TextObject::processParameter(const PARAMETER_TYPE& value)
{
if (BaseUnit::processParameter(value))
return true;
bool parsed = true;
switch (stringToProp[value.first])
{
case tpFont:
{
delete currentText;
currentText = new Text;
PENJIN_ERRORS err = currentText->loadFont(value.second,fontSize);
if (err != PENJIN_OK)
{
printf("ERROR: Loading font \"%s\" in size %i failed: %s\n",value.second.c_str(),fontSize,TTF_GetError());
delete currentText;
currentText = NULL;
return false;
}
break;
}
case tpFontSize:
{
int val = StringUtility::stringToInt(value.second);
if (val > 0)
{
fontSize = val;
if (currentText)
currentText->setFontSize(val);
}
else
parsed = false;
break;
}
case BaseUnit::upSize:
{
// As Penjin's text size calculation is often incorrect you can manually
// overwrite the value to prevent drawing errors
// Bloody inconvenient, I am a lazy fuck, deal with it
size = StringUtility::stringToVec<Vector2di>(value.second);
break;
}
case tpText:
{
line = StringUtility::upper(value.second);
break;
}
default:
parsed = false;
}
return parsed;
}
void TextObject::updateScreenPosition(const Vector2di& offset)
{
currentText->setPosition(position - offset);
}
void TextObject::render(SDL_Surface* screen)
{
currentText->setColour(col);
currentText->print(screen,line);
}
void TextObject::explode()
{
SDL_Surface* temp = SDL_CreateRGBSurface(SDL_SWSURFACE,getWidth(),getHeight(),GFX::getVideoSurface()->format->BitsPerPixel,0,0,0,0);
Colour none = BLACK;
if (col != MAGENTA)
{
SDL_FillRect(temp, NULL, SDL_MapRGB(temp->format,255,0,255));
none = MAGENTA;
}
currentText->setPosition(0,0);
currentText->print(temp,line);
int bpp = temp->format->BytesPerPixel;
Colour pix = MAGENTA;
Vector2df vel(0,0);
int time = 0;
int inc;
switch (ENGINE->settings->getParticleDensity())
{
case Settings::pdOff:
MUSIC_CACHE->playSound("sounds/die.wav",parent->chapterPath);
toBeRemoved = true;
SDL_FreeSurface(temp);
return;
case Settings::pdFew:
inc = round(max((float)(getWidth() + getHeight()) / 32.0f,4.0f));
break;
case Settings::pdMany:
inc = round(max((float)(getWidth() + getHeight()) / 64.0f,2.0f));
break;
case Settings::pdTooMany:
inc = 1;
break;
default:
inc = round(max((float)(getWidth() + getHeight()) / 64.0f,2.0f));
break;
}
for (int X = getWidth()-1; X >= 0; X-=inc)
{
for (int Y = getHeight()-1; Y >= 0; Y-=inc)
{
Uint8 *p = (Uint8 *)temp->pixels + Y * temp->pitch + X * bpp;
switch(bpp)
{
case 1:
pix.convertColour(*p,temp->format);
break;
case 2:
pix.convertColour(*(Uint16 *)p,temp->format);
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
pix.convertColour(p[0] << 16 | p[1] << 8 | p[2],temp->format);
else
pix.convertColour(p[0] | p[1] << 8 | p[2] << 16,temp->format);
break;
case 4:
pix.convertColour( *(Uint32 *)p,temp->format);
break;
default:
pix.setColour(MAGENTA); /* shouldn't happen, but avoids warnings */
}
if (pix != none)
{
vel.x = Random::nextFloat(-5,5);
vel.y = Random::nextFloat(-8,-3);
time = Random::nextInt(45,75);
parent->addParticle(this,pix,position + Vector2df(X,Y),vel,time);
}
}
}
MUSIC_CACHE->playSound("sounds/die.wav",parent->chapterPath);
toBeRemoved = true;
SDL_FreeSurface(temp);
}
///---protected---
///---private---