-
Notifications
You must be signed in to change notification settings - Fork 28
/
RGFW.h
9849 lines (7744 loc) · 316 KB
/
RGFW.h
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
/*
*
* RGFW 1.2-dev
*
* Copyright (C) 2022-25 ColleagueRiley
*
* libpng license
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
*
*/
/*
(MAKE SURE RGFW_IMPLEMENTATION is in exactly one header or you use -D RGFW_IMPLEMENTATION)
#define RGFW_IMPLEMENTATION - makes it so source code is included with header
*/
/*
#define RGFW_IMPLEMENTATION - (required) makes it so the source code is included
#define RGFW_DEBUG - (optional) makes it so RGFW prints debug messages anderrors when they're found
#define RGFW_OSMESA - (optional) use OSmesa as backend (instead of system's opengl api + regular opengl)
#define RGFW_BUFFER - (optional) just draw directly to (RGFW) window pixel buffer that is drawn to screen (the buffer is in the RGBA format)
#define RGFW_EGL - (optional) use EGL for loading an OpenGL context (instead of the system's opengl api)
#define RGFW_OPENGL_ES1 - (optional) use EGL to load and use Opengl ES (version 1) for backend rendering (instead of the system's opengl api)
This version doesn't work for desktops (I'm pretty sure)
#define RGFW_OPENGL_ES2 - (optional) use OpenGL ES (version 2)
#define RGFW_OPENGL_ES3 - (optional) use OpenGL ES (version 3)
#define RGFW_DIRECTX - (optional) use directX for the rendering backend (rather than opengl) (windows only, defaults to opengl for unix)
#define RGFW_WEBGPU - (optional) use webGPU for rendering (Web ONLY)
#define RGFW_NO_API - (optional) don't use any rendering API (no opengl, no vulkan, no directX)
#define RGFW_LINK_EGL (optional) (windows only) if EGL is being used, if EGL functions should be defined dymanically (using GetProcAddress)
#define RGFW_LINK_OSMESA (optional) (windows only) if EGL is being used, if OS Mesa functions should be defined dymanically (using GetProcAddress)
#define RGFW_X11 (optional) (unix only) if X11 should be used. This option is turned on by default by unix systems except for MacOS
#define RGFW_WGL_LOAD (optional) (windows only) if WGL should be loaded dynamically during runtime
#define RGFW_NO_X11_CURSOR (optional) (unix only) don't use XCursor
#define RGFW_NO_X11_CURSOR_PRELOAD (optional) (unix only) Use XCursor, but don't link it in code, (you'll have to link it with -lXcursor)
#define RGFW_NO_LOAD_WINMM (optional) (windows only) Use winmm (timeBeginPeriod), but don't link it in code, (you'll have to link it with -lwinmm)
#define RGFW_NO_WINMM (optional) (windows only) don't use winmm
#define RGFW_NO_IOKIT (optional) (macOS) don't use IOKit
#define RGFW_NO_UNIX_CLOCK
#define RGFW_NO_DPI - Do not include calculate DPI (no XRM nor libShcore included)
#define RGFW_ALLOC_DROPFILES (optional) if room should be allocating for drop files (by default it's global data)
#define RGFW_alloc(userptr, size) x - choose what default function to use to allocate, by default the standard malloc is used
#define RGFW_free(userptr, ptr) x - choose what default function to use to allocated memory, by default the standard free is used
#define RGFW_USERPTR x - choose the default userptr sent to the malloc call, (NULL by default)
#define RGFW_EXPORT - Use when building RGFW
#define RGFW_IMPORT - Use when linking with RGFW (not as a single-header)
#define RGFW_USE_INT - force the use c-types rather than stdint.h (for systems that might not have stdint.h (msvc))
*/
/*
Example to get you started :
linux : gcc main.c -lX11 -lXrandr -lGL -lm
windows : gcc main.c -lopengl32 -lwinmm -lshell32 -lgdi32
macos : gcc main.c -framework Foundation -framework AppKit -framework OpenGL -framework IOKit
#define RGFW_IMPLEMENTATION
#include "RGFW.h"
u8 icon[4 * 3 * 3] = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF};
int main() {
RGFW_window* win = RGFW_createWindow("name", RGFW_RECT(500, 500, 500, 500), (u64)0);
RGFW_window_setIcon(win, icon, RGFW_AREA(3, 3), 4);
for (;;) {
RGFW_window_checkEvent(win); // NOTE: checking events outside of a while loop may cause input lag
if (win->event.type == RGFW_quit || RGFW_isPressed(win, RGFW_Escape))
break;
RGFW_window_swapBuffers(win);
glClearColor(0xFF, 0XFF, 0xFF, 0xFF);
glClear(GL_COLOR_BUFFER_BIT);
}
RGFW_window_close(win);
}
compiling :
if you wish to compile the library all you have to do is create a new file with this in it
rgfw.c
#define RGFW_IMPLEMENTATION
#include "RGFW.h"
You may also want to add
`#define RGFW_EXPORT` when compiling and
`#define RGFW_IMPORT`when linking RGFW on it's own:
this reduces inline functions and prevents bloat in the object file
then you can use gcc (or whatever compile you wish to use) to compile the library into object file
ex. gcc -c RGFW.c -fPIC
after you compile the library into an object file, you can also turn the object file into an static or shared library
(commands ar and gcc can be replaced with whatever equivalent your system uses)
static : ar rcs RGFW.a RGFW.o
shared :
windows:
gcc -shared RGFW.o -lwinmm -lopengl32 -lshell32 -lgdi32 -o RGFW.dll
linux:
gcc -shared RGFW.o -lX11 -lGL -lXrandr -o RGFW.so
macos:
gcc -shared RGFW.o -framework Foundation -framework AppKit -framework OpenGL -framework IOKit
*/
/*
Credits :
EimaMei/Sacode : Much of the code for creating windows using winapi, Wrote the Silicon library, helped with MacOS Support, siliapp.h -> referencing
stb - This project is heavily inspired by the stb single header files
GLFW:
certain parts of winapi and X11 are very poorly documented,
GLFW's source code was referenced and used throughout the project (used code is marked in some way),
this mainly includes, code for drag and drops, code for setting the icon to a bitmap and the code for managing the clipboard for X11 (as these parts are not documented very well)
GLFW Copyright, https::/github.com/GLFW/GLFW
Copyright (c) 2002-2006 Marcus Geelnard
Copyright (c) 2006-2019 Camilla Löwy
contributors : (feel free to put yourself here if you contribute)
krisvers -> code review
EimaMei (SaCode) -> code review
Code-Nycticebus -> bug fixes
Rob Rohan -> X11 bugs and missing features, MacOS/Cocoa fixing memory issues/bugs
AICDG (@THISISAGOODNAME) -> vulkan support (example)
@Easymode -> support, testing/debugging, bug fixes and reviews
Joshua Rowe (omnisci3nce) - bug fix, review (macOS)
@lesleyrs -> bug fix, review (OpenGL)
Nick Porcino (meshula) - testing, organization, review (MacOS, examples)
@DarekParodia -> code review (X11) (C++)
*/
#if _MSC_VER
#pragma comment(lib, "gdi32")
#pragma comment(lib, "shell32")
#pragma comment(lib, "opengl32")
#pragma comment(lib, "winmm")
#pragma comment(lib, "user32")
#endif
#ifndef RGFW_UNUSED
#define RGFW_UNUSED(x) (void)(x)
#endif
#ifndef RGFW_USERPTR
#define RGFW_USERPTR NULL
#endif
#ifndef RGFW_ROUND
#define RGFW_ROUND(x) (int)((x) >= 0 ? (x) + 0.5f : (x) - 0.5f)
#endif
#ifndef RGFW_ALLOC
#include <stdlib.h>
#ifndef __USE_POSIX199309
#define __USE_POSIX199309
#endif
#include <time.h>
#define RGFW_ALLOC(userptr, size) (RGFW_UNUSED(userptr),malloc(size))
#define RGFW_FREE(userptr, ptr) (RGFW_UNUSED(userptr),free(ptr))
#endif
#ifndef RGFW_MEMCPY
#include <string.h>
#ifdef RGFW_WINDOWS
#include <wchar.h>
#include <locale.h>
#endif
#define RGFW_MEMCPY(dist, src, len) memcpy(dist, src, len)
#define RGFW_STRNCMP(s1, s2, max) strncmp(s1, s2, max)
//required for X11
#define RGFW_STRTOL(str, endptr, base) strtol(str, endptr, base)
#else
#undef _INC_STRING
#endif
#if !_MSC_VER
#ifndef inline
#ifndef __APPLE__
#define inline __inline
#endif
#endif
#endif
#ifdef RGFW_WIN95 /* for windows 95 testing (not that it really works) */
#define RGFW_NO_MONITOR
#define RGFW_NO_PASSTHROUGH
#endif
#if defined(RGFW_EXPORT) || defined(RGFW_IMPORT)
#if defined(_WIN32)
#if defined(__TINYC__) && (defined(RGFW_EXPORT) || defined(RGFW_IMPORT))
#define __declspec(x) __attribute__((x))
#endif
#if defined(RGFW_EXPORT)
#define RGFWDEF __declspec(dllexport)
#else
#define RGFWDEF __declspec(dllimport)
#endif
#else
#if defined(RGFW_EXPORT)
#define RGFWDEF __attribute__((visibility("default")))
#endif
#endif
#endif
#ifndef RGFWDEF
#define RGFWDEF inline
#endif
#ifndef RGFW_ENUM
#define RGFW_ENUM(type, name) type name; enum
#endif
#if defined(__cplusplus) && !defined(__EMSCRIPTEN__)
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullability-completeness"
#endif
extern "C" {
#endif
/* makes sure the header file part is only defined once by default */
#ifndef RGFW_HEADER
#define RGFW_HEADER
#if !defined(u8)
#ifdef RGFW_USE_INT /* optional for any system that might not have stdint.h */
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned short u16;
typedef signed short i16;
typedef unsigned long int u32;
typedef signed long int i32;
typedef unsigned long long u64;
typedef signed long long i64;
#else /* use stdint standard types instead of c ""standard"" types */
#include <stdint.h>
typedef uint8_t u8;
typedef int8_t i8;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint64_t u64;
typedef int64_t i64;
#endif
#define u8 u8
#endif
#if !defined(b8) /* RGFW bool type */
typedef u8 b8;
typedef u32 b32;
#define b8 b8
#endif
#define RGFW_TRUE (!(0))
#define RGFW_FALSE 0
/* thse OS macros looks better & are standardized */
/* plus it helps with cross-compiling */
#ifdef __EMSCRIPTEN__
#define RGFW_WEBASM
#if !defined(RGFW_NO_API) && !defined(RGFW_WEBGPU)
#define RGFW_OPENGL
#endif
#ifdef RGFW_EGL
#undef RGFW_EGL
#endif
#include <emscripten/html5.h>
#include <emscripten/key_codes.h>
#ifdef RGFW_WEBGPU
#include <emscripten/html5_webgpu.h>
#endif
#endif
#if defined(RGFW_X11) && defined(__APPLE__)
#define RGFW_MACOS_X11
#undef __APPLE__
#endif
#if defined(_WIN32) && !defined(RGFW_X11) && !defined(RGFW_WEBASM) /* (if you're using X11 on windows some how) */
#define RGFW_WINDOWS
/* make sure the correct architecture is defined */
#if defined(_WIN64)
#define _AMD64_
#undef _X86_
#else
#undef _AMD64_
#ifndef _X86_
#define _X86_
#endif
#endif
#ifndef RGFW_NO_XINPUT
#ifdef __MINGW32__ /* try to find the right header */
#include <xinput.h>
#else
#include <XInput.h>
#endif
#endif
#if defined(RGFW_DIRECTX)
#define OEMRESOURCE
#include <d3d11.h>
#include <dxgi.h>
#include <dxgi.h>
#include <d3dcompiler.h>
#ifndef __cplusplus
#define __uuidof(T) IID_##T
#endif
#endif
#elif defined(RGFW_WAYLAND)
#if !defined(RGFW_NO_API) && (!defined(RGFW_BUFFER) || defined(RGFW_OPENGL))
#define RGFW_EGL
#define RGFW_OPENGL
#include <wayland-egl.h>
#endif
#include <wayland-client.h>
#elif (defined(__unix__) || defined(RGFW_MACOS_X11) || defined(RGFW_X11)) && !defined(RGFW_WEBASM)
#define RGFW_MACOS_X11
#define RGFW_X11
#include <X11/Xlib.h>
#elif defined(__APPLE__) && !defined(RGFW_MACOS_X11) && !defined(RGFW_X11) && !defined(RGFW_WEBASM)
#define RGFW_MACOS
#endif
#if (defined(RGFW_OPENGL_ES1) || defined(RGFW_OPENGL_ES2) || defined(RGFW_OPENGL_ES3)) && !defined(RGFW_EGL)
#define RGFW_EGL
#endif
#if !defined(RGFW_OSMESA) && !defined(RGFW_EGL) && !defined(RGFW_OPENGL) && !defined(RGFW_DIRECTX) && !defined(RGFW_BUFFER) && !defined(RGFW_NO_API)
#define RGFW_OPENGL
#endif
#ifdef RGFW_EGL
#include <EGL/egl.h>
#elif defined(RGFW_OSMESA)
#ifndef __APPLE__
#include <GL/osmesa.h>
#else
#include <OpenGL/osmesa.h>
#endif
#endif
#if defined(RGFW_OPENGL) && defined(RGFW_X11)
#ifndef GLX_MESA_swap_control
#define GLX_MESA_swap_control
#endif
#include <GL/glx.h> /* GLX defs, xlib.h, gl.h */
#endif
#ifndef RGFW_ALPHA
#define RGFW_ALPHA 128 /* alpha value for RGFW_transparent (WINAPI ONLY, macOS + linux don't need this) */
#endif
/*
RGFW_allocator (optional)
you can ignore this if you use standard malloc/free
*/
typedef void* (* RGFW_allocatorMallocfunc)(void* userdata, size_t size);
typedef void (* RGFW_allocatorFreefunc)(void* userdata, void* ptr);
typedef struct RGFW_allocator {
void* userdata;
RGFW_allocatorMallocfunc alloc;
RGFW_allocatorFreefunc free;
} RGFW_allocator;
RGFWDEF RGFW_allocator RGFW_loadAllocator(RGFW_allocator allocator);
RGFWDEF void* RGFW_alloc(size_t len);
RGFWDEF void RGFW_free(void* ptr);
/*
regular RGFW stuff
*/
typedef RGFW_ENUM(u8, RGFW_eventTypes) {
/*! event codes */
RGFW_noEvent = 0, /*!< no event has been sent */
RGFW_keyPressed, /* a key has been pressed */
RGFW_keyReleased, /*!< a key has been released*/
/*! key event note
the code of the key pressed is stored in
RGFW_Event.key
!!Keycodes defined at the bottom of the RGFW_HEADER part of this file!!
while a string version is stored in
RGFW_Event.KeyString
RGFW_Event.lockState holds the current lockState
this means if CapsLock, NumLock are active or not
*/
RGFW_mouseButtonPressed, /*!< a mouse button has been pressed (left,middle,right)*/
RGFW_mouseButtonReleased, /*!< a mouse button has been released (left,middle,right)*/
RGFW_mousePosChanged, /*!< the position of the mouse has been changed*/
/*! mouse event note
the x and y of the mouse can be found in the vector, RGFW_Event.point
RGFW_Event.button holds which mouse button was pressed
*/
RGFW_gamepadConnected, /*!< a gamepad was connected */
RGFW_gamepadDisconnected, /*!< a gamepad was disconnected */
RGFW_gamepadButtonPressed, /*!< a gamepad button was pressed */
RGFW_gamepadButtonReleased, /*!< a gamepad button was released */
RGFW_gamepadAxisMove, /*!< an axis of a gamepad was moved*/
/*! gamepad event note
RGFW_Event.gamepad holds which gamepad was altered, if any
RGFW_Event.button holds which gamepad button was pressed
RGFW_Event.axis holds the data of all the axis
RGFW_Event.axisesCount says how many axis there are
*/
RGFW_windowMoved, /*!< the window was moved (b the user) */
RGFW_windowResized, /*!< the window was resized (by the user), [on webASM this means the browser was resized] */
RGFW_focusIn, /*!< window is in focus now */
RGFW_focusOut, /*!< window is out of focus now */
RGFW_mouseEnter, /* mouse entered the window */
RGFW_mouseLeave, /* mouse left the window */
RGFW_windowRefresh, /* The window content needs to be refreshed */
/* attribs change event note
The event data is sent straight to the window structure
with win->r.x, win->r.y, win->r.w and win->r.h
*/
RGFW_quit, /*!< the user clicked the quit button*/
RGFW_dnd, /*!< a file has been dropped into the window*/
RGFW_dnd_init /*!< the start of a dnd event, when the place where the file drop is known */
/* dnd data note
The x and y coords of the drop are stored in the vector RGFW_Event.point
RGFW_Event.droppedFilesCount holds how many files were dropped
This is also the size of the array which stores all the dropped file string,
RGFW_Event.droppedFiles
*/
};
/*! mouse button codes (RGFW_Event.button) */
enum RGFW_mouse_codes {
RGFW_mouseNone = 0, /*!< no mouse button is pressed*/
RGFW_mouseLeft, /*!< left mouse button is pressed*/
RGFW_mouseMiddle, /*!< mouse-wheel-button is pressed*/
RGFW_mouseRight, /*!< right mouse button is pressed*/
RGFW_mouseScrollUp, /*!< mouse wheel is scrolling up*/
RGFW_mouseScrollDown /*!< mouse wheel is scrolling down*/
};
#ifndef RGFW_MAX_PATH
#define RGFW_MAX_PATH 260 /* max length of a path (for dnd) */
#endif
#ifndef RGFW_MAX_DROPS
#define RGFW_MAX_DROPS 260 /* max items you can drop at once */
#endif
/* for RGFW_Event.lockstate */
#define RGFW_CAPSLOCK (1L << 1)
#define RGFW_NUMLOCK (1L << 2)
/*! gamepad button codes (based on xbox/playstation), you may need to change these values per controller */
typedef RGFW_ENUM(u8, RGFW_gamepadCodes) {
RGFW_gamepadNone = 0, /*!< or PS X button */
RGFW_gamepadA, /*!< or PS X button */
RGFW_gamepadB, /*!< or PS circle button */
RGFW_gamepadY, /*!< or PS triangle button */
RGFW_gamepadX, /*!< or PS square button */
RGFW_gamepadStart, /*!< start button */
RGFW_gamepadSelect, /*!< select button */
RGFW_gamepadHome, /*!< home button */
RGFW_gamepadUp, /*!< dpad up */
RGFW_gamepadDown, /*!< dpad down*/
RGFW_gamepadLeft, /*!< dpad left */
RGFW_gamepadRight, /*!< dpad right */
RGFW_gamepadL1, /*!< left bump */
RGFW_gamepadL2, /*!< left trigger*/
RGFW_gamepadR1, /*!< right bumper */
RGFW_gamepadR2, /*!< right trigger */
RGFW_gamepadL3, /* left thumb stick */
RGFW_gamepadR3, /*!< right thumb stick */
RGFW_gamepadFinal
};
/*! basic vector type, if there's not already a point/vector type of choice */
#ifndef RGFW_point
typedef struct { i32 x, y; } RGFW_point;
#endif
/*! basic rect type, if there's not already a rect type of choice */
#ifndef RGFW_rect
typedef struct { i32 x, y, w, h; } RGFW_rect;
#endif
/*! basic area type, if there's not already a area type of choice */
#ifndef RGFW_area
typedef struct { u32 w, h; } RGFW_area;
#endif
#define RGFW_POINT(x, y) (RGFW_point){(i32)(x), (i32)(y)}
#define RGFW_RECT(x, y, w, h) (RGFW_rect){(i32)(x), (i32)(y), (i32)(w), (i32)(h)}
#define RGFW_AREA(w, h) (RGFW_area){(u32)(w), (u32)(h)}
#ifndef RGFW_NO_MONITOR
/*! structure for monitor data */
typedef struct RGFW_monitor {
char name[128]; /*!< monitor name */
RGFW_rect rect; /*!< monitor Workarea */
float scaleX, scaleY; /*!< monitor content scale*/
float pixelRatio; /*!< pixel ratio for monitor (1.0 for regular, 2.0 for hiDPI) */
float physW, physH; /*!< monitor physical size in inches*/
} RGFW_monitor;
/*
NOTE : Monitor functions should be ran only as many times as needed (not in a loop)
*/
/*! get an array of all the monitors (max 6) */
RGFWDEF RGFW_monitor* RGFW_getMonitors(void);
/*! get the primary monitor */
RGFWDEF RGFW_monitor RGFW_getPrimaryMonitor(void);
#endif
/* NOTE: some parts of the data can represent different things based on the event (read comments in RGFW_Event struct) */
/*! Event structure for checking/getting events */
typedef struct RGFW_Event {
/*! drag and drop data */
/* 260 max paths with a max length of 260 */
#ifdef RGFW_ALLOC_DROPFILES
char** droppedFiles;
#else
char droppedFiles[RGFW_MAX_DROPS][RGFW_MAX_PATH]; /*!< dropped files*/
#endif
u32 droppedFilesCount; /*!< house many files were dropped */
u32 type; /*!< which event has been sent?*/
RGFW_point point; /*!< mouse x, y of event (or drop point) */
u8 key; /*!< the physical key of the event, refers to where key is physically !!Keycodes defined at the bottom of the RGFW_HEADER part of this file!! */
u8 keyChar; /*!< mapped key char of the event*/
b8 repeat; /*!< key press event repeated (the key is being held) */
b8 inFocus; /*!< if the window is in focus or not (this is always true for MacOS windows due to the api being weird) */
u8 lockState;
u8 button; /* !< which mouse (or gamepad) button was pressed */
double scroll; /*!< the raw mouse scroll value */
u16 gamepad; /*! which gamepad this event applies to (if applicable to any) */
u8 axisesCount; /*!< number of axises */
u8 whichAxis; /* which axis was effected */
RGFW_point axis[4]; /*!< x, y of axises (-100 to 100) */
u64 frameTime, frameTime2; /*!< this is used for counting the fps */
} RGFW_Event;
/*! source data for the window (used by the APIs) */
typedef struct RGFW_window_src {
#ifdef RGFW_WINDOWS
HWND window; /*!< source window */
HDC hdc; /*!< source HDC */
u32 hOffset; /*!< height offset for window */
#if (defined(RGFW_OPENGL)) && !defined(RGFW_OSMESA) && !defined(RGFW_EGL)
HGLRC ctx; /*!< source graphics context */
#elif defined(RGFW_OSMESA)
OSMesaContext ctx;
#elif defined(RGFW_DIRECTX)
IDXGISwapChain* swapchain;
ID3D11RenderTargetView* renderTargetView;
ID3D11DepthStencilView* pDepthStencilView;
#elif defined(RGFW_EGL)
EGLSurface EGL_surface;
EGLDisplay EGL_display;
EGLContext EGL_context;
#endif
#if defined(RGFW_OSMESA) || defined(RGFW_BUFFER)
HDC hdcMem;
HBITMAP bitmap;
#endif
RGFW_area maxSize, minSize; /*!< for setting max/min resize (RGFW_WINDOWS) */
#elif defined(RGFW_X11)
Display* display; /*!< source display */
Window window; /*!< source window */
#if (defined(RGFW_OPENGL)) && !defined(RGFW_OSMESA) && !defined(RGFW_EGL)
GLXContext ctx; /*!< source graphics context */
#elif defined(RGFW_OSMESA)
OSMesaContext ctx;
#elif defined(RGFW_EGL)
EGLSurface EGL_surface;
EGLDisplay EGL_display;
EGLContext EGL_context;
#endif
#if defined(RGFW_OSMESA) || defined(RGFW_BUFFER)
XImage* bitmap;
GC gc;
#endif
#elif defined(RGFW_WAYLAND)
struct wl_display* display;
struct wl_surface* surface;
struct wl_buffer* wl_buffer;
struct wl_keyboard* keyboard;
struct xdg_surface* xdg_surface;
struct xdg_toplevel* xdg_toplevel;
struct zxdg_toplevel_decoration_v1* decoration;
RGFW_Event events[20];
i32 eventLen;
size_t eventIndex;
#if defined(RGFW_EGL)
struct wl_egl_window* window;
EGLSurface EGL_surface;
EGLDisplay EGL_display;
EGLContext EGL_context;
#elif defined(RGFW_OSMESA)
OSMesaContext ctx;
#endif
#elif defined(RGFW_MACOS)
void* window;
b8 dndPassed;
#if (defined(RGFW_OPENGL)) && !defined(RGFW_OSMESA) && !defined(RGFW_EGL)
void* ctx; /*!< source graphics context */
#elif defined(RGFW_OSMESA)
OSMesaContext ctx;
#elif defined(RGFW_EGL)
EGLSurface EGL_surface;
EGLDisplay EGL_display;
EGLContext EGL_context;
#endif
void* view; /*apple viewpoint thingy*/
#if defined(RGFW_OSMESA) || defined(RGFW_BUFFER)
void* bitmap; /*!< API's bitmap for storing or managing */
void* image;
#endif
#elif defined(RGFW_WEBASM)
#ifdef RGFW_WEBGPU
WGPUInstance ctx;
WGPUDevice device;
WGPUQueue queue;
#else
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx;
#endif
#endif
} RGFW_window_src;
/*! Optional arguments for making a windows */
typedef RGFW_ENUM(u16, RGFW_windowArgs) {
RGFW_transparent = (1L<<9), /*!< the window is transparent (only properly works on X11 and MacOS, although it's although for windows) */
RGFW_noBorder = (1L<<3), /*!< the window doesn't have border */
RGFW_noResize = (1L<<4), /*!< the window cannot be resized by the user */
RGFW_allowDND = (1L<<5), /*!< the window supports drag and drop*/
RGFW_hideMouse = (1L<<6), /*! the window should hide the mouse or not (can be toggled later on) using `RGFW_window_mouseShow*/
RGFW_fullscreen = (1L<<8), /* the window is fullscreen by default or not */
RGFW_center = (1L<<10), /*! center the window on the screen */
RGFW_openglSoftware = (1L<<11), /*! use OpenGL software rendering */
RGFW_cocoaMoveToResourceDir = (1L << 12), /* (cocoa only), move to resource folder */
RGFW_scaleToMonitor = (1L << 13), /* scale the window to the screen */
RGFW_noInitAPI = (1L << 2), /* DO not init an API (mostly for bindings, you should use `#define RGFW_NO_API` in C */
RGFW_noGPURender = (1L<<14), /* don't render (using the GPU based API)*/
RGFW_noCPURender = (1L<<15), /* don't render (using the CPU based buffer rendering)*/
RGFW_windowHide = (1L << 16)/* the window is hidden */
};
typedef struct RGFW_window {
RGFW_window_src src; /*!< src window data */
#if defined(RGFW_OSMESA) || defined(RGFW_BUFFER)
u8* buffer; /*!< buffer for non-GPU systems (OSMesa, basic software rendering) */
/* when rendering using RGFW_BUFFER, the buffer is in the RGBA format */
#endif
void* userPtr; /* ptr for usr data */
RGFW_Event event; /*!< current event */
RGFW_rect r; /*!< the x, y, w and h of the struct */
RGFW_point _lastMousePoint; /*!< last cusor point (for raw mouse data) */
u32 _winArgs; /*!< windows args (for RGFW to check) */
} RGFW_window; /*!< Window structure for managing the window */
#if defined(RGFW_X11) || defined(RGFW_MACOS)
typedef u64 RGFW_thread; /*!< thread type unix */
#else
typedef void* RGFW_thread; /*!< thread type for window */
#endif
/** * @defgroup Window_management
* @{ */
/*!
* the class name for X11 and WinAPI. apps with the same class will be grouped by the WM
* by default the class name will == the root window's name
*/
RGFWDEF void RGFW_setClassName(const char* name);
/*! this has to be set before createWindow is called, else the fulscreen size is used */
RGFWDEF void RGFW_setBufferSize(RGFW_area size); /*!< the buffer cannot be resized (by RGFW) */
/* NOTE: (windows)If the executable has an icon resource named RGFW_ICON, it will be set as the initial icon for the window.*/
RGFWDEF RGFW_window* RGFW_createWindow(
const char* name, /* name of the window */
RGFW_rect rect, /* rect of window */
RGFW_windowArgs args /* extra arguments (NULL / (u16)0 means no args used)*/
); /*!< function to create a window struct */
/*! get the size of the screen to an area struct */
RGFWDEF RGFW_area RGFW_getScreenSize(void);
/*!
this function checks an *individual* event (and updates window structure attributes)
this means, using this function without a while loop may cause event lag
ex.
while (RGFW_window_checkEvent(win) != NULL) [this keeps checking events until it reaches the last one]
this function is optional if you choose to use event callbacks,
although you still need some way to tell RGFW to process events eg. `RGFW_window_checkEvents`
*/
RGFWDEF RGFW_Event* RGFW_window_checkEvent(RGFW_window* win); /*!< check current event (returns a pointer to win->event or NULL if there is no event)*/
/*!
for RGFW_window_eventWait and RGFW_window_checkEvents
waitMS -> Allows th e function to keep checking for events even after `RGFW_window_checkEvent == NULL`
if waitMS == 0, the loop will not wait for events
if waitMS == a positive integer, the loop will wait that many miliseconds after there are no more events until it returns
if waitMS == a negative integer, the loop will not return until it gets another event
*/
typedef RGFW_ENUM(i32, RGFW_eventWait) {
RGFW_waitNext = -1,
RGFW_noWait = 0
};
/*! sleep until RGFW gets an event or the timer ends (defined by OS) */
RGFWDEF void RGFW_window_eventWait(RGFW_window* win, i32 waitMS);
/*!
check all the events until there are none left,
this should only be used if you're using callbacks only
*/
RGFWDEF void RGFW_window_checkEvents(RGFW_window* win, i32 waitMS);
/*!
Tell RGFW_window_eventWait to stop waiting, to be ran from another thread
*/
RGFWDEF void RGFW_stopCheckEvents(void);
/*! window managment functions*/
RGFWDEF void RGFW_window_close(RGFW_window* win); /*!< close the window and free leftover data */
/*! moves window to a given point */
RGFWDEF void RGFW_window_move(RGFW_window* win,
RGFW_point v/*!< new pos*/
);
#ifndef RGFW_NO_MONITOR
/*! move to a specific monitor */
RGFWDEF void RGFW_window_moveToMonitor(RGFW_window* win, RGFW_monitor m /* monitor */);
#endif
/*! resize window to a current size/area */
RGFWDEF void RGFW_window_resize(RGFW_window* win, /*!< source window */
RGFW_area a/*!< new size*/
);
/*! set the minimum size a user can shrink a window to a given size/area */
RGFWDEF void RGFW_window_setMinSize(RGFW_window* win, RGFW_area a);
/*! set the minimum size a user can extend a window to a given size/area */
RGFWDEF void RGFW_window_setMaxSize(RGFW_window* win, RGFW_area a);
RGFWDEF void RGFW_window_maximize(RGFW_window* win); /*!< maximize the window size */
RGFWDEF void RGFW_window_minimize(RGFW_window* win); /*!< minimize the window (in taskbar (per OS))*/
RGFWDEF void RGFW_window_restore(RGFW_window* win); /*!< restore the window from minimized (per OS)*/
/*! if the window should have a border or not (borderless) based on bool value of `border` */
RGFWDEF void RGFW_window_setBorder(RGFW_window* win, b8 border);
/*! turn on / off dnd (RGFW_allowDND stil must be passed to the window)*/
RGFWDEF void RGFW_window_setDND(RGFW_window* win, b8 allow);
#ifndef RGFW_NO_PASSTHROUGH
/*!! turn on / off mouse passthrough */
RGFWDEF void RGFW_window_setMousePassthrough(RGFW_window* win, b8 passthrough);
#endif
/*! rename window to a given string */
RGFWDEF void RGFW_window_setName(RGFW_window* win,
char* name
);
RGFWDEF void RGFW_window_setIcon(RGFW_window* win, /*!< source window */
u8* icon /*!< icon bitmap */,
RGFW_area a /*!< width and height of the bitmap*/,
i32 channels /*!< how many channels the bitmap has (rgb : 3, rgba : 4) */
); /*!< image resized by default */
/*!< sets mouse to bitmap (very simular to RGFW_window_setIcon), image NOT resized by default*/
RGFWDEF void RGFW_window_setMouse(RGFW_window* win, u8* image, RGFW_area a, i32 channels);
/*!< sets the mouse to a standard API cursor (based on RGFW_MOUSE, as seen at the end of the RGFW_HEADER part of this file) */
RGFWDEF void RGFW_window_setMouseStandard(RGFW_window* win, u8 mouse);
RGFWDEF void RGFW_window_setMouseDefault(RGFW_window* win); /*!< sets the mouse to the default mouse icon */
/*
Locks cursor at the center of the window
win->event.point become raw mouse movement data
this is useful for a 3D camera
*/
RGFWDEF void RGFW_window_mouseHold(RGFW_window* win, RGFW_area area);
/*! stop holding the mouse and let it move freely */
RGFWDEF void RGFW_window_mouseUnhold(RGFW_window* win);
/*! hide the window */
RGFWDEF void RGFW_window_hide(RGFW_window* win);
/*! show the window */
RGFWDEF void RGFW_window_show(RGFW_window* win);
/*
makes it so `RGFW_window_shouldClose` returns true
by setting the window event.type to RGFW_quit
*/
RGFWDEF void RGFW_window_setShouldClose(RGFW_window* win);
/*! where the mouse is on the screen */
RGFWDEF RGFW_point RGFW_getGlobalMousePoint(void);
/*! where the mouse is on the window */
RGFWDEF RGFW_point RGFW_window_getMousePoint(RGFW_window* win);
/*! show the mouse or hide the mouse*/
RGFWDEF void RGFW_window_showMouse(RGFW_window* win, i8 show);
/*! move the mouse to a set x, y pos*/
RGFWDEF void RGFW_window_moveMouse(RGFW_window* win, RGFW_point v);
/*! if the window should close (RGFW_close was sent or escape was pressed) */
RGFWDEF b8 RGFW_window_shouldClose(RGFW_window* win);
/*! if window is fullscreen'd */
RGFWDEF b8 RGFW_window_isFullscreen(RGFW_window* win);
/*! if window is hidden */
RGFWDEF b8 RGFW_window_isHidden(RGFW_window* win);
/*! if window is minimized */
RGFWDEF b8 RGFW_window_isMinimized(RGFW_window* win);
/*! if window is maximized */
RGFWDEF b8 RGFW_window_isMaximized(RGFW_window* win);
/** @} */
/** * @defgroup Monitor
* @{ */
#ifndef RGFW_NO_MONITOR
/*
scale the window to the monitor,
this is run by default if the user uses the arg `RGFW_scaleToMonitor` during window creation
*/
RGFWDEF void RGFW_window_scaleToMonitor(RGFW_window* win);
/*! get the struct of the window's monitor */
RGFWDEF RGFW_monitor RGFW_window_getMonitor(RGFW_window* win);
#endif
/** @} */
/** * @defgroup Input
* @{ */
/*! if window == NULL, it checks if the key is pressed globally. Otherwise, it checks only if the key is pressed while the window in focus.*/
RGFWDEF b8 RGFW_isPressed(RGFW_window* win, u8 key); /*!< if key is pressed (key code)*/
RGFWDEF b8 RGFW_wasPressed(RGFW_window* win, u8 key); /*!< if key was pressed (checks previous state only) (key code)*/
RGFWDEF b8 RGFW_isHeld(RGFW_window* win, u8 key); /*!< if key is held (key code)*/
RGFWDEF b8 RGFW_isReleased(RGFW_window* win, u8 key); /*!< if key is released (key code)*/
/* if a key is pressed and then released, pretty much the same as RGFW_isReleased */
RGFWDEF b8 RGFW_isClicked(RGFW_window* win, u8 key /*!< key code*/);
/*! if a mouse button is pressed */
RGFWDEF b8 RGFW_isMousePressed(RGFW_window* win, u8 button /*!< mouse button code */ );
/*! if a mouse button is held */
RGFWDEF b8 RGFW_isMouseHeld(RGFW_window* win, u8 button /*!< mouse button code */ );
/*! if a mouse button was released */
RGFWDEF b8 RGFW_isMouseReleased(RGFW_window* win, u8 button /*!< mouse button code */ );
/*! if a mouse button was pressed (checks previous state only) */
RGFWDEF b8 RGFW_wasMousePressed(RGFW_window* win, u8 button /*!< mouse button code */ );
/** @} */
/** * @defgroup Clipboard
* @{ */
RGFWDEF char* RGFW_readClipboard(size_t* size); /*!< read clipboard data */
RGFWDEF void RGFW_clipboardFree(char* str); /*!< the string returned from RGFW_readClipboard must be freed */
RGFWDEF void RGFW_writeClipboard(const char* text, u32 textLen); /*!< write text to the clipboard */
/** @} */
/**
Event callbacks,
these are completely optional, you can use the normal
RGFW_checkEvent() method if you prefer that
* @defgroup Callbacks
* @{
*/
/*! RGFW_windowMoved, the window and its new rect value */
typedef void (* RGFW_windowmovefunc)(RGFW_window* win, RGFW_rect r);
/*! RGFW_windowResized, the window and its new rect value */
typedef void (* RGFW_windowresizefunc)(RGFW_window* win, RGFW_rect r);
/*! RGFW_quit, the window that was closed */
typedef void (* RGFW_windowquitfunc)(RGFW_window* win);
/*! RGFW_focusIn / RGFW_focusOut, the window who's focus has changed and if its inFocus */
typedef void (* RGFW_focusfunc)(RGFW_window* win, b8 inFocus);
/*! RGFW_mouseEnter / RGFW_mouseLeave, the window that changed, the point of the mouse (enter only) and if the mouse has entered */
typedef void (* RGFW_mouseNotifyfunc)(RGFW_window* win, RGFW_point point, b8 status);
/*! RGFW_mousePosChanged, the window that the move happened on and the new point of the mouse */
typedef void (* RGFW_mouseposfunc)(RGFW_window* win, RGFW_point point);
/*! RGFW_dnd_init, the window, the point of the drop on the windows */
typedef void (* RGFW_dndInitfunc)(RGFW_window* win, RGFW_point point);
/*! RGFW_windowRefresh, the window that needs to be refreshed */
typedef void (* RGFW_windowrefreshfunc)(RGFW_window* win);
/*! RGFW_keyPressed / RGFW_keyReleased, the window that got the event, the mapped key, the physical key, the string version, the state of mod keys, if it was a press (else it's a release) */
typedef void (* RGFW_keyfunc)(RGFW_window* win, u32 key, u32 mappedKey, u8 lockState, b8 pressed);
/*! RGFW_mouseButtonPressed / RGFW_mouseButtonReleased, the window that got the event, the button that was pressed, the scroll value, if it was a press (else it's a release) */
typedef void (* RGFW_mousebuttonfunc)(RGFW_window* win, u8 button, double scroll, b8 pressed);
/*!gamepad /gamepad, the window that got the event, the button that was pressed, the scroll value, if it was a press (else it's a release) */
typedef void (* RGFW_gamepadButtonfunc)(RGFW_window* win, u16 gamepad, u8 button, b8 pressed);
/*! RGFW_gamepadAxisMove, the window that got the event, the gamepad in question, the axis values and the amount of axises */
typedef void (* RGFW_gamepadAxisfunc)(RGFW_window* win, u16 gamepad, RGFW_point axis[2], u8 axisesCount, u8 whichAxis);
/*! RGFW_gamepadConnected/RGFW_gamepadDisconnected, the window that got the event, the gamepad in question, if the controller was connected (or disconnected if false) */
typedef void (* RGFW_gamepadfunc)(RGFW_window* win, u16 gamepad, b8 connected);
/*! RGFW_dnd, the window that had the drop, the drop data and the amount files dropped returns previous callback function (if it was set) */
#ifdef RGFW_ALLOC_DROPFILES
typedef void (* RGFW_dndfunc)(RGFW_window* win, char** droppedFiles, u32 droppedFilesCount);
#else
typedef void (* RGFW_dndfunc)(RGFW_window* win, char droppedFiles[RGFW_MAX_DROPS][RGFW_MAX_PATH], u32 droppedFilesCount);
#endif
/*! set callback for a window move event returns previous callback function (if it was set) */
RGFWDEF RGFW_windowmovefunc RGFW_setWindowMoveCallback(RGFW_windowmovefunc func);
/*! set callback for a window resize event returns previous callback function (if it was set) */
RGFWDEF RGFW_windowresizefunc RGFW_setWindowResizeCallback(RGFW_windowresizefunc func);
/*! set callback for a window quit event returns previous callback function (if it was set) */