-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathGameLib.h
More file actions
5665 lines (4896 loc) · 187 KB
/
Copy pathGameLib.h
File metadata and controls
5665 lines (4896 loc) · 187 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.h - A single-header game library for beginners
//
// Homepage: https://github.com/skywind3000/GameLib
// Copyright (c) 2026 skywind3000 (Lin Wei)
//
// Based on Win32 GDI, no SDL or other third-party libraries needed.
// Works with Dev C++ (GCC 4.9.2+), can make shooting games, Tetris,
// maze games, etc.
//
// How to use (single file project, most common):
//
// #include "GameLib.h"
//
// int main() {
// GameLib game;
// game.Open(640, 480, "My Game", true);
//
// int x = 320, y = 240;
//
// while (!game.IsClosed()) {
// if (game.IsKeyDown(KEY_UP)) y -= 3;
// if (game.IsKeyDown(KEY_DOWN)) y += 3;
// if (game.IsKeyDown(KEY_LEFT)) x -= 3;
// if (game.IsKeyDown(KEY_RIGHT)) x += 3;
//
// game.Clear(COLOR_BLACK);
// game.FillRect(x - 10, y - 10, 20, 20, COLOR_RED);
// game.DrawText(10, 10, "Move the box!", COLOR_WHITE);
// game.Update();
//
// game.WaitFrame(60);
// }
// return 0;
// }
//
// Multi-file project: add this line before #include in the main .cpp file
// #define GAMELIB_IMPLEMENTATION
// #include "GameLib.h"
// In other .cpp files, add this line
// #define GAMELIB_NO_IMPLEMENTATION
// #include "GameLib.h"
//
// Compile command (MinGW / Dev C++):
// g++ -o game.exe main.cpp -mwindows
//
// Last Modified: 2026/04/21
//
//=====================================================================
#ifndef GAMELIB_H
#define GAMELIB_H
// Default behavior: include enables implementation (good for single file projects)
#ifndef GAMELIB_NO_IMPLEMENTATION
#ifndef GAMELIB_IMPLEMENTATION
#define GAMELIB_IMPLEMENTATION
#endif
#endif
// Version Info
#define GAMELIB_VERSION_MAJOR 1
#define GAMELIB_VERSION_MINOR 9
#define GAMELIB_VERSION_PATCH 9
//---------------------------------------------------------------------
// System header files
//---------------------------------------------------------------------
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <mmsystem.h>
#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>
//---------------------------------------------------------------------
// Link library: only needs -mwindows, gdi32 and winmm are loaded
// via LoadLibrary
//---------------------------------------------------------------------
#ifdef _MSC_VER
#pragma comment(lib, "user32.lib")
#endif
//---------------------------------------------------------------------
// Dynamically loaded function pointer types
//---------------------------------------------------------------------
// gdi32.dll
typedef int (WINAPI *PFN_SetDIBitsToDevice)(
HDC, int, int, DWORD, DWORD, int, int, UINT, UINT,
const void*, const BITMAPINFO*, UINT);
typedef HGDIOBJ (WINAPI *PFN_GetStockObject)(int);
typedef HDC (WINAPI *PFN_CreateCompatibleDC)(HDC);
typedef BOOL (WINAPI *PFN_DeleteDC)(HDC);
typedef HBITMAP (WINAPI *PFN_CreateDIBSection)(HDC, const BITMAPINFO*, UINT, void**, HANDLE, DWORD);
typedef HGDIOBJ (WINAPI *PFN_SelectObject)(HDC, HGDIOBJ);
typedef BOOL (WINAPI *PFN_DeleteObject)(HGDIOBJ);
typedef BOOL (WINAPI *PFN_BitBlt)(HDC, int, int, int, int, HDC, int, int, DWORD);
typedef BOOL (WINAPI *PFN_StretchBlt)(HDC, int, int, int, int, HDC, int, int, int, int, DWORD);
typedef int (WINAPI *PFN_SetStretchBltMode)(HDC, int);
typedef HFONT (WINAPI *PFN_CreateFontW)(int, int, int, int, int, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, const WCHAR*);
typedef BOOL (WINAPI *PFN_TextOutW)(HDC, int, int, LPCWSTR, int);
typedef COLORREF (WINAPI *PFN_SetTextColor)(HDC, COLORREF);
typedef int (WINAPI *PFN_SetBkMode)(HDC, int);
typedef BOOL (WINAPI *PFN_GetTextExtentPoint32W)(HDC, LPCWSTR, int, SIZE*);
typedef BOOL (WINAPI *PFN_GdiFlush)(void);
// winmm.dll (timer + MCI)
typedef DWORD (WINAPI *PFN_timeBeginPeriod)(UINT);
typedef DWORD (WINAPI *PFN_timeEndPeriod)(UINT);
typedef UINT (WINAPI *PFN_timeSetEvent)(UINT, UINT, DWORD_PTR, DWORD_PTR, UINT);
typedef UINT (WINAPI *PFN_timeKillEvent)(UINT);
typedef MCIERROR (WINAPI *PFN_mciSendStringW)(LPCWSTR, LPWSTR, UINT, HWND);
// winmm.dll (waveOut)
typedef MMRESULT (WINAPI *PFN_waveOutOpen)(LPHWAVEOUT, UINT, LPCWAVEFORMATEX, DWORD_PTR, DWORD_PTR, DWORD);
typedef MMRESULT (WINAPI *PFN_waveOutClose)(HWAVEOUT);
typedef MMRESULT (WINAPI *PFN_waveOutPrepareHeader)(HWAVEOUT, LPWAVEHDR, UINT);
typedef MMRESULT (WINAPI *PFN_waveOutUnprepareHeader)(HWAVEOUT, LPWAVEHDR, UINT);
typedef MMRESULT (WINAPI *PFN_waveOutWrite)(HWAVEOUT, LPWAVEHDR, UINT);
typedef MMRESULT (WINAPI *PFN_waveOutReset)(HWAVEOUT);
// gdiplus.dll
typedef int (WINAPI *PFN_GdiplusStartup)(ULONG_PTR*, void*, void*);
typedef void (WINAPI *PFN_GdiplusShutdown)(ULONG_PTR);
typedef int (WINAPI *PFN_GdipCreateBitmapFromStream)(void*, void**);
typedef int (WINAPI *PFN_GdipGetImageWidth)(void*, UINT*);
typedef int (WINAPI *PFN_GdipGetImageHeight)(void*, UINT*);
typedef int (WINAPI *PFN_GdipBitmapLockBits)(void*, const int*, UINT, int, void*);
typedef int (WINAPI *PFN_GdipBitmapUnlockBits)(void*, void*);
typedef int (WINAPI *PFN_GdipDisposeImage)(void*);
// ole32.dll
typedef HRESULT (WINAPI *PFN_CreateStreamOnHGlobal)(HGLOBAL, BOOL, void**);
//=====================================================================
// Part 1: Constants
//=====================================================================
//---------------------------------------------------------------------
// 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
// Color helper macros
#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)))
// Color component extraction
#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 (using Windows Virtual Key Code)
//---------------------------------------------------------------------
#define KEY_LEFT VK_LEFT
#define KEY_RIGHT VK_RIGHT
#define KEY_UP VK_UP
#define KEY_DOWN VK_DOWN
#define KEY_SPACE VK_SPACE
#define KEY_ENTER VK_RETURN
#define KEY_ESCAPE VK_ESCAPE
#define KEY_TAB VK_TAB
#define KEY_SHIFT VK_SHIFT
#define KEY_CONTROL VK_CONTROL
#define KEY_BACK VK_BACK
#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 VK_F1
#define KEY_F2 VK_F2
#define KEY_F3 VK_F3
#define KEY_F4 VK_F4
#define KEY_F5 VK_F5
#define KEY_F6 VK_F6
#define KEY_F7 VK_F7
#define KEY_F8 VK_F8
#define KEY_F9 VK_F9
#define KEY_F10 VK_F10
#define KEY_F11 VK_F11
#define KEY_F12 VK_F12
#define KEY_ADD VK_ADD
#define KEY_SUBTRACT VK_SUBTRACT
//---------------------------------------------------------------------
// Mouse button constants
//---------------------------------------------------------------------
#define MOUSE_LEFT 0
#define MOUSE_RIGHT 1
#define MOUSE_MIDDLE 2
// Message box button and result constants
#define MESSAGEBOX_OK 0
#define MESSAGEBOX_YESNO 1
#define MESSAGEBOX_RESULT_OK 1
#define MESSAGEBOX_RESULT_YES 2
#define MESSAGEBOX_RESULT_NO 3
//---------------------------------------------------------------------
// Sprite drawing flags
//---------------------------------------------------------------------
#define SPRITE_FLIP_H 1 // flip horizontal
#define SPRITE_FLIP_V 2 // flip vertical
#define SPRITE_COLORKEY 4 // enable transparent color
#define SPRITE_ALPHA 8 // enable alpha blending
// Default Color Key: magenta (255, 0, 255), common transparent color in 2D games
#ifndef COLORKEY_DEFAULT
#define COLORKEY_DEFAULT 0xFFFF00FF
#endif
#ifndef GAMELIB_DEFAULT_FONT_NAME
#define GAMELIB_DEFAULT_FONT_NAME "Microsoft YaHei"
#endif
//=====================================================================
// Part 2: Class Declaration
//=====================================================================
//---------------------------------------------------------------------
// GameLib Main Class
//---------------------------------------------------------------------
class GameLib
{
public:
GameLib();
~GameLib();
// -------- Window and Main Loop --------
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);
// -------- Frame Buffer --------
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);
// -------- Drawing --------
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);
// -------- Text Rendering (built-in 8x8 font) --------
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, ...);
// -------- Font Text Rendering (scalable fonts, Unicode support) --------
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);
// -------- Sprite System (managed by integer ID) --------
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;
// -------- Tilemap System --------
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);
// -------- Grid Helpers --------
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);
// -------- Input --------
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;
// -------- Sound --------
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;
// -------- Scene Management --------
void SetScene(int scene);
int GetScene() const;
bool IsSceneChanged() const;
int GetPreviousScene() const;
// -------- UI Helpers (built-in 8x8 font) --------
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);
// -------- 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);
// -------- Helper Functions --------
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:
// disable copy
GameLib(const GameLib &);
GameLib &operator=(const GameLib &);
// internal window management
static LRESULT CALLBACK _WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
static int _InitWindowClass();
void _DispatchMessages();
void _InitDIBInfo(void *ptr, int width, int height);
void _DestroyGraphicsResources();
void _DestroyTimingResources();
void _PresentFrame(HDC hdc);
void _UpdateClientSize();
void _SetMouseFromWindowCoords(int x, int y);
void _UpdateTitleFps();
// internal pixel drawing (no bounds check, for fast drawing after clipping)
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;
// internal sprite management
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);
// internal tilemap management
int _AllocTilemapSlot();
int _GetTilesetTileCount(int tilesetId, int tileSize) const;
private:
// window state
HWND _hwnd;
bool _closing;
bool _active;
bool _showFps;
bool _mouseVisible;
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;
// frame buffer
uint32_t *_framebuffer;
uint32_t *_presentBuffer;
int *_presentMapX;
int *_presentMapY;
int _presentWidth;
int _presentHeight;
// text scale lookup tables (dynamic, max 1024)
int *_textSrcRowMap;
int *_textSrcColMap;
// DIB Section (for scalable font text rendering on current backend)
HDC _memDC;
HBITMAP _dibSection;
HBITMAP _oldBmp;
// DIB info (for SetDIBitsToDevice, kept for compatibility)
unsigned char _bmi_data[sizeof(BITMAPINFO) + 16 * sizeof(RGBQUAD)];
unsigned char _present_bmi_data[sizeof(BITMAPINFO) + 16 * sizeof(RGBQUAD)];
// input state
int _keys[512];
int _keys_prev[512];
int _mouseX;
int _mouseY;
int _mouseButtons[3];
int _mouseButtons_prev[3];
int _mouseWheelDelta;
uint32_t _uiActiveId;
// timing
uint64_t _timeStartCounter;
uint64_t _timePrevCounter;
uint64_t _fpsTimeCounter;
uint64_t _frameStartCounter;
uint64_t _perfFrequency;
double _deltaTime;
double _fps;
double _fpsAccum;
HANDLE _timerEvent; // event signaled by multimedia timer
UINT _timerId; // multimedia timer ID (from timeSetEvent)
bool _timerPeriodActive;
// sprite storage
struct GameSprite { int width, height; uint32_t *pixels; uint32_t colorKey; bool used; };
std::vector<GameSprite> _sprites;
// tilemap storage
struct GameTilemap {
int cols, rows; // map grid size
int tileSize; // tile size in pixels
int tilesetId; // tileset sprite ID
int tilesetCols; // tiles per row in tileset
int *tiles; // tile ID array (cols * rows, -1 = empty)
bool used; // is this slot in use
};
std::vector<GameTilemap> _tilemaps;
// music state (MCI)
bool _musicPlaying;
bool _musicLoop;
bool _musicIsMidi;
std::wstring _musicAlias;
// audio mixer state (waveOut software mixer)
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;
uint32_t position;
int repeat;
int volume;
bool is_playing;
_Channel() : id(0), wav(NULL), position(0), repeat(1),
volume(1000), is_playing(false) {}
};
std::map<std::string, _WavData*> _wav_cache;
std::map<int, _Channel*> _audio_channels;
int64_t _next_channel_id;
bool _audio_initialized;
int _master_volume;
HWAVEOUT _hWaveOut;
WAVEHDR *_wave_hdr[2];
CRITICAL_SECTION _audio_lock;
volatile bool _audio_closing;
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];
// internal audio methods
_WavData *_LoadWAVFromFile(const char *filename);
_WavData *_ConvertToTargetFormat(_WavData *src);
_WavData *_LoadOrCacheWAV(const char *filename);
int _AllocateChannel();
void _ReleaseChannel(int channel_id);
void _MixAudio(int16_t *output_buffer, int sample_count);
void _ClampAndConvert(int32_t *input, int16_t *output, int count);
bool _InitAudioBackend();
void _ShutdownAudioBackend();
static void CALLBACK _WaveOutCallback(HWAVEOUT hwo, UINT uMsg,
DWORD_PTR dwInstance,
DWORD_PTR dwParam1, DWORD_PTR dwParam2);
// scene state
int _scene;
int _pendingScene;
bool _hasPendingScene;
bool _sceneChanged;
int _previousScene;
// random seed initialized flag
static bool _srandDone;
};
//=====================================================================
// Part 3: 8x8 Font Data (ASCII 32-126)
//=====================================================================
// 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 }, // 32 ' '
{ 0x18,0x3C,0x3C,0x18,0x18,0x00,0x18,0x00 }, // 33 '!'
{ 0x6C,0x6C,0x24,0x00,0x00,0x00,0x00,0x00 }, // 34 '"'
{ 0x6C,0x6C,0xFE,0x6C,0xFE,0x6C,0x6C,0x00 }, // 35 '#'
{ 0x18,0x7E,0xC0,0x7C,0x06,0xFC,0x18,0x00 }, // 36 '$'
{ 0x00,0xC6,0xCC,0x18,0x30,0x66,0xC6,0x00 }, // 37 '%'
{ 0x38,0x6C,0x38,0x76,0xDC,0xCC,0x76,0x00 }, // 38 '&'
{ 0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00 }, // 39 '''
{ 0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00 }, // 40 '('
{ 0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00 }, // 41 ')'
{ 0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00 }, // 42 '*'
{ 0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00 }, // 43 '+'
{ 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30 }, // 44 ','
{ 0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00 }, // 45 '-'
{ 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00 }, // 46 '.'
{ 0x06,0x0C,0x18,0x30,0x60,0xC0,0x80,0x00 }, // 47 '/'
{ 0x7C,0xCE,0xDE,0xF6,0xE6,0xC6,0x7C,0x00 }, // 48 '0'
{ 0x18,0x38,0x78,0x18,0x18,0x18,0x7E,0x00 }, // 49 '1'
{ 0x7C,0xC6,0x06,0x1C,0x70,0xC6,0xFE,0x00 }, // 50 '2'
{ 0x7C,0xC6,0x06,0x3C,0x06,0xC6,0x7C,0x00 }, // 51 '3'
{ 0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x1E,0x00 }, // 52 '4'
{ 0xFE,0xC0,0xFC,0x06,0x06,0xC6,0x7C,0x00 }, // 53 '5'
{ 0x38,0x60,0xC0,0xFC,0xC6,0xC6,0x7C,0x00 }, // 54 '6'
{ 0xFE,0xC6,0x0C,0x18,0x30,0x30,0x30,0x00 }, // 55 '7'
{ 0x7C,0xC6,0xC6,0x7C,0xC6,0xC6,0x7C,0x00 }, // 56 '8'
{ 0x7C,0xC6,0xC6,0x7E,0x06,0x0C,0x78,0x00 }, // 57 '9'
{ 0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x00 }, // 58 ':'
{ 0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x30 }, // 59 ';'
{ 0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x00 }, // 60 '<'
{ 0x00,0x00,0x7E,0x00,0x7E,0x00,0x00,0x00 }, // 61 '='
{ 0x60,0x30,0x18,0x0C,0x18,0x30,0x60,0x00 }, // 62 '>'
{ 0x7C,0xC6,0x0C,0x18,0x18,0x00,0x18,0x00 }, // 63 '?'
{ 0x7C,0xC6,0xDE,0xDE,0xDC,0xC0,0x7C,0x00 }, // 64 '@'
{ 0x38,0x6C,0xC6,0xC6,0xFE,0xC6,0xC6,0x00 }, // 65 'A'
{ 0xFC,0xC6,0xC6,0xFC,0xC6,0xC6,0xFC,0x00 }, // 66 'B'
{ 0x7C,0xC6,0xC0,0xC0,0xC0,0xC6,0x7C,0x00 }, // 67 'C'
{ 0xF8,0xCC,0xC6,0xC6,0xC6,0xCC,0xF8,0x00 }, // 68 'D'
{ 0xFE,0xC0,0xC0,0xFC,0xC0,0xC0,0xFE,0x00 }, // 69 'E'
{ 0xFE,0xC0,0xC0,0xFC,0xC0,0xC0,0xC0,0x00 }, // 70 'F'
{ 0x7C,0xC6,0xC0,0xCE,0xC6,0xC6,0x7E,0x00 }, // 71 'G'
{ 0xC6,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0x00 }, // 72 'H'
{ 0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00 }, // 73 'I'
{ 0x1E,0x0C,0x0C,0x0C,0xCC,0xCC,0x78,0x00 }, // 74 'J'
{ 0xC6,0xCC,0xD8,0xF0,0xD8,0xCC,0xC6,0x00 }, // 75 'K'
{ 0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xFE,0x00 }, // 76 'L'
{ 0xC6,0xEE,0xFE,0xD6,0xC6,0xC6,0xC6,0x00 }, // 77 'M'
{ 0xC6,0xE6,0xF6,0xDE,0xCE,0xC6,0xC6,0x00 }, // 78 'N'
{ 0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00 }, // 79 'O'
{ 0xFC,0xC6,0xC6,0xFC,0xC0,0xC0,0xC0,0x00 }, // 80 'P'
{ 0x7C,0xC6,0xC6,0xC6,0xD6,0xDE,0x7C,0x06 }, // 81 'Q'
{ 0xFC,0xC6,0xC6,0xFC,0xD8,0xCC,0xC6,0x00 }, // 82 'R'
{ 0x7C,0xC6,0xC0,0x7C,0x06,0xC6,0x7C,0x00 }, // 83 'S'
{ 0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00 }, // 84 'T'
{ 0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00 }, // 85 'U'
{ 0xC6,0xC6,0xC6,0xC6,0x6C,0x38,0x10,0x00 }, // 86 'V'
{ 0xC6,0xC6,0xC6,0xD6,0xFE,0xEE,0xC6,0x00 }, // 87 'W'
{ 0xC6,0xC6,0x6C,0x38,0x6C,0xC6,0xC6,0x00 }, // 88 'X'
{ 0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x00 }, // 89 'Y'
{ 0xFE,0x06,0x0C,0x18,0x30,0x60,0xFE,0x00 }, // 90 'Z'
{ 0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00 }, // 91 '['
{ 0xC0,0x60,0x30,0x18,0x0C,0x06,0x02,0x00 }, // 92 '\'
{ 0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00 }, // 93 ']'
{ 0x10,0x38,0x6C,0xC6,0x00,0x00,0x00,0x00 }, // 94 '^'
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE }, // 95 '_'
{ 0x30,0x18,0x0C,0x00,0x00,0x00,0x00,0x00 }, // 96 '`'
{ 0x00,0x00,0x78,0x0C,0x7C,0xCC,0x76,0x00 }, // 97 'a'
{ 0xC0,0xC0,0xFC,0xC6,0xC6,0xC6,0xFC,0x00 }, // 98 'b'
{ 0x00,0x00,0x7C,0xC6,0xC0,0xC6,0x7C,0x00 }, // 99 'c'
{ 0x06,0x06,0x7E,0xC6,0xC6,0xC6,0x7E,0x00 }, //100 'd'
{ 0x00,0x00,0x7C,0xC6,0xFE,0xC0,0x7C,0x00 }, //101 'e'
{ 0x1C,0x36,0x30,0x7C,0x30,0x30,0x30,0x00 }, //102 'f'
{ 0x00,0x00,0x7E,0xC6,0xC6,0x7E,0x06,0x7C }, //103 'g'
{ 0xC0,0xC0,0xFC,0xC6,0xC6,0xC6,0xC6,0x00 }, //104 'h'
{ 0x18,0x00,0x38,0x18,0x18,0x18,0x3C,0x00 }, //105 'i'
{ 0x0C,0x00,0x1C,0x0C,0x0C,0x0C,0xCC,0x78 }, //106 'j'
{ 0xC0,0xC0,0xCC,0xD8,0xF0,0xD8,0xCC,0x00 }, //107 'k'
{ 0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00 }, //108 'l'
{ 0x00,0x00,0xCC,0xFE,0xD6,0xC6,0xC6,0x00 }, //109 'm'
{ 0x00,0x00,0xFC,0xC6,0xC6,0xC6,0xC6,0x00 }, //110 'n'
{ 0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0x00 }, //111 'o'
{ 0x00,0x00,0xFC,0xC6,0xC6,0xFC,0xC0,0xC0 }, //112 'p'
{ 0x00,0x00,0x7E,0xC6,0xC6,0x7E,0x06,0x06 }, //113 'q'
{ 0x00,0x00,0xDC,0xE6,0xC0,0xC0,0xC0,0x00 }, //114 'r'
{ 0x00,0x00,0x7E,0xC0,0x7C,0x06,0xFC,0x00 }, //115 's'
{ 0x30,0x30,0x7C,0x30,0x30,0x36,0x1C,0x00 }, //116 't'
{ 0x00,0x00,0xC6,0xC6,0xC6,0xC6,0x7E,0x00 }, //117 'u'
{ 0x00,0x00,0xC6,0xC6,0xC6,0x6C,0x38,0x00 }, //118 'v'
{ 0x00,0x00,0xC6,0xC6,0xD6,0xFE,0x6C,0x00 }, //119 'w'
{ 0x00,0x00,0xC6,0x6C,0x38,0x6C,0xC6,0x00 }, //120 'x'
{ 0x00,0x00,0xC6,0xC6,0xC6,0x7E,0x06,0x7C }, //121 'y'
{ 0x00,0x00,0xFE,0x0C,0x38,0x60,0xFE,0x00 }, //122 'z'
{ 0x0E,0x18,0x18,0x70,0x18,0x18,0x0E,0x00 }, //123 '{'
{ 0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x00 }, //124 '|'
{ 0x70,0x18,0x18,0x0E,0x18,0x18,0x70,0x00 }, //125 '}'
{ 0x76,0xDC,0x00,0x00,0x00,0x00,0x00,0x00 }, //126 '~'
};
//=====================================================================
// Part 4: Implementation
//=====================================================================
#ifdef GAMELIB_IMPLEMENTATION
//---------------------------------------------------------------------
// Dynamically loaded function pointers (global, valid during process lifetime)
//---------------------------------------------------------------------
static PFN_SetDIBitsToDevice _gl_SetDIBitsToDevice = NULL;
static PFN_GetStockObject _gl_GetStockObject = NULL;
static PFN_CreateCompatibleDC _gl_CreateCompatibleDC = NULL;
static PFN_DeleteDC _gl_DeleteDC = NULL;
static PFN_CreateDIBSection _gl_CreateDIBSection = NULL;
static PFN_SelectObject _gl_SelectObject = NULL;
static PFN_DeleteObject _gl_DeleteObject = NULL;
static PFN_BitBlt _gl_BitBlt = NULL;
static PFN_StretchBlt _gl_StretchBlt = NULL;
static PFN_SetStretchBltMode _gl_SetStretchBltMode = NULL;
static PFN_CreateFontW _gl_CreateFontW = NULL;
static PFN_TextOutW _gl_TextOutW = NULL;
static PFN_SetTextColor _gl_SetTextColor = NULL;
static PFN_SetBkMode _gl_SetBkMode = NULL;
static PFN_GetTextExtentPoint32W _gl_GetTextExtentPoint32W = NULL;
static PFN_GdiFlush _gl_GdiFlush = NULL;
static PFN_timeBeginPeriod _gl_timeBeginPeriod = NULL;
static PFN_timeEndPeriod _gl_timeEndPeriod = NULL;
static PFN_timeSetEvent _gl_timeSetEvent = NULL;
static PFN_timeKillEvent _gl_timeKillEvent = NULL;
static PFN_mciSendStringW _gl_mciSendStringW = NULL;
// winmm.dll (waveOut - loaded separately for software mixer)
static PFN_waveOutOpen _gl_waveOutOpen = NULL;
static PFN_waveOutClose _gl_waveOutClose = NULL;
static PFN_waveOutPrepareHeader _gl_waveOutPrepareHeader = NULL;
static PFN_waveOutUnprepareHeader _gl_waveOutUnprepareHeader = NULL;
static PFN_waveOutWrite _gl_waveOutWrite = NULL;
static PFN_waveOutReset _gl_waveOutReset = NULL;
static int _gamelib_apis_loaded = 0;
static uint64_t _gamelib_query_performance_counter()
{
LARGE_INTEGER counter;
counter.QuadPart = 0;
QueryPerformanceCounter(&counter);
return (uint64_t)counter.QuadPart;
}
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 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);
}
}
template <typename T>
static T _gamelib_load_proc(HMODULE module, const char *name)
{
FARPROC proc = GetProcAddress(module, name);
T typed = NULL;
if (!proc) return typed;
if (sizeof(typed) != sizeof(proc)) return typed;
memcpy(&typed, &proc, sizeof(typed));
return typed;
}
static int _gamelib_load_apis()
{
if (_gamelib_apis_loaded) return 0;
HMODULE hGdi32 = LoadLibraryA("gdi32.dll");
HMODULE hWinmm = LoadLibraryA("winmm.dll");
if (!hGdi32 || !hWinmm) {
if (hGdi32) FreeLibrary(hGdi32);
if (hWinmm) FreeLibrary(hWinmm);
return -1;
}
_gl_SetDIBitsToDevice = _gamelib_load_proc<PFN_SetDIBitsToDevice>(hGdi32, "SetDIBitsToDevice");
_gl_GetStockObject = _gamelib_load_proc<PFN_GetStockObject>(hGdi32, "GetStockObject");
_gl_CreateCompatibleDC = _gamelib_load_proc<PFN_CreateCompatibleDC>(hGdi32, "CreateCompatibleDC");
_gl_DeleteDC = _gamelib_load_proc<PFN_DeleteDC>(hGdi32, "DeleteDC");
_gl_CreateDIBSection = _gamelib_load_proc<PFN_CreateDIBSection>(hGdi32, "CreateDIBSection");
_gl_SelectObject = _gamelib_load_proc<PFN_SelectObject>(hGdi32, "SelectObject");
_gl_DeleteObject = _gamelib_load_proc<PFN_DeleteObject>(hGdi32, "DeleteObject");
_gl_BitBlt = _gamelib_load_proc<PFN_BitBlt>(hGdi32, "BitBlt");
_gl_StretchBlt = _gamelib_load_proc<PFN_StretchBlt>(hGdi32, "StretchBlt");
_gl_SetStretchBltMode = _gamelib_load_proc<PFN_SetStretchBltMode>(hGdi32, "SetStretchBltMode");
_gl_CreateFontW = _gamelib_load_proc<PFN_CreateFontW>(hGdi32, "CreateFontW");
_gl_TextOutW = _gamelib_load_proc<PFN_TextOutW>(hGdi32, "TextOutW");
_gl_SetTextColor = _gamelib_load_proc<PFN_SetTextColor>(hGdi32, "SetTextColor");
_gl_SetBkMode = _gamelib_load_proc<PFN_SetBkMode>(hGdi32, "SetBkMode");
_gl_GetTextExtentPoint32W = _gamelib_load_proc<PFN_GetTextExtentPoint32W>(hGdi32, "GetTextExtentPoint32W");
_gl_GdiFlush = _gamelib_load_proc<PFN_GdiFlush>(hGdi32, "GdiFlush");
_gl_timeBeginPeriod = _gamelib_load_proc<PFN_timeBeginPeriod>(hWinmm, "timeBeginPeriod");
_gl_timeEndPeriod = _gamelib_load_proc<PFN_timeEndPeriod>(hWinmm, "timeEndPeriod");
_gl_timeSetEvent = _gamelib_load_proc<PFN_timeSetEvent>(hWinmm, "timeSetEvent");
_gl_timeKillEvent = _gamelib_load_proc<PFN_timeKillEvent>(hWinmm, "timeKillEvent");
_gl_mciSendStringW = _gamelib_load_proc<PFN_mciSendStringW>(hWinmm, "mciSendStringW");
// waveOut APIs for software mixer (loaded from same winmm.dll)
_gl_waveOutOpen = _gamelib_load_proc<PFN_waveOutOpen>(hWinmm, "waveOutOpen");
_gl_waveOutClose = _gamelib_load_proc<PFN_waveOutClose>(hWinmm, "waveOutClose");
_gl_waveOutPrepareHeader = _gamelib_load_proc<PFN_waveOutPrepareHeader>(hWinmm, "waveOutPrepareHeader");
_gl_waveOutUnprepareHeader = _gamelib_load_proc<PFN_waveOutUnprepareHeader>(hWinmm, "waveOutUnprepareHeader");
_gl_waveOutWrite = _gamelib_load_proc<PFN_waveOutWrite>(hWinmm, "waveOutWrite");
_gl_waveOutReset = _gamelib_load_proc<PFN_waveOutReset>(hWinmm, "waveOutReset");
if (!_gl_SetDIBitsToDevice || !_gl_GetStockObject ||
!_gl_CreateCompatibleDC || !_gl_DeleteDC ||
!_gl_CreateDIBSection || !_gl_SelectObject || !_gl_DeleteObject ||
!_gl_BitBlt || !_gl_StretchBlt || !_gl_SetStretchBltMode ||
!_gl_CreateFontW || !_gl_TextOutW ||
!_gl_SetTextColor || !_gl_SetBkMode || !_gl_GetTextExtentPoint32W ||
!_gl_timeBeginPeriod || !_gl_timeEndPeriod ||
!_gl_mciSendStringW) {
// NULL out all pointers to prevent dangling state
_gl_SetDIBitsToDevice = NULL; _gl_GetStockObject = NULL;
_gl_CreateCompatibleDC = NULL; _gl_DeleteDC = NULL;
_gl_CreateDIBSection = NULL; _gl_SelectObject = NULL;
_gl_DeleteObject = NULL; _gl_BitBlt = NULL;
_gl_StretchBlt = NULL; _gl_SetStretchBltMode = NULL;
_gl_CreateFontW = NULL; _gl_TextOutW = NULL;
_gl_SetTextColor = NULL; _gl_SetBkMode = NULL;
_gl_GetTextExtentPoint32W = NULL; _gl_GdiFlush = NULL;
_gl_timeBeginPeriod = NULL;
_gl_timeEndPeriod = NULL; _gl_timeSetEvent = NULL;
_gl_timeKillEvent = NULL;
_gl_mciSendStringW = NULL;
_gl_waveOutOpen = NULL; _gl_waveOutClose = NULL;
_gl_waveOutPrepareHeader = NULL; _gl_waveOutUnprepareHeader = NULL;
_gl_waveOutWrite = NULL; _gl_waveOutReset = NULL;
FreeLibrary(hGdi32);
FreeLibrary(hWinmm);
return -1;
}
_gamelib_apis_loaded = 1;
return 0;
}
static bool _gamelib_mci_play_music_alias(const wchar_t *alias, HWND callbackWindow, bool isMidi, bool loop);
static void _gamelib_close_music_alias(const wchar_t *alias);
static wchar_t *_gamelib_utf8_to_wide(const char *text, int *outLen);
static FILE *_gamelib_fopen_utf8(const char *filename, const wchar_t *mode);
//---------------------------------------------------------------------
// GDI+ dynamic loading (for PNG/JPG image loading)
//---------------------------------------------------------------------
static PFN_GdiplusStartup _gl_GdiplusStartup = NULL;
static PFN_GdipCreateBitmapFromStream _gl_GdipCreateBitmapFromStream = NULL;
static PFN_GdipGetImageWidth _gl_GdipGetImageWidth = NULL;
static PFN_GdipGetImageHeight _gl_GdipGetImageHeight = NULL;
static PFN_GdipBitmapLockBits _gl_GdipBitmapLockBits = NULL;
static PFN_GdipBitmapUnlockBits _gl_GdipBitmapUnlockBits = NULL;
static PFN_GdipDisposeImage _gl_GdipDisposeImage = NULL;
static PFN_CreateStreamOnHGlobal _gl_CreateStreamOnHGlobal = NULL;
static int _gamelib_gdiplus_ready = 0;
static int _gamelib_gdiplus_failed = 0;
static ULONG_PTR _gamelib_gdip_token = 0;
// Initialize GDI+: load gdiplus.dll / ole32.dll and call GdiplusStartup
static int _gamelib_gdiplus_init()
{
if (_gamelib_gdiplus_ready) return 0;
if (_gamelib_gdiplus_failed) return -5;
HMODULE hGdiPlus = LoadLibraryA("gdiplus.dll");
if (!hGdiPlus) { _gamelib_gdiplus_failed = 1; return -1; }
HMODULE hOle32 = LoadLibraryA("ole32.dll");
if (!hOle32) { FreeLibrary(hGdiPlus); _gamelib_gdiplus_failed = 1; return -2; }
_gl_GdiplusStartup = _gamelib_load_proc<PFN_GdiplusStartup>(hGdiPlus, "GdiplusStartup");
_gl_GdipCreateBitmapFromStream = _gamelib_load_proc<PFN_GdipCreateBitmapFromStream>(hGdiPlus, "GdipCreateBitmapFromStream");