-
Notifications
You must be signed in to change notification settings - Fork 6
/
sprite_anim_player.cpp
473 lines (441 loc) · 12.9 KB
/
sprite_anim_player.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/************************************************************************
* Copyright (c) 2005-2007 [email protected] *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <iostream>
#include <sstream>
#include <SDL_opengl.h>
#include <unistd.h>
#include "gl_screen.h"
#include "opengta.h"
#include "log.h"
#include "spritemanager.h"
#include "m_exceptions.h"
#include "gl_camera.h"
#include "dataholder.h"
#include "gl_font.h"
extern int global_EC;
extern int global_Done;
std::string style_file("STYLE001.GRY");
float screen_gamma = 1.0f;
OpenGTA::Car * car = NULL;
Vector3D _p(4, 0.01f, 4);
OpenGTA::Pedestrian ped(Vector3D(0.5f, 0.5f, 0.5f), Vector3D(4, 0.01f, 4), 0xffffffff);
OpenGTA::SpriteObject::Animation pedAnim(0, 0);
OpenGL::DrawableFont m_font;
int frame_offset = 0;
int first_offset = 0;
int second_offset = 0;
int now_frame = 0;
bool play_anim = false;
unsigned int play_anim_time = 0;
int bbox_toggle = 0;
int texsprite_toggle = 0;
int c_c = 1;
int car_model = 0;
int car_remap = -1;
int car_last_model_ok = 0;
bool playWithCar = false;
uint32_t car_delta = 0;
int spr_type = (int)ped.sprType;
namespace OpenGTA {
void ai_step_fake(OpenGTA::Pedestrian*) {
}
}
void on_exit() {
if (car)
delete car;
SDL_Quit();
PHYSFS_deinit();
}
void safe_try_model(uint8_t model_id) {
if (car)
delete car;
car = NULL;
try {
car = new OpenGTA::Car(_p, 0, 0, model_id, car_remap);
}
catch (Util::UnknownKey & uk) {
car = NULL;
ERROR << "not a model" << std::endl;
return;
}
car_last_model_ok = model_id;
}
void run_init(const char*) {
PHYSFS_init("mapview");
PHYSFS_addToSearchPath(PHYSFS_getBaseDir(), 1);
PHYSFS_addToSearchPath("gtadata.zip", 1);
OpenGL::ScreenHolder::Instance().activate(640, 480);
SDL_EnableKeyRepeat( 100, SDL_DEFAULT_REPEAT_INTERVAL );
OpenGTA::StyleHolder::Instance().load(style_file);
OpenGTA::StyleHolder::Instance().get().setDeltaHandling(true);
OpenGTA::MainMsgHolder::Instance().load("ENGLISH.FXT");
m_font.loadFont("F_MTEXT.FON");
m_font.setScale(1);
glClearColor(1, 1, 1, 1);
if (playWithCar)
car = new OpenGTA::Car(_p, 0, 0, car_model);
}
const char* spr_type_name(int t) {
switch(t) {
case 0:
return "arrow";
case 1:
return "digit";
case 2:
return "boat";
case 3:
return "box";
case 4:
return "bus";
case 5:
return "car";
case 6:
return "object";
case 7:
return "ped";
case 8:
return "speedo";
case 9:
return "tank";
case 10:
return "tr light";
case 11:
return "train";
case 12:
return "tr door";
case 13:
return "bike";
case 14:
return "tram";
case 15:
return "wbus";
case 16:
return "wcar";
case 17:
return "ex";
case 18:
return "tumcar";
case 19:
return "tumtruck";
case 20:
return "ferry";
}
return "???";
}
const char* vtype2name(int vt) {
switch(vt) {
case 0:
return "bus";
case 3:
return "motorcycle";
case 4:
return "car";
case 8:
return "train";
}
return "";
}
void drawScene(Uint32 ticks) {
GL_CHECKERROR;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
OpenGL::ScreenHolder::Instance().set3DProjection();
OpenGL::CameraHolder::Instance().update(ticks);
if (playWithCar) {
if (car) {
car->update(ticks);
OpenGTA::SpriteManagerHolder::Instance().draw(*car);
}
OpenGL::ScreenHolder::Instance().setFlatProjection();
glPushMatrix();
glTranslatef(10, 10, 0);
std::ostringstream sprite_info_str;
std::ostringstream ostr;
ostr << "car" << int(car_model);
if (car) {
sprite_info_str << vtype2name(car->carInfo.vtype)<< " model: " << int(car_model) << " name: " <<
OpenGTA::MainMsgHolder::Instance().get().getText(ostr.str());
}
else
sprite_info_str << "not a model: " << int(car_model);
m_font.drawString(sprite_info_str.str());
glPopMatrix();
}
else {
if (play_anim && ticks > play_anim_time + 200) {
now_frame++;
if (now_frame > second_offset)
now_frame = first_offset;
ped.anim.firstFrameOffset = now_frame;
play_anim_time = ticks;
}
OpenGTA::SpriteManagerHolder::Instance().draw(ped);
OpenGL::ScreenHolder::Instance().setFlatProjection();
glPushMatrix();
glTranslatef(10, 10, 0);
std::ostringstream sprite_info_str;
sprite_info_str << spr_type_name(spr_type) << " offset " << frame_offset;
m_font.drawString(sprite_info_str.str());
glPopMatrix();
}
SDL_GL_SwapBuffers();
GL_CHECKERROR;
}
void handleKeyPress( SDL_keysym *keysym ) {
OpenGL::Camera & cam = OpenGL::CameraHolder::Instance();
OpenGTA::GraphicsBase & style = OpenGTA::StyleHolder::Instance().get();
bool update_anim = false;
switch ( keysym->sym ) {
case SDLK_ESCAPE:
global_Done = 1;
break;
case SDLK_TAB:
c_c += 1; c_c %= 2;
glClearColor(c_c, c_c, c_c, 0);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
break;
case 'k':
if (car_delta > 0)
car_delta -= 1;
if (car)
car->delta = car_delta;
break;
case 'l':
if (car_delta < 32)
car_delta += 1;
if (car) {
car->delta = car_delta;
}
break;
case '+':
cam.translateBy(Vector3D(0, -0.5f, 0));
break;
case '-':
cam.translateBy(Vector3D(0, 0.5f, 0));
break;
case '1':
if (playWithCar) {
if (car->animState.get_item(1))
car->closeDoor(0);
else
car->openDoor(0);
}
break;
case '2':
if (playWithCar) {
if (car->animState.get_item(2))
car->closeDoor(1);
else
car->openDoor(1);
}
break;
case '3':
if (playWithCar) {
if (car->animState.get_item(3))
car->closeDoor(2);
else
car->openDoor(2);
}
break;
case '4':
if (playWithCar) {
if (car->animState.get_item(4))
car->closeDoor(3);
else
car->openDoor(3);
}
break;
case ',':
if (playWithCar) {
car_model -= 1;
if (car_model < 0)
car_model = 0;
}
frame_offset -= 1;
if (frame_offset < 0)
frame_offset = 0;
update_anim = true;
break;
case '.':
if (playWithCar) {
car_model += 1;
if (car_model > 88)
car_model = 88;
}
frame_offset += 1;
if (frame_offset >= style.spriteNumbers.countByType(ped.sprType))
frame_offset -= 1;
update_anim = true;
break;
case 'n':
if (playWithCar) {
car_remap -= 1;
if (car_remap < -1)
car_remap = -1;
INFO << "remap: " << int(car_remap) << std::endl;
}
do {
spr_type -= 1;
if (spr_type < 0)
spr_type = 0;
} while (style.spriteNumbers.countByType(
(OpenGTA::GraphicsBase::SpriteNumbers::SpriteTypes) spr_type) == 0);
ped.sprType = (OpenGTA::GraphicsBase::SpriteNumbers::SpriteTypes)spr_type;
frame_offset = 0;
update_anim = 1;
break;
case 'm':
if (playWithCar) {
car_remap += 1;
if (car_remap > 11)
car_remap = 11;
INFO << "remap: " << int(car_remap) << std::endl;
}
do {
spr_type += 1;
if (spr_type > 20)
spr_type = (int)ped.sprType;
} while (style.spriteNumbers.countByType(
(OpenGTA::GraphicsBase::SpriteNumbers::SpriteTypes) spr_type) == 0);
ped.sprType = (OpenGTA::GraphicsBase::SpriteNumbers::SpriteTypes)spr_type;
frame_offset = 0;
update_anim = 1;
break;
case 's':
if (playWithCar) {
car->setSirenAnim(true);
}
break;
case SDLK_F2:
bbox_toggle = (bbox_toggle ? 0 : 1);
OpenGTA::SpriteManagerHolder::Instance().setDrawBBox(bbox_toggle);
break;
case SDLK_F3:
texsprite_toggle = (texsprite_toggle ? 0 : 1);
OpenGTA::SpriteManagerHolder::Instance().setDrawTexBorder(texsprite_toggle);
break;
case SDLK_F5:
first_offset = frame_offset;
std::cout << "First frame: " << first_offset << std::endl;
break;
case SDLK_F6:
second_offset = frame_offset;
std::cout << "Last frame: " << second_offset << std::endl;
break;
case SDLK_F7:
play_anim = (play_anim ? false : true);
if (play_anim)
std::cout << "Playing: " << first_offset << " .. " << second_offset << std::endl;
now_frame = first_offset;
break;
case SDLK_F8:
playWithCar = playWithCar ? false : true;
update_anim = true;
break;
default:
break;
}
if (update_anim) {
pedAnim.firstFrameOffset = frame_offset;
ped.anim = pedAnim;
if (playWithCar)
safe_try_model(car_model);
}
}
void usage(const char* a0) {
std::cout << "USAGE: " << a0 << " [style-filename]" << std::endl;
std::cout << std::endl << "Default is: STYLE001.GRY" << std::endl <<
"Keys:" << std::endl <<
" + - : zoom in/out" << std::endl <<
" , . : previous/next frame offset" << std::endl <<
" n m : previous/next sprite-type" << std::endl <<
" tab : black/white background" << std::endl <<
" F2 : toggle BBox drawn" << std::endl <<
" F3 : toggle tex-border drawn" << std::endl <<
" F5 : prepare animation: first-frame = current frame" << std::endl <<
" F6 : prepare animation: last-frame = current frame" << std::endl <<
" F7 : toggle: play frames" << std::endl <<
" F8 : toggle: special-car-mode" << std::endl << std::endl <<
"In car-mode:" << std::endl <<
" , . : choose model" << std::endl <<
" n m : choose remap" << std::endl <<
" 1, 2, 3, 4 : open car door (if exists)" << std::endl <<
" s : toggle siren anim (if exists)" << std::endl;
}
void parse_args(int argc, char* argv[]) {
int index;
int c;
opterr = 0;
#define VIEWER_FLAGS ""
while ((c = getopt (argc, argv, VIEWER_FLAGS)) != -1)
switch (c)
{
case 'h':
case '?':
usage(argv[0]);
exit(0);
default:
if (isprint (optopt))
ERROR << "Unknown option `-" << char(optopt) << "'" << std::endl;
else
ERROR << "Unknown option character `" << optopt << "'" << std::endl;
abort ();
}
for (index = optind; index < argc; index++)
style_file = std::string(argv[index]);
}
void run_main() {
SDL_Event event;
glEnable(GL_TEXTURE_2D);
glPolygonMode(GL_FRONT, GL_FILL);
glEnable(GL_CULL_FACE);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0);
OpenGL::Camera & cam = OpenGL::CameraHolder::Instance();
cam.setVectors( Vector3D(4, 5, 4), Vector3D(4, 0.0f, 4.0f), Vector3D(0, 0, -1) );
cam.setFollowMode(ped.pos);
while(!global_Done && !global_EC) {
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
handleKeyPress(&event.key.keysym);
break;
case SDL_KEYUP:
// handleKeyUp(&event.key.keysym);
break;
case SDL_VIDEORESIZE:
OpenGL::ScreenHolder::Instance().resize(event.resize.w, event.resize.h);
break;
case SDL_QUIT:
global_Done = 1;
break;
case SDL_MOUSEMOTION:
//std::cout << "Mouse move: x " << float(event.motion.x)/screen->w << " y " << float(event.motion.y)/screen->h << std::endl;
break;
default:
break;
}
}
Uint32 now_ticks = SDL_GetTicks();
drawScene(now_ticks);
}
}