/
sys_sdl_c.c
1702 lines (1572 loc) · 61.6 KB
/
sys_sdl_c.c
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
/*
* libtcod 1.6.3
* Copyright (c) 2008,2009,2010,2012,2013,2016,2017 Jice & Mingos & rmtew
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The name of Jice or Mingos may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY JICE, MINGOS AND RMTEW ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JICE, MINGOS OR RMTEW BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef TCOD_SDL2
#include <mouse.h>
#include <sys.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <console.h>
#include <libtcod_int.h>
#include <parser.h>
static TCOD_SDL_driver_t *sdl=NULL;
/* library initialization function */
#ifdef TCOD_WINDOWS
#include <windows.h>
BOOL APIENTRY DllMain( HINSTANCE hModule, DWORD reason, LPVOID reserved) {
switch (reason ) {
case DLL_PROCESS_ATTACH : sdl = SDL_implementation_factory(); break;
default : break;
}
return TRUE;
}
#else
void __attribute__ ((constructor)) DllMain(void) {
sdl = SDL_implementation_factory();
}
#endif
#if defined(__ANDROID__)
#define TCOD_TOUCH_INPUT
#define MAX_TOUCH_FINGERS 5
typedef struct {
int nupdates; /* how many updates have happened since the first finger was pressed. */
uint32_t ticks0; /* current number of ticks at start of touch event sequence. */
SDL_FingerID finger_id; /* the last finger which was pressed. */
int coords[MAX_TOUCH_FINGERS][2]; /* absolute position of each finger. */
int coords_delta[MAX_TOUCH_FINGERS][2]; /* absolute position of each finger. */
int consolecoords[MAX_TOUCH_FINGERS][2]; /* cell coordinates in the root console for each finger. */
int consolecoords_delta[MAX_TOUCH_FINGERS][2]; /* cell coordinates in the root console for each finger. */
int nfingers; /* number of unique fingers employed at any time during this. */
int nfingerspressed; /* number of those fingers currently still pressed. */
SDL_FingerID finger_ids[MAX_TOUCH_FINGERS];
char fingerspressed[MAX_TOUCH_FINGERS];
} TCOD_touch_t;
#endif
/* to enable bitmap locking. Is there any use ?? makes the OSX port renderer to fail */
/*#define USE_SDL_LOCKS */
/* image support stuff */
bool TCOD_sys_check_bmp(const char *filename);
SDL_Surface *TCOD_sys_read_bmp(const char *filename);
void TCOD_sys_write_bmp(const SDL_Surface *surf, const char *filename);
bool TCOD_sys_check_png(const char *filename);
SDL_Surface *TCOD_sys_read_png(const char *filename);
void TCOD_sys_write_png(const SDL_Surface *surf, const char *filename);
typedef struct {
char *extension;
bool (*check_type)(const char *filename);
SDL_Surface *(*read)(const char *filename);
void (*write)(const SDL_Surface *surf, const char *filename);
} image_support_t;
static image_support_t image_type[] = {
{ "BMP", TCOD_sys_check_bmp, TCOD_sys_read_bmp, TCOD_sys_write_bmp },
{ "PNG", TCOD_sys_check_png, TCOD_sys_read_png, TCOD_sys_write_png },
{ NULL, NULL, NULL, NULL },
};
scale_data_t scale_data={0};
SDL_Window* window=NULL;
SDL_Renderer* renderer=NULL;
float scale_factor=1.0f;
SDL_Surface* charmap=NULL;
char *last_clipboard_text = NULL;
static bool has_startup=false;
#define MAX_SCALE_FACTOR 5.0f
/* font transparent color */
static TCOD_color_t fontKeyCol={0,0,0};
static uint32_t sdl_key=0, rgb_mask=0, nrgb_mask=0;
/* mouse stuff */
static bool mousebl=false;
static bool mousebm=false;
static bool mousebr=false;
static bool mouse_force_bl=false;
static bool mouse_force_bm=false;
static bool mouse_force_br=false;
#ifdef TCOD_TOUCH_INPUT
static bool mouse_touch=true;
#endif
/* minimum length for a frame (when fps are limited) */
static int min_frame_length=0;
static int min_frame_length_backup=0;
/* number of frames in the last second */
static int fps=0;
/* current number of frames */
static int cur_fps=0;
/* length of the last rendering loop */
static float last_frame_length=0.0f;
static TCOD_color_t *charcols=NULL;
static bool *first_draw=NULL;
static bool key_status[TCODK_CHAR+1];
int oldFade=-1;
/* convert SDL vk to a char (depends on the keyboard layout) */
typedef struct {
SDL_Keycode sdl_key;
int tcod_key;
} vk_to_c_entry;
#define NUM_VK_TO_C_ENTRIES 10
static vk_to_c_entry vk_to_c[NUM_VK_TO_C_ENTRIES];
/* convert ASCII code to TCOD layout position */
static int init_ascii_to_tcod[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 77, 0, 0, 0, 0, 0, /* ASCII 0 to 15 */
71, 70, 72, 0, 0, 0, 0, 0, 64, 65, 67, 66, 0, 73, 68, 69, /* ASCII 16 to 31 */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* ASCII 32 to 47 */
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* ASCII 48 to 63 */
32, 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110, /* ASCII 64 to 79 */
111,112,113,114,115,116,117,118,119,120,121, 33, 34, 35, 36, 37, /* ASCII 80 to 95 */
38,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142, /* ASCII 96 to 111 */
143,144,145,146,147,148,149,150,151,152,153, 39, 40, 41, 42, 0, /* ASCII 112 to 127 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ASCII 128 to 143 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ASCII 144 to 159 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ASCII 160 to 175 */
43, 44, 45, 46, 49, 0, 0, 0, 0, 81, 78, 87, 88, 0, 0, 55, /* ASCII 176 to 191 */
53, 50, 52, 51, 47, 48, 0, 0, 85, 86, 82, 84, 83, 79, 80, 0, /* ASCII 192 to 207 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 54, 0, 0, 0, 0, 0, /* ASCII 208 to 223 */
74, 75, 57, 58, 59, 60, 61, 62, 63, 0, 0, 0, 0, 0, 0, 0, /* ASCII 224 to 239 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ASCII 240 to 255 */
};
static void alloc_ascii_tables(void) {
if ( TCOD_ctx.ascii_to_tcod ) free(TCOD_ctx.ascii_to_tcod);
if ( charcols ) {
free(charcols);
free(first_draw);
}
TCOD_ctx.ascii_to_tcod = (int *)calloc(sizeof(int),TCOD_ctx.max_font_chars);
charcols = (TCOD_color_t *)calloc(sizeof(TCOD_color_t),TCOD_ctx.max_font_chars);
first_draw =(bool *)calloc(sizeof(bool),TCOD_ctx.max_font_chars);
memcpy(TCOD_ctx.ascii_to_tcod,init_ascii_to_tcod,sizeof(int)*256);
}
static void check_ascii_to_tcod(void) {
if ( TCOD_ctx.fontNbCharHoriz * TCOD_ctx.fontNbCharVertic != TCOD_ctx.max_font_chars ) {
TCOD_ctx.max_font_chars=TCOD_ctx.fontNbCharHoriz * TCOD_ctx.fontNbCharVertic;
alloc_ascii_tables();
}
}
void TCOD_sys_register_SDL_renderer(SDL_renderer_t renderer) {
TCOD_ctx.sdl_cbk=renderer;
}
void TCOD_sys_map_ascii_to_font(int asciiCode, int fontCharX, int fontCharY) {
if ( asciiCode > 0 && asciiCode < TCOD_ctx.max_font_chars )
TCOD_ctx.ascii_to_tcod[asciiCode] = fontCharX + fontCharY * TCOD_ctx.fontNbCharHoriz;
}
void TCOD_sys_load_font(void) {
int i;
bool hasTransparent=false;
int x,y;
if ( charmap ) SDL_FreeSurface(charmap);
charmap=TCOD_sys_load_image(TCOD_ctx.font_file);
if (charmap == NULL ) TCOD_fatal("SDL : cannot load %s",TCOD_ctx.font_file);
if ( (float)(charmap->w / TCOD_ctx.fontNbCharHoriz) != charmap->w / TCOD_ctx.fontNbCharHoriz
|| (float)(charmap->h / TCOD_ctx.fontNbCharVertic) != charmap->h / TCOD_ctx.fontNbCharVertic ) TCOD_fatal(" %s size is not a multiple of font layout (%dx%d)\n",
TCOD_ctx.font_file,TCOD_ctx.fontNbCharHoriz,TCOD_ctx.fontNbCharVertic);
TCOD_ctx.font_width=charmap->w/TCOD_ctx.fontNbCharHoriz;
TCOD_ctx.font_height=charmap->h/TCOD_ctx.fontNbCharVertic;
/* allocated bool array for colored flags */
if ( TCOD_ctx.colored ) free(TCOD_ctx.colored);
TCOD_ctx.colored=(bool *)calloc(sizeof(bool), TCOD_ctx.fontNbCharHoriz*TCOD_ctx.fontNbCharVertic);
check_ascii_to_tcod();
/* figure out what kind of font we have */
/* check if the alpha layer is actually used */
if ( charmap->format->BytesPerPixel == 4 ) {
printf ("32bits font... checking for alpha layer... ");
for (x=0; !hasTransparent && x < charmap->w; x ++ ) {
for (y=0;!hasTransparent && y < charmap->h; y++ ) {
uint8_t*pixel=(uint8_t*)(charmap->pixels) + y * charmap->pitch + x * charmap->format->BytesPerPixel;
uint8_t alpha=*((pixel)+charmap->format->Ashift/8);
if ( alpha < 255 ) {
hasTransparent=true;
}
}
}
printf (hasTransparent ? "present\n" : "not present\n");
} else if ( charmap->format->BytesPerPixel != 3 ) {
/* convert to 24 bits */
SDL_Surface *temp;
printf ("font bpp < 24. converting to 24bits\n");
temp=(SDL_Surface *)TCOD_sys_get_surface(charmap->w,charmap->h,false);
SDL_BlitSurface(charmap,NULL,temp,NULL);
SDL_FreeSurface(charmap);
charmap=temp;
} else {
printf ("24 bits font.\n");
}
if (! hasTransparent ) {
/* alpha layer not used */
int keyx,keyy;
uint8_t*pixel;
/* the key color is found on the character corresponding to space ' ' */
if ( TCOD_ctx.font_tcod_layout ) {
keyx = TCOD_ctx.font_width/2;
keyy = TCOD_ctx.font_height/2;
} else if (TCOD_ctx.font_in_row) {
keyx = ((int)(' ') % TCOD_ctx.fontNbCharHoriz ) * TCOD_ctx.font_width + TCOD_ctx.font_width/2;
keyy = ((int)(' ') / TCOD_ctx.fontNbCharHoriz ) * TCOD_ctx.font_height + TCOD_ctx.font_height/2;
} else {
keyx = ((int)(' ') / TCOD_ctx.fontNbCharVertic ) * TCOD_ctx.font_width + TCOD_ctx.font_width/2;
keyy = ((int)(' ') % TCOD_ctx.fontNbCharVertic ) * TCOD_ctx.font_height + TCOD_ctx.font_height/2;
}
pixel=(uint8_t*)(charmap->pixels) + keyy * charmap->pitch + keyx * charmap->format->BytesPerPixel;
fontKeyCol.r=*((pixel)+charmap->format->Rshift/8);
fontKeyCol.g=*((pixel)+charmap->format->Gshift/8);
fontKeyCol.b=*((pixel)+charmap->format->Bshift/8);
printf ("key color : %d %d %d\n",fontKeyCol.r,fontKeyCol.g,fontKeyCol.b);
if ( ! TCOD_ctx.font_greyscale && charmap->format->BytesPerPixel == 4 ) {
/* 32 bits font but alpha layer not used. convert to 24 bits (faster) */
SDL_Surface *temp;
printf ("32bits font with no alpha => converting to faster 24 bits\n");
temp=(SDL_Surface *)TCOD_sys_get_surface(charmap->w,charmap->h,false);
SDL_BlitSurface(charmap,NULL,temp,NULL);
SDL_FreeSurface(charmap);
charmap=temp;
}
}
/* detect colored tiles */
for (i=0; i < TCOD_ctx.fontNbCharHoriz*TCOD_ctx.fontNbCharVertic; i++ ) {
int px,py,cx,cy;
bool end=false;
cx=(i%TCOD_ctx.fontNbCharHoriz);
cy=(i/TCOD_ctx.fontNbCharHoriz);
for( px=0; !end && px < TCOD_ctx.font_width; px++ ) {
for (py=0; !end && py < TCOD_ctx.font_height; py++ ) {
uint8_t*pixel=(uint8_t*)(charmap->pixels) + (cy*TCOD_ctx.font_height+py) * charmap->pitch
+ (cx*TCOD_ctx.font_width+px) * charmap->format->BytesPerPixel;
uint8_t r=*((pixel)+charmap->format->Rshift/8);
uint8_t g=*((pixel)+charmap->format->Gshift/8);
uint8_t b=*((pixel)+charmap->format->Bshift/8);
if ( charmap->format->BytesPerPixel == 3 ) {
/* ignore key color */
if ( r == fontKeyCol.r && g == fontKeyCol.g && b == fontKeyCol.b ) continue;
}
/* colored tile if a pixel is not desaturated */
if ( r != g || g !=b || b != r ) {
TCOD_ctx.colored[i]=true;
printf ("character for ascii code %d is colored\n",i);
end=true;
}
}
}
}
/* convert 24/32 bits greyscale to 32bits font with alpha layer */
if ( ! hasTransparent && TCOD_ctx.font_greyscale ) {
bool invert=( fontKeyCol.r > 128 ); /* black on white font ? */
/* convert the surface to 32 bits if needed */
if ( charmap->format->BytesPerPixel != 4 ) {
SDL_Surface *temp;
printf("24bits greyscale font. converting to 32bits\n");
temp=(SDL_Surface *)TCOD_sys_get_surface(charmap->w,charmap->h,true);
SDL_BlitSurface(charmap,NULL,temp,NULL);
SDL_FreeSurface(charmap);
charmap=temp;
}
for (i=0; i < TCOD_ctx.fontNbCharHoriz*TCOD_ctx.fontNbCharVertic; i++ ) {
int cx,cy;
cx=(i%TCOD_ctx.fontNbCharHoriz);
cy=(i/TCOD_ctx.fontNbCharHoriz);
/* fill the surface with white (except colored tiles), use alpha layer for characters */
for (x=cx*TCOD_ctx.font_width; x < (cx+1)*TCOD_ctx.font_width; x ++ ) {
for (y=cy*TCOD_ctx.font_height;y < (cy+1)*TCOD_ctx.font_height; y++ ) {
if ( ! TCOD_ctx.colored[i]) {
uint8_t*pixel=(uint8_t*)(charmap->pixels) + y * charmap->pitch + x * charmap->format->BytesPerPixel;
uint8_t r=*((pixel)+charmap->format->Rshift/8);
*((pixel)+charmap->format->Ashift/8) = (invert ? 255-r : r);
*((pixel)+charmap->format->Rshift/8)=255;
*((pixel)+charmap->format->Gshift/8)=255;
*((pixel)+charmap->format->Bshift/8)=255;
} else {
uint8_t*pixel=(uint8_t*)(charmap->pixels) + y * charmap->pitch + x * charmap->format->BytesPerPixel;
uint8_t r=*((pixel)+charmap->format->Rshift/8);
uint8_t g=*((pixel)+charmap->format->Gshift/8);
uint8_t b=*((pixel)+charmap->format->Bshift/8);
if ( r == fontKeyCol.r && g == fontKeyCol.g && b == fontKeyCol.b ) {
*((pixel)+charmap->format->Ashift/8) = 0;
} else {
*((pixel)+charmap->format->Ashift/8) = 255;
}
}
}
}
}
}
sdl_key=SDL_MapRGB(charmap->format,fontKeyCol.r,fontKeyCol.g,fontKeyCol.b);
rgb_mask=charmap->format->Rmask|charmap->format->Gmask|charmap->format->Bmask;
nrgb_mask = ~ rgb_mask;
sdl_key &= rgb_mask; /* remove the alpha part */
if ( charmap->format->BytesPerPixel == 3 ) SDL_SetColorKey(charmap,SDL_TRUE|SDL_RLEACCEL,sdl_key);
for (i=0; i < TCOD_ctx.fontNbCharHoriz*TCOD_ctx.fontNbCharVertic; i++ ) {
charcols[i]=fontKeyCol;
first_draw[i]=true;
}
check_ascii_to_tcod();
if (!TCOD_ctx.font_tcod_layout) {
/* apply standard ascii mapping */
if ( TCOD_ctx.font_in_row ) {
/* for font in row */
for (i=0; i < TCOD_ctx.max_font_chars; i++ ) TCOD_ctx.ascii_to_tcod[i]=i;
} else {
/* for font in column */
for (i=0; i < TCOD_ctx.max_font_chars; i++ ) {
int fy = i % TCOD_ctx.fontNbCharVertic;
int fx = i / TCOD_ctx.fontNbCharVertic;
TCOD_ctx.ascii_to_tcod[i]=fx + fy * TCOD_ctx.fontNbCharHoriz;
}
}
}
}
void TCOD_sys_set_custom_font(const char *fontFile,int nb_ch, int nb_cv, int flags) {
strcpy(TCOD_ctx.font_file,fontFile);
/* if layout not defined, assume ASCII_INCOL */
if (flags == 0 || flags == TCOD_FONT_TYPE_GREYSCALE) flags |= TCOD_FONT_LAYOUT_ASCII_INCOL;
TCOD_ctx.font_in_row=((flags & TCOD_FONT_LAYOUT_ASCII_INROW) != 0);
TCOD_ctx.font_greyscale = ((flags & TCOD_FONT_TYPE_GREYSCALE) != 0 );
TCOD_ctx.font_tcod_layout = ((flags & TCOD_FONT_LAYOUT_TCOD) != 0 );
if ( nb_ch> 0 ) {
TCOD_ctx.fontNbCharHoriz=nb_ch;
TCOD_ctx.fontNbCharVertic=nb_cv;
} else {
if ( ( flags & TCOD_FONT_LAYOUT_ASCII_INROW ) || ( flags & TCOD_FONT_LAYOUT_ASCII_INCOL ) ) {
TCOD_ctx.fontNbCharHoriz=16;
TCOD_ctx.fontNbCharVertic=16;
} else {
TCOD_ctx.fontNbCharHoriz=32;
TCOD_ctx.fontNbCharVertic=8;
}
}
if ( TCOD_ctx.font_tcod_layout ) TCOD_ctx.font_in_row=true;
check_ascii_to_tcod();
/*
screw up things on linux64. apparently, useless
TCOD_sys_load_font();
*/
}
void find_resolution(void) {
TCOD_ctx.actual_fullscreen_width=TCOD_ctx.fullscreen_width>TCOD_ctx.root->w*TCOD_ctx.font_width?TCOD_ctx.fullscreen_width:TCOD_ctx.root->w*TCOD_ctx.font_width;
TCOD_ctx.actual_fullscreen_height=TCOD_ctx.fullscreen_height>TCOD_ctx.root->h*TCOD_ctx.font_height?TCOD_ctx.fullscreen_height:TCOD_ctx.root->h*TCOD_ctx.font_height;
sdl->get_closest_mode(&TCOD_ctx.actual_fullscreen_width,&TCOD_ctx.actual_fullscreen_height);
}
void *TCOD_sys_create_bitmap_for_console(TCOD_console_t console) {
int w,h;
w = TCOD_console_get_width(console) * TCOD_ctx.font_width;
h = TCOD_console_get_height(console) * TCOD_ctx.font_height;
return TCOD_sys_get_surface(w,h,false);
}
static void TCOD_sys_render(void *vbitmap, TCOD_console_data_t* console) {
sdl->render(sdl, vbitmap, console);
}
void TCOD_sys_console_to_bitmap(void *vbitmap,
TCOD_console_data_t* console, TCOD_console_data_t* cache) {
static SDL_Surface *charmap_backup=NULL;
SDL_Surface *bitmap = (SDL_Surface *)vbitmap;
int x,y;
uint32_t sdl_back=0, sdl_fore=0;
TCOD_color_t fading_color = TCOD_console_get_fading_color();
int fade = (int)TCOD_console_get_fade();
/* can only track changes on the root console */
bool track_changes = (cache && oldFade == fade);
uint8_t bpp = charmap->format->BytesPerPixel;
int *c = console->ch_array;
int *oc;
TCOD_color_t *ofg, *obg, *nfg, *nbg;
int hdelta;
if ( bpp == 4 ) {
hdelta=(charmap->pitch - TCOD_ctx.font_width*bpp)/4;
} else {
hdelta=(charmap->pitch - TCOD_ctx.font_width*bpp);
}
nfg = TCOD_image_get_colors(console->fg_colors);
nbg = TCOD_image_get_colors(console->bg_colors);
if (track_changes) {
oc = cache->ch_array;
ofg = TCOD_image_get_colors(cache->fg_colors);
obg = TCOD_image_get_colors(cache->bg_colors);
}
if ( charmap_backup == NULL ) {
charmap_backup=(SDL_Surface *)TCOD_sys_get_surface(charmap->w,charmap->h,true);
SDL_BlitSurface(charmap,NULL,charmap_backup,NULL);
}
#ifdef USE_SDL_LOCKS
if ( SDL_MUSTLOCK( bitmap ) && SDL_LockSurface( bitmap ) < 0 ) return;
#endif
for (y = 0; y < console->h; y++) {
for (x = 0; x < console->w; x++) {
SDL_Rect srcRect,dstRect;
bool changed=true;
if ( track_changes ) {
changed=false;
if (
nbg->r != obg->r || nbg->g != obg->g || nbg->b != obg->b ||
nfg->r != ofg->r || nfg->g != ofg->g || nfg->b != ofg->b ||
*c != *oc) {
changed=true;
}
}
if ( changed ) {
TCOD_color_t b=*nbg;
dstRect.x=x*TCOD_ctx.font_width;
dstRect.y=y*TCOD_ctx.font_height;
dstRect.w=TCOD_ctx.font_width;
dstRect.h=TCOD_ctx.font_height;
/* draw background */
if ( fade != 255 ) {
b.r = ((int)b.r) * fade / 255 + ((int)fading_color.r) * (255-fade)/255;
b.g = ((int)b.g) * fade / 255 + ((int)fading_color.g) * (255-fade)/255;
b.b = ((int)b.b) * fade / 255 + ((int)fading_color.b) * (255-fade)/255;
}
sdl_back=SDL_MapRGB(bitmap->format,b.r,b.g,b.b);
SDL_FillRect(bitmap,&dstRect,sdl_back);
if ( *c != ' ' ) {
/* draw foreground */
int ascii = TCOD_ctx.ascii_to_tcod[*c];
TCOD_color_t *curtext = &charcols[ascii];
bool first = first_draw[ascii];
TCOD_color_t f=*nfg;
if ( fade != 255 ) {
f.r = ((int)f.r) * fade / 255 + ((int)fading_color.r) * (255-fade)/255;
f.g = ((int)f.g) * fade / 255 + ((int)fading_color.g) * (255-fade)/255;
f.b = ((int)f.b) * fade / 255 + ((int)fading_color.b) * (255-fade)/255;
}
/* only draw character if foreground color != background color */
if ( f.r != b.r || f.g != b.g || f.b != b.b ) {
if ( charmap->format->Amask == 0
&& f.r == fontKeyCol.r && f.g == fontKeyCol.g && f.b == fontKeyCol.b ) {
/* cannot draw with the key color... */
if ( f.r < 255 ) f.r++; else f.r--;
}
srcRect.x = (ascii%TCOD_ctx.fontNbCharHoriz)*TCOD_ctx.font_width;
srcRect.y = (ascii/TCOD_ctx.fontNbCharHoriz)*TCOD_ctx.font_height;
srcRect.w=TCOD_ctx.font_width;
srcRect.h=TCOD_ctx.font_height;
if ( charmap && (first || curtext->r != f.r || curtext->g != f.g || curtext->b!=f.b) ) {
/* change the character color in the font */
first_draw[ascii]=false;
sdl_fore=SDL_MapRGB(charmap->format,f.r,f.g,f.b) & rgb_mask;
*curtext=f;
#ifdef USE_SDL_LOCKS
if ( SDL_MUSTLOCK(charmap) ) {
if ( SDL_LockSurface(charmap) < 0 ) return;
}
#endif
if ( bpp == 4 ) {
/* 32 bits font : fill the whole character with foreground color */
uint32_t *pix = (uint32_t *)(((uint8_t*)charmap->pixels)+srcRect.x*bpp + srcRect.y*charmap->pitch);
int h=TCOD_ctx.font_height;
if ( ! TCOD_ctx.colored[ascii] ) {
while ( h-- ) {
int w=TCOD_ctx.font_width;
while ( w-- ) {
(*pix) &= nrgb_mask;
(*pix) |= sdl_fore;
pix++;
}
pix += hdelta;
}
} else {
/* colored character : multiply color with foreground color */
uint32_t *pixorig = (uint32_t *)(((uint8_t*)charmap_backup->pixels)+srcRect.x*bpp + srcRect.y*charmap_backup->pitch);
int hdelta_backup=(charmap_backup->pitch - TCOD_ctx.font_width*4)/4;
while (h> 0) {
int w=TCOD_ctx.font_width;
while ( w > 0 ) {
int r=(int)(*((uint8_t*)(pixorig)+charmap_backup->format->Rshift/8));
int g=(int)(*((uint8_t*)(pixorig)+charmap_backup->format->Gshift/8));
int b=(int)(*((uint8_t*)(pixorig)+charmap_backup->format->Bshift/8));
(*pix) &= nrgb_mask; /* erase the color */
r = r * f.r / 255;
g = g * f.g / 255;
b = b * f.b / 255;
/* set the new color */
(*pix) |= (r<<charmap->format->Rshift)|(g<<charmap->format->Gshift)|(b<<charmap->format->Bshift);
w--;
pix++;
pixorig++;
}
h--;
pix += hdelta;
pixorig += hdelta_backup;
}
}
} else {
/* 24 bits font : fill only non key color pixels */
uint32_t *pix = (uint32_t *)(((uint8_t*)charmap->pixels)+srcRect.x*bpp + srcRect.y*charmap->pitch);
int h=TCOD_ctx.font_height;
if ( ! TCOD_ctx.colored[ascii] ) {
while ( h-- ) {
int w=TCOD_ctx.font_width;
while ( w-- ) {
if (((*pix) & rgb_mask) != sdl_key ) {
(*pix) &= nrgb_mask;
(*pix) |= sdl_fore;
}
pix = (uint32_t *) (((uint8_t*)pix)+3);
}
pix = (uint32_t *) (((uint8_t*)pix)+hdelta);
}
} else {
/* colored character : multiply color with foreground color */
uint32_t *pixorig = (uint32_t *)(((uint8_t*)charmap_backup->pixels)+srcRect.x*4 + srcRect.y*charmap_backup->pitch);
/* charmap_backup is always 32 bits */
int hdelta_backup=(charmap_backup->pitch - TCOD_ctx.font_width*4)/4;
while (h> 0) {
int w=TCOD_ctx.font_width;
while ( w > 0 ) {
if (((*pixorig) & rgb_mask) != sdl_key ) {
int r=(int)(*((uint8_t*)(pixorig)+charmap_backup->format->Rshift/8));
int g=(int)(*((uint8_t*)(pixorig)+charmap_backup->format->Gshift/8));
int b=(int)(*((uint8_t*)(pixorig)+charmap_backup->format->Bshift/8));
(*pix) &= nrgb_mask; /* erase the color */
r = r * f.r / 255;
g = g * f.g / 255;
b = b * f.b / 255;
/* set the new color */
(*pix) |= (r<<charmap->format->Rshift)|(g<<charmap->format->Gshift)|(b<<charmap->format->Bshift);
}
w--;
pix = (uint32_t *) (((uint8_t*)pix)+3);
pixorig ++;
}
h--;
pix = (uint32_t *) (((uint8_t*)pix)+hdelta);
pixorig += hdelta_backup;
}
}
}
#ifdef USE_SDL_LOCKS
if ( SDL_MUSTLOCK(charmap) ) {
SDL_UnlockSurface(charmap);
}
#endif
}
SDL_BlitSurface(charmap,&srcRect,bitmap,&dstRect);
}
}
}
c++;
nfg++;
nbg++;
if (track_changes) {
oc++;
ofg++;
obg++;
}
}
}
#ifdef USE_SDL_LOCKS
if ( SDL_MUSTLOCK( bitmap ) ) SDL_UnlockSurface( bitmap );
#endif
if (cache) {
/* update previous values cache */
memcpy(cache->ch_array, console->ch_array,
sizeof(console->ch_array[0]) * console->w * console->h);
TCOD_image_mipmap_copy_internal(console->fg_colors, cache->fg_colors);
TCOD_image_mipmap_copy_internal(console->bg_colors, cache->bg_colors);
}
}
void *TCOD_sys_get_surface(int width, int height, bool alpha) {
return sdl->create_surface(width,height,alpha);
}
void TCOD_sys_update_char(int asciiCode, int fontx, int fonty, TCOD_image_t img, int x, int y) {
int px,py;
int iw,ih;
static TCOD_color_t pink={255,0,255};
TCOD_sys_map_ascii_to_font(asciiCode,fontx,fonty);
TCOD_image_get_size(img,&iw,&ih);
for (px=0; px < TCOD_ctx.font_width; px ++ ) {
for (py=0;py < TCOD_ctx.font_height; py++ ) {
TCOD_color_t col=TCOD_white;
uint8_t*pixel;
uint8_t bpp;
if ( (unsigned)(x+px) < (unsigned)iw && (unsigned)(y+py) < (unsigned)ih ) {
col=TCOD_image_get_pixel(img,x+px,y+py);
}
pixel=(uint8_t*)(charmap->pixels) + (fonty*TCOD_ctx.font_height+py) * charmap->pitch + (fontx*TCOD_ctx.font_width+px) * charmap->format->BytesPerPixel;
bpp = charmap->format->BytesPerPixel;
if (bpp == 4 ) {
*((pixel)+charmap->format->Ashift/8) = col.r;
*((pixel)+charmap->format->Rshift/8)=255;
*((pixel)+charmap->format->Gshift/8)=255;
*((pixel)+charmap->format->Bshift/8)=255;
} else {
*((pixel)+charmap->format->Rshift/8)=col.r;
*((pixel)+charmap->format->Gshift/8)=col.g;
*((pixel)+charmap->format->Bshift/8)=col.b;
}
}
}
charcols[asciiCode]=pink;
TCOD_sys_set_dirty_character_code(asciiCode);
}
void TCOD_sys_startup(void) {
if (has_startup) return;
#ifndef NDEBUG
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
#endif
TCOD_IFNOT(SDL_Init(SDL_INIT_VIDEO) >= 0 ) return;
memset(&TCOD_ctx.key_state,0,sizeof(TCOD_key_t));
TCOD_ctx.max_font_chars=256;
alloc_ascii_tables();
#ifndef NO_OPENGL
TCOD_opengl_init_attributes();
#endif
has_startup=true;
}
void TCOD_sys_shutdown(void) {
if (!has_startup) return;
TCOD_sys_uninit();
sdl->shutdown();
SDL_Quit();
memset(&scale_data, 0, sizeof(scale_data_t));
has_startup = false;
}
static void TCOD_sys_load_player_config(void) {
const char *renderer;
const char *font;
int fullscreenWidth,fullscreenHeight;
/* define file structure */
TCOD_parser_t parser=TCOD_parser_new();
TCOD_parser_struct_t libtcod = TCOD_parser_new_struct(parser, "libtcod");
TCOD_struct_add_property(libtcod, "renderer", TCOD_TYPE_STRING, false);
TCOD_struct_add_property(libtcod, "font", TCOD_TYPE_STRING, false);
TCOD_struct_add_property(libtcod, "fontInRow", TCOD_TYPE_BOOL, false);
TCOD_struct_add_property(libtcod, "fontGreyscale", TCOD_TYPE_BOOL, false);
TCOD_struct_add_property(libtcod, "fontTcodLayout", TCOD_TYPE_BOOL, false);
TCOD_struct_add_property(libtcod, "fontNbCharHoriz", TCOD_TYPE_INT, false);
TCOD_struct_add_property(libtcod, "fontNbCharVertic", TCOD_TYPE_INT, false);
TCOD_struct_add_property(libtcod, "fullscreen", TCOD_TYPE_BOOL, false);
TCOD_struct_add_property(libtcod, "fullscreenWidth", TCOD_TYPE_INT, false);
TCOD_struct_add_property(libtcod, "fullscreenHeight", TCOD_TYPE_INT, false);
TCOD_struct_add_property(libtcod, "fullscreenScaling", TCOD_TYPE_BOOL, false);
/* parse file */
TCOD_parser_run(parser,"./libtcod.cfg",NULL);
/* set user preferences */
renderer=TCOD_parser_get_string_property(parser, "libtcod.renderer");
if ( renderer != NULL ) {
/* custom renderer */
if ( TCOD_strcasecmp(renderer,"GLSL") == 0 ) TCOD_ctx.renderer=TCOD_RENDERER_GLSL;
else if ( TCOD_strcasecmp(renderer,"OPENGL") == 0 ) TCOD_ctx.renderer=TCOD_RENDERER_OPENGL;
else if ( TCOD_strcasecmp(renderer,"SDL") == 0 ) TCOD_ctx.renderer=TCOD_RENDERER_SDL;
else printf ("Warning : unknown renderer '%s' in libtcod.cfg\n", renderer);
}
font=TCOD_parser_get_string_property(parser, "libtcod.font");
if ( font != NULL ) {
/* custom font */
if ( TCOD_sys_file_exists(font)) {
int fontNbCharHoriz,fontNbCharVertic;
strcpy(TCOD_ctx.font_file,font);
TCOD_ctx.font_in_row=TCOD_parser_get_bool_property(parser,"libtcod.fontInRow");
TCOD_ctx.font_greyscale=TCOD_parser_get_bool_property(parser,"libtcod.fontGreyscale");
TCOD_ctx.font_tcod_layout=TCOD_parser_get_bool_property(parser,"libtcod.fontTcodLayout");
fontNbCharHoriz=TCOD_parser_get_int_property(parser,"libtcod.fontNbCharHoriz");
fontNbCharVertic=TCOD_parser_get_int_property(parser,"libtcod.fontNbCharVertic");
if ( fontNbCharHoriz > 0 ) TCOD_ctx.fontNbCharHoriz=fontNbCharHoriz;
if ( fontNbCharVertic > 0 ) TCOD_ctx.fontNbCharVertic=fontNbCharVertic;
if ( charmap ) {
SDL_FreeSurface(charmap);
charmap=NULL;
}
} else {
printf ("Warning : font file '%s' does not exist\n",font);
}
}
/* custom fullscreen resolution */
TCOD_ctx.fullscreen=TCOD_parser_get_bool_property(parser,"libtcod.fullscreen");
fullscreenWidth=TCOD_parser_get_int_property(parser,"libtcod.fullscreenWidth");
fullscreenHeight=TCOD_parser_get_int_property(parser,"libtcod.fullscreenHeight");
if ( fullscreenWidth > 0 ) TCOD_ctx.fullscreen_width=fullscreenWidth;
if ( fullscreenHeight > 0 ) TCOD_ctx.fullscreen_height=fullscreenHeight;
}
TCOD_renderer_t TCOD_sys_get_renderer(void) {
return TCOD_ctx.renderer;
}
void TCOD_sys_set_renderer(TCOD_renderer_t renderer) {
if ( renderer == TCOD_ctx.renderer ) return;
TCOD_ctx.renderer=renderer;
TCOD_sys_uninit();
TCOD_sys_init(TCOD_ctx.root, TCOD_ctx.fullscreen);
TCOD_console_set_window_title(TCOD_ctx.window_title);
TCOD_console_set_dirty(0,0,TCOD_ctx.root->w,TCOD_ctx.root->h);
}
void TCOD_sys_init_screen_offset(void) {
TCOD_ctx.fullscreen_offsetx=(TCOD_ctx.actual_fullscreen_width-TCOD_ctx.root->w*TCOD_ctx.font_width)/2;
TCOD_ctx.fullscreen_offsety=(TCOD_ctx.actual_fullscreen_height-TCOD_ctx.root->h*TCOD_ctx.font_height)/2;
}
bool TCOD_sys_init(TCOD_console_data_t *console, bool fullscreen) {
static TCOD_renderer_t last_renderer=TCOD_RENDERER_SDL;
static char last_font[512]="";
TCOD_sys_startup();
/* check if there is a user (player) config file */
if ( TCOD_sys_file_exists("./libtcod.cfg")) {
/* yes, read it */
TCOD_sys_load_player_config();
if (TCOD_ctx.fullscreen) fullscreen=true;
}
if (last_renderer != TCOD_ctx.renderer || ! charmap || strcmp(last_font,TCOD_ctx.font_file) != 0) {
/* reload the font when switching renderer to restore original character colors */
TCOD_sys_load_font();
}
sdl->create_window(console->w, console->h, fullscreen);
memset(key_status,0,sizeof(bool)*(TCODK_CHAR+1));
return true;
}
void TCOD_sys_uninit(void) {
if (!has_startup) return;
sdl->destroy_window();
}
void TCOD_sys_save_bitmap(void *bitmap, const char *filename) {
image_support_t *img=image_type;
while ( img->extension != NULL && strcasestr(filename,img->extension) == NULL ) img++;
if ( img->extension == NULL || img->write == NULL ) img=image_type; /* default to bmp */
img->write((SDL_Surface *)bitmap,filename);
}
void TCOD_sys_save_screenshot(const char *filename) {
char buf[128];
if ( filename == NULL ) {
/* generate filename */
int idx=0;
do {
FILE *f=NULL;
sprintf(buf,"./screenshot%03d.png",idx);
f=fopen(buf,"rb");
if ( ! f ) filename=buf;
else {
idx++;
fclose(f);
}
} while(!filename);
}
sdl->save_screenshot(filename);
}
void TCOD_sys_set_fullscreen(bool fullscreen) {
TCOD_ctx.fullscreen=fullscreen;
sdl->set_fullscreen(fullscreen);
}
void TCOD_sys_set_scale_factor(float value) {
float old_scale_factor = scale_factor;
scale_factor = value;
if (scale_factor + 1e-3 < scale_data.min_scale_factor)
scale_factor = scale_data.min_scale_factor;
else if (scale_factor - 1e-3 > MAX_SCALE_FACTOR)
scale_factor = MAX_SCALE_FACTOR;
/*printf("scale_factor: %0.3f -> %0.3f (wanted: %0.3f)", old_scale_factor, scale_factor, value);*/
}
void TCOD_sys_set_window_title(const char *title) {
strcpy(TCOD_ctx.window_title,title);
sdl->set_window_title(title);
}
void TCOD_sys_flush(bool render) {
static uint32_t old_time,new_time=0, elapsed=0;
int32_t frame_time,time_to_wait;
if ( render ) {
TCOD_sys_render(NULL, TCOD_ctx.root);
}
old_time=new_time;
new_time=TCOD_sys_elapsed_milli();
/* If TCOD has been terminated and restarted. */
if (old_time > new_time)
old_time = elapsed = 0;
if ( new_time / 1000 != elapsed ) {
/* update fps every second */
fps=cur_fps;
cur_fps=0;
elapsed=new_time/1000;
}
/* if too fast, wait */
frame_time=(new_time - old_time);
last_frame_length = frame_time * 0.001f;
cur_fps++;
time_to_wait=min_frame_length-frame_time;
if (old_time > 0 && time_to_wait > 0) {
TCOD_sys_sleep_milli(time_to_wait);
new_time = TCOD_sys_elapsed_milli();
frame_time=(new_time - old_time);
}
last_frame_length = frame_time * 0.001f;
}
static char TCOD_sys_get_vk(SDL_Keycode sdl_key) {
int i;
for (i = 0; i < NUM_VK_TO_C_ENTRIES; i++) {
if (vk_to_c[i].sdl_key == sdl_key) {
vk_to_c[i].sdl_key = 0;
return vk_to_c[i].tcod_key;
}
}
return 0;
}
static void TCOD_sys_set_vk(SDL_Keycode sdl_key, char tcod_key) {
int i;
for (i = 0; i < NUM_VK_TO_C_ENTRIES; i++) {
if (vk_to_c[i].sdl_key == 0) {
vk_to_c[i].sdl_key = sdl_key;
vk_to_c[i].tcod_key = tcod_key;
break;
}
}
}
static void TCOD_sys_convert_event(SDL_Event *ev, TCOD_key_t *ret) {
SDL_KeyboardEvent *kev=&ev->key;
/* SDL2 does not map keycodes and modifiers to characters, this is on the developer.
Presumably in order to avoid problems with different keyboard layouts, they
are expected to write their own key mapping editing code for the user. */
if (SDLK_SCANCODE_MASK == (kev->keysym.sym & SDLK_SCANCODE_MASK))
ret->c = 0;
else
ret->c = kev->keysym.sym;
if ( ( kev->keysym.mod & (KMOD_LCTRL|KMOD_RCTRL) ) != 0 ) {
/* when pressing CTRL-A, we don't get unicode for 'a', but unicode for CTRL-A = 1. Fix it */
if ( kev->keysym.sym >= SDLK_a && kev->keysym.sym <= SDLK_z ) {
ret->c = 'a'+(kev->keysym.sym - SDLK_a);
}
}
if ( ev->type == SDL_KEYDOWN ) TCOD_sys_set_vk(kev->keysym.sym, ret->c);
else if (ev->type == SDL_KEYUP ) ret->c = TCOD_sys_get_vk(kev->keysym.sym);
switch(kev->keysym.sym) {
case SDLK_ESCAPE : ret->vk=TCODK_ESCAPE;break;
case SDLK_SPACE : ret->vk=TCODK_SPACE; break;
case SDLK_BACKSPACE : ret->vk=TCODK_BACKSPACE;break;
case SDLK_TAB : ret->vk=TCODK_TAB;break;
case SDLK_RETURN : ret->vk=TCODK_ENTER;break;
case SDLK_PAUSE : ret->vk=TCODK_PAUSE;break;
case SDLK_PAGEUP : ret->vk=TCODK_PAGEUP;break;
case SDLK_PAGEDOWN : ret->vk=TCODK_PAGEDOWN;break;
case SDLK_HOME : ret->vk=TCODK_HOME;break;
case SDLK_END : ret->vk=TCODK_END;break;
case SDLK_DELETE : ret->vk=TCODK_DELETE;break;
case SDLK_INSERT : ret->vk=TCODK_INSERT; break;
case SDLK_LALT : case SDLK_RALT : ret->vk=TCODK_ALT;break;
case SDLK_LCTRL : case SDLK_RCTRL : ret->vk=TCODK_CONTROL;break;
case SDLK_LSHIFT : case SDLK_RSHIFT : ret->vk=TCODK_SHIFT;break;
case SDLK_PRINTSCREEN : ret->vk=TCODK_PRINTSCREEN;break;
case SDLK_LEFT : ret->vk=TCODK_LEFT;break;
case SDLK_UP : ret->vk=TCODK_UP;break;
case SDLK_RIGHT : ret->vk=TCODK_RIGHT;break;
case SDLK_DOWN : ret->vk=TCODK_DOWN;break;
case SDLK_F1 : ret->vk=TCODK_F1;break;
case SDLK_F2 : ret->vk=TCODK_F2;break;
case SDLK_F3 : ret->vk=TCODK_F3;break;
case SDLK_F4 : ret->vk=TCODK_F4;break;
case SDLK_F5 : ret->vk=TCODK_F5;break;
case SDLK_F6 : ret->vk=TCODK_F6;break;
case SDLK_F7 : ret->vk=TCODK_F7;break;
case SDLK_F8 : ret->vk=TCODK_F8;break;
case SDLK_F9 : ret->vk=TCODK_F9;break;
case SDLK_F10 : ret->vk=TCODK_F10;break;
case SDLK_F11 : ret->vk=TCODK_F11;break;
case SDLK_F12 : ret->vk=TCODK_F12;break;
case SDLK_0 : ret->vk=TCODK_0;break;
case SDLK_1 : ret->vk=TCODK_1;break;
case SDLK_2 : ret->vk=TCODK_2;break;
case SDLK_3 : ret->vk=TCODK_3;break;
case SDLK_4 : ret->vk=TCODK_4;break;
case SDLK_5 : ret->vk=TCODK_5;break;
case SDLK_6 : ret->vk=TCODK_6;break;
case SDLK_7 : ret->vk=TCODK_7;break;
case SDLK_8 : ret->vk=TCODK_8;break;
case SDLK_9 : ret->vk=TCODK_9;break;
case SDLK_RGUI : ret->vk=TCODK_RWIN;break;
case SDLK_LGUI : ret->vk=TCODK_LWIN;break;
case SDLK_NUMLOCKCLEAR : ret->vk=TCODK_NUMLOCK;break;
case SDLK_KP_0 : ret->vk=TCODK_KP0;break;
case SDLK_KP_1 : ret->vk=TCODK_KP1;break;
case SDLK_KP_2 : ret->vk=TCODK_KP2;break;
case SDLK_KP_3 : ret->vk=TCODK_KP3;break;
case SDLK_KP_4 : ret->vk=TCODK_KP4;break;
case SDLK_KP_5 : ret->vk=TCODK_KP5;break;
case SDLK_KP_6 : ret->vk=TCODK_KP6;break;
case SDLK_KP_7 : ret->vk=TCODK_KP7;break;
case SDLK_KP_8 : ret->vk=TCODK_KP8;break;
case SDLK_KP_9 : ret->vk=TCODK_KP9;break;
case SDLK_KP_DIVIDE : ret->vk=TCODK_KPDIV;break;
case SDLK_KP_MULTIPLY : ret->vk=TCODK_KPMUL;break;
case SDLK_KP_PLUS : ret->vk=TCODK_KPADD;break;
case SDLK_KP_MINUS : ret->vk=TCODK_KPSUB;break;
case SDLK_KP_ENTER : ret->vk=TCODK_KPENTER;break;
case SDLK_KP_PERIOD : ret->vk=TCODK_KPDEC;break;
default : ret->vk=TCODK_CHAR; break;
}
}
static TCOD_key_t TCOD_sys_SDLtoTCOD(SDL_Event *ev, int flags) {
TCOD_key_t *ret = &TCOD_ctx.key_state;
ret->vk=TCODK_NONE;
ret->c=0;
ret->pressed=0;
switch (ev->type) {
/* handled in TCOD_sys_handle_event */
/*
case SDL_QUIT :
case SDL_VIDEOEXPOSE :
case SDL_MOUSEBUTTONDOWN : {
case SDL_MOUSEBUTTONUP : {
break;
*/
case SDL_KEYUP : {
SDL_KeyboardEvent *kev=&ev->key;
TCOD_key_t tmpkey;
switch(kev->keysym.sym) {
case SDLK_LALT : ret->lalt=0; break;
case SDLK_RALT : ret->ralt=0; break;
case SDLK_LCTRL : ret->lctrl=0; break;
case SDLK_RCTRL : ret->rctrl=0; break;
case SDLK_LSHIFT : ret->shift=0; break;
case SDLK_RSHIFT : ret->shift=0; break;
case SDLK_LGUI : ret->lmeta=0; break;
case SDLK_RGUI : ret->rmeta=0; break;
default:break;
}
TCOD_sys_convert_event(ev,&tmpkey);
key_status[tmpkey.vk]=false;
if ( flags & TCOD_KEY_RELEASED ) {
ret->vk=tmpkey.vk;
ret->c=tmpkey.c;
ret->pressed=0;