-
Notifications
You must be signed in to change notification settings - Fork 1
/
RSGL.h
2379 lines (1862 loc) · 78.7 KB
/
RSGL.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) 2021-24 ColleagueRiley [email protected]
*
* 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.
*
*
*/
/*
define args
(MAKE SURE RSGL_IMPLEMENTATION is in exactly one header or you use -DRSGL_IMPLEMENTATION)
#define RSGL_IMPLEMENTATION - makes it so source code is included with header
#define RSGL_NO_TEXT - do not include text rendering functions
#define RSGL_NO_WIDGETS - do not include widgets
#define RSGL_NO_SAVE_IMAGE - do not save/load images (don't use RSGL_drawImage if you use this),
RSGL_drawImage saves the file name + texture so it can load it
when you ask for it later. This disables that
#define RSGL_INIT_FONTS [number of fonts] - set hFow much room should be pre-allocated for fonts by fontstash
this avoids performance issues related to RSGL_REALLOC
RSGL_INIT_FONTS = 4 by default
#define RSGL_INIT_IMAGES [number of fonts] - set how much room should be pre-allocated for images by RSGL
this avoids performance issues related to RSGL_REALLOC
RSGL_INIT_IMAGES = 20 by default
#define RSGL_NEW_IMAGES [number of fonts] - set how much room should be RSGL_REALLOCated at a time for images by RSGL
this avoids performance issues related to RSGL_REALLOC
RSGL_NEW_IMAGES 10 by default
#define RSGL_MAX_BATCHES [number of batches] - set max number of batches to be allocated
#define RSGL_MAX_VERTS [number of verts] - set max number of verts to be allocated (global, not per batch)
#define RSGL_RENDER_LEGACY - use legacy rendering (ex. opengl) functions
#define RSGL_NO_STB_IMAGE - do not include stb_image.h (& don't define image loading funcs)
#define RSGL_NO_STB_IMAGE_IMP - declare stb funcs but don't define
#define RSGL_NO_DEPS_FOLDER - Do not use '/deps' for the deps includes, use "./"
*/
#ifndef RSGL_INIT_FONTS
#define RSGL_INIT_FONTS 4
#endif
#ifndef RSGL_NEW_FONTS
#define RSGL_NEW_FONTS 2
#endif
#ifndef RSGL_INIT_IMAGES
#define RSGL_INIT_IMAGES 20
#endif
#ifndef RSGL_NEW_IMAGES
#define RSGL_NEW_IMAGES 10
#endif
#ifndef RSGL_MAX_BATCHES
#define RSGL_MAX_BATCHES 2028
#endif
#ifndef RSGL_MAX_VERTS
#define RSGL_MAX_VERTS 8192
#endif
#ifndef RSGL_MALLOC
#define RSGL_MALLOC malloc
#define RSGL_REALLOC realloc
#define RSGL_FREE free
#endif
#ifndef RSGL_UNUSED
#define RSGL_UNUSED(x) (void) (x);
#endif
/*
RSGL basicDraw types
*/
#ifndef RSGL_QUADS
#define RSGL_POINTS 0x0000
#define RSGL_LINES 0x0001
#define RSGL_LINE_LOOP 0x0002
#define RSGL_LINE_STRIP 0x0003
#define RSGL_TRIANGLES 0x0004
#define RSGL_TRIANGLE_STRIP 0x0005
#define RSGL_TRIANGLE_FAN 0x0006
#define RSGL_QUADS 0x0007
/* these are to ensure GL_DEPTH_TEST is disabled when they're being rendered */
#define RSGL_POINTS_2D 0x0010
#define RSGL_LINES_2D 0x0011
#define RSGL_LINE_LOOP_2D 0x0012
#define RSGL_LINE_STRIP_2D 0x0013
#define RSGL_TRIANGLES_2D 0x0014
#define RSGL_TRIANGLE_STRIP_2D 0x0015
#define RSGL_TRIANGLE_FAN_2D 0x0016
#define RSGL_TRIANGLES_2D_BLEND 0x0114
#endif
#ifndef RSGL_H
#define RSGL_H
#ifndef RSGLDEF
#ifdef __APPLE__
#define RSGLDEF extern inline
#else
#define RSGLDEF inline
#endif
#endif
#if !defined(u8)
#define u8 u8
#if defined(_MSC_VER) || defined(__SYMBIAN32__)
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
#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)
#define b8 b8
typedef u8 b8;
typedef u32 b32;
#endif
#include <stdbool.h>
#include <stddef.h>
#define RSGL_between(x, lower, upper) (((lower) <= (x)) && ((x) <= (upper)))
#define RSGL_ENUM(type, name) type name; enum
/*
*******
RSGL_[shape]
*******
*/
typedef struct RSGL_rect {
i32 x, y, w, h;
} RSGL_rect;
#define RSGL_RECT(x, y, w, h) (RSGL_rect){x, y, w, h}
typedef struct RSGL_rectF { float x, y, w, h; } RSGL_rectF;
#define RSGL_RECTF(x, y, w, h) (RSGL_rectF){x, y, w, h}
typedef struct RSGL_point {
i32 x, y;
} RSGL_point;
#define RSGL_POINT(x, y) (RSGL_point){x, y}
typedef struct RSGL_area {
u32 w, h;
} RSGL_area;
#define RSGL_AREA(w, h) (RSGL_area){w, h}
/*
*******
RSGL_[shape]
*******
*/
typedef struct RSGL_pointF { float x, y; } RSGL_pointF;
#define RSGL_POINTF(x, y) (RSGL_pointF){x, y}
typedef struct RSGL_point3D {
i32 x, y, z;
} RSGL_point3D;
typedef struct RSGL_point3DF { float x, y, z; } RSGL_point3DF;
#define RSGL_POINT3D(x, y, z) (RSGL_point3D){x, y, z}
#define RSGL_POINT3DF(x, y, z) (RSGL_point3DF){x, y, z}
typedef struct RSGL_areaF { float w, h;} RSGL_areaF;
#define RSGL_AREAF(w, h) (RSGL_areaF){w, h}
typedef struct RSGL_circle {
i32 x, y, d;
} RSGL_circle;
#define RSGL_CIRCLE(x, y, d) (RSGL_circle){x, y, d}
typedef struct RSGL_circleF { float x, y, d; } RSGL_circleF;
#define RSGL_CIRCLEF(x, y, d) (RSGL_circleF){x, y, d}
typedef struct RSGL_triangle {
RSGL_point p1, p2, p3;
} RSGL_triangle;
#define RSGL_TRIANGLE(p1, p2, p3) (RSGL_triangle){p1, p2, p3}
typedef struct RSGL_triangleF { RSGL_pointF p1, p2, p3; } RSGL_triangleF;
#define RSGL_TRIANGLEF(p1, p2, p3) (RSGL_triangleF){p1, p2, p3}
#define RSGL_createTriangle(x1, y1, x2, y2, x3, y3) (RSGL_triangle){{x1, y1}, {x2, y2}, {x3, y3}}
#define RSGL_createTriangleF(x1, y1, x2, y2, x3, y3) (RSGL_triangleF){{x1, y1}, {x2, y2}, {x3, y3}}
typedef struct RSGL_cube {
i32 x, y, z, w, h, l;
} RSGL_cube;
#define RSGL_CUBE(x, y, z, w, h, l) (RSGL_cube){x, y, z, w, h, l}
typedef struct RSGL_cubeF { float x, y, z, w, h, l; } RSGL_cubeF;
#define RSGL_CUBEF(x, y, z, w, h, l) (RSGL_cubeF){x, y, z, w, h, l}
/*
the color stucture is in
ABGR by default for performance reasons
(converting color to hex for example)
*/
typedef struct RSGL_color {
u8 a, b, g, r;
} RSGL_color;
#define RSGL_RGBA(r, g, b, a) ((RSGL_color){(a), (b), (g), (r)})
#define RSGL_RGB(r, g, b) ((RSGL_color){255, (b), (g), (r)})
#define RSGL_COLOR_TO_HEX(color) ((u32)(color) & 0xFFFFFF00)
#define RSGL_RGB_TO_HEX(r, g, b, a) (RSGL_COLOR_TO_HEX(RSGL_RGBA(r, g, b, a)))
#define RSGL_RGBA_TO_HEX(r, g, b) (RSGL_COLOR_TO_HEX(RSGL_RGB(r, g, b, a)))
/* toggle the use of legacy OpenGL, on by default unless it fails to load */
RSGLDEF void RSGL_legacy(i32 legacy);
/*
******************
RSGL_BACKEND
this stuff is used by RSGL for abstracting backends
or used by backend modules
******************
*/
typedef RSGL_ENUM(u8, RSGL_event_types) {
/*! event codes */
RSGL_keyPressed = 1, /* a key has been pressed */
RSGL_keyReleased, /*!< a key has been released*/
RSGL_mouseButtonPressed, /*!< a mouse button has been pressed (left,middle,right)*/
RSGL_mouseButtonReleased, /*!< a mouse button has been released (left,middle,right)*/
RSGL_mousePosChanged, /*!< the position of the mouse has been changed*/
};
typedef RSGL_ENUM(u8, RSGL_mouseCodes) {
RSGL_mouseLeft = 1, /*!< left mouse button is pressed*/
RSGL_mouseMiddle = 2, /*!< mouse-wheel-button is pressed*/
RGL_mouseRight = 3, /*!< right mouse button is pressed*/
RSGL_mouseScrollUp = 4, /*!< mouse wheel is scrolling up*/
RSGL_mouseScrollDown = 5 /*!< mouse wheel is scrolling down*/
};
typedef RSGL_ENUM(u8, RSGL_mouseIcons) {
RSGL_MOUSE_ARROW = 1,
RSGL_MOUSE_IBEAM = 2,
RSGL_MOUSE_POINTING_HAND = 4,
};
typedef RSGL_ENUM(u8, RSGL_keys) {
RSGL_key_none = 0,
RSGL_Up,
RSGL_Down,
RSGL_Left,
RSGL_Right,
RSGL_Tab,
RSGL_BackSpace,
RSGL_last_key
};
typedef struct {
u32 type; /*!< event type */
RSGL_point point; /*!< cursor point */
u8 button; /*!< which mouse button has been clicked (0) left (1) middle (2) right OR which joystick button was pressed*/
double scroll; /*!< the raw mouse scroll value */
u32 keyCode; /*!< code of key pressed */
char ch; /*!< key char */
} RSGL_Event;
/*!< it checks if the key is pressed globally */
RSGLDEF b8 RSGL_isPressed(u32 key); /*!< if key is pressed (key code)*/
RSGLDEF b8 RSGL_isReleased(u32 key); /*!< if key is released (key code)*/
/*!, if mouse button is pressed globally */
RSGLDEF b8 RSGL_isMousePressed(u32 key);
RSGLDEF b8 RSGL_isMouseReleased(u32 key);
/*!< get current mouse point */
RSGLDEF RSGL_point RSGL_getMousePoint(void);
/*!< send event to RSGL (should be used by the platform in RSGL_checkEvent ) */
RSGLDEF void RSGL_sendEvent(void* userPtr, RSGL_Event event);
/*! these functions are to be defined by the backend */
/*!< sets the mouse to an standard mouse */
RSGLDEF void RSGL_setMouseStandard(void* userPtr, u32 mouse);
/*! mark released keys as not released after its frame is done (should be used by the platform in RSGL_checkEvent */
RSGLDEF void RSGL_clearReleased(void);
/*!< check events and update internal data for event *
* returns true if there was an event */
RSGLDEF b8 RSGL_checkEvent(void* userPtr);
/*
*********************
RSGL_GRAPHICS_CONTEXT
*********************
*/
RSGLDEF void RSGL_init(
RSGL_area r, /* graphics context size */
void* loader /* opengl prozc address ex. wglProcAddress */
);
RSGLDEF void RSGL_updateSize(RSGL_area r);
RSGLDEF void RSGL_clear(RSGL_color c);
RSGLDEF void RSGL_free(void);
/*
*******
RSGL_draw
*******
*/
/*
RSGL_draw args
RSGL has internal args which control how RSGL draws certain things
by default these args clear after each RSGL_draw<whatever> call
but you can run RSGL_setClearArgs to enable or disable this behavior
you can also run RSGL_clearArgs to clear the args by hand
*/
/* RSGL_args */
typedef struct RSGL_drawArgs {
float* gradient; /* does not allocate any memory */
u32 texture;
u32 gradient_len;
RSGL_rect currentRect; /* size of current surface */
RSGL_point3D rotate;
bool fill;
RSGL_point3DF center;
float lineWidth;
i32 legacy;
u32 program;
} RSGL_drawArgs;
RSGLDEF void RSGL_rotate(RSGL_point3D rotate); /* apply rotation to drawing */
RSGLDEF void RSGL_setTexture(u32 texture); /* apply texture to drawing */
RSGLDEF void RSGL_setProgram(u32 program); /* use shader program for drawing */
RSGLDEF void RSGL_setGradient(
float* gradient, /* array of gradients */
size_t len /* length of array */
); /* apply gradient to drawing, based on color list*/
RSGLDEF void RSGL_fill(bool fill); /* toggle filling, if fill is false it runs RSGL_draw<whatever>_outline instead */
RSGLDEF void RSGL_center(RSGL_point3DF center); /* the center of the drawing (or shape), this is used for rotation */
/* args clear after a draw function by default, this toggles that */
RSGLDEF void RSGL_setClearArgs(bool clearArgs); /* toggles if args are cleared by default or not */
RSGLDEF void RSGL_clearArgs(void); /* clears the args */
/* calculate the align a smaller rect with larger rect */
typedef RSGL_ENUM(u8, RSGL_alignment) {
RSGL_ALIGN_NONE = (1 << 0),
/* horizontal */
RSGL_ALIGN_LEFT = (1 << 1),
RSGL_ALIGN_CENTER = (1 << 2),
RSGL_ALIGN_RIGHT = (1 << 3),
/* vertical */
RSGL_ALIGN_UP = (1 << 4),
RSGL_ALIGN_MIDDLE = (1 << 5),
RSGL_ALIGN_DOWN = (1 << 6),
RSGL_ALIGN_HORIZONTAL = RSGL_ALIGN_LEFT | RSGL_ALIGN_CENTER | RSGL_ALIGN_RIGHT,
RSGL_ALIGN_VERTICAL = RSGL_ALIGN_UP | RSGL_ALIGN_MIDDLE | RSGL_ALIGN_DOWN,
/* ex : alignment = (RSGL_ALIGN_LEFT | RSGL_ALIGN_MIDDLE) */
};
/* align smaller rect onto larger rect based on a given alignment */
RSGLDEF RSGL_rect RSGL_alignRect(RSGL_rect larger, RSGL_rect smaller, u16 alignment);
RSGLDEF RSGL_rectF RSGL_alignRectF(RSGL_rectF larger, RSGL_rectF smaller, u16 alignment);
#ifndef RSGL_GET_WORLD_X
#define RSGL_GET_WORLD_X(x) (float)(2.0f * (x) / RSGL_args.currentRect.w - 1.0f)
#define RSGL_GET_WORLD_Y(y) (float)(1.0f + -2.0f * (y) / RSGL_args.currentRect.h)
#define RSGL_GET_WORLD_Z(z) (float)(z)
#endif
#define RSGL_GET_MATRIX_X(x, y, z) (matrix.m[0] * x + matrix.m[4] * y + matrix.m[8] * z + matrix.m[12])
#define RSGL_GET_MATRIX_Y(x, y, z) (matrix.m[1] * x + matrix.m[5] * y + matrix.m[9] * z + matrix.m[13])
#define RSGL_GET_MATRIX_Z(x, y, z) (matrix.m[2] * x + matrix.m[6] * y + matrix.m[10] * z + matrix.m[14])
#define RSGL_GET_MATRIX_POINT(x, y, z) RSGL_GET_MATRIX_X(x, y, z), RSGL_GET_MATRIX_Y(x, y, z), RSGL_GET_MATRIX_Z(x, y, z)
#define RSGL_GET_WORLD_POINT(x, y, z) RSGL_GET_WORLD_X((x)), RSGL_GET_WORLD_Y((y)), RSGL_GET_WORLD_Z((z))
#define RSGL_GET_FINAL_POINT(x, y, z) RSGL_GET_MATRIX_POINT(RSGL_GET_WORLD_X((x)), RSGL_GET_WORLD_Y((y)), RSGL_GET_WORLD_Z((z)))
typedef struct RSGL_MATRIX {
float m[16];
} RSGL_MATRIX;
RSGLDEF RSGL_MATRIX RSGL_initDrawMatrix(RSGL_point3DF center);
/*
RSGL_basicDraw is a function used internally by RSGL, but you can use it yourself
RSGL_basicDraw batches a given set of points based on the data to be rendered
*/
RSGLDEF void RSGL_basicDraw(
u32 TYPE, /* type of shape, RSGL_QUADS, RSGL_TRIANGLES, RSGL_LINES, RSGL_QUADS_2D */
float* points, /* array of 3D points */
float* texPoints, /* array of 2D texture points (must be same length as points)*/
RSGL_color c, /* the color to draw the shape */
size_t len /* the length of the points array */
);
typedef struct RSGL_BATCH {
size_t start, len; /* when batch starts and it's length */
u32 type, tex;
float lineWidth;
} RSGL_BATCH; /* batch data type for rendering */
typedef struct RSGL_RENDER_INFO {
RSGL_BATCH* batches;
float* verts;
float* texCoords;
float* colors;
size_t len; /* number of batches*/
size_t vert_len; /* number of verts */
} RSGL_RENDER_INFO; /* render data */
/*
All of these functions are to be defined by the external render backend
*/
/* renders the current batches */
RSGLDEF void RSGL_renderBatch(RSGL_RENDER_INFO* info);
RSGLDEF void RSGL_renderInit(void* proc, RSGL_RENDER_INFO* info); /* init render backend */
RSGLDEF void RSGL_renderFree(void); /* free render backend */
RSGLDEF void RSGL_renderClear(float r, float g, float b, float a);
RSGLDEF void RSGL_renderViewport(i32 x, i32 y, i32 w, i32 h);
/* create a texture based on a given bitmap, this must be freed later using RSGL_deleteTexture or opengl*/
RSGLDEF u32 RSGL_renderCreateTexture(u8* bitmap, RSGL_area memsize, u8 channels);
/* updates an existing texture wiht a new bitmap */
RSGLDEF void RSGL_renderUpdateTexture(u32 texture, u8* bitmap, RSGL_area memsize, u8 channels);
/* delete a texture */
RSGLDEF void RSGL_renderDeleteTexture(u32 tex);
/* custom shader program */
typedef struct RSGL_programInfo {
u32 vShader, fShader, program;
} RSGL_programInfo;
RSGLDEF RSGL_programInfo RSGL_renderCreateProgram(const char* VShaderCode, const char* FShaderCode, char* posName, char* texName, char* colorName);
RSGLDEF void RSGL_renderDeleteProgram(RSGL_programInfo program);
RSGLDEF void RSGL_renderSetShaderValue(u32 program, char* var, float value[], u8 len);
/* these are RFont functions that also must be defined by the renderer
32 RFont_create_atlas(u32 atlasWidth, u32 atlasHeight);
void RFont_bitmap_to_atlas(u32 atlas, u8* bitmap, float x, float y, float w, float h);
*/
/* RSGL translation */
RSGLDEF RSGL_MATRIX RSGL_matrixMultiply(float left[16], float right[16]);
RSGLDEF RSGL_MATRIX RSGL_rotatef(RSGL_MATRIX* matrix, float angle, float x, float y, float z);
RSGLDEF RSGL_MATRIX RSGL_translatef(RSGL_MATRIX* matrix, float x, float y, float z);
/* 2D shape drawing */
/* in the function names, F means float */
RSGLDEF void RSGL_drawPoint(RSGL_point p, RSGL_color c);
RSGLDEF void RSGL_drawPointF(RSGL_pointF p, RSGL_color c);
RSGLDEF void RSGL_plotLines(RSGL_pointF* lines, size_t points_count, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawTriangle(RSGL_triangle t, RSGL_color c);
RSGLDEF void RSGL_drawTriangleF(RSGL_triangleF t, RSGL_color c);
RSGLDEF void RSGL_drawTriangleHyp(RSGL_pointF p, size_t angle, float hypotenuse, RSGL_color color);
RSGLDEF void RSGL_drawRect(RSGL_rect r, RSGL_color c);
RSGLDEF void RSGL_drawRectF(RSGL_rectF r, RSGL_color c);
RSGLDEF void RSGL_drawRoundRect(RSGL_rect r, RSGL_point rounding, RSGL_color c);
RSGLDEF void RSGL_drawRoundRectF(RSGL_rectF r, RSGL_point rounding, RSGL_color c);
RSGLDEF void RSGL_drawPolygon(RSGL_rect r, u32 sides, RSGL_color c);
RSGLDEF void RSGL_drawPolygonF(RSGL_rectF r, u32 sides, RSGL_color c);
RSGLDEF void RSGL_drawArc(RSGL_rect o, RSGL_point arc, RSGL_color color);
RSGLDEF void RSGL_drawArcF(RSGL_rectF o, RSGL_pointF arc, RSGL_color color);
RSGLDEF void RSGL_drawCircle(RSGL_circle c, RSGL_color color);
RSGLDEF void RSGL_drawCircleF(RSGL_circleF c, RSGL_color color);
RSGLDEF void RSGL_drawOval(RSGL_rect o, RSGL_color c);
RSGLDEF void RSGL_drawOvalF(RSGL_rectF o, RSGL_color c);
RSGLDEF void RSGL_drawLine(RSGL_point p1, RSGL_point p2, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawLineF(RSGL_pointF p1, RSGL_pointF p2, u32 thickness, RSGL_color c);
/* 2D outlines */
/* thickness means the thickness of the line */
RSGLDEF void RSGL_drawTriangleOutline(RSGL_triangle t, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawTriangleFOutline(RSGL_triangleF t, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawRectOutline(RSGL_rect r, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawRectFOutline(RSGL_rectF r, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawRoundRectOutline(RSGL_rect r, RSGL_point rounding, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawRoundRectFOutline(RSGL_rectF r, RSGL_point rounding, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawPolygonOutline(RSGL_rect r, u32 sides, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawPolygonFOutline(RSGL_rectF r, u32 sides, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawArcOutline(RSGL_rect o, RSGL_point arc, u32 thickness, RSGL_color color);
RSGLDEF void RSGL_drawArcFOutline(RSGL_rectF o, RSGL_pointF arc, u32 thickness, RSGL_color color);
RSGLDEF void RSGL_drawCircleOutline(RSGL_circle c, u32 thickness, RSGL_color color);
RSGLDEF void RSGL_drawCircleFOutline(RSGL_circleF c, u32 thickness, RSGL_color color);
RSGLDEF void RSGL_drawOvalFOutline(RSGL_rectF o, u32 thickness, RSGL_color c);
RSGLDEF void RSGL_drawOvalOutline(RSGL_rect o, u32 thickness, RSGL_color c);
/* format a string */
#ifndef RSGL_NO_TEXT
RSGLDEF const char* RFont_fmt(const char* string, ...);
#define RSGL_strFmt RFont_fmt
/* loads a font into RSGL, returns it's index into the RSGL_fonts array, this is used as an id in later functions */
RSGLDEF i32 RSGL_loadFont(const char* font);
/* sets font as the current font in use based on index in RSGL_font, given when it was loaded */
RSGLDEF void RSGL_setFont(i32 font);
/* sets source RFont font as the current font, given when it was loaded */
struct RFont_font;
RSGLDEF void RSGL_setRFont(struct RFont_font* font);
RSGLDEF void RSGL_drawText_len(const char* text, size_t len, RSGL_circle c, RSGL_color color);
RSGLDEF void RSGL_drawText(const char* text, RSGL_circle c, RSGL_color color);
#define RSGL_drawTextF(text, font, c, color) \
RSGL_setFont(font);\
RSGL_drawText(text, c, color);
/* align text onto larger rect based on a given alignment */
RSGLDEF RSGL_circle RSGL_alignText(const char* str, RSGL_circle c, RSGL_rectF larger, u8 alignment);
/* align text based on a length */
RSGLDEF RSGL_circle RSGL_alignText_len(const char* str, size_t str_len, RSGL_circle c, RSGL_rectF larger, u8 alignment);
/*
returns the width of a text when rendered with the set font with the size of `fontSize
stops at `textEnd` or when it reaches '\0'
*/
RSGLDEF RSGL_area RSGL_textArea(const char* text, u32 fontSize, size_t textEnd);
RSGLDEF RSGL_area RSGL_textLineArea(const char* text, u32 fontSize, size_t textEnd, size_t line);
#define RSGL_textAreaF(text, fontSize, textEnd) \
RSGL_setFont(font);\
RSGL_textAreaF(text, fontSize, textEnd);
#endif /* RSGL_NO_TEXT */
/*
this creates a texture based on a given image, draws it on a rectangle and then returns the loaded texture
if the rectangle's width and height are 0 it doesn't draw the image
the texture is loaded into RSGL_image, this means it doesn't need to be freed
but you can still free it early
*/
typedef struct RSGL_image { u32 tex; RSGL_area srcSize; char file[255]; } RSGL_image;
RSGLDEF RSGL_image RSGL_drawImage(const char* image, RSGL_rect r);
#define RSGL_loadImage(image) ((RSGL_image) RSGL_drawImage(image, (RSGL_rect){0, 0, 0, 0}))
/*
these two functions can be used before RSGL_renderCreateTexture in order to create
an swizzle'd texutre or atlas
*/
/*
*******
RSGL_widgets
*******
*/
#ifndef RSGL_NO_WIDGETS
typedef RSGL_ENUM(u8, RSGL_widgetState) {
RSGL_STATE_NONE = 0,
RSGL_HOVERED = 1,
RSGL_PRESSED = 2,
RSGL_RELEASED = 3,
};
typedef RSGL_ENUM(u32, RSGL_widgetStyle) {
RSGL_STYLE_NONE = 0,
RSGL_OFFSET_X = (1 << 1), // the next widget will be offset by this widget's width
RSGL_OFFSET_Y = (1 << 2), // the next widget will be offset by this widget's height
RSGL_OFFSET_MODE = RSGL_OFFSET_X | RSGL_OFFSET_Y,
RSGL_STYLE_ROUND = (1 << 3), // draw button as a rounded rectangle
RSGL_SHAPE_POLYGON = (1 << 4), // draw a polygon instead of a rect
RSGL_NO_SHAPE = (1 << 5), // do not render the widget
RSGL_SHAPE_INFO = RSGL_STYLE_ROUND | RSGL_SHAPE_POLYGON | RSGL_NO_SHAPE,
/*
if either of these flags are enabled
RSGL's default style system is used
*/
RSGL_STYLE_DARK = (1L << 6), /* dark mode*/
RSGL_STYLE_LIGHT = (1L << 7), /* light mode*/
RSGL_STYLE_MODE = RSGL_STYLE_LIGHT | RSGL_STYLE_DARK,
RSGL_NO_TITLE = (1L << 8), /* do not draw cantainer handle */
RSGL_NO_CONTAINER = (1L << 9), /* do not draw container */
RSGL_SLIDER_CIRCLE = (1L << 10),
RSGL_STYLE_RED = (1 << 18),
RSGL_STYLE_BLUE = (1 << 19),
RSGL_STYLE_GREEN = (1 << 20),
RSGL_STYLE_YELLOW = (1 << 21),
RSGL_STYLE_TEAL = (1 << 22),
RSGL_STYLE_PURPLE = (1 << 23),
RSGL_STYLE_COLOR = RSGL_STYLE_RED | RSGL_STYLE_BLUE | RSGL_STYLE_GREEN | RSGL_STYLE_YELLOW | RSGL_STYLE_TEAL | RSGL_STYLE_PURPLE
};
RSGLDEF void RSGL_openBlankContainer(RSGL_rect r);
RSGLDEF void RSGL_openContainer(const char* name, RSGL_rect* r, RSGL_color background, u32 args, b8* grabbed, b8* toggle);
RSGLDEF void RSGL_widgetOutline(u32 outline, RSGL_color idle, RSGL_color hover, RSGL_color press, RSGL_color release);
RSGLDEF void RSGL_widgetColor(RSGL_color idle, RSGL_color hover, RSGL_color press, RSGL_color release);
RSGLDEF void RSGL_widgetPolygonPoints(u32 points);
RSGLDEF void RSGL_widgetRounding(RSGL_point point);
RSGLDEF void RSGL_widgetAlign(u8 align);
RSGLDEF void RSGL_evalTheme(RSGL_widgetStyle buttonStyle);
RSGLDEF void RSGL_scaleRect(RSGL_rectF* rect, RSGL_widgetStyle args);
RSGLDEF void RSGL_label(const char* str, RSGL_rectF);
RSGLDEF RSGL_widgetState RSGL_labeledButton(const char* str, RSGL_rectF rect, RSGL_widgetStyle args);
RSGLDEF void RSGL_separator(RSGL_rectF rect);
RSGLDEF void RSGL_openSubContainer(RSGL_rectF rect, u32 args, b8* open);
RSGLDEF void RSGL_closeSubContainer(void);
RSGLDEF RSGL_widgetState RSGL_button(RSGL_rectF rect, RSGL_widgetStyle args);
RSGLDEF b8 RSGL_toggleButton(RSGL_rectF rect, RSGL_widgetStyle args, b8* toggle);
RSGLDEF b8 RSGL_checkbox(RSGL_rectF rect, RSGL_widgetStyle args, b8* checked);
RSGLDEF void RSGL_grab(RSGL_rectF rect, RSGL_widgetStyle args, b8* grabbed);
RSGLDEF void RSGL_slider(RSGL_rectF rect, RSGL_widgetStyle args, float* value, b8* grabbed);
RSGLDEF RSGL_widgetState RSGL_radioButtons(RSGL_rectF rect, u32 count, RSGL_widgetStyle args, u8* selected, u8* index);
RSGLDEF b8 RSGL_combobox(RSGL_rectF rect, u32 args, char* strings[], size_t len, b8* open, size_t* index);
#endif /* RSGL_NO_WIDGETS */
/*
*******
extra
*******
*/
/* ** collision functions ** */
RSGLDEF bool RSGL_circleCollidePoint(RSGL_circle c, RSGL_point p);
RSGLDEF bool RSGL_circleCollideRect(RSGL_circle c, RSGL_rect r);
RSGLDEF bool RSGL_circleCollide(RSGL_circle cir1, RSGL_circle cir2);
RSGLDEF bool RSGL_rectCollidePoint(RSGL_rect r, RSGL_point p);
RSGLDEF bool RSGL_rectCollide(RSGL_rect r, RSGL_rect r2);
RSGLDEF bool RSGL_pointCollide(RSGL_point p, RSGL_point p2);
RSGLDEF bool RSGL_circleCollidePointF(RSGL_circleF c, RSGL_pointF p);
RSGLDEF bool RSGL_circleCollideRectF(RSGL_circleF c, RSGL_rectF r);
RSGLDEF bool RSGL_circleCollideF(RSGL_circleF cir1, RSGL_circleF cir2);
RSGLDEF bool RSGL_rectCollidePointF(RSGL_rectF r, RSGL_pointF p);
RSGLDEF bool RSGL_rectCollideF(RSGL_rectF r, RSGL_rectF r2);
RSGLDEF bool RSGL_pointCollideF(RSGL_pointF p, RSGL_pointF p2);
#endif /* ndef RSGL_H */
/*
(Notes on how to manage Silicon (macos) included)
Example to get you started :
linux : gcc main.c -lX11 -lXcursor -lGL
windows : gcc main.c -lopengl32 -lshell32 -lgdi32
macos:
<Silicon> can be replaced to where you have the Silicon headers stored
<libSilicon.a> can be replaced to wherever you have libSilicon.a
clang main.c -I<Silicon> <libSilicon.a> -framework Foundation -framework AppKit -framework OpenGL -framework CoreVideo
NOTE(EimaMei): If you want the MacOS experience to be fully single header, then I'd be best to install Silicon (after compiling)
by going to the `Silicon` folder and running `make install`. After this you can easily include Silicon via `#include <Silicon/silicon.h>'
and link it by doing `-lSilicon`
(TODO: make new example)
compiling :
if you wish to compile the library all you have to do is create a new file with this in it
RSGL.c
#define RSGL_IMPLEMENTATION
#include "RSGL.h"
then you can use gcc (or whatever compile you wish to use) to compile the library into object file
ex. gcc -c RSGL.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 RSGL.a RSGL.o
shared :
windows:
gcc -shared RSGL.o -lshell32 -lgdi32 -o RSGL.dll
linux:
gcc -shared RSGL.o -lX11 -lXcursor -o RSGL.so
macos:
<Silicon/include> can be replaced to where you have the Silicon headers stored
<libSilicon.a> can be replaced to wherever you have libSilicon.a
gcc -shared RSGL.o -framework Foundation -framework AppKit -framework CoreVideo
ex.
gcc main.c -framework Foundation -framework AppKit -framework CoreVideo
*/
#ifdef RSGL_IMPLEMENTATION
#ifndef RSGL_BACKEND
#include "RSGL_rgfw.h"
#endif
#ifdef RSGL_RENDER_LEGACY
#define RFONT_RENDER_LEGACY
#endif
#define STB_IMAGE_IMPLEMENTATION
#include <assert.h>
#ifndef RSGL_NO_TEXT
#define RFONT_IMPLEMENTATION
#define RFONT_RENDER_LEGACY
#define RFONT_NO_OPENGL
#define RFont_area RSGL_area
#ifndef RSGL_NO_DEPS_FOLDER
#include "deps/RFont.h"
#else
#include "RFont.h"
#endif
#endif /* RSGL_NO_TEXT */
#if !defined(RSGL_NO_STB_IMAGE) && !defined(RSGL_NO_STB_IMAGE_IMP)
#ifndef RSGL_NO_DEPS_FOLDER
#include "deps/stb_image.h"
#else
#include <stb_image.h>
#endif
#endif
#ifdef RSGL_NO_STB_IMAGE_IMP
u8* stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
#endif
#include <time.h>
#include <stdbool.h>
#ifndef RSGL_NO_TEXT
typedef struct RSGL_fontData {
char* name;
RFont_font* f;
} RSGL_fontData;
typedef struct RSGL_fontsData {
RFont_font* f;
RSGL_fontData* fonts;
size_t len;
size_t cap;
} RSGL_fontsData;
RSGL_fontsData RSGL_font = {NULL, NULL, 0, 0};
#endif
RSGL_drawArgs RSGL_args = {NULL, 1, 0, { }, {0, 0, 0}, 1, RSGL_POINT3DF(-1, -1, -1), 1, 0, 0};
bool RSGL_argsClear = false;
RSGL_image* RSGL_images = NULL;
size_t RSGL_images_len = 0;
RSGLDEF bool RSGL_cstr_equal(const char* str, const char* str2);
bool RSGL_cstr_equal(const char* str, const char* str2) {
char* s;
char* s2 = (char*)str2;
for (s = (char*)str; *s && *s2; s++) {
if (*s != *s2)
return false;
s2++;
}
if (*s == '\0' && *s2 == '\0')
return true;
return false;
}
RSGL_rect RSGL_alignRect(RSGL_rect larger, RSGL_rect smaller, u16 alignment) {
RSGL_rectF r = RSGL_alignRectF(
RSGL_RECTF(larger.x, larger.y, larger.w, larger.y),
RSGL_RECTF(smaller.x, smaller.y, smaller.w, smaller.h),
alignment
);
return RSGL_RECT(r.x, r.y, r.w, r.h);
}
RSGL_rectF RSGL_alignRectF(RSGL_rectF larger, RSGL_rectF smaller, u16 alignment) {
RSGL_rectF aligned = smaller;
switch (alignment & RSGL_ALIGN_HORIZONTAL) {
case RSGL_ALIGN_LEFT:
aligned.x = larger.x;
break;
case RSGL_ALIGN_CENTER:
aligned.x = larger.x + ((larger.w - smaller.w) / 2.0);
break;
case RSGL_ALIGN_RIGHT:
aligned.x = (larger.x + larger.w) - smaller.w;
break;
default: break;
}
switch (alignment & RSGL_ALIGN_VERTICAL) {
case RSGL_ALIGN_UP:
aligned.y = larger.y;
break;
case RSGL_ALIGN_MIDDLE:
aligned.y = larger.y + ((larger.h - smaller.h) / 2.0);
break;
case RSGL_ALIGN_DOWN:
aligned.y = (larger.y + larger.h) - smaller.h;
break;
default: break;
}
return aligned;
}
RSGL_MATRIX RSGL_initDrawMatrix(RSGL_point3DF center) {
RSGL_MATRIX matrix = (RSGL_MATRIX) {
{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
}
};
if (RSGL_args.rotate.x || RSGL_args.rotate.y || RSGL_args.rotate.z) {
if (RSGL_args.center.x != -1 && RSGL_args.center.y != -1 && RSGL_args.center.z != -1)
center = RSGL_args.center;
matrix = RSGL_translatef(&matrix, center.x, center.y, center.z);
matrix = RSGL_rotatef(&matrix, RSGL_args.rotate.z, 0, 0, 1);
matrix = RSGL_rotatef(&matrix, RSGL_args.rotate.y, 0, 1, 0);
matrix = RSGL_rotatef(&matrix, RSGL_args.rotate.x, 1, 0, 0);
matrix = RSGL_translatef(&matrix, -center.x, -center.y, -center.z);
}
return matrix;
}
RSGL_RENDER_INFO RSGL_renderInfo = {NULL, NULL, NULL, NULL, 0, 0};
#ifndef RSGL_CUSTOM_RENDER
#include "RSGL_gl.h"
#endif
void RSGL_basicDraw(u32 type, float* points, float* texPoints, RSGL_color c, size_t len) {
if (RSGL_renderInfo.len + 1 >= RSGL_MAX_BATCHES || RSGL_renderInfo.vert_len + len >= RSGL_MAX_VERTS) {
RSGL_renderBatch(&RSGL_renderInfo);
}
RSGL_BATCH* batch = NULL;
if (
RSGL_renderInfo.len == 0 ||
RSGL_renderInfo.batches[RSGL_renderInfo.len - 1].tex != RSGL_args.texture ||
RSGL_renderInfo.batches[RSGL_renderInfo.len - 1].lineWidth != RSGL_args.lineWidth ||
RSGL_renderInfo.batches[RSGL_renderInfo.len - 1].type != type ||
RSGL_renderInfo.batches[RSGL_renderInfo.len - 1].type == RSGL_TRIANGLE_FAN_2D
) {
RSGL_renderInfo.len += 1;
batch = &RSGL_renderInfo.batches[RSGL_renderInfo.len - 1];
batch->start = RSGL_renderInfo.vert_len;
batch->len = 0;
batch->type = type;
batch->tex = RSGL_args.texture;
batch->lineWidth = RSGL_args.lineWidth;
} else {
batch = &RSGL_renderInfo.batches[RSGL_renderInfo.len - 1];
}
if (batch == NULL)
return;
batch->len += len;
memcpy(&RSGL_renderInfo.verts[RSGL_renderInfo.vert_len * 3], points, len * sizeof(float) * 3);
memcpy(&RSGL_renderInfo.texCoords[RSGL_renderInfo.vert_len * 2], texPoints, len * sizeof(float) * 2);
float color[4] = {c.r / 255.0f, c.g / 255.0f, c.b / 255.0f, c.a / 255.0f};
if (RSGL_args.gradient_len && RSGL_args.gradient && (i64)(len - 1) > 0) {
memcpy(&RSGL_renderInfo.colors[RSGL_renderInfo.vert_len * 4], color, sizeof(float) * 4);
memcpy(&RSGL_renderInfo.colors[RSGL_renderInfo.vert_len * 4 + 4], RSGL_args.gradient, (len - 1) * sizeof(float) * 4);
}
else {
size_t i;
for (i = 0; i < len * 4; i += 4)
memcpy(&RSGL_renderInfo.colors[(RSGL_renderInfo.vert_len * 4) + i], color, sizeof(float) * 4);
}
RSGL_renderInfo.vert_len += len;
if (RSGL_argsClear) {
RSGL_setTexture(0);
RSGL_clearArgs();
}
}
void RSGL_legacy(i32 legacy) {
if (RSGL_args.legacy != 2)
RSGL_args.legacy = legacy;
}
/*
********************
RSGL_BACKEND
*********************
*/
typedef struct RSGL_key {
u8 pressed : 1;
u8 released : 1;
} RSGL_key;
RSGL_key RSGL_keyMap[RSGL_last_key] = {{0}};
RSGL_key RSGL_mouseMap[5] = {{0}};
RSGL_point RSGL_mousePoint = {0, 0};
/* where the mouse was when it was pressed, this is used checking if a button was released */
RSGL_point RSGL_mousePressPoint = {0, 0};
b8 RSGL_wasMousePosChanged = 0;
char RSGL_charQueue[20] = {0};
size_t RSGL_charQueue_size = 0;
u8 RSGL_isPressed(u32 key) { return RSGL_keyMap[key].pressed; }
b8 RSGL_isReleased(u32 key) { return RSGL_keyMap[key].released; }
b8 RSGL_isMousePressed(u32 button) { return RSGL_mouseMap[button].pressed; }