-
Notifications
You must be signed in to change notification settings - Fork 27
/
engine.cpp
412 lines (359 loc) · 12.6 KB
/
engine.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//////////////////////////////////////////////////////////////////////
// Yet Another Tibia Client
//////////////////////////////////////////////////////////////////////
// Base engine class
//////////////////////////////////////////////////////////////////////
// This program 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 2
// of the License, or (at your option) any later version.
//
// This program 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, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
/// \file engine.cpp
/// This file will contain generic code for all engines.
/// Currently it contains only the code to make callbacks
/// work (in effect, translations from C++ style calls to
/// a class into C-style functions).
///
/// Functions are designed to be valid GLICT callbacks.
#include <GLICT/globals.h>
#include <GLICT/types.h>
#include <sstream>
#include <cmath>
#include <algorithm>
#include "util.h"
#include "defines.h"
#include "engine.h"
#include "font.h"
#include "options.h"
#include "product.h"
Engine* g_engine;
extern int g_frames;
int ptrx, ptry;
void resetDefaultCursor();
// first we'll define all the C-style callbacks we'll use elsewhere
void Engine::draw_rectangle(float left, float right, float top, float bottom, GLICTCOLORCONST glictColor &col)
{
g_engine->drawRectangle((int)left, (int)top, (int)(right-left), (int)(bottom-top), oRGBA(col.r * 255, col.g * 255, col.b * 255, col.a * 255));
}
void Engine::draw_rectangle_lines(float left, float right, float top, float bottom, GLICTCOLORCONST glictColor &col)
{
g_engine->drawRectangleLines((int)left, (int)top, (int)(right-left), (int)(bottom-top), oRGBA(col.r * 255, col.g * 255, col.b * 255, col.a * 255));
}
void Engine::font_render(const char* txt, const void* font, float fontsize, float x, float y)
{
YATCFont* f = (YATCFont*)font;
float cx = x, cy = y ;
float sizesofar = 0.f;
float linessofar = 0.f;
for(const char* t = txt; *t; ++t){
switch(*t) {
default:
f->Blit(*t,(int)cx,(int)cy);
cx += f->getWidth(*t) + f->getSpacing();
sizesofar += f->getWidth(*t) + f->getSpacing();
break;
case '\t':
cx += (int)sizesofar % 15;// FIXME (ivucica#5#) use fmod
sizesofar += (int)sizesofar % 15;
break;
case '\n':
case '\r':
cx -= sizesofar;
cy += f->getHeight('\n');
linessofar += 1.;
sizesofar = 0;
if ((*t == '\n' && *(t + 1) == '\r') ||
(*t == '\r' && *(t + 1)=='\n' ))
t++;
break;
}
}
}
float Engine::font_size(const char* txt, const void* font, float fontsize)
{
YATCFont* f = (YATCFont*)font;
int size = 0, len = strlen(txt);
int maxsize = 0;
for(int i = 0; i < len; i++) {
size += f->getWidth(txt[i]) + f->getSpacing();
if(txt[i] == '\n' || txt[i] == '\r'){
if(size > maxsize){
maxsize = size;
}
if(i < (len - 1) && ((txt[i] == '\n' && txt[i + 1] == '\r') ||
(txt[i] == '\r' && txt[i + 1] == '\n'))){
i++;
}
size = 0;
}
}
if(size > maxsize){
maxsize = size;
}
return maxsize;
}
void Engine::font_color(const void* font, glictColor &col){
YATCFont* f = (YATCFont*)font;
f->resetColor();
f->addColor(col.r,col.g,col.b);
}
// set the callbacks up in the constructor
Engine::Engine()
{
glictGlobals.paintrectCallback = Engine::draw_rectangle;
glictGlobals.paintrectlinesCallback = Engine::draw_rectangle_lines;
glictGlobals.enableGlTranslate = false;
glictGlobals.windowTitleColor.r = glictGlobals.windowTitleColor.g = glictGlobals.windowTitleColor.b = 0.6;
doResize(options.w, options.h);
m_video_bpp = options.bpp;
m_screen = NULL;
initFont(&m_sysfont, "system");
initFont(&m_minifont, "minifont");
initFont(&m_aafont, "aafont");
initFont(&m_gamefont, "gamefont");
m_fps = 0.;
m_ui = NULL;
m_light = NULL;
m_cursorBasic = m_cursorUse = NULL;
// remember default cursor
resetDefaultCursor();
g_frames = 0;
m_lastfpsupdate = SDL_GetTicks();
}
Engine::~Engine()
{
glictDeleteFont("system");
glictDeleteFont("minifont");
glictDeleteFont("aafont");
glictDeleteFont("gamefont");
delete m_ui;
delete m_light;
}
void Engine::performFpsCalc() {
char caption[255];
static int interval = 1000;
if (SDL_GetTicks() >= m_lastfpsupdate + interval) {
m_lastfpsupdate = SDL_GetTicks();
m_fps = g_frames * 1000. / interval;
g_frames = 0;
sprintf(caption, PRODUCTNAME " - fps: %g", g_engine->m_fps );
SDL_WM_SetCaption(caption, PRODUCTNAME);
}
}
void Engine::initFont(glictFont **fnt, const char *fontname)
{
*fnt = glictCreateFont(fontname);
if(!*fnt){
std::stringstream s;
s << "Cannot load font '" << fontname << "'.";
NativeGUIError(s.str().c_str(), "Error loading font");
}
(*fnt)->SetRenderFunc(Engine::font_render);
(*fnt)->SetSizeFunc(Engine::font_size);
#if (GLICT_APIREV >= 85)
(*fnt)->SetColorFunc(Engine::font_color);
#else
#warning GLICT font coloring is not available. Upgrade to GLICT APIREV 85+.
#endif
}
void Engine::drawText(const char* text, const char* font, int x, int y, uint8_t color)
{
YATCFont *f = (YATCFont*)(glictFindFont(font)->GetFontParam());
if (f)
{
float r = (color / 36) / 5.;
float g = ((color / 6) % 6) / 5.;
float b = (color % 6) / 5.;
if (color == 255) // we'll just use otherwise useless 255 for drawing with 0.75, 0.75, 0.75 if needed
f->addColor(0.75, 0.75, 0.75);
else if (color!=215)
f->addColor(r,g,b);
else
f->resetColor();
}
glictFontRender(text, font, x, y);
}
void Engine::drawText(const char* text, const char* font, int x, int y, oRGBA color)
{
YATCFont *f = (YATCFont*)(glictFindFont(font)->GetFontParam());
if (f)
{
if (color.r == color.g && color.g == color.b && color.b == 1.)
{
f->resetColor();
}
else
{
f->addColor(color.r/255, color.g/255, color.b/255);
}
}
glictFontRender(text, font, x, y);
}
void Engine::drawTextGW(const char* text, const char* font, int x, int y, float scale, uint8_t color)
{
// NOTE (nfries88): keeps all rendering in the game area.
//x = std::min(std::max(1, x), (m_width - 176) - (int)sizeText(text, font)); // NOTE (Kilouco): Doesn't works this way anymore.
//y = std::max(1, y);
YATCFont *f = (YATCFont*)(glictFindFont(font)->GetFontParam());
if (f)
{
float r = (color / 36) / 5.;
float g = ((color / 6) % 6) / 5.;
float b = (color % 6) / 5.;
if (color == 255) // we'll just use otherwise useless 255 for drawing with 0.75, 0.75, 0.75 if needed
f->addColor(0.75, 0.75, 0.75);
else if (color!=215)
f->addColor(r,g,b);
else
f->resetColor();
}
std::string temp_text = text;
std::string new_line_text, old_line_text;
new_line_text = old_line_text = temp_text;
int linecount = 1;
size_t iter_pos, temp_x, temp_y;
// NOTE (Kilouco): Here we centralize all the message and handle positions so it will never go offscreen.
while (1) {
iter_pos = old_line_text.find_first_of("\n");
temp_x = x;
temp_y = y;
if(iter_pos == std::string::npos || iter_pos <= 0) {
int text_size = sizeText(old_line_text.c_str(), font);
volatile float centralizationoffset = text_size / 2;
if (temp_x + centralizationoffset > (480 * scale) + 2)
temp_x = (480 * scale - 2) - text_size;
else if (x - centralizationoffset < 2)
temp_x = 2;
else
temp_x -= centralizationoffset;
if (temp_y < 2)
temp_y = 2;
glictFontRender(old_line_text.c_str(), font, temp_x, temp_y + (12 * (linecount - 1)));
break;
}
else {
new_line_text = old_line_text.substr(iter_pos+1);
old_line_text.resize(iter_pos);
int text_size = sizeText(old_line_text.c_str(), font);
volatile float centralizationoffset = text_size / 2;
if (temp_x + centralizationoffset > (480 * scale) + 2)
temp_x = (480 * scale - 2) - text_size;
else if (x - centralizationoffset < 2)
temp_x = 2;
else
temp_x -= centralizationoffset;
if (temp_y < 2)
temp_y = 2;
glictFontRender(old_line_text.c_str(), font, temp_x, temp_y + (12 * (linecount - 1)));
old_line_text = new_line_text;
linecount++;
}
}
}
void Engine::drawTextGW(const char* text, const char* font, int x, int y, float scale, oRGBA color)
{
// NOTE (nfries88): keeps all rendering in the game area.
//x = std::min(std::max(1, x), (m_width - 176) - (int)sizeText(text, font));
//y = std::max(1, y);
YATCFont *f = (YATCFont*)(glictFindFont(font)->GetFontParam());
if (f)
{
if (color.r == color.g && color.g == color.b && color.b == 1.)
{
f->resetColor();
}
else
{
f->addColor(color.r/255, color.g/255, color.b/255);
}
}
std::string temp_text = text;
std::string new_line_text, old_line_text;
new_line_text = old_line_text = temp_text;
int linecount = 1;
size_t iter_pos, temp_x, temp_y;
// NOTE (Kilouco): Here we centralize all the message and handle positions so it will never go offscreen.
while (1) {
iter_pos = old_line_text.find_first_of("\n");
temp_x = x;
temp_y = y;
if(iter_pos == std::string::npos || iter_pos <= 0) {
int text_size = sizeText(old_line_text.c_str(), font);
volatile float centralizationoffset = text_size / 2;
if (temp_x + centralizationoffset > (480 * scale) + 2)
temp_x = (480 * scale - 2) - text_size;
else if (x - centralizationoffset < 2)
temp_x = 2;
else
temp_x -= centralizationoffset;
if (temp_y < 2)
temp_y = 2;
glictFontRender(old_line_text.c_str(), font, temp_x, temp_y + (12 * (linecount - 1)));
break;
}
else {
new_line_text = old_line_text.substr(iter_pos+1);
old_line_text.resize(iter_pos);
int text_size = sizeText(old_line_text.c_str(), font);
volatile float centralizationoffset = text_size / 2;
if (temp_x + centralizationoffset > (480 * scale) + 2)
temp_x = (480 * scale - 2) - text_size;
else if (x - centralizationoffset < 2)
temp_x = 2;
else
temp_x -= centralizationoffset;
if (temp_y < 2)
temp_y = 2;
glictFontRender(old_line_text.c_str(), font, temp_x, temp_y + (12 * (linecount - 1)));
old_line_text = new_line_text;
linecount++;
}
}
}
void Engine::doResize(int& w, int& h)
{
#ifndef WINCE
// NOTE (nfries88): Make sure the window is sized as per the options
w = std::max(w, 656);
h = std::max(h, 352+options.consoleh);
#endif
m_width = w;
m_height = h;
glictGlobals.w = w;
glictGlobals.h = h;
options.w = w;
options.h = h;
}
void Engine::drawLightmap(vertex* lightmap, int type, int width, int height, float scale)
{
int index = 0;
int scaledSize = (int)std::floor((32.f * scale));
for (int i = 0; i < width; ++i){
for (int j = 0; j < height; ++j){
index = (j * width) + i;
if(type == 1 && m_light != NULL){
int n = std::floor(8 - (8 * (lightmap[index].alpha / 255.0f)));
if (n >= 0 && n < 8)
m_light->Blit(lightmap[index].x, lightmap[index].y, 32 * n, 0, 32, 32, scaledSize, scaledSize);
}
else if(type != 0){ // always default to the better light unless "no light" is specified (this value might be 3 while in SDL engine).
drawRectangle(lightmap[index].x, lightmap[index].y, scaledSize, scaledSize, oRGBA(lightmap[index].r, lightmap[index].g,
lightmap[index].b, (lightmap[index].alpha)));
}
}
}
}
void Engine::reloadGlobalGfx()
{
delete m_ui;
m_ui = createSprite("Tibia.pic", 3);
}