-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathGameLib.SDL.h
More file actions
5061 lines (4434 loc) · 164 KB
/
Copy pathGameLib.SDL.h
File metadata and controls
5061 lines (4434 loc) · 164 KB
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
//=====================================================================
//
// GameLib.SDL.h - A single-header cross-platform game library
//
// Homepage: https://github.com/skywind3000/GameLib
// Copyright (c) 2026 skywind3000 (Lin Wei)
//
// Uses SDL2 as the platform backend and keeps a GameLib-style API.
// The current scaffold already supports window creation, input,
// framebuffer rendering, built-in 8x8 text, software sprite drawing,
// and tilemaps. SDL_image / SDL_ttf / SDL_mixer features are enabled
// automatically when their headers are available at compile time.
//
// How to use (single file project, most common):
//
// #include "GameLib.SDL.h"
//
// int main() {
// GameLib game;
// game.Open(640, 480, "My Game", true);
// while (!game.IsClosed()) {
// game.Clear(COLOR_BLACK);
// game.DrawText(10, 10, "Hello SDL", COLOR_WHITE);
// game.Update();
// game.WaitFrame(60);
// }
// return 0;
// }
//
// Multi-file project: add this line before #include in the main .cpp file
// #define GAMELIB_SDL_IMPLEMENTATION
// #include "GameLib.SDL.h"
// In other .cpp files, add this line
// #define GAMELIB_SDL_NO_IMPLEMENTATION
// #include "GameLib.SDL.h"
//
// Compile command (MinGW / Dev C++):
// g++ -o game main.cpp -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
//
//=====================================================================
#ifndef GAMELIB_SDL_H
#define GAMELIB_SDL_H
#ifdef GAMELIB_H
#error GameLib.h and GameLib.SDL.h cannot be included in the same translation unit.
#endif
#ifndef SDL_MAIN_HANDLED
#define SDL_MAIN_HANDLED
#endif
#ifndef GAMELIB_SDL_NO_IMPLEMENTATION
#ifndef GAMELIB_SDL_IMPLEMENTATION
#define GAMELIB_SDL_IMPLEMENTATION
#endif
#endif
#define GAMELIB_SDL_VERSION_MAJOR 1
#define GAMELIB_SDL_VERSION_MINOR 9
#define GAMELIB_SDL_VERSION_PATCH 9
#include <stdint.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdarg.h>
#include <vector>
#include <string>
#include <map>
// SDL headers and extension library detection (moved here from
// GAMELIB_SDL_IMPLEMENTATION so that the GameLib class declaration
// below can use the real SDL types instead of fragile forward
// declarations that break across different SDL builds).
#if defined(__has_include)
#if __has_include(<SDL.h>)
#include <SDL.h>
#elif __has_include(<SDL2/SDL.h>)
#include <SDL2/SDL.h>
#else
#error SDL2 headers not found. Install SDL2 and add its include path.
#endif
#else
#include <SDL.h>
#endif
#if GAMELIB_SDL_DISABLE_IMAGE
#define GAMELIB_SDL_HAS_IMAGE 0
#elif defined(__has_include)
#if __has_include(<SDL_image.h>)
#include <SDL_image.h>
#define GAMELIB_SDL_HAS_IMAGE 1
#elif __has_include(<SDL2/SDL_image.h>)
#include <SDL2/SDL_image.h>
#define GAMELIB_SDL_HAS_IMAGE 1
#else
#define GAMELIB_SDL_HAS_IMAGE 0
#endif
#else
#define GAMELIB_SDL_HAS_IMAGE 0
#endif
#if GAMELIB_SDL_DISABLE_TTF
#define GAMELIB_SDL_HAS_TTF 0
#elif defined(__has_include)
#if __has_include(<SDL_ttf.h>)
#include <SDL_ttf.h>
#define GAMELIB_SDL_HAS_TTF 1
#elif __has_include(<SDL2/SDL_ttf.h>)
#include <SDL2/SDL_ttf.h>
#define GAMELIB_SDL_HAS_TTF 1
#else
#define GAMELIB_SDL_HAS_TTF 0
#endif
#else
#define GAMELIB_SDL_HAS_TTF 0
#endif
#if GAMELIB_SDL_DISABLE_MIXER
#define GAMELIB_SDL_HAS_MIXER 0
#elif defined(__has_include)
#if __has_include(<SDL_mixer.h>)
#include <SDL_mixer.h>
#define GAMELIB_SDL_HAS_MIXER 1
#elif __has_include(<SDL2/SDL_mixer.h>)
#include <SDL2/SDL_mixer.h>
#define GAMELIB_SDL_HAS_MIXER 1
#else
#define GAMELIB_SDL_HAS_MIXER 0
#endif
#else
#define GAMELIB_SDL_HAS_MIXER 0
#endif
#if GAMELIB_SDL_HAS_MIXER && (defined(__EMSCRIPTEN__) || defined(GAMELIB_SDL_MIXER_CHANNEL))
#define GAMELIB_SDL_USE_MIXER_CHANNELS 1
#else
#define GAMELIB_SDL_USE_MIXER_CHANNELS 0
#endif
// Forward declarations for extension types when their headers are not available.
// These match the official SDL struct tag naming so they won't conflict if the
// headers are later included from elsewhere.
#if !GAMELIB_SDL_HAS_TTF
typedef struct _TTF_Font TTF_Font;
#endif
#if !GAMELIB_SDL_HAS_MIXER
typedef struct Mix_Chunk Mix_Chunk;
typedef struct _Mix_Music Mix_Music;
#endif
// Color constants (ARGB format: 0xAARRGGBB)
#define COLOR_BLACK 0xFF000000
#define COLOR_WHITE 0xFFFFFFFF
#define COLOR_RED 0xFFFF0000
#define COLOR_GREEN 0xFF00FF00
#define COLOR_BLUE 0xFF0000FF
#define COLOR_YELLOW 0xFFFFFF00
#define COLOR_CYAN 0xFF00FFFF
#define COLOR_MAGENTA 0xFFFF00FF
#define COLOR_ORANGE 0xFFFF8800
#define COLOR_PINK 0xFFFF88CC
#define COLOR_PURPLE 0xFF8800FF
#define COLOR_GRAY 0xFF888888
#define COLOR_DARK_GRAY 0xFF444444
#define COLOR_LIGHT_GRAY 0xFFCCCCCC
#define COLOR_DARK_RED 0xFF880000
#define COLOR_DARK_GREEN 0xFF008800
#define COLOR_DARK_BLUE 0xFF000088
#define COLOR_SKY_BLUE 0xFF87CEEB
#define COLOR_BROWN 0xFF8B4513
#define COLOR_GOLD 0xFFFFD700
#define COLOR_TRANSPARENT 0x00000000
#define COLOR_RGB(r, g, b) ((uint32_t)(0xFF000000 | (((r) & 0xFF) << 16) | (((g) & 0xFF) << 8) | ((b) & 0xFF)))
#define COLOR_ARGB(a, r, g, b) ((uint32_t)((((a) & 0xFF) << 24) | (((r) & 0xFF) << 16) | (((g) & 0xFF) << 8) | ((b) & 0xFF)))
#define COLOR_GET_A(c) (((c) >> 24) & 0xFF)
#define COLOR_GET_R(c) (((c) >> 16) & 0xFF)
#define COLOR_GET_G(c) (((c) >> 8) & 0xFF)
#define COLOR_GET_B(c) ((c) & 0xFF)
// Keyboard constants (kept source-compatible with the Win32 GameLib values)
#define KEY_LEFT 0x25
#define KEY_RIGHT 0x27
#define KEY_UP 0x26
#define KEY_DOWN 0x28
#define KEY_SPACE 0x20
#define KEY_ENTER 0x0D
#define KEY_ESCAPE 0x1B
#define KEY_TAB 0x09
#define KEY_SHIFT 0x10
#define KEY_CONTROL 0x11
#define KEY_BACK 0x08
#define KEY_A 0x41
#define KEY_B 0x42
#define KEY_C 0x43
#define KEY_D 0x44
#define KEY_E 0x45
#define KEY_F 0x46
#define KEY_G 0x47
#define KEY_H 0x48
#define KEY_I 0x49
#define KEY_J 0x4A
#define KEY_K 0x4B
#define KEY_L 0x4C
#define KEY_M 0x4D
#define KEY_N 0x4E
#define KEY_O 0x4F
#define KEY_P 0x50
#define KEY_Q 0x51
#define KEY_R 0x52
#define KEY_S 0x53
#define KEY_T 0x54
#define KEY_U 0x55
#define KEY_V 0x56
#define KEY_W 0x57
#define KEY_X 0x58
#define KEY_Y 0x59
#define KEY_Z 0x5A
#define KEY_0 0x30
#define KEY_1 0x31
#define KEY_2 0x32
#define KEY_3 0x33
#define KEY_4 0x34
#define KEY_5 0x35
#define KEY_6 0x36
#define KEY_7 0x37
#define KEY_8 0x38
#define KEY_9 0x39
#define KEY_F1 0x70
#define KEY_F2 0x71
#define KEY_F3 0x72
#define KEY_F4 0x73
#define KEY_F5 0x74
#define KEY_F6 0x75
#define KEY_F7 0x76
#define KEY_F8 0x77
#define KEY_F9 0x78
#define KEY_F10 0x79
#define KEY_F11 0x7A
#define KEY_F12 0x7B
#define KEY_ADD 0x6E // Numpad + (VK_ADD equivalent)
#define KEY_SUBTRACT 0x6D // Numpad - (VK_SUBTRACT equivalent)
#define MOUSE_LEFT 0
#define MOUSE_RIGHT 1
#define MOUSE_MIDDLE 2
#define MESSAGEBOX_OK 0
#define MESSAGEBOX_YESNO 1
#define MESSAGEBOX_RESULT_OK 1
#define MESSAGEBOX_RESULT_YES 2
#define MESSAGEBOX_RESULT_NO 3
#define SPRITE_FLIP_H 1
#define SPRITE_FLIP_V 2
#define SPRITE_COLORKEY 4
#define SPRITE_ALPHA 8
#ifndef COLORKEY_DEFAULT
#define COLORKEY_DEFAULT 0xFFFF00FF
#endif
#ifndef GAMELIB_SDL_DEFAULT_FONT
#define GAMELIB_SDL_DEFAULT_FONT ""
#endif
// Optional SDL extension backends are auto-enabled when their headers are present.
// Define these macros to 1 before including GameLib.SDL.h if you want to force-disable
// a backend and avoid linking the corresponding extension library.
#ifndef GAMELIB_SDL_DISABLE_IMAGE
#define GAMELIB_SDL_DISABLE_IMAGE 0
#endif
#ifndef GAMELIB_SDL_DISABLE_TTF
#define GAMELIB_SDL_DISABLE_TTF 0
#endif
#ifndef GAMELIB_SDL_DISABLE_MIXER
#define GAMELIB_SDL_DISABLE_MIXER 0
#endif
// Windows DPI behavior: match GameLib.h by default.
// "unaware" lets Windows scale the whole window with the system scale factor,
// so a logical 800x600 window stays comfortably readable on high-DPI displays.
// Override this macro before including GameLib.SDL.h if you want SDL's DPI-aware modes.
#ifndef GAMELIB_SDL_WINDOWS_DPI_AWARENESS
#define GAMELIB_SDL_WINDOWS_DPI_AWARENESS "unaware"
#endif
class GameLib
{
public:
GameLib();
~GameLib();
int Open(int width, int height, const char *title, bool center = false, bool resizable = false);
bool IsClosed() const;
void Update();
void WaitFrame(int fps);
double GetDeltaTime() const;
double GetFPS() const;
double GetTime() const;
int GetWidth() const;
int GetHeight() const;
uint32_t *GetFramebuffer();
void WinResize(int width, int height);
void SetMaximized(bool maximized);
void SetTitle(const char *title);
void ShowFps(bool show);
void ShowMouse(bool show);
void AspectLock(bool lock, uint32_t color = COLOR_BLACK);
int ShowMessage(const char *text, const char *title = NULL, int buttons = MESSAGEBOX_OK);
void Clear(uint32_t color = COLOR_BLACK);
void SetPixel(int x, int y, uint32_t color);
uint32_t GetPixel(int x, int y) const;
void SetClip(int x, int y, int w, int h);
void ClearClip();
void GetClip(int *x, int *y, int *w, int *h) const;
int GetClipX() const;
int GetClipY() const;
int GetClipW() const;
int GetClipH() const;
void Screenshot(const char *filename);
void DrawLine(int x1, int y1, int x2, int y2, uint32_t color);
void DrawRect(int x, int y, int w, int h, uint32_t color);
void FillRect(int x, int y, int w, int h, uint32_t color);
void DrawCircle(int cx, int cy, int r, uint32_t color);
void FillCircle(int cx, int cy, int r, uint32_t color);
void DrawEllipse(int cx, int cy, int rx, int ry, uint32_t color);
void FillEllipse(int cx, int cy, int rx, int ry, uint32_t color);
void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, uint32_t color);
void FillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, uint32_t color);
void DrawText(int x, int y, const char *text, uint32_t color);
void DrawNumber(int x, int y, int number, uint32_t color);
void DrawTextScale(int x, int y, const char *text, uint32_t color, int w, int h);
void DrawPrintf(int x, int y, uint32_t color, const char *fmt, ...);
void DrawPrintfScale(int x, int y, uint32_t color, int w, int h, const char *fmt, ...);
void DrawTextFont(int x, int y, const char *text, uint32_t color, const char *fontName, int fontSize);
void DrawTextFont(int x, int y, const char *text, uint32_t color, int fontSize);
void DrawPrintfFont(int x, int y, uint32_t color, const char *fontName, int fontSize, const char *fmt, ...);
void DrawPrintfFont(int x, int y, uint32_t color, int fontSize, const char *fmt, ...);
int GetTextWidthFont(const char *text, const char *fontName, int fontSize);
int GetTextWidthFont(const char *text, int fontSize);
int GetTextHeightFont(const char *text, const char *fontName, int fontSize);
int GetTextHeightFont(const char *text, int fontSize);
int CreateSprite(int width, int height);
int LoadSpriteBMP(const char *filename);
int LoadSprite(const char *filename);
void FreeSprite(int id);
void DrawSprite(int id, int x, int y);
void DrawSpriteEx(int id, int x, int y, int flags);
void DrawSpriteRegion(int id, int x, int y, int sx, int sy, int sw, int sh);
void DrawSpriteRegionEx(int id, int x, int y, int sx, int sy, int sw, int sh, int flags = 0);
void DrawSpriteScaled(int id, int x, int y, int w, int h, int flags = 0);
void DrawSpriteRotated(int id, int cx, int cy, double angleDeg, int flags = 0);
void DrawSpriteFrame(int id, int x, int y, int frameW, int frameH, int frameIndex, int flags = 0);
void DrawSpriteFrameScaled(int id, int x, int y, int frameW, int frameH, int frameIndex, int w, int h, int flags = 0);
void DrawSpriteFrameRotated(int id, int cx, int cy, int frameW, int frameH, int frameIndex,
double angleDeg, int flags = 0);
void SetSpritePixel(int id, int x, int y, uint32_t color);
uint32_t GetSpritePixel(int id, int x, int y) const;
int GetSpriteWidth(int id) const;
int GetSpriteHeight(int id) const;
void SetSpriteColorKey(int id, uint32_t color);
uint32_t GetSpriteColorKey(int id) const;
bool IsKeyDown(int key) const;
bool IsKeyPressed(int key) const;
bool IsKeyReleased(int key) const;
int GetMouseX() const;
int GetMouseY() const;
bool IsMouseDown(int button) const;
bool IsMousePressed(int button) const;
bool IsMouseReleased(int button) const;
int GetMouseWheelDelta() const;
bool IsActive() const;
int PlayBeep(int frequency, int duration, int repeat = 1, int volume = 1000);
int PlayWAV(const char *filename, int repeat = 1, int volume = 1000);
int PlayPCM(const int16_t *pcm, int nchannels, int nsamples, int sample_rate, int repeat = 1, int volume = 1000);
int StopWAV(int channel);
int IsPlaying(int channel);
int SetVolume(int channel, int volume);
void StopAll();
int SetMasterVolume(int volume);
int GetMasterVolume() const;
bool PlayMusic(const char *filename, bool loop = true);
void StopMusic();
bool IsMusicPlaying() const;
int CreateTilemap(int cols, int rows, int tileSize, int tilesetId);
bool SaveTilemap(const char *filename, int mapId) const;
int LoadTilemap(const char *filename, int tilesetId);
void FreeTilemap(int mapId);
void SetTile(int mapId, int col, int row, int tileId);
int GetTile(int mapId, int col, int row) const;
int GetTilemapCols(int mapId) const;
int GetTilemapRows(int mapId) const;
int GetTileSize(int mapId) const;
int WorldToTileCol(int mapId, int x) const;
int WorldToTileRow(int mapId, int y) const;
int GetTileAtPixel(int mapId, int x, int y) const;
void FillTileRect(int mapId, int col, int row, int cols, int rows, int tileId);
void ClearTilemap(int mapId, int tileId = -1);
void DrawTilemap(int mapId, int x, int y, int flags = 0);
void DrawGrid(int x, int y, int rows, int cols, int cellSize, uint32_t color);
void FillCell(int gridX, int gridY, int row, int col, int cellSize, uint32_t color);
// -------- UI System --------
bool Button(int x, int y, int w, int h, const char *text, uint32_t color);
bool Checkbox(int x, int y, const char *text, bool *checked);
bool RadioBox(int x, int y, const char *text, int *value, int index);
bool ToggleButton(int x, int y, int w, int h, const char *text, bool *toggled, uint32_t color);
// -------- Scene Management --------
void SetScene(int scene);
int GetScene() const;
bool IsSceneChanged() const;
int GetPreviousScene() const;
// -------- Save / Load Data --------
static bool SaveInt(const char *filename, const char *key, int value);
static bool SaveFloat(const char *filename, const char *key, float value);
static bool SaveString(const char *filename, const char *key, const char *value);
static int LoadInt(const char *filename, const char *key, int defaultValue = 0);
static float LoadFloat(const char *filename, const char *key, float defaultValue = 0.0f);
static const char *LoadString(const char *filename, const char *key,
const char *defaultValue = "");
static bool HasSaveKey(const char *filename, const char *key);
static bool DeleteSaveKey(const char *filename, const char *key);
static bool DeleteSave(const char *filename);
static int Random(int minVal, int maxVal);
static bool RectOverlap(int x1, int y1, int w1, int h1,
int x2, int y2, int w2, int h2);
static bool CircleOverlap(int cx1, int cy1, int r1,
int cx2, int cy2, int r2);
static bool PointInRect(int px, int py, int x, int y, int w, int h);
static float Distance(int x1, int y1, int x2, int y2);
private:
GameLib(const GameLib &);
GameLib &operator=(const GameLib &);
void _DestroyWindowResources();
void _UpdateTitleFps();
void _UpdateWindowSize();
void _SetMouseFromWindowCoords(int x, int y);
void _SyncInputState();
void _SetPixelFast(int x, int y, uint32_t color);
void _DrawHLine(int x1, int x2, int y, uint32_t color);
bool _ClipRectToCurrentClip(int *x0, int *y0, int *x1, int *y1) const;
int _AllocSpriteSlot();
void _DrawSpriteAreaFast(int id, int x, int y, int sx, int sy, int sw, int sh, int flags);
void _DrawSpriteAreaScaled(int id, int x, int y, int sx, int sy, int sw, int sh,
int dw, int dh, int flags);
void _DrawSpriteAreaRotated(int id, int cx, int cy, int sx, int sy, int sw, int sh,
double angleDeg, int flags);
int _AllocTilemapSlot();
int _GetTilesetTileCount(int tilesetId, int tileSize) const;
bool _EnsureImageReady();
bool _EnsureTtfReady();
bool _EnsureMixerReady();
TTF_Font *_GetFont(const char *fontName, int fontSize);
void _BlendSurfaceToFramebuffer(int x, int y, SDL_Surface *surface);
private:
SDL_Window *_window;
SDL_Renderer *_renderer;
SDL_Texture *_frameTexture;
bool _closing;
bool _active;
bool _showFps;
bool _mouseVisible;
bool _focused;
bool _minimized;
bool _sdlReady;
bool _imageReady;
bool _ttfReady;
bool _mixerReady;
bool _audioReady;
std::string _title;
int _width;
int _height;
int _windowWidth;
int _windowHeight;
int _clipX;
int _clipY;
int _clipW;
int _clipH;
bool _resizable;
// aspect lock
bool _aspectLock;
uint32_t _aspectColor;
int _aspectOffsetX;
int _aspectOffsetY;
int _aspectContentW;
int _aspectContentH;
uint32_t *_framebuffer;
// text scale lookup tables (dynamic, max 1024)
int *_textSrcRowMap;
int *_textSrcColMap;
int _keys[512];
int _keys_prev[512];
int _mouseX;
int _mouseY;
int _mouseButtons[3];
int _mouseButtons_prev[3];
int _mouseWheelDelta;
uint32_t _uiActiveId;
uint64_t _timeStartCounter;
uint64_t _timePrevCounter;
uint64_t _fpsTimeCounter;
uint64_t _frameStartCounter;
uint64_t _perfFrequency;
double _deltaTime;
double _fps;
double _fpsAccum;
struct GameSprite {
int width;
int height;
uint32_t *pixels;
uint32_t colorKey;
bool used;
};
std::vector<GameSprite> _sprites;
struct GameTilemap {
int cols;
int rows;
int tileSize;
int tilesetId;
int tilesetCols;
int *tiles;
bool used;
};
std::vector<GameTilemap> _tilemaps;
struct GameFontCacheEntry {
std::string key;
TTF_Font *font;
};
std::vector<GameFontCacheEntry> _fontCache;
// Multi-channel audio mixer state
struct _WavData {
uint8_t *buffer;
uint32_t size;
uint32_t sample_rate;
uint16_t channels;
uint16_t bits_per_sample;
int ref_count;
bool temporary;
_WavData() : buffer(NULL), size(0), sample_rate(0), channels(0),
bits_per_sample(0), ref_count(0), temporary(false) {}
~_WavData() { if (buffer) delete[] buffer; }
};
struct _Channel {
int id;
_WavData *wav;
int volume;
int repeat;
uint32_t position;
bool is_playing;
};
std::map<std::string, _WavData*> _wav_cache;
std::map<int, _Channel*> _audio_channels;
int64_t _next_channel_id;
bool _audio_initialized;
int _master_volume;
SDL_AudioDeviceID _audioDevice;
SDL_AudioSpec _audioSpec;
bool _audioSelfInit;
static const int _MAX_CHANNELS = 32;
static const int _AUDIO_BUFFER_FRAMES = 2048;
static const int _AUDIO_OUTPUT_CHANNELS = 2;
static const int _AUDIO_BUFFER_TOTAL = _AUDIO_BUFFER_FRAMES * _AUDIO_OUTPUT_CHANNELS;
static const int _AUDIO_BUFFER_BYTES = _AUDIO_BUFFER_TOTAL * sizeof(int16_t);
int32_t _mix_buffer[_AUDIO_BUFFER_TOTAL];
bool _InitAudioBackend();
void _ShutdownAudioBackend();
static void _SDLAudioCallback(void *userdata, Uint8 *stream, int len);
void _MixAudio(int16_t *output, int sample_count);
void _ClampAndConvert(int32_t *input, int16_t *output, int count);
_WavData *_LoadWAVFromFile(const char *filename);
_WavData *_ConvertToTargetFormat(_WavData *src);
_WavData *_LoadOrCacheWAV(const char *filename);
int _AllocateChannel();
void _ReleaseChannel(int channel_id);
Mix_Music *_currentMusic;
bool _musicPlaying;
int _mixerInitFlags;
#if GAMELIB_SDL_USE_MIXER_CHANNELS
std::map<std::string, Mix_Chunk*> _chunk_cache;
std::map<int, Mix_Chunk*> _temp_chunks;
static void _MixerChannelFinishedCallback(int channel);
#endif
// scene state
int _scene;
int _pendingScene;
bool _hasPendingScene;
bool _sceneChanged;
int _previousScene;
static bool _srandDone;
};
// Classic 8x8 bitmap font, 8 bytes per char, one byte per row, MSB on left
static const unsigned char _gamelib_font8x8[95][8] = {
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0x18,0x3C,0x3C,0x18,0x18,0x00,0x18,0x00 },
{ 0x6C,0x6C,0x24,0x00,0x00,0x00,0x00,0x00 },
{ 0x6C,0x6C,0xFE,0x6C,0xFE,0x6C,0x6C,0x00 },
{ 0x18,0x7E,0xC0,0x7C,0x06,0xFC,0x18,0x00 },
{ 0x00,0xC6,0xCC,0x18,0x30,0x66,0xC6,0x00 },
{ 0x38,0x6C,0x38,0x76,0xDC,0xCC,0x76,0x00 },
{ 0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00 },
{ 0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00 },
{ 0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00 },
{ 0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00 },
{ 0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00 },
{ 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30 },
{ 0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00 },
{ 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00 },
{ 0x06,0x0C,0x18,0x30,0x60,0xC0,0x80,0x00 },
{ 0x7C,0xCE,0xDE,0xF6,0xE6,0xC6,0x7C,0x00 },
{ 0x18,0x38,0x78,0x18,0x18,0x18,0x7E,0x00 },
{ 0x7C,0xC6,0x06,0x1C,0x70,0xC6,0xFE,0x00 },
{ 0x7C,0xC6,0x06,0x3C,0x06,0xC6,0x7C,0x00 },
{ 0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x1E,0x00 },
{ 0xFE,0xC0,0xFC,0x06,0x06,0xC6,0x7C,0x00 },
{ 0x38,0x60,0xC0,0xFC,0xC6,0xC6,0x7C,0x00 },
{ 0xFE,0xC6,0x0C,0x18,0x30,0x30,0x30,0x00 },
{ 0x7C,0xC6,0xC6,0x7C,0xC6,0xC6,0x7C,0x00 },
{ 0x7C,0xC6,0xC6,0x7E,0x06,0x0C,0x78,0x00 },
{ 0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x00 },
{ 0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x30 },
{ 0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x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C6,0x00 },
{ 0x00,0x00,0xFC,0xC6,0xC6,0xC6,0xC6,0x00 },
{ 0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0x00 },
{ 0x00,0x00,0xFC,0xC6,0xC6,0xFC,0xC0,0xC0 },
{ 0x00,0x00,0x7E,0xC6,0xC6,0x7E,0x06,0x06 },
{ 0x00,0x00,0xDC,0xE6,0xC0,0xC0,0xC0,0x00 },
{ 0x00,0x00,0x7E,0xC0,0x7C,0x06,0xFC,0x00 },
{ 0x30,0x30,0x7C,0x30,0x30,0x36,0x1C,0x00 },
{ 0x00,0x00,0xC6,0xC6,0xC6,0xC6,0x7E,0x00 },
{ 0x00,0x00,0xC6,0xC6,0xC6,0x6C,0x38,0x00 },
{ 0x00,0x00,0xC6,0xC6,0xD6,0xFE,0x6C,0x00 },
{ 0x00,0x00,0xC6,0x6C,0x38,0x6C,0xC6,0x00 },
{ 0x00,0x00,0xC6,0xC6,0xC6,0x7E,0x06,0x7C },
{ 0x00,0x00,0xFE,0x0C,0x38,0x60,0xFE,0x00 },
{ 0x0E,0x18,0x18,0x70,0x18,0x18,0x0E,0x00 },
{ 0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x00 },
{ 0x70,0x18,0x18,0x0E,0x18,0x18,0x70,0x00 },
{ 0x76,0xDC,0x00,0x00,0x00,0x00,0x00,0x00 }
};
#ifdef GAMELIB_SDL_IMPLEMENTATION
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#endif
bool GameLib::_srandDone = false;
static int _gamelib_floor_div(int value, int divisor)
{
if (divisor <= 0) return 0;
int q = value / divisor;
int r = value % divisor;
if (r != 0 && ((r > 0) != (divisor > 0))) q--;
return q;
}
static int _gamelib_round_to_int(double value)
{
if (value >= 0.0) return (int)(value + 0.5);
return (int)(value - 0.5);
}
static uint32_t _gamelib_alpha_blend(uint32_t dst, uint32_t src)
{
uint32_t sa = COLOR_GET_A(src);
if (sa == 0) return dst;
if (sa == 255) return src;
uint32_t ia = 255 - sa;
uint32_t or_ = (sa * COLOR_GET_R(src) + ia * COLOR_GET_R(dst)) / 255;
uint32_t og = (sa * COLOR_GET_G(src) + ia * COLOR_GET_G(dst)) / 255;
uint32_t ob = (sa * COLOR_GET_B(src) + ia * COLOR_GET_B(dst)) / 255;
return COLOR_ARGB(255, or_, og, ob);
}
static void _gamelib_blend_pixel(uint32_t *dst, uint32_t src)
{
if (!dst) return;
uint32_t sa = COLOR_GET_A(src);
if (sa == 0) return;
if (sa == 255) {
*dst = src;
return;
}
*dst = _gamelib_alpha_blend(*dst, src);
}
static uint32_t _gamelib_ui_lighten(uint32_t color, int amount);
static uint32_t _gamelib_ui_darken(uint32_t color, int amount);
static int _gamelib_ui_text_width(const char *text);
static int _gamelib_ui_text_height(const char *text);
static uint32_t _gamelib_ui_button_text_color(uint32_t color);
static uint32_t _gamelib_ui_make_id(uint32_t kind, int x, int y, int w, int h, const char *text);
static void _gamelib_ui_draw_bevel_rect(GameLib *game, int x, int y, int w, int h,
uint32_t face, bool pressed);
static void _gamelib_ui_draw_text_with_shadow(GameLib *game, int x, int y, const char *text,
uint32_t color, uint32_t shadow);
static void _gamelib_ui_draw_checkbox_mark(GameLib *game, int x, int y, int size,
bool pressed, uint32_t color);
static void _gamelib_plot_circle_points_unique(GameLib *game,
int cx, int cy, int x, int y,
uint32_t color)
{
if (!game) return;
game->SetPixel(cx + x, cy + y, color);
if (x != 0) game->SetPixel(cx - x, cy + y, color);
if (y != 0) {
game->SetPixel(cx + x, cy - y, color);
if (x != 0) game->SetPixel(cx - x, cy - y, color);
}
if (x == y) return;
game->SetPixel(cx + y, cy + x, color);
game->SetPixel(cx - y, cy + x, color);
if (x != 0) {
game->SetPixel(cx + y, cy - x, color);
game->SetPixel(cx - y, cy - x, color);
}
}
static void _gamelib_plot_ellipse_points_unique(GameLib *game,
int cx, int cy, int x, int y,
uint32_t color)
{
if (!game) return;
game->SetPixel(cx + x, cy + y, color);
if (x != 0) game->SetPixel(cx - x, cy + y, color);
if (y != 0) {
game->SetPixel(cx + x, cy - y, color);
if (x != 0) game->SetPixel(cx - x, cy - y, color);
}
}
static bool _gamelib_rw_read_text_line(SDL_RWops *rw, std::string &line)
{
line.clear();
if (!rw) return false;
char ch = 0;
while (SDL_RWread(rw, &ch, 1, 1) == 1) {
if (ch == '\n') break;
line.push_back(ch);
}
if (line.empty() && ch != '\n') return false;
if (!line.empty() && line[line.size() - 1] == '\r') line.resize(line.size() - 1);
return true;
}
static void _gamelib_strip_utf8_bom(std::string &line)
{
if (line.size() >= 3 &&
(unsigned char)line[0] == 0xEF &&
(unsigned char)line[1] == 0xBB &&
(unsigned char)line[2] == 0xBF) {
line.erase(0, 3);
}
}
static bool _gamelib_parse_int_tokens(const std::string &line, int *values, int maxCount, int *outCount)
{
if (outCount) *outCount = 0;
if (maxCount < 0) return false;
const char *cursor = line.c_str();
int count = 0;
while (*cursor) {
while (*cursor == ' ' || *cursor == '\t') cursor++;
if (!*cursor) break;
if (count >= maxCount) break;
char *endPtr = NULL;
long value = strtol(cursor, &endPtr, 10);
if (endPtr == cursor) return false;
if (value < (long)INT_MIN || value > (long)INT_MAX) return false;
if (*endPtr && *endPtr != ' ' && *endPtr != '\t') return false;
values[count++] = (int)value;
cursor = endPtr;
}
if (outCount) *outCount = count;
return true;
}
static bool _gamelib_rw_write_text(SDL_RWops *rw, const char *text, size_t len)
{
if (!rw) return false;
if (!text) return len == 0;
if (len == 0) return true;
return SDL_RWwrite(rw, text, 1, len) == len;
}
static int _gamelib_sdl_map_scancode(SDL_Scancode scancode)
{
switch (scancode) {
case SDL_SCANCODE_LEFT: return KEY_LEFT;
case SDL_SCANCODE_RIGHT: return KEY_RIGHT;
case SDL_SCANCODE_UP: return KEY_UP;
case SDL_SCANCODE_DOWN: return KEY_DOWN;
case SDL_SCANCODE_SPACE: return KEY_SPACE;
case SDL_SCANCODE_RETURN:
case SDL_SCANCODE_KP_ENTER: return KEY_ENTER;
case SDL_SCANCODE_ESCAPE: return KEY_ESCAPE;
case SDL_SCANCODE_TAB: return KEY_TAB;
case SDL_SCANCODE_LSHIFT:
case SDL_SCANCODE_RSHIFT: return KEY_SHIFT;
case SDL_SCANCODE_LCTRL:
case SDL_SCANCODE_RCTRL: return KEY_CONTROL;
case SDL_SCANCODE_BACKSPACE: return KEY_BACK;
case SDL_SCANCODE_A: return KEY_A;
case SDL_SCANCODE_B: return KEY_B;
case SDL_SCANCODE_C: return KEY_C;
case SDL_SCANCODE_D: return KEY_D;
case SDL_SCANCODE_E: return KEY_E;
case SDL_SCANCODE_F: return KEY_F;
case SDL_SCANCODE_G: return KEY_G;
case SDL_SCANCODE_H: return KEY_H;
case SDL_SCANCODE_I: return KEY_I;
case SDL_SCANCODE_J: return KEY_J;
case SDL_SCANCODE_K: return KEY_K;
case SDL_SCANCODE_L: return KEY_L;
case SDL_SCANCODE_M: return KEY_M;
case SDL_SCANCODE_N: return KEY_N;
case SDL_SCANCODE_O: return KEY_O;
case SDL_SCANCODE_P: return KEY_P;
case SDL_SCANCODE_Q: return KEY_Q;
case SDL_SCANCODE_R: return KEY_R;
case SDL_SCANCODE_S: return KEY_S;
case SDL_SCANCODE_T: return KEY_T;
case SDL_SCANCODE_U: return KEY_U;
case SDL_SCANCODE_V: return KEY_V;
case SDL_SCANCODE_W: return KEY_W;
case SDL_SCANCODE_X: return KEY_X;
case SDL_SCANCODE_Y: return KEY_Y;
case SDL_SCANCODE_Z: return KEY_Z;
case SDL_SCANCODE_0: return KEY_0;
case SDL_SCANCODE_1: return KEY_1;
case SDL_SCANCODE_2: return KEY_2;
case SDL_SCANCODE_3: return KEY_3;
case SDL_SCANCODE_4: return KEY_4;
case SDL_SCANCODE_5: return KEY_5;
case SDL_SCANCODE_6: return KEY_6;
case SDL_SCANCODE_7: return KEY_7;
case SDL_SCANCODE_8: return KEY_8;
case SDL_SCANCODE_9: return KEY_9;
case SDL_SCANCODE_F1: return KEY_F1;
case SDL_SCANCODE_F2: return KEY_F2;
case SDL_SCANCODE_F3: return KEY_F3;
case SDL_SCANCODE_F4: return KEY_F4;
case SDL_SCANCODE_F5: return KEY_F5;
case SDL_SCANCODE_F6: return KEY_F6;
case SDL_SCANCODE_F7: return KEY_F7;
case SDL_SCANCODE_F8: return KEY_F8;
case SDL_SCANCODE_F9: return KEY_F9;
case SDL_SCANCODE_F10: return KEY_F10;
case SDL_SCANCODE_F11: return KEY_F11;
case SDL_SCANCODE_F12: return KEY_F12;
case SDL_SCANCODE_KP_PLUS: return KEY_ADD;
case SDL_SCANCODE_KP_MINUS: return KEY_SUBTRACT;
default: return -1;
}
}
GameLib::GameLib()
{
_window = NULL;
_renderer = NULL;
_frameTexture = NULL;
_closing = false;
_active = true;
_showFps = false;
_mouseVisible = true;
_focused = true;
_minimized = false;
_sdlReady = false;
_imageReady = false;
_ttfReady = false;
_mixerReady = false;
_audioReady = false;
_title = "";
_width = 0;
_height = 0;
_windowWidth = 0;
_windowHeight = 0;
_clipX = 0;
_clipY = 0;
_clipW = 0;
_clipH = 0;
_resizable = false;
_aspectLock = false;
_aspectColor = COLOR_BLACK;
_aspectOffsetX = 0;
_aspectOffsetY = 0;
_aspectContentW = 0;
_aspectContentH = 0;
_framebuffer = NULL;
_textSrcRowMap = NULL;
_textSrcColMap = NULL;
memset(_keys, 0, sizeof(_keys));
memset(_keys_prev, 0, sizeof(_keys_prev));
_mouseX = 0;