/
simplyrend.h
2446 lines (2136 loc) · 76.6 KB
/
simplyrend.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
/*
_____ _ __ ____ __
/ ___/ (_)____ ___ ____ / /__ __ / __ \ ___ ____ ____/ /
\__ \ / // __ `__ \ / __ \ / // / / // /_/ // _ \ / __ \ / __ /
___/ // // / / / / // /_/ // // /_/ // _, _// __// / / // /_/ /
/____//_//_/ /_/ /_// .___//_/ \__, //_/ |_| \___//_/ /_/ \____/
/_/ /____/
v1.2
by: Wassimulator
Using OpenGL Version: 4.3
A simple lightweight immediate mode renderer for 2D graphics, written in C and OpenGL.
The approach is not the most efficient, but it's simple and it should work for small
scale indie purposes. The goal behind this is to help get the program up and running
quickly, while also setting up a foundation for expansion in features later.
"I hate doing this twice", well this is why I made this.
# included libraries in package:
1 - emaths.h - v1.0 or higher by: Wassimulator
2 - stb_rect_pack - v1.01 or higher by: Sean Barrett
3 - stb_image - v2.26 or higher by: Sean Barrett
4 - stb_truetype - v1.26 or higher by: Sean Barrett
# Important notes:
- Include OpenGL headers and load OpenGL functions with glad or otherwise before including this file,
here's a permalink to save you the trouble: https://glad.dav1d.de/#language=c&specification=gl&api=gl%3D4.3&api=gles1%3Dnone&api=gles2%3Dnone&api=glsc2%3Dnone&profile=compatibility&loader=on
- SymplyRend calls gladLoadGL() for you, you don't need to call it manually.
- Call SR_Init() before using any other functions.
- Call SR_StartFrame() at the beginning of each frame.
- SimplyRend does not create window contexts, the user should
handle that with SDL, or otherwise.
- Use floating point color convension (0.0 to 1.0) for each value,
otherwise use SR_Color_i(r, g, b, a) to convert to floating point colors.
- the first two shader programs are reserved for rendering basic shapes
and textured quads.
- FrameWidth and FrameHeight are the draw logic size, not the window size.
They are preferably set at the start and never changed during the program.
- WindwWidth and WindowHeight are the window size, they can be changed at any time.
The distinction is there to allow for window resizing while maintaining realtive
positions.
- you need to handle displaying the frame, whether with SDL or otherwise, Window management
is not included in SimplyRend.
# Usage:
# relevant front end functions:
- SR_Init() // initilizes the library
- SR_StartFrame() // starts a new frame
- SR_LoadFont() // loads a font
- SR_LoadSprite() // loads a sprite
- SR_PackSpritesToTexture() // generates a texture atlas out of a range of sprites
- SR_PushSpriteRect() // draws a sprite
- SR_PushSpriteRectF() // draws a sprite with floating point coordinates
- SR_PushRect_Frame() // draws a framed rectangle
- SR_PushRect_Fill() // draws a filled rectangle
- SR_PushLine() // draws a two point line
- SR_PushLines() // draws a multiple point line
- SR_PushText() // renders text
- SR_PushLayer() // starts a new render layer
- SR_Render() // renders the current frame using all collected data
more info at functions definitions. Search for the names.
- Initialize SimplyRend with SR_Init()
- Before the game loop load all your sprites by declaring SR_Sprite* pointers
and loading them with SR_LoadSprite() to generate sprite info and load to RAM.
- Use SR_PackSpritesToTexture() to pack a range of sprites into a single texture,
be wary of hardware limitations, the function may not pack all sprites.
You can use the function as many times as you want, even to pack single textures.
The texture ID info will be stored in the sprite struct. But unless you plan on
manipulating the behavior, you can ignore this.
- Load fonts at the start of the program with SR_LoadFont()
- Render Functions buffer render data on the RAM and the render Call in the end of the
frame sends it to the GPU. Immediate mode.
Render functions have multiple versions and can accept different parameters.
- custom shader creation is still in development.
All relative functions and structs start with the prefix "SR_"
All Render related functions start with the prefix "SR_Push"
this is till work in progress and bugs may occur, as features get added. It is in a running state though.
*/
///////////////////////////////////////////////////////////////////////////////
// Includes:
#pragma once
#include <stdio.h>
#include <iostream>
#include <emaths.h>
#ifndef STB_IMAGE_STATIC
#define STB_IMAGE_STATIC
#endif
#include <stb_image.h>
#ifndef STB_RECT_PACK_IMPLEMENTATION
#define STB_RECT_PACK_IMPLEMENTATION
#endif
#ifndef STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#endif
#include <stb_rect_pack.h>
#include <stb_truetype.h>
#define STBTT_STATIC
#define SR_SHADER_COLORED 0
#define SR_SHADER_TEXTURED 1
float Z_SHIFT = 0.00001f;
float Z_START = 0.0f;
///////////////////////////////////////////////////////////////////////////////
// Data structures:
typedef unsigned long long SR_uint;
struct SR_Point;
struct SR_PointF;
struct SR_Rect;
struct SR_RectF;
struct SR_Color;
struct SR_Sprite;
struct SR_Sprites;
struct SR_Vertex;
struct SR_ObjectRects;
struct SR_ObjectLines;
struct SR_RenderLayer;
struct SR_RenderLayers;
struct SR_Uniform;
struct SR_Uniforms;
struct SR_Program;
struct SR_Programs;
struct SR_Texture;
struct SR_Textures;
struct SR_Font;
struct SR_Fonts;
struct SR_Debug;
struct SR_Target;
struct SR_Buffers;
typedef struct SR_Point
{
int x;
int y;
} SR_Point;
typedef struct SR_PointF
{
float x;
float y;
SR_PointF(float X, float Y) : x(X), y(Y){};
} SR_PointF;
typedef struct SR_Rect
{
int x, y;
int w, h;
} SR_Rect;
typedef struct SR_RectF
{
float x;
float y;
float w;
float h;
SR_RectF() : x(0.0f), y(0.0f), w(0.0f), h(0.0f){};
SR_RectF(float X, float Y, float W, float H) : x(X), y(Y), w(W), h(H){};
} SR_RectF;
typedef struct SR_Color
{
union
{
float E[4];
struct
{
float r, g, b, a;
};
};
SR_Color() : E{0, 0, 0, 255} {}
SR_Color(float r, float g, float b, float a) : E{r, g, b, a} {}
} SR_Color;
typedef struct SR_Sprite
{
int w, h, n;
int x, y;
SR_Texture *texture;
unsigned char *PixelsRGBA;
SR_Color ModColor;
int frames;
bool animated;
int frame_i = 0;
int ID = 0;
} SR_Sprite;
typedef struct SR_Sprites
{
SR_Sprite *S;
int Count;
} SR_Sprites;
typedef struct SR_Vertex
{
Emaths::v2 P;
Emaths::v2 UV;
SR_Color C;
float Z;
} SR_Vertex;
typedef struct SR_ObjectRects
{
struct glrect
{
SR_Vertex V[6];
};
GLuint VBO = 0;
GLuint VAO = 0;
glrect *R;
int *render_layer_index;
int Count;
} SR_or;
typedef struct SR_ObjectLines
{
struct glline
{
SR_Vertex V[2];
};
GLuint VBO = 0;
GLuint VAO = 0;
glline *L;
int *render_layer_index;
int Count;
} SR_ol;
typedef struct SR_RenderLayer
{
struct scissor
{
bool on = false;
SR_Rect rect;
};
struct postprocess
{
bool on = false;
SR_Target *Origin;
SR_Target *Target;
SR_Program *Program;
};
bool NoAspectFix = false;
bool OneMinusAlpha = false;
bool clear = false;
SR_Color ClearColor;
SR_Target *Target;
scissor Clip;
postprocess PP;
SR_Texture *texture;
} SR_RenderLayer;
typedef struct SR_RenderLayers
{
SR_Texture *last_tex_ptr = nullptr;
bool persistent_target_on = false;
SR_Target *persistent_target;
bool persistent_clip_on = false;
SR_Rect persistent_clip_rect;
SR_RenderLayer *L;
int count;
int index;
} SR_RenderLayers;
typedef enum SR_Uniform_type
{
SR_UNIFORM_INT,
SR_UNIFORM_UINT,
SR_UNIFORM_FLOAT
} SR_Uniform_type;
typedef struct SR_Uniform
{
void *Data;
int Count;
SR_Uniform_type Type;
GLint Location;
SR_uint ID;
} SR_Uniform;
typedef struct SR_Uniforms
{
SR_Uniform *U;
int Count;
} SR_Uniforms;
typedef struct SR_Program
{
SR_uint ID;
GLuint glID;
GLuint Vertex;
GLuint Fragment;
SR_Uniforms Uniforms;
} SR_Program;
typedef struct SR_Programs
{
SR_Program *P;
SR_uint Count;
} SR_Programs;
typedef struct SR_Texture
{
GLuint glID;
SR_uint ID;
} SR_Texture;
typedef struct SR_Textures
{
SR_Texture *T;
SR_uint Count;
} SR_Textures;
typedef struct SR_Font
{
SR_uint ID;
SR_Sprite *Sprite;
int *sizes;
stbtt_packedchar **CharData;
int CharCount;
int SizesCount;
int First;
unsigned char *Pixels;
stbtt_fontinfo Info;
stbtt_pack_context PackContex;
SR_Texture *Atlas;
int W, H;
} SR_Font;
typedef struct SR_Fonts
{
SR_Font *F;
SR_uint Count;
} SR_Fonts;
typedef struct SR_Debug
{
SR_uint buffered_vertex_data_size = 0;
SR_uint estimated_VRAM = 0;
int draw_calls = 0;
} SR_Debug;
typedef struct SR_Target
{
GLuint VBO;
GLuint VAO;
GLuint FBO;
SR_Sprite *Sprite_color;
SR_Sprite *Sprite_depth;
SR_or::glrect Rect;
bool DoNotTouch = false;
int ID;
} SR_Target;
typedef struct SR_Buffers
{
SR_Target *B;
SR_uint Count;
} SR_Buffers;
typedef struct SR_Context
{
SR_uint max_texture_dimension; // corresponds to, but is not necessarily equal to: GL_MAX_TEXTURE_SIZE
SR_ObjectRects Rects;
SR_ObjectRects TexRects;
SR_ObjectLines Lines;
SR_RenderLayers Layers;
SR_Programs Programs;
SR_Textures Textures;
SR_Fonts Fonts;
SR_Sprites Sprites;
SR_Buffers Buffers;
SR_Debug Debug;
int FrameWidth;
int FrameHeight;
int WindowWidth;
int WindowHeight;
float z_index = Z_START;
bool startframe = false;
SR_Color DrawColor;
SR_ObjectRects::glrect ScreenRect;
} SR_Context;
SR_Context simplyrend_context = {};
///////////////////////////////////////////////////////////////////////////////
// Functions:
bool SR_GL_error()
{
int ErrorCode = glGetError();
GLenum Error = glGetError();
if (Error)
{
printf("Encountered OpenGL error code: %d %d\n", ErrorCode, Error);
return true;
}
else
return false;
}
SR_Color SR_Color_i(int r, int g, int b, int a)
{
SR_Color C;
C.r = r / 255.0f;
C.g = g / 255.0f;
C.b = b / 255.0f;
C.a = a / 255.0f;
return C;
}
// creates and pushes a new render program onto the array heap of programs:
SR_Program *SR_CreateProgram(const char *VertexCode, const char *FragmentCode)
{
SR_Context *O = &simplyrend_context;
SR_Program *result = nullptr;
SR_Program *P = &O->Programs.P[O->Programs.Count];
memset(P, 0, sizeof(SR_Program));
int id = O->Programs.Count;
O->Programs.Count++;
GLuint Pipeline = glCreateProgram();
GLuint VertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glAttachShader(Pipeline, VertexShader);
glAttachShader(Pipeline, FragmentShader);
const GLint VertexCodeLength = strlen(VertexCode);
glShaderSource(VertexShader, 1, &VertexCode, &VertexCodeLength);
glCompileShader(VertexShader);
const GLint FragmentCodeLength = strlen(FragmentCode);
glShaderSource(FragmentShader, 1, &FragmentCode, &FragmentCodeLength);
glCompileShader(FragmentShader);
glLinkProgram(Pipeline);
int NoErrors = 0;
glGetProgramiv(Pipeline, GL_LINK_STATUS, &NoErrors);
bool error = false;
if (NoErrors == 0)
{
GLsizei BufferLength;
GLchar Buffer[4096];
glGetShaderInfoLog(FragmentShader, 4096, &BufferLength, Buffer);
if (BufferLength > 0)
{
std::cerr << "Error (Shader):" << Buffer << std::endl;
error = true;
}
glGetShaderInfoLog(VertexShader, 4096, &BufferLength, Buffer);
if (BufferLength > 0)
{
std::cerr << "Error (Shader):" << Buffer << std::endl;
error = true;
}
glGetProgramInfoLog(Pipeline, 4096, &BufferLength, Buffer);
if (BufferLength > 0)
{
std::cerr << "Error(Program):" << Buffer << std::endl;
error = true;
}
}
if (!error)
{
P->glID = Pipeline;
P->Vertex = VertexShader;
P->Fragment = FragmentShader;
P->ID = id;
result = P;
}
else
{
O->Programs.Count--;
}
return result;
}
/*
Generate an SR_Target object and returns a pointer to it. The SR_Target is linked to a texture and a frame buffer object that
get generated using this call.
Call this once per target at the beginning of the program after initializing the library.
There is no Target destruction function yet as per this version of the library.
*/
SR_Target *SR_CreateTarget(SR_RectF *Rect)
{
SR_Context *O = &simplyrend_context;
SR_Sprite *S_color = &O->Sprites.S[O->Sprites.Count];
int sprite_id = O->Sprites.Count;
O->Sprites.Count++;
SR_Sprite *S_Depth = &O->Sprites.S[O->Sprites.Count];
int sprite_id_depth = O->Sprites.Count;
SR_Target *B = &O->Buffers.B[O->Buffers.Count];
int target_id = O->Buffers.Count;
SR_Texture *Target_texture_color = &O->Textures.T[O->Textures.Count];
int texture_id = O->Textures.Count;
O->Textures.Count++;
SR_Texture *Target_texture_depth = &O->Textures.T[O->Textures.Count];
int texture_id_depth = O->Textures.Count;
// Target has a pointer to a sprite which has a texture id of the target.
// Target --> Sprite --> Texture_ID
// TODO: Rects don't really need to be stored on the stack, they can be generated in the SR_Render call, or even in the vertex shader (pro mode)
B->Sprite_color = S_color;
B->Sprite_depth = S_Depth;
B->ID = target_id;
S_color->ID = sprite_id;
S_color->texture = Target_texture_color;
S_Depth->ID = sprite_id_depth;
S_Depth->texture = Target_texture_depth;
B->DoNotTouch = false;
float x, y, w, h;
if (Rect)
{
x = Rect->x;
y = Rect->y;
w = Rect->w;
h = Rect->h;
B->Rect.V[0].P = Emaths::v2(x, y);
B->Rect.V[1].P = Emaths::v2(x + w, y);
B->Rect.V[2].P = Emaths::v2(x, y + h);
B->Rect.V[3].P = Emaths::v2(x, y + h);
B->Rect.V[4].P = Emaths::v2(x + w, y);
B->Rect.V[5].P = Emaths::v2(x + w, y + h);
B->Rect.V[0].UV = Emaths::v2(0, 1);
B->Rect.V[1].UV = Emaths::v2(1, 1);
B->Rect.V[2].UV = Emaths::v2(0, 0);
B->Rect.V[3].UV = Emaths::v2(0, 0);
B->Rect.V[4].UV = Emaths::v2(1, 1);
B->Rect.V[5].UV = Emaths::v2(1, 0);
}
else
{
w = O->WindowWidth;
h = O->WindowHeight;
S_color->w = w;
S_color->h = h;
B->Rect = O->ScreenRect;
}
bool success = false;
// if (O->Buffers.Count > 0)
{
// TODO: You really don't need a seperate VBO and VAO if they all are the same unless the format changes, put this in global
glGenVertexArrays(1, &B->VAO);
glBindVertexArray(B->VAO);
glGenBuffers(1, &B->VBO);
glBindBuffer(GL_ARRAY_BUFFER, B->VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, P));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, C));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_TRUE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, Z));
glBufferData(GL_ARRAY_BUFFER, sizeof(SR_ObjectRects::glrect) * 1, NULL, GL_STREAM_DRAW);
glGenFramebuffers(1, &B->FBO);
glBindFramebuffer(GL_FRAMEBUFFER, B->FBO);
glGenTextures(1, &Target_texture_color->glID);
glBindTexture(GL_TEXTURE_2D, Target_texture_color->glID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Target_texture_color->glID, 0);
glGenTextures(1, &Target_texture_depth->glID);
glBindTexture(GL_TEXTURE_2D, Target_texture_depth->glID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, w, h, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, Target_texture_depth->glID, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
success = true;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// TODO: write an SR_CreateTexture function that takes care of this for you
// idea: pool allocation: http://www.gingerbill.org/article/2019/02/16/memory-allocation-strategies-004/
O->Debug.estimated_VRAM += w * h * 4;
}
// else
// success = true;
O->Sprites.Count++;
O->Textures.Count++;
O->Buffers.Count++;
return success ? B : nullptr;
}
SR_Target *SR_CreateTargetForSprite(SR_Sprite *Sprite)
{
SR_Context *O = &simplyrend_context;
SR_Sprite *S = Sprite;
SR_Target *B = &O->Buffers.B[O->Buffers.Count];
int target_id = O->Buffers.Count;
SR_Texture *Target_texture = Sprite->texture;
// Target has a pointer to a sprite which has a texture id of the target.
// Target --> Sprite --> Texture_ID
B->Sprite_color = S;
B->DoNotTouch = true;
B->ID = target_id;
{
B->Rect.V[0].P = Emaths::v2(0, 0);
B->Rect.V[1].P = Emaths::v2(0 + S->w, 0);
B->Rect.V[2].P = Emaths::v2(0, 0 + S->h);
B->Rect.V[3].P = Emaths::v2(0, 0 + S->h);
B->Rect.V[4].P = Emaths::v2(0 + S->w, 0);
B->Rect.V[5].P = Emaths::v2(0 + S->w, 0 + S->h);
B->Rect.V[0].UV = Emaths::v2(0, 1);
B->Rect.V[1].UV = Emaths::v2(1, 1);
B->Rect.V[2].UV = Emaths::v2(0, 0);
B->Rect.V[3].UV = Emaths::v2(0, 0);
B->Rect.V[4].UV = Emaths::v2(1, 1);
B->Rect.V[5].UV = Emaths::v2(1, 0);
}
bool success = false;
glGenVertexArrays(1, &B->VAO);
glBindVertexArray(B->VAO);
glGenBuffers(1, &B->VBO);
glBindBuffer(GL_ARRAY_BUFFER, B->VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, P));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, C));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_TRUE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, Z));
glBufferData(GL_ARRAY_BUFFER, sizeof(SR_ObjectRects::glrect) * 1, NULL, GL_STREAM_DRAW);
glGenFramebuffers(1, &B->FBO);
glBindFramebuffer(GL_FRAMEBUFFER, B->FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Target_texture->glID, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
success = true;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
O->Buffers.Count++;
return success ? B : nullptr;
}
void SR_DestroyTarget(SR_Target *Target)
{
if (Target == nullptr)
return;
SR_Context *O = &simplyrend_context;
glDeleteFramebuffers(1, &Target->FBO);
glDeleteVertexArrays(1, &Target->VAO);
glDeleteBuffers(1, &Target->VBO);
if (!Target->DoNotTouch)
{
glDeleteTextures(1, &Target->Sprite_color->texture->glID);
glDeleteTextures(1, &Target->Sprite_depth->texture->glID);
O->Debug.estimated_VRAM -= O->max_texture_dimension * O->max_texture_dimension * 4;
}
O->Buffers.Count--;
}
void SR_SetTarget(SR_Target *Target, bool persistent)
{
SR_Context *O = &simplyrend_context;
O->Layers.L[O->Layers.index].Target = Target;
O->Layers.persistent_target_on = persistent;
O->Layers.persistent_target = Target;
}
SR_Texture *SR_GetBufferText(SR_Target *Target)
{
SR_Texture *result = nullptr;
if (Target != 0)
{
SR_Context *O = &simplyrend_context;
result = Target->Sprite_color->texture;
}
return result;
}
GLuint SR_GetBufferFBO(SR_Target *Target)
{
GLuint result = 0;
if (Target != 0)
{
SR_Context *O = &simplyrend_context;
result = Target->FBO;
}
return result;
}
// since this uses OpenGL, the z order is important and pushing the render layer guarantees a seperate draw call from that point forward.
// This also resets the render target to the screen.
// TODO: consider making this "StarLayer" and "EndLayer"
int SR_PushLayer(bool clear, bool NoAspectFix = false, bool OneMinusAlpha = false)
{
SR_Context *O = &simplyrend_context;
O->Layers.index++;
int layer = O->Layers.index;
if (O->Layers.index >= O->Layers.count)
{
O->Layers.count++;
O->TexRects.render_layer_index = (int *)realloc(O->TexRects.render_layer_index, (O->Layers.count + 1) * sizeof(int));
O->Lines.render_layer_index = (int *)realloc(O->Lines.render_layer_index, (O->Layers.count + 1) * sizeof(int));
O->Rects.render_layer_index = (int *)realloc(O->Rects.render_layer_index, (O->Layers.count + 1) * sizeof(int));
O->Layers.L = (SR_RenderLayer *)realloc(O->Layers.L, (O->Layers.count + 1) * sizeof(SR_RenderLayer));
memset(&O->Layers.L[O->Layers.count - 1], 0, sizeof(SR_RenderLayer));
}
O->Layers.last_tex_ptr = nullptr;
O->Rects.render_layer_index[layer] = O->Rects.Count;
O->TexRects.render_layer_index[layer] = O->TexRects.Count;
O->Lines.render_layer_index[layer] = O->Lines.Count;
if (O->startframe)
{
O->Layers.L[layer].Target = nullptr;
O->Layers.L[layer].Clip.on = 0;
O->Layers.L[layer].PP.on = 0;
O->Layers.L[layer].clear = clear;
O->Layers.L[layer].ClearColor = O->DrawColor;
O->Layers.L[layer].NoAspectFix = NoAspectFix;
O->Layers.L[layer].OneMinusAlpha = OneMinusAlpha;
}
if (O->Layers.persistent_target_on)
{
O->Layers.L[layer].Target = O->Layers.persistent_target;
}
if (O->Layers.persistent_clip_on)
{
O->Layers.L[layer].Clip.on = true;
O->Layers.L[layer].Clip.rect = O->Layers.persistent_clip_rect;
}
return layer;
}
// call this at the beginning of every frame within the program loop, you may change the window width and height
// the change should reflect window resizing in the window context controlled by you outside this library
void SR_StartFrame(int WindowWidth, int WindowHeight)
{
SR_Context *O = &simplyrend_context;
O->startframe = false;
SR_PushLayer(0);
O->Layers.index = 0;
SR_SetTarget(0, false);
O->startframe = true;
O->Rects.Count = 0;
O->TexRects.Count = 0;
O->Lines.Count = 0;
O->z_index = Z_START;
O->Layers.index = 0;
O->Layers.last_tex_ptr = nullptr;
for (int i = 0; i < O->Layers.count; i++)
{
O->Rects.render_layer_index[i] = 0;
O->TexRects.render_layer_index[i] = 0;
O->Lines.render_layer_index[i] = 0;
O->Layers.L[i].Clip.on = false;
O->Layers.L[i].PP.on = false;
O->Layers.L[i].Target = nullptr;
}
if (WindowWidth != O->WindowWidth || WindowHeight != O->WindowHeight)
{
glViewport(0, 0, WindowWidth, WindowHeight);
for (int i = 0; i < O->Buffers.Count; i++)
{
SR_Target *B = &O->Buffers.B[i];
if (B->DoNotTouch)
continue;
glBindTexture(GL_TEXTURE_2D, B->Sprite_color->texture->glID);
O->Debug.estimated_VRAM -= B->Sprite_color->w * B->Sprite_color->h * 4;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WindowWidth, WindowHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
B->Sprite_color->w = WindowWidth;
B->Sprite_color->h = WindowHeight;
glBindTexture(GL_TEXTURE_2D, B->Sprite_depth->texture->glID);
O->Debug.estimated_VRAM -= B->Sprite_depth->w * B->Sprite_depth->h * 4;
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, WindowWidth, WindowHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
B->Sprite_depth->w = WindowWidth;
B->Sprite_depth->h = WindowHeight;
O->Debug.estimated_VRAM += B->Sprite_color->w * B->Sprite_color->h * 4;
}
}
O->WindowWidth = WindowWidth;
O->WindowHeight = WindowHeight;
}
void SR_InitScreenRect()
{
SR_Context *O = &simplyrend_context;
SR_ObjectRects::glrect Screen;
float x = -1;
float y = -1;
float w = 2;
float h = 2;
Screen.V[0].P = Emaths::v2(x, y);
Screen.V[1].P = Emaths::v2(x + w, y);
Screen.V[2].P = Emaths::v2(x, y + h);
Screen.V[3].P = Emaths::v2(x, y + h);
Screen.V[4].P = Emaths::v2(x + w, y);
Screen.V[5].P = Emaths::v2(x + w, y + h);
Screen.V[0].UV = Emaths::v2(0, 1);
Screen.V[1].UV = Emaths::v2(1, 1);
Screen.V[2].UV = Emaths::v2(0, 0);
Screen.V[3].UV = Emaths::v2(0, 0);
Screen.V[4].UV = Emaths::v2(1, 1);
Screen.V[5].UV = Emaths::v2(1, 0);
for (int i = 0; i < 6; i++)
{
Screen.V[i].C.r = 1;
Screen.V[i].C.g = 1;
Screen.V[i].C.b = 1;
Screen.V[i].C.a = 1;
Screen.V[i].Z = O->z_index;
}
O->z_index -= Z_SHIFT;
O->ScreenRect = Screen;
}
/* Initializes the OpenGL context and the relevant data structures for SimplyRend,
- MaxRects: Assign maximum number of rectangles extimated to be used as an upper bound,
The larger MaxRects, the larger the RAM usage.
- FrameWidth and FrameHeight are the dimensions of the framebuffer, these should be constant. */
void SR_Init(SR_uint MaxRects, int FrameWidth, int FrameHeight, int WindowWidth, int WindowHeight, int (*load_opengl)(void))
{
int loadgl = load_opengl();
memset(&simplyrend_context, 0, sizeof(SR_Context));
SR_Context *O = &simplyrend_context;
O->TexRects.render_layer_index = nullptr;
O->Lines.render_layer_index = nullptr;
O->Rects.render_layer_index = nullptr;
O->Layers.L = nullptr;
SR_InitScreenRect();
O->FrameWidth = FrameWidth;
O->FrameHeight = FrameHeight;
O->WindowWidth = WindowWidth;
O->WindowHeight = WindowHeight;
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// allocate memory for the vertex buffers:
O->Rects.R = (SR_or::glrect *)malloc(sizeof(SR_or::glrect) * MaxRects);
O->TexRects.R = (SR_or::glrect *)malloc(sizeof(SR_or::glrect) * MaxRects);
O->Lines.L = (SR_ol::glline *)malloc(sizeof(SR_ol::glline) * MaxRects);
O->Sprites.S = (SR_Sprite *)realloc(O->Sprites.S, sizeof(SR_Sprite) * (MaxRects));
O->Textures.T = (SR_Texture *)realloc(O->Textures.T, sizeof(SR_Texture) * (100));
O->Buffers.B = (SR_Target *)realloc(O->Buffers.B, sizeof(SR_Target) * (100));
O->Fonts.F = (SR_Font *)realloc(O->Fonts.F, sizeof(SR_Font) * (100));
O->Programs.P = (SR_Program *)realloc(O->Programs.P, sizeof(SR_Program) * (100));
// assign attributes to the vertex buffers:
glGenVertexArrays(1, &O->Rects.VAO);
glBindVertexArray(O->Rects.VAO);
glGenBuffers(1, &O->Rects.VBO);
glBindBuffer(GL_ARRAY_BUFFER, O->Rects.VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, P));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, C));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_TRUE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, Z));
glBufferData(GL_ARRAY_BUFFER, sizeof(SR_ObjectRects::glrect) * MaxRects, NULL, GL_STREAM_DRAW);
glGenVertexArrays(1, &O->Lines.VAO);
glBindVertexArray(O->Lines.VAO);
glGenBuffers(1, &O->Lines.VBO);
glBindBuffer(GL_ARRAY_BUFFER, O->Lines.VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, P));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, C));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_TRUE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, Z));
glBufferData(GL_ARRAY_BUFFER, sizeof(SR_ObjectLines::glline) * MaxRects, NULL, GL_STREAM_DRAW);
glGenVertexArrays(1, &O->TexRects.VAO);
glBindVertexArray(O->TexRects.VAO);
glGenBuffers(1, &O->TexRects.VBO);
glBindBuffer(GL_ARRAY_BUFFER, O->TexRects.VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, P));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, C));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_TRUE, sizeof(SR_Vertex), (void *)offsetof(SR_Vertex, Z));
glBufferData(GL_ARRAY_BUFFER, sizeof(SR_ObjectRects::glrect) * MaxRects, NULL, GL_STREAM_DRAW);
// create the default shaders:
const char *VertexShaders[] =
{
R"###(
#version 430
layout(location = 0) in vec2 Position;
layout(location = 1) in vec4 ColorIn;
layout(location = 2) in float z_order;
smooth out vec4 ColorOut;
layout(location = 0) uniform vec2 scale;
layout(location = 1) uniform float FW;
layout(location = 2) uniform float FH;
vec2 Pixel2GL(vec2 P)
{
vec2 Res = P / vec2(FW, FH);
Res = Res * 2 - 1;
Res.y *= -1;
return Res;
}
void main()
{
gl_Position = vec4(scale * Pixel2GL(Position.xy), z_order, 1.0);
ColorOut = ColorIn;
}
)###",
R"###(
#version 430 core
layout (location = 0) in vec4 vertex;
layout (location = 1) in vec4 TexModColor;
layout (location = 2) in float z_order;
smooth out vec2 TexCoords;
smooth out vec4 TexMod;
layout(location = 0) uniform vec2 scale;
layout(location = 1) uniform float FW;
layout(location = 2) uniform float FH;
vec2 Pixel2GL(vec2 P)
{
vec2 Res = P / vec2(FW, FH);
Res = Res * 2 - 1;
Res.y *= -1;
return Res;
}
void main()
{
TexMod = TexModColor;
gl_Position = vec4(scale * Pixel2GL(vertex.xy), z_order, 1.0);
TexCoords = vertex.zw;
}
)###"};
const char *FragmentShaders[] =
{
R"###(
#version 430
out vec4 FragmentShader;
smooth in vec4 ColorOut;
void main()
{
FragmentShader = ColorOut;