-
Notifications
You must be signed in to change notification settings - Fork 16
/
RGFW.h
9015 lines (7051 loc) · 286 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
/*
* Copyright (C) 2023-24 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_PRINT_ERRORS - (optional) makes it so RGFW prints errors 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_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_MALLOC x - choose what function to use to allocate, by default the standard malloc is used
#define RGFW_CALLOC x - choose what function to use to allocate (calloc), by default the standard calloc is used
#define RGFW_FREE x - choose what function to use to allocated memory, by default the standard free is used
#define RGFW_EXPORT - Use when building RGFW
#define RGFW_IMPORT - Use when linking with RGFW (not as a single-header)
#define RGFW_STD_INT - force the use stdint.h (for systems that might not have stdint.h (msvc))
*/
/*
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)
*/
#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_MALLOC
#include <stdlib.h>
#ifndef __USE_POSIX199309
#define __USE_POSIX199309
#endif
#include <time.h>
#define RGFW_MALLOC malloc
#define RGFW_CALLOC calloc
#define RGFW_FREE free
#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
#ifdef __clang__
#define RGFWDEF static inline
#else
#define RGFWDEF inline
#endif
#endif
#ifndef RGFW_ENUM
#define RGFW_ENUM(type, name) type name; enum
#endif
#ifndef RGFW_UNUSED
#define RGFW_UNUSED(x) (void)(x);
#endif
#if defined(__cplusplus) && !defined(__EMSCRIPTEN__)
extern "C" {
#endif
/* makes sure the header file part is only defined once by default */
#ifndef RGFW_HEADER
#define RGFW_HEADER
#if !defined(u8)
#if ((defined(_MSC_VER) || defined(__SYMBIAN32__)) && !defined(RGFW_STD_INT)) /* MSVC might not have stdint.h */
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned short u16;
typedef signed short i16;
typedef unsigned int u32;
typedef signed int i32;
typedef unsigned long u64;
typedef signed 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
#endif
#if !defined(b8) /* RGFW bool type */
typedef u8 b8;
typedef u32 b32;
#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)
#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_WINDOW (WINAPI ONLY, macOS + linux don't need this) */
#endif
/*! Optional arguments for making a windows */
#define RGFW_TRANSPARENT_WINDOW (1L<<9) /*!< the window is transparent (only properly works on X11 and MacOS, although it's although for windows) */
#define RGFW_NO_BORDER (1L<<3) /*!< the window doesn't have border */
#define RGFW_NO_RESIZE (1L<<4) /*!< the window cannot be resized by the user */
#define RGFW_ALLOW_DND (1L<<5) /*!< the window supports drag and drop*/
#define RGFW_HIDE_MOUSE (1L<<6) /*! the window should hide the mouse or not (can be toggled later on) using `RGFW_window_mouseShow*/
#define RGFW_FULLSCREEN (1L<<8) /* the window is fullscreen by default or not */
#define RGFW_CENTER (1L<<10) /*! center the window on the screen */
#define RGFW_OPENGL_SOFTWARE (1L<<11) /*! use OpenGL software rendering */
#define RGFW_COCOA_MOVE_TO_RESOURCE_DIR (1L << 12) /* (cocoa only), move to resource folder */
#define RGFW_SCALE_TO_MONITOR (1L << 13) /* scale the window to the screen */
#define RGFW_NO_INIT_API (1L << 2) /* DO not init an API (mostly for bindings, you should use `#define RGFW_NO_API` in C */
#define RGFW_NO_GPU_RENDER (1L<<14) /* don't render (using the GPU based API)*/
#define RGFW_NO_CPU_RENDER (1L<<15) /* don't render (using the CPU based buffer rendering)*/
#define RGFW_WINDOW_HIDE (1L << 16)/* the window is hidden */
typedef RGFW_ENUM(u8, RGFW_event_types) {
/*! event codes */
RGFW_keyPressed = 1, /* 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.keyCode
!!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_jsButtonPressed, /*!< a joystick button was pressed */
RGFW_jsButtonReleased, /*!< a joystick button was released */
RGFW_jsAxisMove, /*!< an axis of a joystick was moved*/
/*! joystick event note
RGFW_Event.joystick holds which joystick was altered, if any
RGFW_Event.button holds which joystick button was pressed
RGFW_Event.axis holds the data of all the axis
RGFW_Event.axisCount says how many axis there are
*/
RGFW_windowMoved, /*!< the window was moved (by 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) */
#define RGFW_mouseLeft 1 /*!< left mouse button is pressed*/
#define RGFW_mouseMiddle 2 /*!< mouse-wheel-button is pressed*/
#define RGFW_mouseRight 3 /*!< right mouse button is pressed*/
#define RGFW_mouseScrollUp 4 /*!< mouse wheel is scrolling up*/
#define RGFW_mouseScrollDown 5 /*!< 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)
/*! joystick button codes (based on xbox/playstation), you may need to change these values per controller */
#ifndef RGFW_joystick_codes
typedef RGFW_ENUM(u8, RGFW_joystick_codes) {
RGFW_JS_A = 0, /*!< or PS X button */
RGFW_JS_B = 1, /*!< or PS circle button */
RGFW_JS_Y = 2, /*!< or PS triangle button */
RGFW_JS_X = 3, /*!< or PS square button */
RGFW_JS_START = 9, /*!< start button */
RGFW_JS_SELECT = 8, /*!< select button */
RGFW_JS_HOME = 10, /*!< home button */
RGFW_JS_UP = 13, /*!< dpad up */
RGFW_JS_DOWN = 14, /*!< dpad down*/
RGFW_JS_LEFT = 15, /*!< dpad left */
RGFW_JS_RIGHT = 16, /*!< dpad right */
RGFW_JS_L1 = 4, /*!< left bump */
RGFW_JS_L2 = 5, /*!< left trigger*/
RGFW_JS_R1 = 6, /*!< right bumper */
RGFW_JS_R2 = 7, /*!< right trigger */
};
#endif
/*! 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
#ifndef __cplusplus
#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)}
#else
#define RGFW_POINT(x, y) {(i32)(x), (i32)(y)}
#define RGFW_RECT(x, y, w, h) {(i32)(x), (i32)(y), (i32)(w), (i32)(h)}
#define RGFW_AREA(w, h) {(u32)(w), (u32)(h)}
#endif
#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 physW, physH; /*!< monitor physical size */
} 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 {
char keyName[16]; /*!< key name of 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 keyCode; /*!< keycode of event !!Keycodes defined at the bottom of the RGFW_HEADER part of this file!! */
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 button was pressed */
double scroll; /*!< the raw mouse scroll value */
u16 joystick; /*! which joystick this event applies to (if applicable to any) */
u8 axisesCount; /*!< number of axises */
RGFW_point axis[2]; /*!< 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)
u32 display;
void* displayLink;
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;
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(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) */
RGFWDEF RGFW_window* RGFW_createWindow(
const char* name, /* name of the window */
RGFW_rect rect, /* rect of window */
u16 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_NEXT = -1,
RGFW_NO_WAIT = 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_ALLOW_DND 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_SCALE_TO_MONITOR` 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
* @{ */
/*error handling*/
RGFWDEF b8 RGFW_Error(void); /*!< returns true if an error has occurred (doesn't print errors itself) */
/*! returns true if the key should be shifted */
RGFWDEF b8 RGFW_shouldShift(u32 keycode, u8 lockState);
/*! get char from RGFW keycode (using a LUT), uses shift'd version if shift = true */
RGFWDEF char RGFW_keyCodeToChar(u32 keycode, b8 shift);
/*! get char from RGFW keycode (using a LUT), uses lockState for shouldShift) */
RGFWDEF char RGFW_keyCodeToCharAuto(u32 keycode, u8 lockState);
/*! 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 keycode, 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 keycode, char keyName[16], 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);
/*! RGFW_jsButtonPressed / RGFW_jsButtonReleased, 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_jsButtonfunc)(RGFW_window* win, u16 joystick, u8 button, b8 pressed);
/*! RGFW_jsAxisMove, the window that got the event, the joystick in question, the axis values and the amount of axises */
typedef void (* RGFW_jsAxisfunc)(RGFW_window* win, u16 joystick, RGFW_point axis[2], u8 axisesCount);
/*! 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) */
RGFWDEF RGFW_windowquitfunc RGFW_setWindowQuitCallback(RGFW_windowquitfunc func);
/*! set callback for a mouse move event returns previous callback function (if it was set) */
RGFWDEF RGFW_mouseposfunc RGFW_setMousePosCallback(RGFW_mouseposfunc func);
/*! set callback for a window refresh event returns previous callback function (if it was set) */
RGFWDEF RGFW_windowrefreshfunc RGFW_setWindowRefreshCallback(RGFW_windowrefreshfunc func);
/*! set callback for a window focus change event returns previous callback function (if it was set) */
RGFWDEF RGFW_focusfunc RGFW_setFocusCallback(RGFW_focusfunc func);
/*! set callback for a mouse notify event returns previous callback function (if it was set) */
RGFWDEF RGFW_mouseNotifyfunc RGFW_setMouseNotifyCallBack(RGFW_mouseNotifyfunc func);
/*! set callback for a drop event event returns previous callback function (if it was set) */
RGFWDEF RGFW_dndfunc RGFW_setDndCallback(RGFW_dndfunc func);
/*! set callback for a start of a drop event returns previous callback function (if it was set) */
RGFWDEF RGFW_dndInitfunc RGFW_setDndInitCallback(RGFW_dndInitfunc func);
/*! set callback for a key (press / release ) event returns previous callback function (if it was set) */
RGFWDEF RGFW_keyfunc RGFW_setKeyCallback(RGFW_keyfunc func);
/*! set callback for a mouse button (press / release ) event returns previous callback function (if it was set) */
RGFWDEF RGFW_mousebuttonfunc RGFW_setMouseButtonCallback(RGFW_mousebuttonfunc func);
/*! set callback for a controller button (press / release ) event returns previous callback function (if it was set) */
RGFWDEF RGFW_jsButtonfunc RGFW_setjsButtonCallback(RGFW_jsButtonfunc func);
/*! set callback for a joystick axis mov event returns previous callback function (if it was set) */
RGFWDEF RGFW_jsAxisfunc RGFW_setjsAxisCallback(RGFW_jsAxisfunc func);
/** @} */
/** * @defgroup Threads
* @{ */
#ifndef RGFW_NO_THREADS
/*! threading functions*/
/*! NOTE! (for X11/linux) : if you define a window in a thread, it must be run after the original thread's window is created or else there will be a memory error */
/*
I'd suggest you use sili's threading functions instead
if you're going to use sili
which is a good idea generally
*/
#if defined(__unix__) || defined(__APPLE__) || defined(RGFW_WEBASM)
typedef void* (* RGFW_threadFunc_ptr)(void*);
#else
typedef DWORD (__stdcall *RGFW_threadFunc_ptr) (LPVOID lpThreadParameter);
#endif
RGFWDEF RGFW_thread RGFW_createThread(RGFW_threadFunc_ptr ptr, void* args); /*!< create a thread*/
RGFWDEF void RGFW_cancelThread(RGFW_thread thread); /*!< cancels a thread*/
RGFWDEF void RGFW_joinThread(RGFW_thread thread); /*!< join thread to current thread */
RGFWDEF void RGFW_setThreadPriority(RGFW_thread thread, u8 priority); /*!< sets the priority priority */
#endif
/** @} */
/** * @defgroup joystick
* @{ */
/*! joystick count starts at 0*/
/*!< register joystick to window based on a number (the number is based on when it was connected eg. /dev/js0)*/
RGFWDEF u16 RGFW_registerJoystick(RGFW_window* win, i32 jsNumber);
RGFWDEF u16 RGFW_registerJoystickF(RGFW_window* win, char* file);
RGFWDEF u32 RGFW_isPressedJS(RGFW_window* win, u16 controller, u8 button);
/** @} */
/** * @defgroup graphics_API
* @{ */
/*!< make the window the current opengl drawing context
NOTE:
if you want to switch the graphics context's thread,
you have to run RGFW_window_makeCurrent(NULL); on the old thread
then RGFW_window_makeCurrent(valid_window) on the new thread
*/
RGFWDEF void RGFW_window_makeCurrent(RGFW_window* win);
/*< updates fps / sets fps to cap (must by ran manually by the user at the end of a frame), returns current fps */
RGFWDEF u32 RGFW_window_checkFPS(RGFW_window* win, u32 fpsCap);
/* supports openGL, directX, OSMesa, EGL and software rendering */
RGFWDEF void RGFW_window_swapBuffers(RGFW_window* win); /*!< swap the rendering buffer */
RGFWDEF void RGFW_window_swapInterval(RGFW_window* win, i32 swapInterval);
RGFWDEF void RGFW_window_setGPURender(RGFW_window* win, i8 set);
RGFWDEF void RGFW_window_setCPURender(RGFW_window* win, i8 set);
/*! native API functions */
#if defined(RGFW_OPENGL) || defined(RGFW_EGL)
/*! OpenGL init hints */
RGFWDEF void RGFW_setGLStencil(i32 stencil); /*!< set stencil buffer bit size (8 by default) */
RGFWDEF void RGFW_setGLSamples(i32 samples); /*!< set number of sampiling buffers (4 by default) */
RGFWDEF void RGFW_setGLStereo(i32 stereo); /*!< use GL_STEREO (GL_FALSE by default) */
RGFWDEF void RGFW_setGLAuxBuffers(i32 auxBuffers); /*!< number of aux buffers (0 by default) */
/*! which profile to use for the opengl verion */
typedef RGFW_ENUM(u8, RGFW_GL_profile) { RGFW_GL_CORE = 0, RGFW_GL_COMPATIBILITY };
/*! Set OpenGL version hint (core or compatibility profile)*/
RGFWDEF void RGFW_setGLVersion(RGFW_GL_profile profile, i32 major, i32 minor);
RGFWDEF void RGFW_setDoubleBuffer(b8 useDoubleBuffer);
RGFWDEF void* RGFW_getProcAddress(const char* procname); /*!< get native opengl proc address */
RGFWDEF void RGFW_window_makeCurrent_OpenGL(RGFW_window* win); /*!< to be called by RGFW_window_makeCurrent */
#elif defined(RGFW_DIRECTX)
typedef struct {
IDXGIFactory* pFactory;
IDXGIAdapter* pAdapter;
ID3D11Device* pDevice;
ID3D11DeviceContext* pDeviceContext;
} RGFW_directXinfo;
/*
RGFW stores a global instance of RGFW_directXinfo,
you can use this function to get a pointer the instance
*/