-
Notifications
You must be signed in to change notification settings - Fork 19
/
graphicslib.cpp
2459 lines (2027 loc) · 80.2 KB
/
graphicslib.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
#include "defines.h"
#define LOAD_FILE_NUMBER_TRIES 4
extern std::string FILEPATH;
extern std::string GAMEPATH;
#include "aux_tools/stringutils.h"
#include "inputlib.h"
extern inputLib input;
#include "timerlib.h"
extern timerLib timer;
#include "soundlib.h"
extern soundLib soundManager;
#include "graphicslib.h"
#include "file/format.h"
extern CURRENT_FILE_FORMAT::st_save game_save;
extern CURRENT_FILE_FORMAT::file_game game_data;
extern graphicsLib_gSurface _explosion_surface;
extern CURRENT_FILE_FORMAT::file_io fio;
#define DEBUG_MSG_DELAY 5000
#include "file/file_io.h"
#include "class_config.h"
#include "strings_map.h"
#include "aux_tools/exception_manager.h"
#ifdef ANDROID
#include <android/log.h>
#endif
#define B sp[i - b]
#define D sp[i - (i>0?1:0)]
#define F sp[i + (i<wd?1:0)]
#define H sp[i + h]
#define E sp[i]
#define E0 tp[i*2]
#define E1 tp[i*2 + 1]
#define E2 tp[i*2 + tpitch]
#define E3 tp[i*2 + 1 + tpitch]
graphicsLib::graphicsLib() : _show_stars(false), game_screen(NULL), _explosion_animation_timer(0), _explosion_animation_pos(0), _timer(0), font(NULL)
{
tileset = NULL;
stars_timer = 0;
RES_DIFF_W = 0;
RES_DIFF_H = 0;
_debug_msg_pos = 0;
color_keys[0].r = 55;
color_keys[0].g = 255;
color_keys[0].b = 0;
color_keys[1].r = 255;
color_keys[1].g = 0;
color_keys[1].b = 255;
color_keys[2].r = 0;
color_keys[2].g = 255;
color_keys[2].b = 255;
_screen_resolution_adjust = st_position(0, 0);
}
graphicsLib::~graphicsLib()
{
}
bool graphicsLib::initGraphics()
{
string filename;
_video_filter = SharedData::get_instance()->game_config.video_filter;
fflush(stdout);
#ifdef DREAMCAST
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK|SDL_INIT_TIMER) < 0 ) {
std::cout << "ERROR: Unable to init SDL. Error: " << SDL_GetError() << std::endl;
SDL_Quit();
exit(-1);
}
#else
// GRAPHIC LIB
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0 ) {
std::cout << "ERROR: Unable to init SDL. Error: " << SDL_GetError() << std::endl;
std::fflush(stdout);
exception_manager::throw_general_exception(std::string("graphicsLib::initGraphics - Unable to init SDL."), SDL_GetError());
}
#endif
atexit(SDL_Quit);
#if defined(PLAYSTATION2) || defined(WII)
if (SDL_NumJoysticks() <= 0) {
std::cout << "No joysticks found" << std::endl;
fflush(stdout);
SDL_Quit();
exit(-1);
}
#endif
input.init_joystick();
fflush(stdout);
// FONT
TTF_Init();
filename = GAMEPATH + std::string("/fonts/pressstart2p.ttf");
filename = StringUtils::clean_filename(filename);
char *buffer = new char[filename.size()+1];
std::strcpy(buffer, filename.c_str());
SDL_RWops *fileRW = SDL_RWFromFile(buffer, "rb");
SDL_RWops *fileOutlineRW = SDL_RWFromFile(buffer, "rb");
SDL_RWops *fileErrorRW = SDL_RWFromFile(buffer, "rb");
if (!fileRW || !fileOutlineRW) {
printf("ERROR::initGraphics - could not open '%s' font\n", buffer);
fflush(stdout);
delete[] buffer;
return false;
} else {
font = TTF_OpenFontRW(fileRW, 1, FONT_SIZE);
// outline-font
outline_font = TTF_OpenFontRW(fileOutlineRW, 1, FONT_SIZE);
error_font = TTF_OpenFontRW(fileErrorRW, 1, FONT_SIZE_ERROR);
#if !defined(DINGUX) && !defined(PSP) && !defined(POCKETGO)
TTF_SetFontOutline(outline_font, 1);
#endif
}
delete[] buffer;
fflush(stdout);
// GAME SCREEN
SDL_ShowCursor( SDL_DISABLE );
#ifdef PC
SDL_WM_SetCaption("RockBot", "RockBot");
#endif
set_video_mode();
// other loading methods
fflush(stdout);
load_shared_graphics();
return true;
}
void graphicsLib::set_window_icon()
{
std::string icon_filename = FILEPATH + "/images/icon_32px.png";
SDL_RWops *rwop = SDL_RWFromFile(icon_filename.c_str(), "rb");
if (rwop) {
SDL_Surface* icon_img = IMG_Load_RW(rwop, 1);
if (icon_img != NULL) {
SDL_WM_SetIcon(icon_img, NULL);
}
} else {
std::cout << "ERROR::graphicsLib::initGraphics(set-window-icon): rwop for [" << icon_filename << "] is NULL " << std::endl;
}
}
void graphicsLib::update_screen_mode()
{
if (SharedData::get_instance()->game_config.video_fullscreen == false) {
scale_int = SharedData::get_instance()->game_config.scale_int;
if (scale_int < 1) {
scale_int = 1;
}
game_screen_scaled = SDL_SetVideoMode(RES_W*scale_int, RES_H*scale_int, VIDEO_MODE_COLORS, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);
} else {
game_screen_scaled = SDL_SetVideoMode(RES_W, RES_H, VIDEO_MODE_COLORS, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
}
}
void graphicsLib::load_shared_graphics()
{
std::string filename = GAMEPATH + "shared/images/config_bg.png";
surfaceFromFile(filename, &config_menu);
water_tile = SDLSurfaceFromFile(GAMEPATH + "/shared/images/water_tile.png");
SDL_SetAlpha(water_tile, SDL_SRCALPHA, 120);
_config_menu_pos.x = 0;
filename = GAMEPATH + "shared/images/backgrounds/weapon_tooltip.png";
surfaceFromFile(filename, &_weapn_tooltip_bg);
}
void graphicsLib::preload()
{
load_icons();
preload_faces();
preload_images();
preload_anim_tiles();
}
void graphicsLib::updateScreen()
{
if (!game_screen) {
std::cout << "ERROR::updateScreen game_screen is NULL\n";
return;
}
if (_show_stars == true) {
anim_stars(); /// @TODO - move to draw class
}
/*
// clear non-used areasfor when screen is greater than game-size
if (game_screen->w > RES_W) {
clear_area_no_adjust(0, 0, _screen_resolution_adjust.x, game_screen->h, 0, 0, 0);
clear_area_no_adjust(_screen_resolution_adjust.x+RES_W, 0, _screen_resolution_adjust.x, game_screen->h, 0, 0, 0);
}
if (game_screen->h > RES_H) {
clear_area_no_adjust(0, 0, game_screen->w, _screen_resolution_adjust.y, 0, 0, 0);
clear_area_no_adjust(0, _screen_resolution_adjust.y + RES_H, game_screen->w, _screen_resolution_adjust.y, 0, 0, 0);
}
*/
if (_screen_resolution_adjust.x > 0 || _screen_resolution_adjust.y > 0) {
copyArea(st_position(-_screen_resolution_adjust.x, -_screen_resolution_adjust.y), &_screen_border, &gameScreen);
}
#ifdef PC
if (SharedData::get_instance()->changed_window_size == true && !SharedData::get_instance()->game_config.video_fullscreen) {
double scale = SharedData::get_instance()->scaleX;
if (SharedData::get_instance()->scaleY < scale) {
scale = SharedData::get_instance()->scaleY;
}
scale_int = (int)scale;
if (scale_int < 1) {
scale_int = 1;
}
SharedData::get_instance()->game_config.scale_int = scale_int;
fio.save_config(SharedData::get_instance()->game_config);
game_screen_scaled = SDL_SetVideoMode(RES_W*scale, RES_H*scale, VIDEO_MODE_COLORS, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);
SharedData::get_instance()->changed_window_size = false;
}
if (scale_int != 1) {
//SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect
SDL_Rect origin_rect = {0, 0, RES_W, RES_H};
Uint16 scalex_int = RES_W*scale_int;
Uint16 scaley_int = RES_H*scale_int;
SDL_Rect dest_rect = {0, 0, scalex_int, scaley_int};
SDL_SoftStretch(game_screen, &origin_rect, game_screen_scaled, &dest_rect);
} else {
copySDLArea(st_rectangle(0, 0, RES_W, RES_H), st_position(0, 0), game_screen, game_screen_scaled, true);
}
SDL_Flip(game_screen_scaled);
#else
SDL_Flip(game_screen);
#endif
}
SDL_Surface *graphicsLib::SDLSurfaceFromFile(string filename)
{
SDL_RWops *rwop;
SDL_Surface *spriteCopy;
filename = StringUtils::clean_filename(filename);
rwop = SDL_RWFromFile(filename.c_str(), "rb");
if (!rwop) {
std::cout << "ERROR::SDLSurfaceFromFile - Error in graphicsLib::SDLSurfaceFromFile - file '" << filename << "' not found\n";
return NULL;
}
spriteCopy = IMG_Load_RW(rwop, 1);
if (spriteCopy == NULL) {
std::cout << "ERROR::::SDLSurfaceFromFile - Error on IMG_Load_RW, could not load image '" << filename << "'. Details: " << IMG_GetError() << std::endl;
}
if (game_screen == NULL || game_screen->format == NULL) {
return NULL;
}
SDL_Surface *res_surface = SDL_DisplayFormat(spriteCopy);
SDL_FreeSurface(spriteCopy);
SDL_SetColorKey(res_surface, SDL_SRCCOLORKEY, SDL_MapRGB(game_screen->format, COLORKEY_R, COLORKEY_G, COLORKEY_B));
return res_surface;
}
void graphicsLib::surfaceFromFile(string filename, struct graphicsLib_gSurface* res)
{
if (res == NULL) {
return;
}
res->freeGraphic();
// try a few times to load the file to prevent intermitent errors //
for (int i=0; i<LOAD_FILE_NUMBER_TRIES; i++) {
res->set_surface(SDLSurfaceFromFile(filename));
if (res->get_surface() != NULL) {
break;
}
}
if (res->get_surface() == NULL) {
std::cout << "ERROR::surfaceFromFile - surfaceFromFile - error loading file: '" << filename << "'" << std::endl;
_debug_msg_pos = 1;
show_debug_msg(filename);
_debug_msg_pos = 0;
fflush(stdout);
timer.delay(1000);
show_debug_msg("EXIT #05");
exception_manager::throw_file_not_found_exception(std::string("graphicsLib::surfaceFromFile"), filename);
} else {
res->width = res->get_surface()->w;
res->height = res->get_surface()->h;
}
}
void graphicsLib::loadTileset(std::string file)
{
string filename = FILEPATH + "images/tilesets/" + file;
if (tileset != NULL) {
SDL_FreeSurface(tileset);
}
tileset = SDLSurfaceFromFile(filename);
if (tileset == NULL) {
cout << "ERROR::GRAPHLIB::loadTileset: Could not find file '" << filename << "'\n";
show_debug_msg("EXIT #06");
exception_manager::throw_file_not_found_exception(std::string("graphicsLib::loadTileset"), filename);
}
}
void graphicsLib::copySDLArea(struct st_rectangle origin_rectangle, struct st_position destiny_pos, SDL_Surface* surfaceOrigin, SDL_Surface* surfaceDestiny, bool fix_colors=true)
{
UNUSED(fix_colors);
if (!surfaceOrigin) {
std::cout << "copySDLArea - ERROR surfaceOrigin is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.1.0");
return;
}
if (!surfaceDestiny) {
std::cout << "copySDLArea - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.1.1");
return;
}
copySDLPortion(origin_rectangle, st_rectangle(destiny_pos.x, destiny_pos.y, origin_rectangle.w, origin_rectangle.h), surfaceOrigin, surfaceDestiny);
}
void graphicsLib::copySDLPortion(st_rectangle original_rect, st_rectangle destiny_rect, SDL_Surface *surfaceOrigin, SDL_Surface *surfaceDestiny)
{
SDL_Rect src, dest;
src.x = original_rect.x;
src.y = original_rect.y;
src.w = original_rect.w;
src.h = original_rect.h;
dest.x = destiny_rect.x;
dest.y = destiny_rect.y;
dest.w = destiny_rect.w;
dest.h = destiny_rect.h;
if (!surfaceOrigin) {
cout << "copySDLArea - ERROR surfaceOrigin is NULL\n";
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "###ROCKBOT###", "ERROR surfaceOrigin is NULL");
#endif
show_debug_msg("ERROR #20");
return;
}
if (!surfaceDestiny) {
std::cout << "copySDLPortion - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "###ROCKBOT###", "ERROR surfaceDestiny is NULL");
#endif
show_debug_msg("ERROR #21");
return;
}
if (src.x >= surfaceOrigin->w || (src.x+src.w) > surfaceOrigin->w) {
fflush(stdout);
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "###ROCKBOT###", "Invalid X portion <<\n");
#endif
return;
}
if (src.y >= surfaceOrigin->h || (src.y+src.h) > surfaceOrigin->h) {
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "###ROCKBOT###", "Invalid Y portion <<\n");
#endif
return;
}
if (surfaceDestiny == game_screen) { // if painting on game_screen, use position adjusts
dest.x += _screen_resolution_adjust.x;
dest.y += _screen_resolution_adjust.y;
}
SDL_BlitSurface(surfaceOrigin, &src, surfaceDestiny, &dest);
}
void graphicsLib::copy_gamescreen_area(st_rectangle origin_rectangle, st_position pos, graphicsLib_gSurface *surfaceDestiny)
{
origin_rectangle.x += _screen_resolution_adjust.x;
origin_rectangle.y += _screen_resolution_adjust.y;
copyArea(origin_rectangle, pos, &gameScreen, surfaceDestiny);
}
void graphicsLib::copyArea(struct st_rectangle origin_rectangle, struct st_position pos, struct graphicsLib_gSurface* surfaceOrigin, struct graphicsLib_gSurface* surfaceDestiny)
{
if (!surfaceDestiny->get_surface()) {
std::cout << "copyArea - ERROR surfaceDestiny is NULL #1 - ignoring..." << std::endl;
int a = 1;
int b = a + 2;
show_debug_msg("ERROR #21.3");
return;
}
if (surfaceDestiny == &gameScreen) {
pos.x += _screen_adjust.x;
pos.y += _screen_adjust.y;
}
copySDLArea(origin_rectangle, pos, surfaceOrigin->get_surface(), surfaceDestiny->get_surface());
}
void graphicsLib::copyArea(struct st_position pos, struct graphicsLib_gSurface* surfaceOrigin, struct graphicsLib_gSurface* surfaceDestiny)
{
if (!surfaceDestiny->get_surface()) {
std::cout << "copyArea - ERROR surfaceDestiny is NULL #2 - ignoring..." << std::endl;
show_debug_msg("ERROR #21.4");
return;
}
st_rectangle origin_rectangle(0, 0, surfaceOrigin->width, surfaceOrigin->height);
copySDLArea(origin_rectangle, pos, surfaceOrigin->get_surface(), surfaceDestiny->get_surface());
}
void graphicsLib::copyAreaWithAdjust(struct st_position pos, struct graphicsLib_gSurface* surfaceOrigin, struct graphicsLib_gSurface* surfaceDestiny)
{
if (!surfaceDestiny->get_surface()) {
std::cout << "copyAreaWithAdjust - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.4");
return;
}
int w = surfaceOrigin->width;
int h = surfaceOrigin->height;
st_rectangle origin_rectangle(0, 0, w, h);
//pos.x += _screen_adjust.x;
//pos.y += _screen_adjust.y;
copySDLArea(origin_rectangle, pos, surfaceOrigin->get_surface(), surfaceDestiny->get_surface());
}
void graphicsLib::copyAreaWithAdjustAndAnimFrame(struct st_position pos, struct graphicsLib_gSurface* surfaceOrigin, struct graphicsLib_gSurface* surfaceDestiny, int frame)
{
if (!surfaceDestiny->get_surface()) {
std::cout << "copyAreaWithAdjust - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.4");
return;
}
int w = surfaceOrigin->width/2;
int h = surfaceOrigin->height;
int origin_x = 0;
if (frame == 1) {
origin_x = surfaceOrigin->width/2;
}
st_rectangle origin_rectangle(origin_x, 0, w, h);
copySDLArea(origin_rectangle, pos, surfaceOrigin->get_surface(), surfaceDestiny->get_surface());
}
void graphicsLib::placeTile(struct st_position pos_origin, struct st_position pos_destiny, struct graphicsLib_gSurface* gSurface)
{
if (!gSurface->get_surface()) {
std::cout << "placeTile - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.5");
return;
}
struct st_rectangle origin_rectangle;
origin_rectangle.x = pos_origin.x * TILESIZE;
origin_rectangle.y = pos_origin.y * TILESIZE;
origin_rectangle.w = TILESIZE;
origin_rectangle.h = TILESIZE;
pos_destiny.x += _screen_adjust.x;
pos_destiny.y += _screen_adjust.y;
copySDLArea(origin_rectangle, pos_destiny, tileset, gSurface->get_surface());
}
void graphicsLib::place_easymode_block_tile(st_position destiny, graphicsLib_gSurface& surface)
{
destiny.x += _screen_adjust.x;
destiny.y += _screen_adjust.y;
copySDLArea(st_rectangle(0, 0, TILESIZE, TILESIZE), destiny, _easymode_block.get_surface(), surface.get_surface());
}
void graphicsLib::place_hardmode_block_tile(st_position destiny, graphicsLib_gSurface &surface)
{
destiny.x += _screen_adjust.x;
destiny.y += _screen_adjust.y;
copySDLArea(st_rectangle(0, 0, TILESIZE, TILESIZE), destiny, _hardmode_block.get_surface(), surface.get_surface());
}
void graphicsLib::place_anim_tile(int anim_tile_id, st_position pos_destiny, struct graphicsLib_gSurface* dest_surface)
{
if (anim_tile_id >= ANIM_TILES_SURFACES.size()) {
std::cout << "place_anim_tile - ERROR Invalid anim-tile-id: " << anim_tile_id << " - ignoring..." << std::endl;
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "###ROCKBOT###", "place_anim_tile - ERROR Invalid anim-tile-id[%d], ignoring.", anim_tile_id);
#endif
return;
}
struct graphicsLib_gSurface* tile_ref = &ANIM_TILES_SURFACES.at(anim_tile_id);
if (tile_ref->get_surface() == NULL) {
std::cout << "place_anim_tile - ERROR surfaceDestiny is NULL for id " << anim_tile_id << " - ignoring..." << std::endl;
char debug_msg[255];
sprintf(debug_msg, "EXIT:place_anim_tile[%d][%d]", anim_tile_id, ANIM_TILES_SURFACES.size());
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "###ROCKBOT###", "place_anim_tile - ERROR surfaceDestiny is NULL for id[%d]", anim_tile_id);
#endif
show_debug_msg(debug_msg);
return;
}
struct st_rectangle origin_rectangle;
origin_rectangle.x = ANIM_TILES_TIMERS.at(anim_tile_id).frame_pos * TILESIZE;
origin_rectangle.y = 0;
origin_rectangle.w = TILESIZE;
origin_rectangle.h = TILESIZE;
pos_destiny.x += _screen_adjust.x;
pos_destiny.y += _screen_adjust.y;
copySDLArea(origin_rectangle, pos_destiny, tile_ref->get_surface(), dest_surface->get_surface());
}
void graphicsLib::update_anim_tiles_timers()
{
for (int anim_tile_id=0; anim_tile_id<ANIM_TILES_TIMERS.size(); anim_tile_id++) {
if (ANIM_TILES_TIMERS.at(anim_tile_id).timer < timer.getTimer()) {
ANIM_TILES_TIMERS.at(anim_tile_id).frame_pos++;
if (ANIM_TILES_TIMERS.at(anim_tile_id).frame_pos >= ANIM_TILES_TIMERS.at(anim_tile_id).max_frames) {
ANIM_TILES_TIMERS.at(anim_tile_id).frame_pos = 0;
}
ANIM_TILES_TIMERS.at(anim_tile_id).timer = timer.getTimer() + ANIM_TILES_TIMERS.at(anim_tile_id).frames_delay[ANIM_TILES_TIMERS.at(anim_tile_id).frame_pos];
}
}
}
void graphicsLib::place_3rd_level_tile(int origin_x, int origin_y, int dest_x, int dest_y)
{
st_position pos_destiny(dest_x+_screen_adjust.x, dest_y+_screen_adjust.y);
if (origin_x < -1) {
int anim_tile_id = (origin_x * -1) - 2;
place_anim_tile(anim_tile_id, pos_destiny, &graphLib.gameScreen);
return;
}
struct st_rectangle origin_rectangle(origin_x*TILESIZE, origin_y*TILESIZE, TILESIZE, TILESIZE);
if (origin_rectangle.x < 0 || origin_rectangle.x > tileset->w) {
return;
} else if (origin_rectangle.y < 0 || origin_rectangle.y> tileset->h) {
return;
}
copySDLArea(origin_rectangle, pos_destiny, tileset, game_screen);
}
void graphicsLib::showSurface(struct graphicsLib_gSurface* surfaceOrigin)
{
if (!game_screen) {
std::cout << "showSurface - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.6");
return;
}
struct st_rectangle origin_rectangle;
struct st_position pos_destiny;
origin_rectangle.x = 0;
origin_rectangle.y = 0;
origin_rectangle.w = RES_W;
origin_rectangle.h = RES_H;
pos_destiny.x = 0;
pos_destiny.y = 0;
//copySDLArea(origin_rectangle, pos_destiny, surfaceOrigin->gSurface, game_screen);
showSurfacePortion(surfaceOrigin, origin_rectangle, st_rectangle(_screen_adjust.x, _screen_adjust.y, surfaceOrigin->width, surfaceOrigin->height));
}
void graphicsLib::showSurfaceRegion(struct graphicsLib_gSurface* surfaceOrigin, const struct st_rectangle origin_rectangle)
{
struct st_position pos_destiny;
pos_destiny.x = 0;
pos_destiny.y = 0;
showSurfaceRegionAt(surfaceOrigin, origin_rectangle, pos_destiny);
}
void graphicsLib::showSurfaceRegionAt(struct graphicsLib_gSurface* surfaceOrigin, const struct st_rectangle origin_rectangle, struct st_position pos_destiny)
{
if (!game_screen) {
std::cout << "showSurfaceRegionAt - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.4");
return;
}
if (surfaceOrigin == NULL) {
std::cout << "showSurfaceRegionAt - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #31.0");
return;
}
copySDLArea(origin_rectangle, pos_destiny, surfaceOrigin->get_surface(), game_screen);
}
void graphicsLib::showSurfacePortion(graphicsLib_gSurface *surfaceOrigin, const st_rectangle origin_rect, st_rectangle destiny_rect)
{
if (!game_screen) {
std::cout << "showSurfacePortion - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.2");
return;
}
copySDLPortion(origin_rect, destiny_rect, surfaceOrigin->get_surface(), game_screen);
}
// ********************************************************************************************** //
// //
// ********************************************************************************************** //
void graphicsLib::showSurfaceAt(struct graphicsLib_gSurface* surfaceOrigin, struct st_position pos, bool fix_colors=true)
{
if (!game_screen) {
std::cout << "showSurfaceAt - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("ERROR #21.7");
return;
}
struct st_rectangle origin_rectangle;
struct st_position pos_destiny;
if (surfaceOrigin->get_surface() == NULL) {
std::cout << "Error: no data in surfaceOrigin at graphicsLib::showSurfaceAt." << std::endl;
show_debug_msg("ERROR #41.0");
return;
}
origin_rectangle.x = 0;
origin_rectangle.y = 0;
origin_rectangle.w = surfaceOrigin->get_surface()->w;
origin_rectangle.h = surfaceOrigin->get_surface()->h;
pos_destiny.x = pos.x;
pos_destiny.y = pos.y;
copySDLArea(origin_rectangle, pos_destiny, surfaceOrigin->get_surface(), game_screen, fix_colors);
}
void graphicsLib::show_white_surface_at(graphicsLib_gSurface *surfaceOrigin, st_position pos)
{
if (surfaceOrigin->get_surface() == NULL) {
std::cout << "CRITICAL ERROR!" << std::endl;
show_debug_msg("ERROR #41.1");
return;
}
// create a new surface
struct graphicsLib_gSurface tmp;
initSurface(st_size(surfaceOrigin->width, surfaceOrigin->height), &tmp);
copyArea(st_position(0, 0), surfaceOrigin, &tmp);
//struct st_position pos, struct graphicsLib_gSurface* surfaceOrigin, struct graphicsLib_gSurface* surfaceDestiny
showSurfaceAt(&tmp, pos, false);
}
void graphicsLib::initSurface(struct st_size size, struct graphicsLib_gSurface* gSurface)
{
if (game_screen == NULL || game_screen->format == NULL) {
return;
}
gSurface->freeGraphic();
SDL_Surface* temp_surface = NULL;
SDL_Surface* rgb_surface = SDL_CreateRGBSurface(SDL_SWSURFACE , size.width, size.height, VIDEO_MODE_COLORS, 0, 0, 0, 0);
if (rgb_surface != NULL) {
temp_surface = SDL_DisplayFormat(rgb_surface);
if (!temp_surface) {
show_debug_msg("EXIT #21.INIT #1");
show_debug_msg("EXIT #41.2");
exception_manager::throw_general_exception(std::string("graphicsLib::initSurface #1"), "NO RAM?");
}
SDL_FreeSurface(rgb_surface);
}
SDL_FillRect(temp_surface, NULL, SDL_MapRGB(game_screen->format, COLORKEY_R, COLORKEY_G, COLORKEY_B));
SDL_SetColorKey(temp_surface, SDL_SRCCOLORKEY, SDL_MapRGB(game_screen->format, COLORKEY_R, COLORKEY_G, COLORKEY_B));
gSurface->set_surface(temp_surface);
if (gSurface->get_surface() == NULL) {
show_debug_msg("EXIT #21.INIT #2");
exception_manager::throw_general_exception(std::string("graphicsLib::initSurface #2"), "NO RAM?");
}
gSurface->width = size.width;
gSurface->height = size.height;
}
void graphicsLib::clear_surface(graphicsLib_gSurface &surface)
{
if (game_screen == NULL || game_screen->format == NULL) {
return;
}
SDL_FillRect(surface.get_surface(), NULL, SDL_MapRGB(game_screen->format, COLORKEY_R, COLORKEY_G, COLORKEY_B));
SDL_SetColorKey(surface.get_surface(), SDL_SRCCOLORKEY, SDL_MapRGB(game_screen->format, COLORKEY_R, COLORKEY_G, COLORKEY_B));
}
void graphicsLib::set_surface_alpha(int alpha, graphicsLib_gSurface& surface)
{
if (surface.width <= 0 || surface.get_surface() == NULL) {
return;
}
if (surface.is_rle_enabled == false) {
SDL_SetColorKey(surface.get_surface(), SDL_RLEACCEL|SDL_SRCCOLORKEY, SDL_MapRGB(game_screen->format, COLORKEY_R, COLORKEY_G, COLORKEY_B));
surface.is_rle_enabled = true;
}
SDL_SetAlpha(surface.get_surface(), SDL_RLEACCEL|SDL_SRCALPHA, alpha);
}
void graphicsLib::set_surface_alpha(int alpha, graphicsLib_gSurface *surface)
{
if (surface->width <= 0 || surface->get_surface() == NULL) {
return;
}
if (surface->is_rle_enabled == false) {
SDL_SetColorKey(surface->get_surface(), SDL_RLEACCEL|SDL_SRCCOLORKEY, SDL_MapRGB(game_screen->format, COLORKEY_R, COLORKEY_G, COLORKEY_B));
surface->is_rle_enabled = true;
}
SDL_SetAlpha(surface->get_surface(), SDL_RLEACCEL|SDL_SRCALPHA, alpha);
}
void graphicsLib::set_surface_alpha_nocolorkey(int alpha, graphicsLib_gSurface &surface)
{
SDL_SetAlpha(surface.get_surface(), SDL_RLEACCEL|SDL_SRCALPHA, alpha);
}
struct graphicsLib_gSurface graphicsLib::surfaceFromRegion(struct st_rectangle rect_origin, struct graphicsLib_gSurface& originalSurface)
{
struct st_position destiny_pos;
destiny_pos.x = 0;
destiny_pos.y = 0;
struct graphicsLib_gSurface res;
initSurface(st_size(rect_origin.w, rect_origin.h), &res);
if (res.get_surface() == NULL) {
std::cout << "surfaceFromRegion - ERROR surfaceDestiny is NULL - ignoring..." << std::endl;
show_debug_msg("EXIT #21.8");
exception_manager::throw_general_exception(std::string("graphicsLib::surfaceFromRegion"), "surfaceDestiny is NULL");
}
/// @NOTE: removed for optimization test
copySDLArea(rect_origin, destiny_pos, originalSurface.get_surface(), res.get_surface());
/// @NOTE: removed for optimization test
return res;
}
void graphicsLib::blank_screen() {
blank_screen(0, 0, 0);
}
void graphicsLib::blank_screen(int r, int g, int b)
{
if (game_screen == NULL || game_screen->format == NULL) {
return;
}
SDL_FillRect(game_screen, NULL, SDL_MapRGB(game_screen->format, r, g, b));
}
void graphicsLib::blank_surface(graphicsLib_gSurface &surface)
{
if (game_screen == NULL || game_screen->format == NULL) {
return;
}
SDL_FillRect(surface.get_surface(), NULL, SDL_MapRGB(game_screen->format, 0, 0, 0));
}
/*
* http://www.zedwood.com/article/cpp-utf-8-mb_substr-function
* code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)
*/
std::string graphicsLib::utf8_substr2(const std::string &str, int start, int length)
{
int i,ix,j,realstart,reallength;
if (length==0) return "";
if (start<0 || length <0)
{
//find j=utf8_strlen(str);
for(j=0,i=0,ix=str.length(); i<ix; i+=1, j++)
{
unsigned char c= str[i];
if (c>=0 && c<=127) i+=0;
else if (c>=192 && c<=223) i+=1;
else if (c>=224 && c<=239) i+=2;
else if (c>=240 && c<=247) i+=3;
else if (c>=248 && c<=255) return "";//invalid utf8
}
if (length !=INT_MAX && j+length-start<=0) return "";
if (start < 0 ) start+=j;
if (length < 0 ) length=j+length-start;
}
j=0,realstart=0,reallength=0;
for(i=0,ix=str.length(); i<ix; i+=1, j++)
{
if (j==start) { realstart=i; }
if (j>=start && (length==INT_MAX || j<=start+length)) { reallength=i-realstart; }
unsigned char c= str[i];
if (c>=0 && c<=127) i+=0;
else if (c>=192 && c<=223) i+=1;
else if (c>=224 && c<=239) i+=2;
else if (c>=240 && c<=247) i+=3;
else if (c>=248 && c<=255) return "";//invalid utf8
}
if (j==start) { realstart=i; }
if (j>=start && (length==INT_MAX || j<=start+length)) { reallength=i-realstart; }
return str.substr(realstart,reallength);
}
void graphicsLib::blank_area(short int x, short int y, short int w, short int h) {
clear_area(x, y, w, h, 0, 0, 0);
}
void graphicsLib::blank_area(short x, short y, short w, short h, graphicsLib_gSurface &surface)
{
clear_surface_area(x, y, w, h, 0, 0, 0, surface);
}
void graphicsLib::draw_rectangle(st_rectangle area, int r, int g, int b, int alpha)
{
graphicsLib_gSurface transparent_area;
graphLib.initSurface(st_size(area.w, area.h), &transparent_area);
graphLib.clear_surface_area(0, 0, area.w, area.h, r, g, b, transparent_area);
set_surface_alpha(alpha, transparent_area);
showSurfaceAt(&transparent_area, st_position(area.x, area.y), false);
}
int graphicsLib::draw_progressive_text(short int x, short int y, string text, bool interrupt) {
return draw_progressive_text(x, y, text, interrupt, 15);
}
int graphicsLib::draw_progressive_text(short x, short y, string text, bool interrupt, int delay)
{
//SDL_Color font_color = {255,255,255};
string temp_text;
std::string temp_char;
int text_x = 0;
int text_y = 0;
unsigned int i;
if (!font) {
printf("ERROR: no fount found - TTF_OpenFont: %s\n", TTF_GetError());
show_debug_msg("EXIT #09");
exception_manager::throw_file_not_found_exception(std::string("graphicsLib::draw_progressive_text, fount is NULL"), std::string(TTF_GetError()));
}
for (i=0; i<text.size(); i++) {
input.read_input();
temp_char = utf8_substr2(text, i, 1);
temp_text = "";
temp_text += temp_char;
draw_text(text_x*9+x, text_y*11+y, temp_text);
text_x++;
if (temp_char.length() > 0 && temp_char.at(0) == '\n') {
text_x = 0;
text_y++;
}
updateScreen();
if (interrupt == true) {
if (input.wait_scape_time(delay) ==1) {
return 1;
}
} else {
timer.delay(delay);
}
}
return 0;
}
void graphicsLib::draw_text(short int x, short int y, string text) {
draw_text(x, y, text, st_color(250, 250, 250));
}
void graphicsLib::draw_text(short x, short y, string text, st_color color)
{
if (text.length() <= 0) {
return;
}
render_text(x, y, text, color, false);
}
void graphicsLib::draw_text(short x, short y, string text, graphicsLib_gSurface &surface)
{
render_text(x, y, text, st_color(255, 255, 255), false);
}
void graphicsLib::draw_error_text(std::string text)
{
SDL_Color font_color = SDL_Color();
font_color.r = 250;
font_color.g = 250;
font_color.b = 250;
SDL_Rect text_pos = {5, 10, 0, 0};
int max_len = 42;
int parts_n = text.length() / max_len;
if (!error_font) {
printf("ERROR: could not load font, message: %s\n", TTF_GetError());
show_debug_msg("EXIT #10");
exception_manager::throw_file_not_found_exception(std::string("graphicsLib::draw_text, fount is NULL"), std::string(TTF_GetError()));
}
for (int i=0; i<=parts_n; i++) {
std::string sub_text = text.substr(i*max_len, max_len);
std::cout << "text.length[" << text.length() << "], parts_n[" << parts_n << "], i[" << i << "], sub_text[" << sub_text << "]" << std::endl;
SDL_Surface* textSF = TTF_RenderUTF8_Solid(error_font, sub_text.c_str(), font_color);
if (!textSF) {
continue;
}
SDL_Surface* textSF_format = SDL_DisplayFormat(textSF);
SDL_FreeSurface(textSF);
if (!textSF_format) {
continue;
}
SDL_BlitSurface(textSF_format, 0, game_screen, &text_pos);
SDL_FreeSurface(textSF_format);
text_pos.y += 10;
}
}
void graphicsLib::draw_centered_text(short y, string text, st_color font_color)
{