liblibgb.c 1.2021.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/gb/gb_gl.h ADDED
@@ -0,0 +1,2592 @@
1
+ /* gb.h - v0.05 - OpenGL Helper Library - public domain
2
+ - no warranty implied; use at your own risk
3
+
4
+ This is a single header file with a bunch of useful stuff
5
+ for working with OpenGL
6
+
7
+ ===========================================================================
8
+ YOU MUST
9
+
10
+ #define GBGL_IMPLEMENTATION
11
+
12
+ in EXACTLY _one_ C or C++ file that includes this header, BEFORE the
13
+ include like this:
14
+
15
+ #define GBGL_IMPLEMENTATION
16
+ #include "gb_gl.h"
17
+
18
+ All other files should just #include "gb_gl.h" without #define
19
+
20
+ Dependencies
21
+ NOTE: You may need change the path
22
+
23
+ This library REQUIRES "stb_image.h" for loading images from file
24
+ This library REQUIRES "gb.h" at this moment in time.
25
+ If you are using the font library (e.g. GBGL_NO_FONTS is _not_ defined):
26
+ This library then REQUIRES "stb_truetype.h" for ttf handling
27
+ This library then REQUIRES "stb_rect_pack.h"
28
+
29
+ NOTE(bill): I may remove these dependencies for the font handling by
30
+ embedding the needed types and procedures.
31
+
32
+ Optional Dependencies
33
+ "gb_math.h" as a lot of useful things in it over <math.h>.
34
+ Why not have a look at it?
35
+
36
+ Optional Defines
37
+ GBGL_NO_FONTS - Do no use font subsystem
38
+ GBGL_NO_BASIC_STATE - Do no use basic state subsystem
39
+
40
+ Steps for supporting dynamic reload:
41
+ You _MUST_ defined you own malloc and free that use whatever
42
+ permanent memory system you are using:
43
+
44
+ #define gbgl_malloc
45
+ #define gbgl_free
46
+
47
+
48
+ ===========================================================================
49
+
50
+ Conventions used:
51
+ gbglTypesAreLikeThis (None core types)
52
+ gbgl_functions_and_variables_like_this
53
+ Prefer // Comments
54
+ Never use _t suffix for types (I think they are stupid...)
55
+
56
+
57
+ Version History:
58
+ 0.06 - Enum convention change
59
+ 0.05 - gbglColour
60
+ 0.04e - Change brace style because why not?
61
+ 0.04d - Use new gb.h file handling system
62
+ 0.04c - Use new gb.h file handling system
63
+ 0.04b - Work with the new gb.h
64
+ 0.04a - Better Documentation
65
+ 0.04 - Remove gb_math.h dependency
66
+ 0.03a - Better Rounded Rect
67
+ 0.03 - Basic State Rendering
68
+ 0.02 - Font Caching and Rendering
69
+ 0.01 - Initial Version
70
+
71
+ LICENSE
72
+ This software is dual-licensed to the public domain and under the following
73
+ license: you are granted a perpetual, irrevocable license to copy, modify,
74
+ publish, and distribute this file as you see fit.
75
+
76
+ WARNING
77
+ - This library is _highly_ experimental and features may not work as expected.
78
+ - This also means that many functions are not documented.
79
+
80
+ CREDITS
81
+ Written by Ginger Bill
82
+
83
+ */
84
+
85
+ #ifndef GBGL_INCLUDE_GB_GL_H
86
+ #define GBGL_INCLUDE_GB_GL_H
87
+
88
+ #ifndef GB_IMPLEMENTATION
89
+ #include "gb.h"
90
+ #endif
91
+
92
+ #ifndef STB_IMAGE_IMPLEMENTATION
93
+ #include "stb_image.h"
94
+ #endif
95
+
96
+
97
+ #if !defined(GBGL_NO_FONTS)
98
+ #ifndef STB_RECT_PACK_IMPLEMENTATION
99
+ #include "stb_rect_pack.h"
100
+ #endif
101
+
102
+ #ifndef STB_TRUETYPE_IMPLEMENTATION
103
+ #include "stb_truetype.h"
104
+ #endif
105
+ #endif
106
+
107
+
108
+ #if defined(GBGL_USE_GB_MATH)
109
+ #ifndef GB_MATH_IMPLEMENTATION
110
+ #include "gb_math.h"
111
+ #endif
112
+
113
+ #define gbgl_lerp(x, y, t) gb_lerp(x, y, t)
114
+ #define gbgl_sin(x) gb_sin(x)
115
+ #define gbgl_cos(x) gb_cos(x)
116
+ #define gbgl_abs(x) gb_abs(x)
117
+ #define gbgl_min(x, y) gb_min(x, y)
118
+ #define gbgl_max(x, y) gb_max(x, y)
119
+ #define gbgl_round(x) gb_round(x)
120
+
121
+ #else
122
+ #if !defined(GBGL_USE_CUSTOM_MATH)
123
+ #include <math.h>
124
+
125
+ #define gbgl_lerp(x, y, t) ((x)*(1.0f-(t)) + (y)*(t))
126
+ #define gbgl_sin(x) sinf(x)
127
+ #define gbgl_cos(x) cosf(x)
128
+ #define gbgl_abs(x) ((x) >= 0 ? (x) : -(x))
129
+ #define gbgl_min(x, y) ((x) < (y) ? (x) : (y))
130
+ #define gbgl_max(x, y) ((x) > (y) ? (x) : (y))
131
+ #define gbgl_round(x) (((x) >= 0.0f) ? floorf((x) + 0.5f) : ceilf((x) - 0.5f))
132
+
133
+ #endif
134
+ #endif
135
+
136
+ #define gbgl_clamp(x, lower, upper) gbgl_min(gbgl_max((x), (lower)), (upper))
137
+ #define gbgl_clamp01(x) gbgl_clamp(x, 0, 1)
138
+
139
+
140
+ #if defined(__cplusplus)
141
+ extern "C" {
142
+ #endif
143
+
144
+
145
+ #ifndef GBGL_DEF
146
+ #define GBGL_DEF extern
147
+ #endif
148
+
149
+ #ifndef gbgl_malloc
150
+ #define gbgl_malloc(sz) malloc(sz)
151
+ #endif
152
+
153
+ #ifndef gbgl_free
154
+ #define gbgl_free(ptr) free(ptr)
155
+ #endif
156
+
157
+ #ifndef GBGL_TAU
158
+ #define GBGL_TAU 6.28318530717958647692528676655900576f
159
+ #endif
160
+
161
+
162
+
163
+ ////////////////////////////////////////////////////////////////
164
+ //
165
+ // Colour Type
166
+ // It's quite useful
167
+ // TODO(bill): Does this need to be in this library?
168
+ // Can I remove the anonymous struct extension?
169
+ //
170
+
171
+ #if defined(_MSC_VER)
172
+ #pragma warning(push)
173
+ #pragma warning(disable:4201)
174
+ #endif
175
+
176
+ typedef union gbglColour {
177
+ u32 rgba; // NOTE(bill): 0xaabbggrr in little endian
178
+ struct { u8 r, g, b, a; };
179
+ u8 e[4];
180
+ } gbglColour;
181
+ GB_STATIC_ASSERT(gb_size_of(gbglColour) == gb_size_of(u32));
182
+
183
+ #if defined(_MSC_VER)
184
+ #pragma warning(pop)
185
+ #endif
186
+
187
+
188
+ GB_DEF gbglColour gbgl_colour(f32 r, f32 g, f32 b, f32 a);
189
+
190
+ gb_global gbglColour const gbglColour_White = {0xffffffff};
191
+ gb_global gbglColour const gbglColour_Grey = {0xff808080};
192
+ gb_global gbglColour const gbglColour_Black = {0xff000000};
193
+
194
+ gb_global gbglColour const gbglColour_Red = {0xff0000ff};
195
+ gb_global gbglColour const gbglColour_Orange = {0xff0099ff};
196
+ gb_global gbglColour const gbglColour_Yellow = {0xff00ffff};
197
+ gb_global gbglColour const gbglColour_Green = {0xff00ff00};
198
+ gb_global gbglColour const gbglColour_Cyan = {0xffffff00};
199
+ gb_global gbglColour const gbglColour_Blue = {0xffff0000};
200
+ gb_global gbglColour const gbglColour_Violet = {0xffff007f};
201
+ gb_global gbglColour const gbglColour_Magenta = {0xffff00ff};
202
+
203
+
204
+ ////////////////////////////////////////////////////////////////
205
+ //
206
+ // Generic Stuff
207
+ //
208
+ //
209
+
210
+
211
+ #ifndef GBGL_VERT_PTR_AA_GENERIC
212
+ #define GBGL_VERT_PTR_AA_GENERIC
213
+ // NOTE(bill) The "default" is just the f32 version
214
+ #define gbgl_vert_ptr_aa(index, element_count, Type, var_name) \
215
+ gbgl_vert_ptr_aa_f32(index, element_count, Type, var_name)
216
+
217
+ #define gbgl_vert_ptr_aa_f32(index, element_count, Type, var_name) do { \
218
+ glVertexAttribPointer(index, \
219
+ element_count, \
220
+ GL_FLOAT, \
221
+ false, \
222
+ gb_size_of(Type), \
223
+ (void const *)(gb_offset_of(Type, var_name))); \
224
+ glEnableVertexAttribArray(index); \
225
+ } while (0)
226
+
227
+ #define gbgl_vert_ptr_aa_u8(index, element_count, Type, var_name) do { \
228
+ glVertexAttribPointer(index, \
229
+ element_count, \
230
+ GL_UNSIGNED_BYTE, \
231
+ false, \
232
+ gb_size_of(Type), \
233
+ (void const *)(gb_offset_of(Type, var_name))); \
234
+ glEnableVertexAttribArray(index); \
235
+ } while (0)
236
+
237
+ #define gbgl_vert_ptr_aa_u8n(index, element_count, Type, var_name) do { \
238
+ glVertexAttribPointer(index, \
239
+ element_count, \
240
+ GL_UNSIGNED_BYTE, \
241
+ true, \
242
+ gb_size_of(Type), \
243
+ (void const *)(gb_offset_of(Type, var_name))); \
244
+ glEnableVertexAttribArray(index); \
245
+ } while (0)
246
+
247
+ #define gbgl_vert_ptr_aa_u32(index, element_count, Type, var_name) do { \
248
+ glVertexAttribIPointer(index, \
249
+ element_count, \
250
+ GL_UNSIGNED_INT, \
251
+ gb_size_of(Type), \
252
+ (void const *)(gb_offset_of(Type, var_name))); \
253
+ glEnableVertexAttribArray(index); \
254
+ } while (0)
255
+
256
+ #define gbgl_vert_ptr_aa_u16(index, element_count, Type, var_name) do { \
257
+ glVertexAttribIPointer(index, \
258
+ element_count, \
259
+ GL_UNSIGNED_SHORT, \
260
+ gb_size_of(Type), \
261
+ (void const *)(gb_offset_of(Type, var_name))); \
262
+ glEnableVertexAttribArray(index); \
263
+ } while (0)
264
+
265
+ #define gbgl_vert_ptr_aa_u16n(index, element_count, Type, var_name) do { \
266
+ glVertexAttribPointer(index, \
267
+ element_count, \
268
+ GL_UNSIGNED_SHORT, \
269
+ true, \
270
+ gb_size_of(Type), \
271
+ (void const *)(gb_offset_of(Type, var_name))); \
272
+ glEnableVertexAttribArray(index); \
273
+ } while (0)
274
+
275
+ #endif
276
+
277
+
278
+ GBGL_DEF u32 gbgl_make_sampler(u32 min_filter, u32 max_filter, u32 s_wrap, u32 t_wrap);
279
+
280
+
281
+
282
+
283
+ ////////////////////////////////////////////////////////////////
284
+ //
285
+ // Data Buffers
286
+ //
287
+ //
288
+
289
+
290
+ typedef enum gbglBufferDataType {
291
+ gbglBufferData_u8 = GL_UNSIGNED_BYTE,
292
+ gbglBufferData_i8 = GL_BYTE,
293
+
294
+ gbglBufferData_u16 = GL_UNSIGNED_SHORT,
295
+ gbglBufferData_i16 = GL_SHORT,
296
+ gbglBufferData_f16 = GL_HALF_FLOAT,
297
+
298
+ gbglBufferData_u32 = GL_UNSIGNED_INT,
299
+ gbglBufferData_i32 = GL_INT,
300
+ gbglBufferData_f32 = GL_FLOAT,
301
+
302
+ gbglBufferData_f8, // NOTE(bill): This is not a "real" OpenGL type but it is needed for internal format enums
303
+ } gbglBufferDataType;
304
+
305
+
306
+ // NOTE(bill) index+1 = channels count
307
+ #if defined(GBGL_USE_SRGB_TEXTURE_FORMAT)
308
+ i32 const gbglTextureFormat[4] = { GL_RED, GL_RG, GL_SRGB8, GL_SRGB8_ALPHA8 };
309
+ #else
310
+ i32 const gbglTextureFormat[4] = { GL_RED, GL_RG, GL_RGB, GL_RGBA };
311
+ #endif
312
+
313
+ i32 const gbglInternalTextureFormat_8[4] = { GL_R8, GL_RG8, GL_RGB8, GL_RGBA8 };
314
+ i32 const gbglInternalTextureFormat_16[4] = { GL_R16, GL_RG16, GL_RGB16, GL_RGBA16 };
315
+ i32 const gbglInternalTextureFormat_32[4] = { GL_R32F, GL_RG32F, GL_RGB32F, GL_RGBA32F };
316
+
317
+ i32 const gbglInternalTextureFormat_u8[4] = { GL_R8UI, GL_RG8UI, GL_RGB8UI, GL_RGB8UI };
318
+ i32 const gbglInternalTextureFormat_i8[4] = { GL_R8I, GL_RG8I, GL_RGB8I, GL_RGB8I };
319
+ i32 const gbglInternalTextureFormat_f8[4] = { GL_R8, GL_RG8, GL_RGB8, GL_RGB8 };
320
+
321
+ i32 const gbglInternalTextureFormat_u16[4] = { GL_R16UI, GL_RG16UI, GL_RGB16UI, GL_RGB16UI };
322
+ i32 const gbglInternalTextureFormat_i16[4] = { GL_R16I, GL_RG16I, GL_RGB16I, GL_RGB16I };
323
+ i32 const gbglInternalTextureFormat_f16[4] = { GL_R16F, GL_RG16F, GL_RGB16F, GL_RGB16F };
324
+
325
+ i32 const gbglInternalTextureFormat_u32[4] = { GL_R32UI, GL_RG32UI, GL_RGB32UI, GL_RGBA32UI };
326
+ i32 const gbglInternalTextureFormat_i32[4] = { GL_R32I, GL_RG32I, GL_RGB32I, GL_RGBA32I };
327
+ i32 const gbglInternalTextureFormat_f32[4] = { GL_R32F, GL_RG32F, GL_RGB32F, GL_RGBA32F };
328
+
329
+ gb_inline i32 gbgl_texture_format(gbglBufferDataType data_type, i32 channel_count) {
330
+ GB_ASSERT(gb_is_between(channel_count, 1, 4));
331
+ switch (data_type) {
332
+ case gbglBufferData_u8: return gbglInternalTextureFormat_u8[channel_count-1];
333
+ case gbglBufferData_i8: return gbglInternalTextureFormat_i8[channel_count-1];
334
+ case gbglBufferData_f8: return gbglInternalTextureFormat_f8[channel_count-1];
335
+ case gbglBufferData_u16: return gbglInternalTextureFormat_u16[channel_count-1];
336
+ case gbglBufferData_i16: return gbglInternalTextureFormat_i16[channel_count-1];
337
+ case gbglBufferData_f16: return gbglInternalTextureFormat_f16[channel_count-1];
338
+ case gbglBufferData_u32: return gbglInternalTextureFormat_u32[channel_count-1];
339
+ case gbglBufferData_i32: return gbglInternalTextureFormat_i32[channel_count-1];
340
+ case gbglBufferData_f32: return gbglInternalTextureFormat_f32[channel_count-1];
341
+ }
342
+ return gbglInternalTextureFormat_f32[4-1];
343
+ }
344
+
345
+ typedef struct gbglTBO {
346
+ u32 buffer_obj_handle;
347
+ u32 buffer_handle;
348
+ } gbglTBO;
349
+
350
+ // NOTE(bill): usage_hint == (GL_STATIC_DRAW, GL_STREAM_DRAW, GL_DYNAMIC_DRAW)
351
+ GBGL_DEF u32 gbgl_make_vbo(void const *data, isize size, i32 usage_hint);
352
+ GBGL_DEF u32 gbgl_make_ebo(void const *data, isize size, i32 usage_hint);
353
+ GBGL_DEF gbglTBO gbgl_make_tbo(gbglBufferDataType data_type, i32 channel_count, void const *data, isize size, i32 usage_hint);
354
+
355
+ GBGL_DEF void gbgl_vbo_copy(u32 vbo_handle, void *const data, isize size, isize offset);
356
+ GBGL_DEF void gbgl_ebo_copy(u32 ebo_handle, void *const data, isize size, isize offset);
357
+ GBGL_DEF void gbgl_tbo_copy(gbglTBO tbo, void *const data, isize size, isize offset);
358
+
359
+
360
+
361
+ GBGL_DEF void gbgl_bind_vbo(u32 vbo_handle);
362
+ GBGL_DEF void gbgl_bind_ebo(u32 ebo_handle);
363
+ GBGL_DEF void gbgl_bind_tbo(gbglTBO tbo, i32 sampler_handle, i32 tex_unit);
364
+
365
+ // NOTE(bill): access = GL_WRITE_ONLY, etc.
366
+ GBGL_DEF void *gbgl_map_vbo(u32 vbo_handle, i32 access);
367
+ GBGL_DEF void *gbgl_map_ebo(u32 ebo_handle, i32 access);
368
+ GBGL_DEF void gbgl_unmap_vbo(void);
369
+ GBGL_DEF void gbgl_unmap_ebo(void);
370
+
371
+
372
+
373
+ ////////////////////////////////////////////////////////////////
374
+ //
375
+ // Shader
376
+ //
377
+ //
378
+
379
+ typedef enum gbglShaderType {
380
+ gbglShader_Vertex,
381
+ gbglShader_Fragment,
382
+ gbglShader_Geometry,
383
+
384
+ gbglShader_Count,
385
+ } gbglShaderType;
386
+
387
+ i32 const gbglShaderMap[gbglShader_Count] = {
388
+ GL_VERTEX_SHADER, /* gbglShader_Vertex */
389
+ GL_FRAGMENT_SHADER, /* gbglShader_Fragment */
390
+ GL_GEOMETRY_SHADER, /* gbglShader_Geometry */
391
+ };
392
+
393
+ typedef enum gbglShaderError {
394
+ gbglShaderError_None,
395
+ gbglShaderError_ShaderCompile,
396
+ gbglShaderError_Linking,
397
+ gbglShaderError_UnableToReadFile,
398
+
399
+ gbglShaderError_Count,
400
+ } gbglShaderError;
401
+
402
+ #ifndef GBGL_MAX_UNIFORM_COUNT
403
+ #define GBGL_MAX_UNIFORM_COUNT 32
404
+ #endif
405
+
406
+ typedef struct gbglShader {
407
+ u32 shaders[gbglShader_Count];
408
+ u32 program;
409
+
410
+ i32 uniform_locs[GBGL_MAX_UNIFORM_COUNT];
411
+ char *uniform_names[GBGL_MAX_UNIFORM_COUNT];
412
+ i32 uniform_count;
413
+
414
+ u32 type_flags;
415
+
416
+ gbFile files[gbglShader_Count];
417
+
418
+ char base_name[64];
419
+ } gbglShader;
420
+
421
+
422
+
423
+ #ifndef GBGL_SHADER_FILE_EXTENSIONS_DEFINED
424
+ #define GBGL_SHADER_FILE_EXTENSIONS_DEFINED
425
+ gb_global char const *gbglShaderFileExtensions[gbglShader_Count] = {".vs", ".fs", ".gs"};
426
+ #endif
427
+
428
+
429
+ GBGL_DEF gbglShaderError gbgl_load_shader_from_file (gbglShader *s, u32 type_bits, char const *filename);
430
+ GBGL_DEF gbglShaderError gbgl_load_shader_from_memory_vf (gbglShader *s, char const *vert_source, char const *frag_source);
431
+ GBGL_DEF gbglShaderError gbgl_load_shader_from_memory_vfg(gbglShader *s, char const *vert_source, char const *frag_source, char const *geom_source);
432
+
433
+ GBGL_DEF void gbgl_destroy_shader (gbglShader *shader);
434
+ GBGL_DEF b32 gbgl_has_shader_changed(gbglShader *shader);
435
+ GBGL_DEF b32 gbgl_reload_shader (gbglShader *shader); // TODO(bill): Return an error code?
436
+ GBGL_DEF void gbgl_use_shader (gbglShader *shader);
437
+ GBGL_DEF b32 gbgl_is_shader_in_use (gbglShader *shader);
438
+
439
+ GBGL_DEF i32 gbgl_get_uniform(gbglShader *shader, char const *name);
440
+
441
+ GBGL_DEF void gbgl_set_uniform_int (gbglShader *s, char const *name, i32 i);
442
+ GBGL_DEF void gbgl_set_uniform_float (gbglShader *s, char const *name, f32 f);
443
+ GBGL_DEF void gbgl_set_uniform_vec2 (gbglShader *s, char const *name, f32 const *v);
444
+ GBGL_DEF void gbgl_set_uniform_vec3 (gbglShader *s, char const *name, f32 const *v);
445
+ GBGL_DEF void gbgl_set_uniform_vec4 (gbglShader *s, char const *name, f32 const *v);
446
+ GBGL_DEF void gbgl_set_uniform_mat4 (gbglShader *s, char const *name, f32 const *m);
447
+ GBGL_DEF void gbgl_set_uniform_mat4_count(gbglShader *s, char const *name, f32 const *m, isize count);
448
+ GBGL_DEF void gbgl_set_uniform_colour (gbglShader *s, char const *name, gbglColour col);
449
+
450
+
451
+ ////////////////////////////////////////////////////////////////
452
+ //
453
+ // Texture
454
+ //
455
+ //
456
+
457
+ typedef enum gbglTextureType {
458
+ gbglgTexture_2D,
459
+ gbglgTexture_CubeMap,
460
+
461
+ gbglgTexture_Count,
462
+ } gbglTextureType;
463
+
464
+ gb_global i32 const GBGL_TEXTURE_TYPE[gbglgTexture_Count] = {
465
+ GL_TEXTURE_2D, /* gbglgTexture_2D */
466
+ GL_TEXTURE_CUBE_MAP, /* gbglgTexture_CubeMap */
467
+ };
468
+
469
+
470
+ typedef struct gbglTexture {
471
+ i32 width, height, channel_count;
472
+ gbglBufferDataType data_type;
473
+ gbglTextureType type;
474
+ u32 handle;
475
+ } gbglTexture;
476
+
477
+ GBGL_DEF b32 gbgl_load_texture2d_from_file (gbglTexture *texture, b32 flip_vertically, char const *filename, ...);
478
+ GBGL_DEF b32 gbgl_load_texture2d_from_memory(gbglTexture *texture, void const *data, i32 width, i32 height, i32 channel_count);
479
+ GBGL_DEF b32 gbgl_init_texture2d_coloured (gbglTexture *texture, gbglColour colour);
480
+ GBGL_DEF void gbgl_destroy_texture (gbglTexture *texture);
481
+
482
+ GBGL_DEF void gbgl_bind_texture2d(gbglTexture const *texture, u32 position, u32 sampler);
483
+
484
+
485
+
486
+
487
+
488
+ ////////////////////////////////////////////////////////////////
489
+ //
490
+ // Render Buffer
491
+ //
492
+ //
493
+
494
+ // TODO(bill): Record depth and stencil and numerous colour attachments
495
+
496
+ typedef struct gbglRenderBuffer {
497
+ i32 width, height;
498
+ i32 channel_count;
499
+ u32 handle;
500
+
501
+ gbglTexture colour_texture;
502
+ } gbglRenderBuffer;
503
+
504
+ #define GBGL_MAX_RENDER_COLOUR_BUFFERS 16
505
+ gb_global u32 const gbglColourBufferAttachments[GBGL_MAX_RENDER_COLOUR_BUFFERS] = {
506
+ GL_COLOR_ATTACHMENT0,
507
+ GL_COLOR_ATTACHMENT1,
508
+ GL_COLOR_ATTACHMENT2,
509
+ GL_COLOR_ATTACHMENT3,
510
+ GL_COLOR_ATTACHMENT4,
511
+ GL_COLOR_ATTACHMENT5,
512
+ GL_COLOR_ATTACHMENT6,
513
+ GL_COLOR_ATTACHMENT7,
514
+ GL_COLOR_ATTACHMENT8,
515
+ GL_COLOR_ATTACHMENT9,
516
+ GL_COLOR_ATTACHMENT10,
517
+ GL_COLOR_ATTACHMENT11,
518
+ GL_COLOR_ATTACHMENT12,
519
+ GL_COLOR_ATTACHMENT13,
520
+ GL_COLOR_ATTACHMENT14,
521
+ GL_COLOR_ATTACHMENT15,
522
+ };
523
+
524
+
525
+ GBGL_DEF b32 gbgl_init_render_buffer (gbglRenderBuffer *rb, i32 width, i32 height, i32 channel_count);
526
+ GBGL_DEF void gbgl_destroy_render_buffer(gbglRenderBuffer *rb);
527
+ GBGL_DEF void gbgl_render_to_buffer(gbglRenderBuffer const *rb);
528
+ GBGL_DEF void gbgl_render_to_screen(i32 width, i32 height);
529
+
530
+
531
+ ////////////////////////////////////////////////////////////////
532
+ //
533
+ // Font Stuff
534
+ //
535
+ //
536
+
537
+ #if !defined(GBGL_NO_FONTS)
538
+
539
+ typedef struct gbglGlyphMapKVPair {
540
+ char32 codepoint;
541
+ u16 index;
542
+ } gbglGlyphMapKVPair;
543
+
544
+ typedef struct gbglGlyphInfo {
545
+ f32 s0, t0, s1, t1;
546
+ i16 xoff, yoff;
547
+ f32 xadv;
548
+ } gbglGlyphInfo;
549
+
550
+ typedef struct gbglKernPair {
551
+ union {
552
+ i32 packed;
553
+ struct { u16 i0, i1; };
554
+ };
555
+ f32 kern;
556
+ } gbglKernPair;
557
+
558
+ typedef enum gbglJustifyType {
559
+ gbglJustify_Left,
560
+ gbglJustify_Centre,
561
+ gbglJustify_Right,
562
+ } gbglJustifyType;
563
+
564
+ typedef enum gbglTextParamType {
565
+ gbglTextParam_Invalid,
566
+ gbglTextParam_MaxWidth,
567
+ gbglTextParam_Justify,
568
+ gbglTextParam_TextureFilter,
569
+
570
+ gbglTextParam_Count,
571
+ } gbglTextParamType;
572
+
573
+ typedef struct gbglTextParam {
574
+ gbglTextParamType type;
575
+ union {
576
+ f32 val_f32;
577
+ i32 val_i32;
578
+ };
579
+ } gbglTextParam;
580
+
581
+ typedef struct gbglFont {
582
+ isize glyph_count;
583
+ isize kern_pair_count;
584
+ i32 bitmap_width, bitmap_height;
585
+ f32 size;
586
+ i32 ascent, descent, line_gap;
587
+ char *ttf_filename;
588
+ gbglTexture texture;
589
+
590
+ gbglGlyphMapKVPair *glyph_map;
591
+ gbglGlyphInfo * glyphs;
592
+ gbglKernPair * kern_table;
593
+
594
+ struct gbglFont *next; // NOTE(bill): Allow as linked list
595
+ } gbglFont;
596
+
597
+ typedef struct gbglFontCachedTTF {
598
+ char * name;
599
+ u8 * ttf;
600
+ stbtt_fontinfo finfo;
601
+ struct gbglFontCachedTTF *next;
602
+ } gbglFontCachedTTF;
603
+
604
+ typedef struct gbglFontCache {
605
+ isize font_char_list_count;
606
+ char *font_char_list;
607
+
608
+ isize codepoint_count;
609
+ char32 *codepoints;
610
+
611
+ stbtt_pack_range *ranges;
612
+ stbtt_packedchar *packed_char_data;
613
+ stbrp_rect * rect_cache;
614
+
615
+ gbglFontCachedTTF *ttf_buffer;
616
+ gbglFont * fonts;
617
+ } gbglFontCache;
618
+
619
+
620
+ #if 0
621
+ GBGL_DEF void gbgl_destroy_font_cache(gbglFontCache *fc);
622
+ #endif
623
+
624
+ // NOTE(bill): gbgl_load_font_from_file will load from file if it is not found
625
+ GBGL_DEF gbglFont *gbgl_load_font_from_file (gbglFontCache *fc, char const *ttf_filename, f32 font_size);
626
+ GBGL_DEF gbglFont *gbgl_get_font_only_from_cache(gbglFontCache *fc, char const *ttf_filename, f32 font_size);
627
+ GBGL_DEF gbglFont *gbgl_cache_font (gbglFontCache *fc, char const *ttf_filename, f32 font_size);
628
+
629
+
630
+ GBGL_DEF b32 gbgl_get_packed_font_dim (gbglFontCache *cache, gbglFontCachedTTF *ttf, i32 *width, i32 *height);
631
+ GBGL_DEF gbglGlyphInfo *gbgl_get_glyph_info (gbglFont *font, char32 codepoint, isize *out_index);
632
+ GBGL_DEF f32 gbgl_get_font_kerning_from_glyph_indices(gbglFont *font, isize left_index, isize right_index);
633
+ GBGL_DEF void gbgl_get_string_dimensions (gbglFont *font, char const *str, f32 *out_width, f32 *out_height);
634
+ GBGL_DEF f32 gbgl_get_sub_string_width (gbglFont *font, char const *str, isize char_count);
635
+ GBGL_DEF i32 gbgl_get_wrapped_line_count (gbglFont *font, char const *str, isize max_len, isize max_width);
636
+ GBGL_DEF f32 gbgl_get_string_width (gbglFont *font, char const *str, isize max_len);
637
+
638
+
639
+ #endif
640
+
641
+ ////////////////////////////////////////////////////////////////
642
+ //
643
+ // Basic State
644
+ //
645
+ //
646
+
647
+ #if !defined(GBGL_NO_BASIC_STATE)
648
+
649
+ #ifndef GBGL_BS_MAX_VERTEX_COUNT
650
+ #define GBGL_BS_MAX_VERTEX_COUNT 32
651
+ #endif
652
+
653
+ #ifndef GBGL_BS_MAX_INDEX_COUNT
654
+ #define GBGL_BS_MAX_INDEX_COUNT 6
655
+ #endif
656
+
657
+
658
+ #if !defined(GBGL_NO_FONTS)
659
+
660
+ #ifndef GBGL_MAX_RENDER_STRING_LENGTH
661
+ #define GBGL_MAX_RENDER_STRING_LENGTH 4096
662
+ #endif
663
+
664
+ #ifndef gbglTextParam_Stack_size
665
+ #define gbglTextParam_Stack_size 128
666
+ #endif
667
+
668
+ #ifndef GBGL_FONT_CHAR_LIST
669
+ #define GBGL_FONT_CHAR_LIST \
670
+ "Āā㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľŁł"\
671
+ "ŃńŅņņŇňʼnŊŋŌōōŎŏŐőŒœŕŖŗŘřŚśŜŝŞşŠšŢţŤťŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽža!ö"\
672
+ "\"#$%%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"\
673
+ "ŠšŒœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõøùúûüýþÿ®™£"\
674
+ " \t\r\n"
675
+ #endif
676
+
677
+ #ifndef GBGL_PT_TO_PX_SCALE
678
+ #define GBGL_PT_TO_PX_SCALE (96.0f / 72.0f)
679
+ #endif
680
+
681
+ #ifndef GBGL_TAB_CHARACTER_WIDTH
682
+ #define GBGL_TAB_CHARACTER_WIDTH 4
683
+ #endif
684
+
685
+ #endif
686
+
687
+ typedef struct gbglBasicVertex {
688
+ f32 x, y;
689
+ f32 u, v;
690
+ } gbglBasicVertex;
691
+
692
+ typedef struct gbglBasicState {
693
+ gbglBasicVertex vertices[GBGL_BS_MAX_VERTEX_COUNT];
694
+ u16 indices[GBGL_BS_MAX_INDEX_COUNT];
695
+
696
+ u32 vao, vbo, ebo;
697
+ u32 nearest_sampler;
698
+ u32 linear_sampler;
699
+ u32 mipmap_sampler;
700
+ gbglShader ortho_tex_shader;
701
+ gbglShader ortho_col_shader;
702
+
703
+ f32 ortho_mat[16];
704
+ i32 width, height;
705
+
706
+ #if !defined(GBGL_NO_FONTS)
707
+ gbglFontCache font_cache;
708
+ gbglShader font_shader;
709
+ gbglBasicVertex font_vertices[GBGL_MAX_RENDER_STRING_LENGTH * 4];
710
+ u16 font_indices[GBGL_MAX_RENDER_STRING_LENGTH * 6];
711
+ u32 font_vao, font_vbo, font_ebo;
712
+ char font_text_buffer[GBGL_MAX_RENDER_STRING_LENGTH * 4]; // NOTE(bill): Maximum of 4 bytes per char for utf-8
713
+ u32 font_samplers[2];
714
+
715
+ gbglTextParam text_param_stack[gbglTextParam_Stack_size];
716
+ isize text_param_stack_count;
717
+ gbglTextParam text_params[gbglTextParam_Count];
718
+ #endif
719
+ } gbglBasicState;
720
+
721
+ GBGL_DEF void gbgl_bs_init(gbglBasicState *bs, i32 window_width, i32 window_height);
722
+ GBGL_DEF void gbgl_bs_set_resolution(gbglBasicState *bs, i32 window_width, i32 window_height);
723
+ GBGL_DEF void gbgl_bs_begin(gbglBasicState *bs, i32 window_width, i32 window_height);
724
+ GBGL_DEF void gbgl_bs_end(gbglBasicState *bs);
725
+
726
+ GBGL_DEF void gbgl_bs_draw_textured_rect(gbglBasicState *bs, gbglTexture *tex, f32 x, f32 y, f32 w, f32 h, b32 v_up);
727
+ GBGL_DEF void gbgl_bs_draw_rect(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, gbglColour col);
728
+ GBGL_DEF void gbgl_bs_draw_rect_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, gbglColour col, f32 thickness);
729
+ GBGL_DEF void gbgl_bs_draw_quad(gbglBasicState *bs,
730
+ f32 x0, f32 y0,
731
+ f32 x1, f32 y1,
732
+ f32 x2, f32 y2,
733
+ f32 x3, f32 y3,
734
+ gbglColour col);
735
+ GBGL_DEF void gbgl_bs_draw_quad_outline(gbglBasicState *bs,
736
+ f32 x0, f32 y0,
737
+ f32 x1, f32 y1,
738
+ f32 x2, f32 y2,
739
+ f32 x3, f32 y3,
740
+ gbglColour col, f32 thickness);
741
+
742
+ GBGL_DEF void gbgl_bs_draw_line(gbglBasicState *bs, f32 x0, f32 y0, f32 x1, f32 y1, gbglColour col, f32 thickness);
743
+
744
+ GBGL_DEF void gbgl_bs_draw_elliptical_arc(gbglBasicState *bs, f32 x, f32 y, f32 radius_a, f32 radius_b, f32 min_angle, f32 max_angle, gbglColour col);
745
+ GBGL_DEF void gbgl_bs_draw_elliptical_arc_outline(gbglBasicState *bs, f32 x, f32 y, f32 radius_a, f32 radius_b,
746
+ f32 min_angle, f32 max_angle, gbglColour col, f32 thickness);
747
+
748
+ GBGL_DEF void gbgl_bs_draw_circle(gbglBasicState *bs, f32 x, f32 y, f32 radius, gbglColour col);
749
+ GBGL_DEF void gbgl_bs_draw_circle_outline(gbglBasicState *bs, f32 x, f32 y, f32 radius, gbglColour col, f32 thickness);
750
+
751
+
752
+ // Corners Flags:
753
+ // 1 - Bottom Left
754
+ // 2 - Bottom Right
755
+ // 4 - Top Right
756
+ // 8 - Top Left
757
+ // NOTE(bill): Apple, please don't sue me!
758
+ GBGL_DEF void gbgl_bs_draw_rounded_rect_corners(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, u32 corners);
759
+ GBGL_DEF void gbgl_bs_draw_rounded_rect(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col);
760
+
761
+ GBGL_DEF void gbgl_bs_draw_rounded_rect_corners_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, f32 thickness, u32 corners);
762
+ GBGL_DEF void gbgl_bs_draw_rounded_rect_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, f32 thickness);
763
+
764
+
765
+ #if !defined(GBGL_NO_FONTS)
766
+ GBGL_DEF isize gbgl_bs_draw_substring(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *str, isize len);
767
+ GBGL_DEF isize gbgl_bs_draw_string (gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *fmt, ...);
768
+ GBGL_DEF isize gbgl_bs_draw_string_va(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *fmt, va_list va);
769
+ #endif
770
+
771
+
772
+ #endif
773
+
774
+ #if defined(__cplusplus)
775
+ }
776
+ #endif
777
+
778
+ #endif
779
+
780
+ ////////////////////////////////////////////////////////////////
781
+ // //
782
+ // //
783
+ // //
784
+ // //
785
+ // //
786
+ // //
787
+ // //
788
+ // //
789
+ // //
790
+ // //
791
+ // //
792
+ // //
793
+ // //
794
+ // //
795
+ // //
796
+ // //
797
+ // //
798
+ // //
799
+ // //
800
+ // //
801
+ // //
802
+ // Implementation //
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
+ #if defined(GBGL_IMPLEMENTATION)
830
+
831
+ gb_inline gbglColour gbgl_colour(f32 r, f32 g, f32 b, f32 a) {
832
+ gbglColour result;
833
+ result.r = cast(u8)(gbgl_clamp01(r) * 255.0f);
834
+ result.g = cast(u8)(gbgl_clamp01(g) * 255.0f);
835
+ result.b = cast(u8)(gbgl_clamp01(b) * 255.0f);
836
+ result.a = cast(u8)(gbgl_clamp01(a) * 255.0f);
837
+ return result;
838
+ }
839
+
840
+
841
+ u32 gbgl_make_sampler(u32 min_filter, u32 max_filter, u32 s_wrap, u32 t_wrap) {
842
+ u32 samp;
843
+ glGenSamplers(1, &samp);
844
+ glSamplerParameteri(samp, GL_TEXTURE_MIN_FILTER, min_filter);
845
+ glSamplerParameteri(samp, GL_TEXTURE_MAG_FILTER, max_filter);
846
+ glSamplerParameteri(samp, GL_TEXTURE_WRAP_S, s_wrap);
847
+ glSamplerParameteri(samp, GL_TEXTURE_WRAP_T, t_wrap);
848
+ return samp;
849
+ }
850
+
851
+
852
+ ////////////////////////////////////////////////////////////////
853
+ //
854
+ // Data Buffers
855
+ //
856
+ //
857
+
858
+ gb_inline u32 gbgl__make_buffer(isize size, void const *data, i32 target, i32 usage_hint) {
859
+ u32 buffer_handle;
860
+ glGenBuffers(1, &buffer_handle);
861
+ glBindBuffer(target, buffer_handle);
862
+ glBufferData(target, size, data, usage_hint);
863
+ return buffer_handle;
864
+ }
865
+
866
+ gb_inline void gbgl__buffer_copy(u32 buffer_handle, i32 target, void const *data, isize size, isize offset) {
867
+ glBindBuffer(target, buffer_handle);
868
+ glBufferSubData(target, offset, size, data);
869
+ }
870
+
871
+ // NOTE(bill): usage_hint == (GL_STATIC_DRAW, GL_STREAM_DRAW, GL_DYNAMIC_DRAW)
872
+ gb_inline u32 gbgl_make_vbo(void const *data, isize size, i32 usage_hint) {
873
+ return gbgl__make_buffer(size, data, GL_ARRAY_BUFFER, usage_hint);
874
+ }
875
+
876
+ gb_inline u32 gbgl_make_ebo(void const *data, isize size, i32 usage_hint) {
877
+ return gbgl__make_buffer(size, data, GL_ELEMENT_ARRAY_BUFFER, usage_hint);
878
+ }
879
+
880
+ gb_inline gbglTBO gbgl_make_tbo(gbglBufferDataType data_type, i32 channel_count, void const *data, isize size, i32 usage_hint) {
881
+ gbglTBO tbo;
882
+ i32 internal_format;
883
+
884
+ tbo.buffer_obj_handle = gbgl__make_buffer(size, data, GL_TEXTURE_BUFFER, usage_hint);
885
+
886
+ glGenTextures(1, &tbo.buffer_handle);
887
+ glBindTexture(GL_TEXTURE_BUFFER, tbo.buffer_handle);
888
+ internal_format = gbgl_texture_format(data_type, channel_count);
889
+ glTexBuffer(GL_TEXTURE_BUFFER, internal_format, tbo.buffer_obj_handle);
890
+ return tbo;
891
+ }
892
+
893
+ gb_inline void gbgl_vbo_copy(u32 vbo_handle, void *const data, isize size, isize offset) {
894
+ gbgl__buffer_copy(vbo_handle, GL_ARRAY_BUFFER, data, size, offset);
895
+ }
896
+
897
+ gb_inline void gbgl_ebo_copy(u32 ebo_handle, void *const data, isize size, isize offset) {
898
+ gbgl__buffer_copy(ebo_handle, GL_ELEMENT_ARRAY_BUFFER, data, size, offset);
899
+ }
900
+
901
+ gb_inline void gbgl_tbo_copy(gbglTBO tbo, void *const data, isize size, isize offset) {
902
+ gbgl__buffer_copy(tbo.buffer_obj_handle, GL_TEXTURE_BUFFER, data, size, offset);
903
+ }
904
+
905
+ gb_inline void gbgl_bind_vbo(u32 vbo_handle) { glBindBuffer(GL_ARRAY_BUFFER, vbo_handle); }
906
+ gb_inline void gbgl_bind_ebo(u32 ebo_handle) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_handle); }
907
+
908
+ gb_inline void gbgl_bind_tbo(gbglTBO tbo, i32 sampler_handle, i32 tex_unit) {
909
+ glActiveTexture(GL_TEXTURE0 + tex_unit);
910
+ glBindTexture(GL_TEXTURE_BUFFER, tbo.buffer_handle);
911
+ glBindSampler(0, sampler_handle);
912
+ }
913
+
914
+ // NOTE(bill): access = GL_WRITE_ONLY, etc.
915
+ gb_inline void * gbgl_map_vbo(u32 vbo_handle, i32 access) {
916
+ gbgl_bind_vbo(vbo_handle);
917
+ return glMapBuffer(GL_ARRAY_BUFFER, access);
918
+ }
919
+
920
+ gb_inline void * gbgl_map_ebo(u32 ebo_handle, i32 access) {
921
+ gbgl_bind_ebo(ebo_handle);
922
+ return glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, access);
923
+ }
924
+
925
+ gb_inline void gbgl_unmap_vbo(void) { glUnmapBuffer(GL_ARRAY_BUFFER); }
926
+ gb_inline void gbgl_unmap_ebo(void) { glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); }
927
+
928
+
929
+
930
+ ////////////////////////////////////////////////////////////////
931
+ //
932
+ // Shader
933
+ //
934
+ //
935
+
936
+
937
+ gbglShaderError gbgl__load_single_shader_from_file(gbglShader *shader, gbglShaderType type, char const *name) {
938
+ gbglShaderError err = gbglShaderError_None;
939
+ gbFileError ferr;
940
+ gb_local_persist char filepath[1024];
941
+ gb_snprintf(filepath, gb_count_of(filepath), "%s%s", name, gbglShaderFileExtensions[type]);
942
+ ferr = gb_file_open(&shader->files[type], filepath);
943
+
944
+ if (ferr != gbFileError_None) {
945
+ err = gbglShaderError_UnableToReadFile;
946
+ } else {
947
+ gb_local_persist char info_log[4096];
948
+ i64 file_size = gb_file_size(&shader->files[type]);
949
+ char *file_source = cast(char *)gbgl_malloc(file_size+1);
950
+
951
+ GB_ASSERT_NOT_NULL(file_source);
952
+ if (file_source) {
953
+ i32 params;
954
+
955
+ gb_file_read_at(&shader->files[type], file_source, file_size, 0);
956
+ file_source[file_size] = '\0';
957
+
958
+ shader->shaders[type] = glCreateShader(gbglShaderMap[type]);
959
+ glShaderSource(shader->shaders[type], 1, &file_source, NULL);
960
+ glCompileShader(shader->shaders[type]);
961
+ glGetShaderiv(shader->shaders[type], GL_COMPILE_STATUS, &params);
962
+ if (!params) {
963
+ gb_printf_err("Shader Source:\n%s\n", file_source);
964
+ glGetShaderInfoLog(shader->shaders[type], gb_size_of(info_log), NULL, info_log);
965
+ gb_printf_err("Shader compilation failed:\n %s\n", info_log);
966
+
967
+ err = gbglShaderError_ShaderCompile;
968
+ }
969
+
970
+ gbgl_free(file_source);
971
+ }
972
+ gb_file_close(&shader->files[type]);
973
+ }
974
+
975
+ return err;
976
+ }
977
+
978
+ gbglShaderError gbgl__load_single_shader_from_memory(gbglShader *s, gbglShaderType type, char const *text) {
979
+ gbglShaderError err = gbglShaderError_None;
980
+ i32 status;
981
+
982
+ s->shaders[type] = glCreateShader(gbglShaderMap[type]);
983
+ glShaderSource(s->shaders[type], 1, &text, 0);
984
+ glCompileShader(s->shaders[type]);
985
+
986
+ glGetShaderiv(s->shaders[type], GL_COMPILE_STATUS, &status);
987
+ if (!status) {
988
+ gb_local_persist char log_info[4096];
989
+ i32 total_len, log_len;
990
+
991
+ gb_printf_err("Unable to compile shader: %s\n", text);
992
+ glGetShaderiv(s->shaders[type], GL_INFO_LOG_LENGTH, &status);
993
+ total_len = status;
994
+
995
+ glGetShaderInfoLog(s->shaders[type], 4095, &log_len, log_info);
996
+ gb_printf_err(log_info);
997
+ err = gbglShaderError_ShaderCompile;
998
+ }
999
+
1000
+ return err;
1001
+ }
1002
+
1003
+ gbglShaderError gbgl__link_shader(gbglShader *shader) {
1004
+ gbglShaderError err = gbglShaderError_None;
1005
+ i32 i, status;
1006
+ shader->program = glCreateProgram();
1007
+ for (i = 0; i < gbglShader_Count; i++) {
1008
+ if (shader->type_flags & GB_BIT(i))
1009
+ glAttachShader(shader->program, shader->shaders[i]);
1010
+ }
1011
+
1012
+ glLinkProgram(shader->program);
1013
+
1014
+ glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
1015
+ if (!status) {
1016
+ gb_local_persist char log_info[4096];
1017
+ glGetProgramInfoLog(shader->program, gb_size_of(log_info), NULL, log_info);
1018
+ gb_printf_err("Shader linking failed:\n %s \n", log_info);
1019
+ err = gbglShaderError_Linking;
1020
+ }
1021
+
1022
+ for (i = 0; i < gbglShader_Count; i++) {
1023
+ if (shader->type_flags & GB_BIT(i))
1024
+ glDetachShader(shader->program, shader->shaders[i]);
1025
+ }
1026
+
1027
+ return err;
1028
+ }
1029
+
1030
+
1031
+
1032
+ gbglShaderError gbgl_load_shader_from_file(gbglShader *shader, u32 type_bits, char const *filename) {
1033
+ gbglShaderError err = gbglShaderError_None;
1034
+ b32 loaded_shader[gbglShader_Count] = {0};
1035
+ i32 i;
1036
+
1037
+ gb_zero_item(shader);
1038
+ shader->type_flags = type_bits;
1039
+ gb_strncpy(shader->base_name, filename, gb_size_of(shader->base_name));
1040
+
1041
+ for (i = 0; i < gbglShader_Count; i++) {
1042
+ if (type_bits & GB_BIT(i)) {
1043
+ err = gbgl__load_single_shader_from_file(shader, cast(gbglShaderType)i, filename);
1044
+ if (err != gbglShaderError_None)
1045
+ return err;
1046
+ loaded_shader[i] = true;
1047
+ }
1048
+ }
1049
+ err = gbgl__link_shader(shader);
1050
+
1051
+ return err;
1052
+ }
1053
+
1054
+
1055
+
1056
+
1057
+ gbglShaderError gbgl_load_shader_from_memory_vf(gbglShader *s, char const *vert_source, char const *frag_source) {
1058
+ gbglShaderError err = gbglShaderError_None;
1059
+
1060
+ gb_zero_item(s);
1061
+ s->type_flags = GB_BIT(gbglShader_Vertex) | GB_BIT(gbglShader_Fragment);
1062
+
1063
+ err = gbgl__load_single_shader_from_memory(s, gbglShader_Vertex, vert_source);
1064
+ if (err != gbglShaderError_None) return err;
1065
+ err = gbgl__load_single_shader_from_memory(s, gbglShader_Fragment, frag_source);
1066
+ if (err != gbglShaderError_None) return err;
1067
+
1068
+ err = gbgl__link_shader(s);
1069
+
1070
+ return err;
1071
+ }
1072
+
1073
+ gbglShaderError gbgl_load_shader_from_memory_vfg(gbglShader *s, char const *vert_source, char const *frag_source, char const *geom_source) {
1074
+ gbglShaderError err = gbglShaderError_None;
1075
+
1076
+ gb_zero_item(s);
1077
+ s->type_flags = GB_BIT(gbglShader_Vertex) | GB_BIT(gbglShader_Fragment) | GB_BIT(gbglShader_Geometry);
1078
+
1079
+ err = gbgl__load_single_shader_from_memory(s, gbglShader_Vertex, vert_source);
1080
+ if (err != gbglShaderError_None) return err;
1081
+ err = gbgl__load_single_shader_from_memory(s, gbglShader_Fragment, frag_source);
1082
+ if (err != gbglShaderError_None) return err;
1083
+ err = gbgl__load_single_shader_from_memory(s, gbglShader_Geometry, geom_source);
1084
+ if (err != gbglShaderError_None) return err;
1085
+
1086
+ err = gbgl__link_shader(s);
1087
+
1088
+ return err;
1089
+ }
1090
+
1091
+ gb_inline void gbgl_destroy_shader(gbglShader *shader) {
1092
+ i32 i;
1093
+ for (i = 0; i < gbglShader_Count; i++) {
1094
+ if (shader->type_flags & GB_BIT(i)) {
1095
+ gb_file_close(&shader->files[i]);
1096
+ glDeleteShader(shader->shaders[i]);
1097
+ }
1098
+ }
1099
+
1100
+ glDeleteProgram(shader->program);
1101
+
1102
+ for (i = 0; i < shader->uniform_count; i++) {
1103
+ gbgl_free(shader->uniform_names[i]);
1104
+ }
1105
+ }
1106
+
1107
+
1108
+ gb_inline b32 gbgl_has_shader_changed(gbglShader *shader) {
1109
+ i32 i;
1110
+ for (i = 0; i < gbglShader_Count; i++) {
1111
+ if (shader->type_flags & GB_BIT(i)) {
1112
+ if (gb_file_has_changed(&shader->files[i])) {
1113
+ return true;
1114
+ }
1115
+ }
1116
+ }
1117
+ return false;
1118
+ }
1119
+
1120
+
1121
+ b32 gbgl_reload_shader(gbglShader *shader) {
1122
+ i32 i;
1123
+ for (i = 0; i < gbglShader_Count; i++) {
1124
+ if (shader->type_flags & GB_BIT(i)) {
1125
+ if (gbgl__load_single_shader_from_file(shader, cast(gbglShaderType)i, shader->base_name) != gbglShaderError_None)
1126
+ return false;
1127
+ }
1128
+ }
1129
+
1130
+ if (gbgl__link_shader(shader) != gbglShaderError_None)
1131
+ return false;
1132
+
1133
+ for (i = 0; i < shader->uniform_count; i++)
1134
+ shader->uniform_locs[i] = glGetUniformLocation(shader->program, shader->uniform_names[i]);
1135
+
1136
+
1137
+ return true;
1138
+ }
1139
+
1140
+ gb_inline void gbgl_use_shader(gbglShader *s) { glUseProgram(s ? s->program : 0); }
1141
+
1142
+ gb_inline b32 gbgl_is_shader_in_use(gbglShader *s) {
1143
+ if (s) {
1144
+ i32 curr = 0;
1145
+ glGetIntegerv(GL_CURRENT_PROGRAM, &curr);
1146
+ return (curr == cast(i32)s->program);
1147
+ }
1148
+ return false;
1149
+ }
1150
+
1151
+
1152
+ i32 gbgl_get_uniform(gbglShader *s, char const *name) {
1153
+ i32 i, loc = -1;
1154
+ for (i = 0; i < s->uniform_count; i++) {
1155
+ if (gb_strcmp(s->uniform_names[i], name) == 0) {
1156
+ return s->uniform_locs[i];
1157
+ }
1158
+ }
1159
+
1160
+ GB_ASSERT_MSG(s->uniform_count < GBGL_MAX_UNIFORM_COUNT,
1161
+ "Uniform array for shader is full");
1162
+
1163
+ loc = glGetUniformLocation(s->program, name);
1164
+ s->uniform_names[s->uniform_count] = gb_alloc_str(gb_heap_allocator(), name);
1165
+ s->uniform_locs[s->uniform_count] = loc;
1166
+ s->uniform_count++;
1167
+
1168
+ return loc;
1169
+ }
1170
+
1171
+
1172
+
1173
+ gb_inline void gbgl_set_uniform_int(gbglShader *s, char const *name, i32 i) {
1174
+ glUniform1i(gbgl_get_uniform(s, name), i);
1175
+ }
1176
+
1177
+ gb_inline void gbgl_set_uniform_float(gbglShader *s, char const *name, f32 f) {
1178
+ glUniform1f(gbgl_get_uniform(s, name), f);
1179
+ }
1180
+
1181
+ gb_inline void gbgl_set_uniform_vec2(gbglShader *s, char const *name, f32 const *v) {
1182
+ glUniform2fv(gbgl_get_uniform(s, name), 1, v);
1183
+ }
1184
+
1185
+ gb_inline void gbgl_set_uniform_vec3(gbglShader *s, char const *name, f32 const *v) {
1186
+ glUniform3fv(gbgl_get_uniform(s, name), 1, v);
1187
+ }
1188
+
1189
+ gb_inline void gbgl_set_uniform_vec4(gbglShader *s, char const *name, f32 const *v) {
1190
+ glUniform4fv(gbgl_get_uniform(s, name), 1, v);
1191
+ }
1192
+
1193
+ gb_inline void gbgl_set_uniform_mat4(gbglShader *s, char const *name, f32 const *m) {
1194
+ gbgl_set_uniform_mat4_count(s, name, m, 1);
1195
+ }
1196
+
1197
+ gb_inline void gbgl_set_uniform_mat4_count(gbglShader *s, char const *name, f32 const *m, isize count) {
1198
+ glUniformMatrix4fv(gbgl_get_uniform(s, name), count, false, m);
1199
+ }
1200
+
1201
+
1202
+ gb_inline void gbgl_set_uniform_colour(gbglShader *s, char const *name, gbglColour col) {
1203
+ f32 v[4];
1204
+ v[0] = col.r / 255.0f;
1205
+ v[1] = col.g / 255.0f;
1206
+ v[2] = col.b / 255.0f;
1207
+ v[3] = col.a / 255.0f;
1208
+ gbgl_set_uniform_vec4(s, name, v);
1209
+ }
1210
+
1211
+
1212
+
1213
+ ////////////////////////////////////////////////////////////////
1214
+ //
1215
+ // Render Buffer
1216
+ //
1217
+ //
1218
+
1219
+
1220
+ b32 gbgl_init_render_buffer(gbglRenderBuffer *rb, i32 width, i32 height, i32 channel_count) {
1221
+ if ((rb->width == width) && (rb->height == height) && (rb->channel_count == channel_count)) return true;
1222
+ gbgl_destroy_render_buffer(rb);
1223
+ gb_zero_item(rb);
1224
+
1225
+ rb->width = width;
1226
+ rb->height = height;
1227
+
1228
+ glEnable(GL_FRAMEBUFFER_SRGB);
1229
+
1230
+ glGenFramebuffers(1, &rb->handle);
1231
+ glBindFramebuffer(GL_FRAMEBUFFER, rb->handle);
1232
+
1233
+ gbgl_load_texture2d_from_memory(&rb->colour_texture, NULL, width, height, channel_count);
1234
+ glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rb->colour_texture.handle, 0);
1235
+
1236
+ glBindTexture(GL_TEXTURE_2D, 0);
1237
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
1238
+
1239
+ {
1240
+ u32 draw_buffers[] = {GL_COLOR_ATTACHMENT0};
1241
+ glDrawBuffers(gb_count_of(draw_buffers), draw_buffers);
1242
+ }
1243
+
1244
+ {
1245
+ u32 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1246
+ if (status != GL_FRAMEBUFFER_COMPLETE) {
1247
+ gb_printf_err("Framebuffer Status: 0x%x\n", status);
1248
+ return false;
1249
+ }
1250
+ }
1251
+
1252
+ return true;
1253
+ }
1254
+
1255
+ gb_inline void gbgl_destroy_render_buffer(gbglRenderBuffer *rb) {
1256
+ if (rb->handle)
1257
+ glDeleteFramebuffers(1, &rb->handle);
1258
+
1259
+ gbgl_destroy_texture(&rb->colour_texture);
1260
+ }
1261
+
1262
+
1263
+ gb_inline void gbgl_render_to_buffer(gbglRenderBuffer const *rb) {
1264
+ GB_ASSERT_NOT_NULL(rb);
1265
+ glViewport(0, 0, rb->width, rb->height);
1266
+ glBindFramebuffer(GL_FRAMEBUFFER, rb->handle);
1267
+ }
1268
+
1269
+ gb_inline void gbgl_render_to_screen(i32 width, i32 height) {
1270
+ glViewport(0, 0, width, height);
1271
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
1272
+ }
1273
+
1274
+
1275
+ ////////////////////////////////////////////////////////////////
1276
+ //
1277
+ // Texture
1278
+ //
1279
+ //
1280
+
1281
+
1282
+ b32 gbgl_load_texture2d_from_memory(gbglTexture *tex, void const *data, i32 width, i32 height, i32 channel_count) {
1283
+ b32 result = true;
1284
+
1285
+ gb_zero_item(tex);
1286
+
1287
+ tex->width = width;
1288
+ tex->height = height;
1289
+ tex->channel_count = channel_count;
1290
+ tex->data_type = gbglBufferData_u8;
1291
+ tex->type = gbglgTexture_2D;
1292
+
1293
+ glGenTextures(1, &tex->handle);
1294
+ glBindTexture(GL_TEXTURE_2D, tex->handle);
1295
+
1296
+
1297
+ glTexImage2D(GL_TEXTURE_2D, 0,
1298
+ gbglInternalTextureFormat_8[channel_count-1],
1299
+ width, height, 0,
1300
+ gbglTextureFormat[channel_count-1],
1301
+ GL_UNSIGNED_BYTE, data);
1302
+
1303
+ glGenerateMipmap(GL_TEXTURE_2D);
1304
+
1305
+ glBindTexture(GL_TEXTURE_2D, 0);
1306
+ glFinish();
1307
+
1308
+
1309
+ return result;
1310
+ }
1311
+
1312
+ b32 gbgl_load_texture2d_from_file(gbglTexture *texture, b32 flip_vertically, char const *filename, ...) {
1313
+ b32 result;
1314
+ u8 *data;
1315
+ int width, height, comp;
1316
+ char *path;
1317
+
1318
+ va_list va;
1319
+ va_start(va, filename);
1320
+ path = gb_bprintf_va(filename, va);
1321
+ va_end(va);
1322
+
1323
+ stbi_set_flip_vertically_on_load(flip_vertically);
1324
+ data = stbi_load(path, &width, &height, &comp, 0);
1325
+ if (data == NULL) {
1326
+ gb_printf_err("Failed to load image: %s\n", path);
1327
+ result = false;
1328
+ } else {
1329
+ result = gbgl_load_texture2d_from_memory(texture, data, width, height, comp);
1330
+ stbi_image_free(data);
1331
+ }
1332
+ return result;
1333
+ }
1334
+
1335
+ gb_inline b32 gbgl_make_texture2d_coloured(gbglTexture *t, gbglColour colour) {
1336
+ return gbgl_load_texture2d_from_memory(t, &colour.rgba, 1, 1, 4);
1337
+ }
1338
+
1339
+
1340
+ gb_inline void gbgl_bind_texture2d(gbglTexture const *t, u32 position, u32 sampler) {
1341
+ if (t != NULL) {
1342
+ GB_ASSERT(t->type == gbglgTexture_2D);
1343
+ }
1344
+
1345
+ if (position > 31) {
1346
+ position = 31;
1347
+ gb_printf_err("Textures can only bound to position [0 ... 31]\n");
1348
+ gb_printf_err("Will bind to position [31]\n");
1349
+ }
1350
+
1351
+ glActiveTexture(GL_TEXTURE0 + position);
1352
+ glEnable(GL_TEXTURE_2D);
1353
+ glBindTexture(GL_TEXTURE_2D, t ? t->handle : 0);
1354
+ glBindSampler(position, sampler);
1355
+ }
1356
+
1357
+ gb_inline void gbgl_destroy_texture(gbglTexture *t) {
1358
+ if (t->handle) {
1359
+ glDeleteTextures(1, &t->handle);
1360
+ }
1361
+ }
1362
+
1363
+
1364
+
1365
+ ////////////////////////////////////////////////////////////////
1366
+ //
1367
+ // Font
1368
+ //
1369
+ //
1370
+ #if !defined(GBGL_NO_FONTS)
1371
+ gb_inline GB_COMPARE_PROC(gbgl__kern_pair_compare) {
1372
+ gbglKernPair *kp0 = cast(gbglKernPair *)a;
1373
+ gbglKernPair *kp1 = cast(gbglKernPair *)b;
1374
+ return kp0->packed - kp1->packed;
1375
+ }
1376
+
1377
+
1378
+ gb_inline GB_COMPARE_PROC(gbgl__glyph_map_compare) {
1379
+ gbglGlyphMapKVPair g0 = *cast(gbglGlyphMapKVPair *)a;
1380
+ gbglGlyphMapKVPair g1 = *cast(gbglGlyphMapKVPair *)b;
1381
+ return g0.codepoint - g1.codepoint;
1382
+ }
1383
+
1384
+
1385
+
1386
+
1387
+ b32 gbgl_get_packed_font_dim(gbglFontCache *cache, gbglFontCachedTTF *ttf, i32 *width, i32 *height) {
1388
+ b32 result = true;
1389
+ stbtt_pack_context spc;
1390
+ b32 ext_w = true;
1391
+ isize sanity_counter = 0, i, j;
1392
+ *width = *height = (1<<5);
1393
+ for (j = 0; j < cache->codepoint_count; j++) {
1394
+ cache->rect_cache[j].x = cache->rect_cache[j].y = 0;
1395
+ }
1396
+
1397
+ for (;;) {
1398
+ i32 res = stbtt_PackBegin(&spc, NULL, *width, *height, 0, 1, 0);
1399
+ GB_ASSERT(res == 1);
1400
+ if (res != 1) {
1401
+ return false;
1402
+ } else {
1403
+ stbrp_context *rp_ctx;
1404
+ b32 fit = true;
1405
+
1406
+ gb_zero_array(cache->rect_cache, cache->codepoint_count);
1407
+ rp_ctx = cast(stbrp_context *)spc.pack_info;
1408
+ stbtt_PackFontRangesGatherRects(&spc, &ttf->finfo, cache->ranges, cache->codepoint_count, cache->rect_cache);
1409
+ gb_sort_array(cache->rect_cache, cache->codepoint_count, rect_height_compare);
1410
+
1411
+ for (i = 0; i < cache->codepoint_count; i++) {
1412
+ stbrp__findresult fr = stbrp__skyline_pack_rectangle(rp_ctx, cache->rect_cache[i].w, cache->rect_cache[i].h);
1413
+ if (fr.prev_link) {
1414
+ cache->rect_cache[i].x = cast(stbrp_coord)fr.x;
1415
+ cache->rect_cache[i].y = cast(stbrp_coord)fr.y;
1416
+ } else {
1417
+ int res;
1418
+ if (ext_w) {
1419
+ ext_w = false;
1420
+ *width <<= 1;
1421
+ } else {
1422
+ ext_w = true;
1423
+ *height <<= 1;
1424
+ }
1425
+ fit = false;
1426
+ for (j = 0; j < cache->codepoint_count; j++) {
1427
+ cache->rect_cache[j].x = cache->rect_cache[j].y = 0;
1428
+ }
1429
+ stbtt_PackEnd(&spc);
1430
+ res = stbtt_PackBegin(&spc, NULL, *width, *height, 0, 1, 0);
1431
+ GB_ASSERT(res == 1);
1432
+ if (res != 1) {
1433
+ result = false;
1434
+ goto done;
1435
+ }
1436
+ break;
1437
+ }
1438
+ }
1439
+ if (fit) {
1440
+ result = true;
1441
+ goto done;
1442
+ }
1443
+ if (++sanity_counter > 32) {
1444
+ result = false;
1445
+ goto done;
1446
+ }
1447
+ }
1448
+ }
1449
+
1450
+ done:
1451
+ stbtt_PackEnd(&spc);
1452
+
1453
+ return result;
1454
+ }
1455
+
1456
+ #if 0
1457
+ void gbgl_destroy_font_cache(gbglFontCache *fc) {
1458
+ gbglFontCachedTTF *curr_ttf = fc->ttf_buffer;
1459
+ gbglFontCachedTTF *next_ttf = NULL;
1460
+
1461
+ gbglFont *curr_font = fc->fonts;
1462
+ gbglFont *next_font = NULL;
1463
+
1464
+ gbgl_free(fc->font_char_list);
1465
+ gbgl_free(fc->codepoints);
1466
+ gbgl_free(fc->ranges);
1467
+ gbgl_free(fc->packed_char_data);
1468
+ gbgl_free(fc->rect_cache);
1469
+
1470
+ // NOTE(bill): Free all linked listed ttfs
1471
+ while (curr_ttf) {
1472
+ gbgl_free(curr_ttf->name);
1473
+ gbgl_free(curr_ttf->ttf);
1474
+
1475
+ next_ttf = curr_ttf->next;
1476
+ gbgl_free(curr_ttf);
1477
+ curr_ttf = next_ttf;
1478
+ }
1479
+
1480
+ // NOTE(bill): Free all linked listed fonts
1481
+ while (curr_font) {
1482
+ gbgl_free(curr_font->ttf_filename);
1483
+ gbgl_destroy_texture(&curr_font->texture);
1484
+ gbgl_free(curr_font->glyph_map);
1485
+ gbgl_free(curr_font->glyphs);
1486
+ gbgl_free(curr_font->kern_table);
1487
+
1488
+ next_font = curr_font->next;
1489
+ gbgl_free(curr_font);
1490
+ curr_font = next_font;
1491
+ }
1492
+ }
1493
+ #endif
1494
+
1495
+
1496
+ gb_inline gbglFont * gbgl_load_font_from_file(gbglFontCache *fc, char const *ttf_filename, f32 font_size) {
1497
+ gbglFont *f = gbgl_get_font_only_from_cache(fc, ttf_filename, font_size);
1498
+ if (f) return f;
1499
+ return gbgl_cache_font(fc, ttf_filename, font_size);
1500
+ }
1501
+
1502
+
1503
+ gb_inline gbglFont * gbgl_get_font_only_from_cache(gbglFontCache *fc, char const *ttf_filename, f32 font_size) {
1504
+ gbglFont *f = fc->fonts;
1505
+ while (f) {
1506
+ if (f->size == font_size && gb_strcmp(ttf_filename, f->ttf_filename) == 0) {
1507
+ return f;
1508
+ }
1509
+ f = f->next;
1510
+ }
1511
+ return NULL;
1512
+ }
1513
+
1514
+ gbglFont * gbgl_cache_font(gbglFontCache *fc, char const *ttf_filename, f32 font_size) {
1515
+ gbglFont *f = gbgl_get_font_only_from_cache(fc, ttf_filename, font_size);
1516
+ gbglFontCachedTTF *ttf = NULL;
1517
+ isize i;
1518
+
1519
+ if (f) { // NOTE(bill): The font is already cached
1520
+ return f;
1521
+ }
1522
+
1523
+ if (!fc->fonts) {
1524
+ fc->fonts = cast(gbglFont *)gbgl_malloc(gb_size_of(gbglFont));
1525
+ f = fc->fonts;
1526
+ } else {
1527
+ f = fc->fonts;
1528
+ while (f && f->next)
1529
+ f = f->next;
1530
+ f->next = cast(gbglFont *)gbgl_malloc(gb_size_of(gbglFont));
1531
+ f = f->next;
1532
+ }
1533
+ GB_ASSERT_NOT_NULL(f);
1534
+ if (!f) {
1535
+ gb_printf_err("Failed to cache font\n");
1536
+ return NULL;
1537
+ }
1538
+
1539
+ gb_zero_item(f);
1540
+
1541
+ // NOTE(bill): Make sure the character list file has been loaded
1542
+ if (!fc->font_char_list) {
1543
+ isize codepoint_count = 0, cpi = 0;
1544
+ fc->font_char_list = GBGL_FONT_CHAR_LIST;
1545
+ fc->font_char_list_count = gb_strlen(GBGL_FONT_CHAR_LIST);
1546
+
1547
+ for (i = 0; i < fc->font_char_list_count; i++) {
1548
+ char32 c;
1549
+ isize utf8_len = gb_utf8_decode_len(fc->font_char_list + i, fc->font_char_list_count, &c);
1550
+ i += utf8_len-1;
1551
+ codepoint_count++;
1552
+ }
1553
+
1554
+ fc->codepoint_count = codepoint_count;
1555
+ fc->ranges = cast(stbtt_pack_range *)gbgl_malloc(gb_size_of(stbtt_pack_range) * codepoint_count);
1556
+ fc->codepoints = cast(char32 *) gbgl_malloc(gb_size_of(char32) * codepoint_count);
1557
+ fc->packed_char_data = cast(stbtt_packedchar *)gbgl_malloc(gb_size_of(stbtt_packedchar) * codepoint_count);
1558
+ fc->rect_cache = cast(stbrp_rect *) gbgl_malloc(gb_size_of(stbrp_rect) * codepoint_count);
1559
+
1560
+ if (!fc->ranges || !fc->codepoints || !fc->packed_char_data) {
1561
+ gb_printf_err("Unable to get memory for fonts");
1562
+ }
1563
+
1564
+ for (i = 0; i < fc->font_char_list_count; i++) {
1565
+ isize utf8_len = gb_utf8_decode_len(fc->font_char_list+i, fc->font_char_list_count, fc->codepoints+cpi);
1566
+ i += utf8_len-1;
1567
+ cpi++;
1568
+ }
1569
+ GB_ASSERT(cpi == fc->codepoint_count);
1570
+ for (i = 0; i < fc->codepoint_count; i++) {
1571
+ fc->ranges[i].first_unicode_codepoint_in_range = fc->codepoints[i];
1572
+ fc->ranges[i].array_of_unicode_codepoints = 0;
1573
+ fc->ranges[i].num_chars = 1;
1574
+ fc->ranges[i].chardata_for_range = fc->packed_char_data + i;
1575
+ }
1576
+ }
1577
+
1578
+ {
1579
+ gbglFontCachedTTF **ttf_cache = &fc->ttf_buffer;
1580
+
1581
+ while (*ttf_cache) {
1582
+ if (gb_strcmp((*ttf_cache)->name, ttf_filename) == 0)
1583
+ break;
1584
+ ttf_cache = &(*ttf_cache)->next;
1585
+ }
1586
+ if (!*ttf_cache) {
1587
+ isize name_len;
1588
+ gbFile file;
1589
+
1590
+
1591
+ *ttf_cache = cast(gbglFontCachedTTF *)gbgl_malloc(gb_size_of(gbglFontCachedTTF));
1592
+ GB_ASSERT_NOT_NULL(*ttf_cache);
1593
+ (*ttf_cache)->name = NULL;
1594
+ (*ttf_cache)->ttf = NULL;
1595
+ gb_zero_item(&(*ttf_cache)->finfo);
1596
+ (*ttf_cache)->next = NULL;
1597
+
1598
+
1599
+ name_len = gb_strlen(ttf_filename);
1600
+ (*ttf_cache)->name = cast(char *)gbgl_malloc(name_len+1);
1601
+ gb_strncpy((*ttf_cache)->name, ttf_filename, name_len);
1602
+ (*ttf_cache)->name[name_len] = '\0';
1603
+
1604
+ if (gb_file_open(&file, ttf_filename) == gbFileError_None) {
1605
+ i64 len = gb_file_size(&file);
1606
+ (*ttf_cache)->ttf = cast(u8 *)gbgl_malloc(len);
1607
+ GB_ASSERT_NOT_NULL((*ttf_cache)->ttf);
1608
+
1609
+ gb_file_read_at(&file, (*ttf_cache)->ttf, len, 0);
1610
+
1611
+ gb_file_close(&file);
1612
+ } else {
1613
+ GB_PANIC("Could not open ttf file");
1614
+ }
1615
+
1616
+ stbtt_InitFont(&(*ttf_cache)->finfo, (*ttf_cache)->ttf, stbtt_GetFontOffsetForIndex((*ttf_cache)->ttf, 0));
1617
+ }
1618
+ ttf = *ttf_cache;
1619
+ GB_ASSERT_NOT_NULL(ttf);
1620
+ }
1621
+
1622
+ // NOTE(bill): Set the range for the this look up
1623
+ for (i = 0; i < fc->codepoint_count; i++)
1624
+ fc->ranges[i].font_size = font_size;
1625
+
1626
+ { // NOTE(bill): Figure out smallest non-square power of 2 texture size
1627
+ i32 w, h;
1628
+ if (gbgl_get_packed_font_dim(fc, ttf, &w, &h)) {
1629
+ isize str_len, i, j;
1630
+
1631
+ // NOTE(bill): Setup the font data
1632
+ f->glyph_count = fc->codepoint_count;
1633
+ f->bitmap_width = w;
1634
+ f->bitmap_height = h;
1635
+ f->size = font_size;
1636
+
1637
+ str_len = gb_strlen(ttf_filename);
1638
+ f->ttf_filename = cast(char *)gbgl_malloc(str_len+1);
1639
+ gb_strncpy(f->ttf_filename, ttf_filename, str_len);
1640
+
1641
+ f->glyph_map = cast(gbglGlyphMapKVPair *)gbgl_malloc(gb_size_of(*f->glyph_map) * f->glyph_count);
1642
+ f->glyphs = cast(gbglGlyphInfo *) gbgl_malloc(gb_size_of(*f->glyphs) * f->glyph_count);
1643
+ if (!f->glyph_map || !f->glyphs) {
1644
+ f = NULL;
1645
+ return f;
1646
+ } else {
1647
+ stbtt_pack_context spc;
1648
+ u8 *px;
1649
+ i32 res;
1650
+ f32 scale;
1651
+
1652
+ px = cast(u8 *)gbgl_malloc(w * h);
1653
+ res = stbtt_PackBegin(&spc, px, w, h, 0, 1, NULL);
1654
+ GB_ASSERT(res == 1);
1655
+ res = stbtt_PackFontRanges(&spc, ttf->ttf, 0, fc->ranges, fc->codepoint_count);
1656
+ GB_ASSERT(res == 1);
1657
+ stbtt_PackEnd(&spc);
1658
+
1659
+ gbgl_load_texture2d_from_memory(&f->texture, px, w, h, 1);
1660
+
1661
+ gbgl_free(px);
1662
+
1663
+ scale = stbtt_ScaleForPixelHeight(&ttf->finfo, font_size);
1664
+ stbtt_GetFontVMetrics(&ttf->finfo, &f->ascent, &f->descent, &f->line_gap);
1665
+ f->ascent = cast(i32)(cast(f32)f->ascent * scale);
1666
+ f->descent = cast(i32)(cast(f32)f->descent * scale);
1667
+ f->line_gap = cast(i32)(cast(f32)f->line_gap * scale);
1668
+
1669
+ for (i = 0; i < f->glyph_count; i++) {
1670
+ gbglGlyphInfo *gi = f->glyphs + i;
1671
+ gi->s0 = cast(f32)fc->packed_char_data[i].x0;
1672
+ gi->t0 = cast(f32)fc->packed_char_data[i].y0;
1673
+ gi->s1 = cast(f32)fc->packed_char_data[i].x1;
1674
+ gi->t1 = cast(f32)fc->packed_char_data[i].y1;
1675
+
1676
+ gi->xoff = cast(i16)fc->packed_char_data[i].xoff;
1677
+ gi->yoff = cast(i16)fc->packed_char_data[i].yoff;
1678
+ gi->xadv = fc->packed_char_data[i].xadvance;
1679
+ }
1680
+
1681
+ for (i = 0; i < f->glyph_count; i++) {
1682
+ f->glyph_map[i].codepoint = fc->codepoints[i];
1683
+ f->glyph_map[i].index = i;
1684
+ }
1685
+
1686
+ gb_sort_array(f->glyph_map, f->glyph_count, gbgl__glyph_map_compare);
1687
+
1688
+ { // Kerning Table
1689
+ isize kps_count = 0;
1690
+ for (i = 0; i < f->glyph_count; i++) {
1691
+ for (j = 0; j < f->glyph_count; j++) {
1692
+ i32 kern = stbtt_GetCodepointKernAdvance(&ttf->finfo, fc->codepoints[i], fc->codepoints[j]);
1693
+ if (kern != 0)
1694
+ kps_count++;
1695
+ }
1696
+ }
1697
+ f->kern_pair_count = kps_count;
1698
+ if (kps_count > 0) {
1699
+ int ikp = 0;
1700
+ f->kern_table = cast(gbglKernPair *)gbgl_malloc(gb_size_of(*f->kern_table) * kps_count);
1701
+ for (i = 0; i < f->glyph_count; i++) {
1702
+ for (j = 0; j < f->glyph_count; j++) {
1703
+ isize kern = stbtt_GetCodepointKernAdvance(&ttf->finfo, fc->codepoints[i], fc->codepoints[j]);
1704
+ if (kern != 0) {
1705
+ gbglKernPair *kp = f->kern_table + ikp++;
1706
+ kp->i0 = cast(u16)i;
1707
+ kp->i1 = cast(u16)j;
1708
+ kp->kern = cast(f32)kern * scale;
1709
+ }
1710
+ }
1711
+ }
1712
+ gb_sort_array(f->kern_table, f->kern_pair_count, gbgl__kern_pair_compare);
1713
+ }
1714
+ }
1715
+ }
1716
+ } else {
1717
+ GB_PANIC("Failure loading font");
1718
+ gb_zero_item(&f);
1719
+ }
1720
+ }
1721
+ return f;
1722
+ }
1723
+
1724
+
1725
+ gb_inline GB_COMPARE_PROC(gbgl__font_glyph_map_search_proc) {
1726
+ gbglGlyphMapKVPair const *gm = cast(gbglGlyphMapKVPair const *)a;
1727
+ char32 ucp = *cast(char32 const *)b;
1728
+ return cast(i32)(cast(i64)gm->codepoint - cast(i64)ucp);
1729
+ }
1730
+
1731
+ gb_inline gbglGlyphInfo * gbgl_get_glyph_info(gbglFont *font, char32 codepoint, isize *out_index) {
1732
+ isize index = gb_binary_search_array(font->glyph_map, font->glyph_count, &codepoint, gbgl__font_glyph_map_search_proc);
1733
+ if (index >= 0) {
1734
+ GB_ASSERT(codepoint == font->glyph_map[index].codepoint);
1735
+ if (out_index)
1736
+ *out_index = font->glyph_map[index].index;
1737
+ return font->glyphs + font->glyph_map[index].index;
1738
+ }
1739
+ return NULL;
1740
+ }
1741
+
1742
+ gb_inline f32 gbgl_get_font_kerning_from_glyph_indices(gbglFont *font, isize left_index, isize right_index) {
1743
+ isize needle = (right_index << 16) | (left_index & 0xff);
1744
+
1745
+ isize f = 0;
1746
+ isize l = font->kern_pair_count - 1;
1747
+ isize m = (f + l) >> 1;
1748
+
1749
+ while (f <= l) {
1750
+ isize cmp = font->kern_table[m].packed - needle;
1751
+ if (cmp < 0)
1752
+ f = m + 1;
1753
+ else if (cmp > 0)
1754
+ l = m - 1;
1755
+ else
1756
+ return font->kern_table[m].kern;
1757
+ m = (f + l) >> 1;
1758
+ }
1759
+ return 0.0f;
1760
+ }
1761
+
1762
+ void gbgl_get_string_dimensions(gbglFont *font, char const *str, f32 *out_width, f32 *out_height) {
1763
+ isize len, char_count, i;
1764
+
1765
+ f32 w = 0.0f;
1766
+ f32 h = 0.0f;
1767
+ char const *ptr = str;
1768
+
1769
+ len = gb_strlen(str);
1770
+ char_count = gb_utf8_strnlen(str, len);
1771
+
1772
+ for (i = 0; i < char_count; i++) {
1773
+ char32 cp;
1774
+ isize byte_len, curr_index;
1775
+ gbglGlyphInfo *gi;
1776
+
1777
+ byte_len = gb_utf8_decode_len(ptr, len-(ptr-str), &cp);
1778
+ ptr += byte_len;
1779
+ gi = gbgl_get_glyph_info(font, cp, &curr_index);
1780
+ if (gi) {
1781
+ f32 kern = 0;
1782
+ if (i < char_count-1) {
1783
+ isize next_index;
1784
+ char32 next_cp = 0;
1785
+ gbglGlyphInfo *ngi;
1786
+ gb_utf8_decode_len(ptr, len-(ptr-str), &next_cp);
1787
+ ngi = gbgl_get_glyph_info(font, next_cp, &next_index);
1788
+ if (ngi) kern = gbgl_get_font_kerning_from_glyph_indices(font, curr_index, next_index);
1789
+ }
1790
+ w += gi->xadv + kern;
1791
+ }
1792
+ }
1793
+
1794
+ if (out_width) *out_width = w;
1795
+ if (out_height) *out_height = h;
1796
+ }
1797
+
1798
+ f32 gbgl_get_sub_string_width(gbglFont *font, char const *str, isize char_count) {
1799
+ isize i, len;
1800
+ f32 w = 0;
1801
+ char const *ptr = str;
1802
+ len = gb_strlen(str);
1803
+ for (i = 0; i < char_count; i++) {
1804
+ if (*ptr == 0) {
1805
+ break;
1806
+ } else {
1807
+ char32 cp;
1808
+ isize byte_len, curr_index;
1809
+ f32 kern = 0;
1810
+ gbglGlyphInfo *gi;
1811
+
1812
+ byte_len = gb_utf8_decode_len(ptr, len-(ptr-str), &cp);
1813
+ ptr += byte_len;
1814
+ if (ptr - str > char_count)
1815
+ break;
1816
+
1817
+ gi = gbgl_get_glyph_info(font, cp, &curr_index);
1818
+ if (i < char_count-1) {
1819
+ isize next_index;
1820
+ char32 next_cp = 0;
1821
+ gb_utf8_decode_len(ptr, len-(ptr-str), &next_cp);
1822
+ gbgl_get_glyph_info(font, next_cp, &next_index);
1823
+ kern = gbgl_get_font_kerning_from_glyph_indices(font, curr_index, next_index);
1824
+ }
1825
+ w += gi->xadv + kern;
1826
+ }
1827
+
1828
+ }
1829
+ return w;
1830
+ }
1831
+
1832
+ i32 gbgl_get_wrapped_line_count(gbglFont *font, char const *str, isize max_len, isize max_width) {
1833
+ isize i, str_len, char_count, line_count = 1;
1834
+ f32 w = 0;
1835
+ char const *ptr = str;
1836
+
1837
+ str_len = gb_strnlen(str, max_len);
1838
+ char_count = gb_utf8_strnlen(str, str_len);
1839
+
1840
+ for (i = 0; i < char_count; i++) {
1841
+ char32 cp;
1842
+ isize byte_len, curr_index;
1843
+ gbglGlyphInfo *gi;
1844
+ f32 kern = 0;
1845
+
1846
+ byte_len = gb_utf8_decode_len(ptr, str_len-(ptr-str), &cp);
1847
+ ptr += byte_len;
1848
+ // NOTE(bill): Check calculation here
1849
+ if (ptr-str >= max_len-6)
1850
+ break;
1851
+
1852
+ gi = gbgl_get_glyph_info(font, cp, &curr_index);
1853
+ if (gi) {
1854
+ if (w + gi->xadv >= cast(f32)max_width) {
1855
+ line_count++;
1856
+ w = 0.0f;
1857
+ }
1858
+ }
1859
+
1860
+ if (i < char_count-1) {
1861
+ char32 next_cp;
1862
+ isize next_index;
1863
+ gb_utf8_decode_len(ptr, str_len-(ptr-str), &next_cp);
1864
+
1865
+ gbgl_get_glyph_info(font, next_cp, &next_index);
1866
+ kern = gbgl_get_font_kerning_from_glyph_indices(font, curr_index, next_index);
1867
+ }
1868
+
1869
+ if (gi) {
1870
+ w += gi->xadv + kern;
1871
+ }
1872
+ }
1873
+
1874
+ return line_count;
1875
+ }
1876
+
1877
+ gb_inline f32 gbgl_get_string_width(gbglFont *font, char const *str, isize max_len) {
1878
+ isize len = gb_strnlen(str, max_len);
1879
+ isize char_count = gb_utf8_strnlen(str, len);
1880
+ return gbgl_get_sub_string_width(font, str, char_count);
1881
+ }
1882
+
1883
+ #endif
1884
+
1885
+
1886
+ ////////////////////////////////////////////////////////////////
1887
+ //
1888
+ // Basic State
1889
+ //
1890
+ //
1891
+
1892
+ #if !defined(GBGL_NO_BASIC_STATE)
1893
+
1894
+
1895
+ void gbgl_bs_init(gbglBasicState *bs, i32 window_width, i32 window_height) {
1896
+ isize i;
1897
+
1898
+ gbgl_bs_set_resolution(bs, window_width, window_height);
1899
+ glGenVertexArrays(1, &bs->vao);
1900
+ glBindVertexArray(bs->vao);
1901
+
1902
+ bs->vbo = gbgl_make_vbo(NULL, gb_size_of(gbglBasicVertex) * GBGL_BS_MAX_VERTEX_COUNT, GL_DYNAMIC_DRAW);
1903
+
1904
+ for (i = 0; i < GBGL_BS_MAX_INDEX_COUNT / 6; i++) {
1905
+ bs->indices[i*6 + 0] = i*4 + 0;
1906
+ bs->indices[i*6 + 1] = i*4 + 1;
1907
+ bs->indices[i*6 + 2] = i*4 + 2;
1908
+ bs->indices[i*6 + 3] = i*4 + 2;
1909
+ bs->indices[i*6 + 4] = i*4 + 3;
1910
+ bs->indices[i*6 + 5] = i*4 + 0;
1911
+ }
1912
+ bs->ebo = gbgl_make_ebo(bs->indices, gb_size_of(u16) * GBGL_BS_MAX_INDEX_COUNT, GL_STATIC_DRAW);
1913
+
1914
+ bs->nearest_sampler = gbgl_make_sampler(GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1915
+ bs->linear_sampler = gbgl_make_sampler(GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1916
+ bs->mipmap_sampler = gbgl_make_sampler(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT);
1917
+
1918
+ gbgl_load_shader_from_memory_vf(&bs->ortho_tex_shader,
1919
+ "#version 410 core\n"
1920
+ "layout (location = 0) in vec4 a_position;\n"
1921
+ "layout (location = 1) in vec2 a_tex_coord;\n"
1922
+ "uniform mat4 u_ortho_mat;\n"
1923
+ "out vec2 v_tex_coord;\n"
1924
+ "void main(void) {\n"
1925
+ " gl_Position = u_ortho_mat * a_position;\n"
1926
+ " v_tex_coord = a_tex_coord;\n"
1927
+ "}\n",
1928
+
1929
+ "#version 410 core\n"
1930
+ "precision mediump float;"
1931
+ "in vec2 v_tex_coord;\n"
1932
+ "layout (binding = 0) uniform sampler2D u_tex;\n"
1933
+ "out vec4 o_colour;\n"
1934
+ "void main(void) {\n"
1935
+ " o_colour = texture2D(u_tex, v_tex_coord);\n"
1936
+ "}\n"
1937
+ );
1938
+
1939
+ gbgl_load_shader_from_memory_vf(&bs->ortho_col_shader,
1940
+ "#version 410 core\n"
1941
+ "precision mediump float;"
1942
+ "layout (location = 0) in vec4 a_position;\n"
1943
+ "uniform mat4 u_ortho_mat;\n"
1944
+ "void main(void) {\n"
1945
+ " gl_Position = u_ortho_mat * a_position;\n"
1946
+ "}\n",
1947
+
1948
+ "#version 410 core\n"
1949
+ "uniform vec4 u_colour;\n"
1950
+ "out vec4 o_colour;\n"
1951
+ "void main(void) {\n"
1952
+ " o_colour = u_colour;\n"
1953
+ "}\n"
1954
+ );
1955
+
1956
+
1957
+ #if !defined(GBGL_NO_FONTS)
1958
+ gbgl_load_shader_from_memory_vf(&bs->font_shader,
1959
+ "#version 410 core\n"
1960
+ "layout (location = 0) in vec4 a_position;\n"
1961
+ "layout (location = 1) in vec2 a_tex_coord;\n"
1962
+ "uniform mat4 u_ortho_mat;\n"
1963
+ "out vec2 v_tex_coord;\n"
1964
+ "void main(void) {\n"
1965
+ " gl_Position = u_ortho_mat * a_position;\n"
1966
+ " v_tex_coord = a_tex_coord;\n"
1967
+ "}\n",
1968
+
1969
+ "#version 410 core\n"
1970
+ "in vec2 v_tex_coord;\n"
1971
+ "uniform vec4 u_colour;\n"
1972
+ "layout (binding = 0) uniform sampler2D u_tex;\n"
1973
+ "out vec4 o_colour;\n"
1974
+ "void main(void) {\n"
1975
+ " o_colour = u_colour * texture2D(u_tex, v_tex_coord).r;\n"
1976
+ "}\n"
1977
+ );
1978
+
1979
+ glGenVertexArrays(1, &bs->font_vao);
1980
+ glBindVertexArray(bs->font_vao);
1981
+
1982
+ bs->font_vbo = gbgl_make_vbo(NULL, gb_size_of(gbglBasicVertex) * GBGL_MAX_RENDER_STRING_LENGTH * 4, GL_DYNAMIC_DRAW);
1983
+
1984
+ for (i = 0; i < GBGL_MAX_RENDER_STRING_LENGTH; i++) {
1985
+ bs->font_indices[i*6 + 0] = i*4 + 0;
1986
+ bs->font_indices[i*6 + 1] = i*4 + 1;
1987
+ bs->font_indices[i*6 + 2] = i*4 + 2;
1988
+ bs->font_indices[i*6 + 3] = i*4 + 2;
1989
+ bs->font_indices[i*6 + 4] = i*4 + 3;
1990
+ bs->font_indices[i*6 + 5] = i*4 + 0;
1991
+ }
1992
+ bs->font_ebo = gbgl_make_ebo(bs->font_indices, gb_size_of(u16) * GBGL_MAX_RENDER_STRING_LENGTH * 6, GL_STATIC_DRAW);
1993
+
1994
+ bs->font_samplers[0] = gbgl_make_sampler(GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1995
+ bs->font_samplers[1] = gbgl_make_sampler(GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1996
+
1997
+ bs->text_params[gbglTextParam_MaxWidth] .val_i32 = 0;
1998
+ bs->text_params[gbglTextParam_Justify] .val_i32 = gbglJustify_Left;
1999
+ bs->text_params[gbglTextParam_TextureFilter].val_i32 = 0;
2000
+ #endif
2001
+ }
2002
+
2003
+ gb_inline void gbgl_bs_set_resolution(gbglBasicState *bs, i32 window_width, i32 window_height) {
2004
+ f32 left = 0.0f;
2005
+ f32 right = cast(f32)window_width;
2006
+ f32 bottom = 0.0f;
2007
+ f32 top = cast(f32)window_height;
2008
+ f32 znear = 0.0f;
2009
+ f32 zfar = 1.0f;
2010
+
2011
+ bs->width = window_width;
2012
+ bs->height = window_height;
2013
+
2014
+ bs->ortho_mat[0] = 2.0f / (right - left);
2015
+ bs->ortho_mat[1] = 0.0f;
2016
+ bs->ortho_mat[2] = 0.0f;
2017
+ bs->ortho_mat[3] = 0.0f;
2018
+
2019
+ bs->ortho_mat[4] = 0.0f;
2020
+ bs->ortho_mat[5] = 2.0f / (top - bottom);
2021
+ bs->ortho_mat[6] = 0.0f;
2022
+ bs->ortho_mat[7] = 0.0f;
2023
+
2024
+ bs->ortho_mat[8] = 0.0f;
2025
+ bs->ortho_mat[9] = 0.0f;
2026
+ bs->ortho_mat[10] = -2.0f / (zfar - znear);
2027
+ bs->ortho_mat[11] = 0.0f;
2028
+
2029
+ bs->ortho_mat[12] = -(right + left) / (right - left);
2030
+ bs->ortho_mat[13] = -(top + bottom) / (top - bottom);
2031
+ bs->ortho_mat[14] = -(zfar + znear) / (zfar - znear);
2032
+ bs->ortho_mat[15] = 1.0f;
2033
+ }
2034
+
2035
+ gb_inline void gbgl_bs_begin(gbglBasicState *bs, i32 window_width, i32 window_height) {
2036
+ glBindVertexArray(bs->vao);
2037
+ glDisable(GL_SCISSOR_TEST);
2038
+ gbgl_bs_set_resolution(bs, window_width, window_height);
2039
+ }
2040
+
2041
+ gb_inline void gbgl_bs_end(gbglBasicState *bs) {
2042
+ glBindVertexArray(0);
2043
+ }
2044
+
2045
+
2046
+
2047
+
2048
+ void gbgl_bs_draw_textured_rect(gbglBasicState *bs, gbglTexture *tex, f32 x, f32 y, f32 w, f32 h, b32 v_up) {
2049
+ bs->vertices[0].x = x;
2050
+ bs->vertices[0].y = y;
2051
+ bs->vertices[0].u = 0.0f;
2052
+ bs->vertices[0].v = v_up ? 0.0f : 1.0f;
2053
+
2054
+ bs->vertices[1].x = x + w;
2055
+ bs->vertices[1].y = y;
2056
+ bs->vertices[1].u = 1.0f;
2057
+ bs->vertices[1].v = v_up ? 0.0f : 1.0f;
2058
+
2059
+ bs->vertices[2].x = x + w;
2060
+ bs->vertices[2].y = y + h;
2061
+ bs->vertices[2].u = 1.0f;
2062
+ bs->vertices[2].v = v_up ? 1.0f : 0.0f;
2063
+
2064
+ bs->vertices[3].x = x;
2065
+ bs->vertices[3].y = y + h;
2066
+ bs->vertices[3].u = 0.0f;
2067
+ bs->vertices[3].v = v_up ? 1.0f : 0.0f;
2068
+
2069
+ gbgl_use_shader(&bs->ortho_tex_shader);
2070
+ gbgl_set_uniform_mat4(&bs->ortho_tex_shader, "u_ortho_mat", bs->ortho_mat);
2071
+ gbgl_bind_texture2d(tex, 0, bs->mipmap_sampler);
2072
+
2073
+ gbgl_vbo_copy(bs->vbo, bs->vertices, 4*gb_size_of(bs->vertices[0]), 0);
2074
+
2075
+ gbgl_vert_ptr_aa(0, 2, gbglBasicVertex, x);
2076
+ gbgl_vert_ptr_aa(1, 2, gbglBasicVertex, u);
2077
+
2078
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bs->ebo);
2079
+
2080
+ glEnable(GL_BLEND);
2081
+ glBlendEquationi(0, GL_FUNC_ADD);
2082
+ glBlendFunci(0, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2083
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);
2084
+ }
2085
+
2086
+ gb_inline void gbgl_bs_draw_rect(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, gbglColour col) {
2087
+ gbgl_bs_draw_quad(bs,
2088
+ x, y,
2089
+ x+w, y,
2090
+ x+w, y+h,
2091
+ x, y+h,
2092
+ col);
2093
+ }
2094
+
2095
+ gb_inline void gbgl_bs_draw_rect_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, gbglColour col, f32 thickness) {
2096
+ gbgl_bs_draw_quad_outline(bs,
2097
+ x, y,
2098
+ x+w, y,
2099
+ x+w, y+h,
2100
+ x, y+h,
2101
+ col,
2102
+ thickness);
2103
+ }
2104
+
2105
+
2106
+ gb_internal void gbgl__bs_setup_ortho_colour_state(gbglBasicState *bs, isize vertex_count, gbglColour col) {
2107
+
2108
+ gbgl_use_shader(&bs->ortho_col_shader);
2109
+ gbgl_set_uniform_mat4(&bs->ortho_col_shader, "u_ortho_mat", bs->ortho_mat);
2110
+ gbgl_set_uniform_colour(&bs->ortho_col_shader, "u_colour", col);
2111
+
2112
+ gbgl_vbo_copy(bs->vbo, bs->vertices, vertex_count*gb_size_of(bs->vertices[0]), 0);
2113
+ gbgl_vert_ptr_aa(0, 2, gbglBasicVertex, x);
2114
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bs->ebo);
2115
+
2116
+ glEnable(GL_BLEND);
2117
+ glBlendEquation(GL_FUNC_ADD);
2118
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2119
+ }
2120
+
2121
+ gb_inline void gbgl_bs_draw_quad(gbglBasicState *bs,
2122
+ f32 x0, f32 y0,
2123
+ f32 x1, f32 y1,
2124
+ f32 x2, f32 y2,
2125
+ f32 x3, f32 y3,
2126
+ gbglColour col) {
2127
+ bs->vertices[0].x = x0;
2128
+ bs->vertices[0].y = y0;
2129
+
2130
+ bs->vertices[1].x = x1;
2131
+ bs->vertices[1].y = y1;
2132
+
2133
+ bs->vertices[2].x = x2;
2134
+ bs->vertices[2].y = y2;
2135
+
2136
+ bs->vertices[3].x = x3;
2137
+ bs->vertices[3].y = y3;
2138
+
2139
+ gbgl__bs_setup_ortho_colour_state(bs, 4, col);
2140
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);
2141
+ }
2142
+
2143
+ gb_inline void gbgl_bs_draw_quad_outline(gbglBasicState *bs,
2144
+ f32 x0, f32 y0,
2145
+ f32 x1, f32 y1,
2146
+ f32 x2, f32 y2,
2147
+ f32 x3, f32 y3,
2148
+ gbglColour col, f32 thickness) {
2149
+ bs->vertices[0].x = x0;
2150
+ bs->vertices[0].y = y0;
2151
+
2152
+ bs->vertices[1].x = x1;
2153
+ bs->vertices[1].y = y1;
2154
+
2155
+ bs->vertices[2].x = x2;
2156
+ bs->vertices[2].y = y2;
2157
+
2158
+ bs->vertices[3].x = x3;
2159
+ bs->vertices[3].y = y3;
2160
+
2161
+ gbgl__bs_setup_ortho_colour_state(bs, 4, col);
2162
+ glLineWidth(thickness);
2163
+ glDrawArrays(GL_LINE_LOOP, 0, 4);
2164
+ }
2165
+
2166
+ gb_inline void gbgl_bs_draw_line(gbglBasicState *bs, f32 x0, f32 y0, f32 x1, f32 y1, gbglColour col, f32 thickness) {
2167
+ bs->vertices[0].x = x0;
2168
+ bs->vertices[0].y = y0;
2169
+
2170
+ bs->vertices[1].x = x1;
2171
+ bs->vertices[1].y = y1;
2172
+
2173
+ gbgl__bs_setup_ortho_colour_state(bs, 2, col);
2174
+ glLineWidth(thickness);
2175
+ glDrawArrays(GL_LINES, 0, 2);
2176
+ }
2177
+
2178
+ gb_inline void gbgl_bs_draw_elliptical_arc(gbglBasicState *bs, f32 x, f32 y, f32 radius_a, f32 radius_b,
2179
+ f32 min_angle, f32 max_angle, gbglColour col) {
2180
+ isize i;
2181
+
2182
+ bs->vertices[0].x = x;
2183
+ bs->vertices[0].y = y;
2184
+
2185
+ for (i = 0; i < 31; i++) {
2186
+ f32 t = cast(f32)i / 30.0f;
2187
+ f32 a = gbgl_lerp(min_angle, max_angle, t);
2188
+ f32 c = gbgl_cos(a);
2189
+ f32 s = gbgl_sin(a);
2190
+ bs->vertices[i+1].x = x + c*radius_a;
2191
+ bs->vertices[i+1].y = y + s*radius_b;
2192
+ }
2193
+
2194
+ gbgl__bs_setup_ortho_colour_state(bs, 32, col);
2195
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 32);
2196
+ }
2197
+
2198
+ gb_inline void gbgl_bs_draw_elliptical_arc_outline(gbglBasicState *bs, f32 x, f32 y, f32 radius_a, f32 radius_b,
2199
+ f32 min_angle, f32 max_angle, gbglColour col, f32 thickness) {
2200
+ isize i;
2201
+
2202
+ for (i = 0; i < 32; i++) {
2203
+ f32 t = cast(f32)i / 31.0f;
2204
+ f32 a = gbgl_lerp(min_angle, max_angle, t);
2205
+ f32 c = gbgl_cos(a);
2206
+ f32 s = gbgl_sin(a);
2207
+ bs->vertices[i+1].x = x + c*radius_a;
2208
+ bs->vertices[i+1].y = y + s*radius_b;
2209
+ }
2210
+
2211
+ gbgl__bs_setup_ortho_colour_state(bs, 32, col);
2212
+ glLineWidth(thickness);
2213
+ glDrawArrays(GL_LINES, 0, 32);
2214
+ }
2215
+
2216
+
2217
+
2218
+ gb_inline void gbgl_bs_draw_circle(gbglBasicState *bs, f32 x, f32 y, f32 radius, gbglColour col) {
2219
+ gbgl_bs_draw_elliptical_arc(bs, x, y, radius, radius, 0, GBGL_TAU, col);
2220
+ }
2221
+
2222
+ gb_inline void gbgl_bs_draw_circle_outline(gbglBasicState *bs, f32 x, f32 y, f32 radius, gbglColour col, f32 thickness) {
2223
+ gbgl_bs_draw_elliptical_arc_outline(bs, x, y, radius, radius, 0, GBGL_TAU, col, thickness);
2224
+ }
2225
+
2226
+ void gbgl_bs_draw_rounded_rect_corners(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, u32 corners) {
2227
+ if ((2.0f*roundness > gbgl_abs(w)) ||
2228
+ (2.0f*roundness > gbgl_abs(h))) {
2229
+ roundness = 0.5f*gbgl_min(gbgl_abs(w), gbgl_abs(h));
2230
+ }
2231
+
2232
+ if (roundness == 0 || corners == 0) {
2233
+ gbgl_bs_draw_rect(bs, x, y, w, h, col);
2234
+ } else {
2235
+ isize i, vc = 0;
2236
+
2237
+ bs->vertices[vc].x = x + 0.5f*w;
2238
+ bs->vertices[vc].y = y + 0.5f*h;
2239
+ vc++;
2240
+
2241
+ if (corners & 1) {
2242
+ for (i = 0; i < 6; i++) {
2243
+ f32 t = cast(f32)i / 5.0f;
2244
+ f32 a = gbgl_lerp(0.5f*GBGL_TAU, 0.75f*GBGL_TAU, t);
2245
+ f32 c = gbgl_cos(a);
2246
+ f32 s = gbgl_sin(a);
2247
+ bs->vertices[vc].x = x + roundness + c*roundness;
2248
+ bs->vertices[vc].y = y + roundness + s*roundness;
2249
+ vc++;
2250
+ }
2251
+ } else {
2252
+ bs->vertices[vc].x = x;
2253
+ bs->vertices[vc].y = y;
2254
+ vc++;
2255
+ }
2256
+
2257
+ if (corners & 2) {
2258
+ for (i = 0; i < 6; i++) {
2259
+ f32 t = cast(f32)i / 5.0f;
2260
+ f32 a = gbgl_lerp(0.75f*GBGL_TAU, 1.00f*GBGL_TAU, t);
2261
+ f32 c = gbgl_cos(a);
2262
+ f32 s = gbgl_sin(a);
2263
+ bs->vertices[vc].x = x + w - roundness + c*roundness;
2264
+ bs->vertices[vc].y = y + roundness + s*roundness;
2265
+ vc++;
2266
+ }
2267
+ } else {
2268
+ bs->vertices[vc].x = x + w;
2269
+ bs->vertices[vc].y = y;
2270
+ vc++;
2271
+ }
2272
+
2273
+
2274
+ if (corners & 4) {
2275
+ for (i = 0; i < 6; i++) {
2276
+ f32 t = cast(f32)i / 5.0f;
2277
+ f32 a = gbgl_lerp(0.00f*GBGL_TAU, 0.25f*GBGL_TAU, t);
2278
+ f32 c = gbgl_cos(a);
2279
+ f32 s = gbgl_sin(a);
2280
+ bs->vertices[vc].x = x + w - roundness + c*roundness;
2281
+ bs->vertices[vc].y = y + h - roundness + s*roundness;
2282
+ vc++;
2283
+ }
2284
+ } else {
2285
+ bs->vertices[vc].x = x + w;
2286
+ bs->vertices[vc].y = y + h;
2287
+ vc++;
2288
+ }
2289
+
2290
+ if (corners & 8) {
2291
+ for (i = 0; i < 6; i++) {
2292
+ f32 t = cast(f32)i / 5.0f;
2293
+ f32 a = gbgl_lerp(0.25f*GBGL_TAU, 0.50f*GBGL_TAU, t);
2294
+ f32 c = gbgl_cos(a);
2295
+ f32 s = gbgl_sin(a);
2296
+ bs->vertices[vc].x = x + roundness + c*roundness;
2297
+ bs->vertices[vc].y = y + h - roundness + s*roundness;
2298
+ vc++;
2299
+ }
2300
+ } else {
2301
+ bs->vertices[vc].x = x;
2302
+ bs->vertices[vc].y = y + h;
2303
+ vc++;
2304
+ }
2305
+
2306
+ if (corners & 1) {
2307
+ bs->vertices[vc].x = x;
2308
+ bs->vertices[vc].y = y + roundness;
2309
+ } else {
2310
+ bs->vertices[vc].x = x;
2311
+ bs->vertices[vc].y = y;
2312
+ }
2313
+ vc++;
2314
+
2315
+ gbgl__bs_setup_ortho_colour_state(bs, vc, col);
2316
+ glDrawArrays(GL_TRIANGLE_FAN, 0, vc);
2317
+ }
2318
+ }
2319
+
2320
+ gb_inline void gbgl_bs_draw_rounded_rect(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col) {
2321
+ gbgl_bs_draw_rounded_rect_corners(bs, x, y, w, h, roundness, col, 1|2|4|8);
2322
+ }
2323
+
2324
+
2325
+ void gbgl_bs_draw_rounded_rect_corners_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, f32 thickness, u32 corners) {
2326
+ if ((2.0f*roundness > gbgl_abs(w)) ||
2327
+ (2.0f*roundness > gbgl_abs(h))) {
2328
+ roundness = 0.5f*gbgl_min(gbgl_abs(w), gbgl_abs(h));
2329
+ }
2330
+
2331
+ if (roundness == 0 || corners == 0) {
2332
+ gbgl_bs_draw_rect_outline(bs, x, y, w, h, col, thickness);
2333
+ } else {
2334
+ isize i, vc = 0;
2335
+
2336
+ if (corners & 1) {
2337
+ for (i = 0; i < 6; i++) {
2338
+ f32 t = cast(f32)i / 5.0f;
2339
+ f32 a = gbgl_lerp(0.5f*GBGL_TAU, 0.75f*GBGL_TAU, t);
2340
+ f32 c = gbgl_cos(a);
2341
+ f32 s = gbgl_sin(a);
2342
+ bs->vertices[vc].x = x + roundness + c*roundness;
2343
+ bs->vertices[vc].y = y + roundness + s*roundness;
2344
+ vc++;
2345
+ }
2346
+ } else {
2347
+ bs->vertices[vc].x = x;
2348
+ bs->vertices[vc].y = y;
2349
+ vc++;
2350
+ }
2351
+
2352
+ if (corners & 2) {
2353
+ for (i = 0; i < 6; i++) {
2354
+ f32 t = cast(f32)i / 5.0f;
2355
+ f32 a = gbgl_lerp(0.75f*GBGL_TAU, 1.00f*GBGL_TAU, t);
2356
+ f32 c = gbgl_cos(a);
2357
+ f32 s = gbgl_sin(a);
2358
+ bs->vertices[vc].x = x + w - roundness + c*roundness;
2359
+ bs->vertices[vc].y = y + roundness + s*roundness;
2360
+ vc++;
2361
+ }
2362
+ } else {
2363
+ bs->vertices[vc].x = x + w;
2364
+ bs->vertices[vc].y = y;
2365
+ vc++;
2366
+ }
2367
+
2368
+
2369
+ if (corners & 4) {
2370
+ for (i = 0; i < 6; i++) {
2371
+ f32 t = cast(f32)i / 5.0f;
2372
+ f32 a = gbgl_lerp(0.00f*GBGL_TAU, 0.25f*GBGL_TAU, t);
2373
+ f32 c = gbgl_cos(a);
2374
+ f32 s = gbgl_sin(a);
2375
+ bs->vertices[vc].x = x + w - roundness + c*roundness;
2376
+ bs->vertices[vc].y = y + h - roundness + s*roundness;
2377
+ vc++;
2378
+ }
2379
+ } else {
2380
+ bs->vertices[vc].x = x + w;
2381
+ bs->vertices[vc].y = y + h;
2382
+ vc++;
2383
+ }
2384
+
2385
+ if (corners & 8) {
2386
+ for (i = 0; i < 6; i++) {
2387
+ f32 t = cast(f32)i / 5.0f;
2388
+ f32 a = gbgl_lerp(0.25f*GBGL_TAU, 0.50f*GBGL_TAU, t);
2389
+ f32 c = gbgl_cos(a);
2390
+ f32 s = gbgl_sin(a);
2391
+ bs->vertices[vc].x = x + roundness + c*roundness;
2392
+ bs->vertices[vc].y = y + h - roundness + s*roundness;
2393
+ vc++;
2394
+ }
2395
+ } else {
2396
+ bs->vertices[vc].x = x;
2397
+ bs->vertices[vc].y = y + h;
2398
+ vc++;
2399
+ }
2400
+
2401
+ gbgl__bs_setup_ortho_colour_state(bs, vc, col);
2402
+ glLineWidth(thickness);
2403
+ glDrawArrays(GL_LINE_LOOP, 0, vc);
2404
+ }
2405
+ }
2406
+
2407
+ gb_inline void gbgl_bs_draw_rounded_rect_outline(gbglBasicState *bs, f32 x, f32 y, f32 w, f32 h, f32 roundness, gbglColour col, f32 thickness) {
2408
+ gbgl_bs_draw_rounded_rect_corners_outline(bs, x, y, w, h, roundness, col, thickness, 1|2|4|8);
2409
+ }
2410
+
2411
+
2412
+
2413
+
2414
+ #if !defined(GBGL_NO_FONTS)
2415
+
2416
+
2417
+ isize gbgl_bs_draw_substring(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *str, isize len) {
2418
+ isize char_count = gb_utf8_strnlen(str, len);
2419
+ isize line_count = 0;
2420
+ if (char_count > 0) {
2421
+ char const *ptr = str;
2422
+
2423
+ f32 sf = 1.0f / cast(f32)font->bitmap_width;
2424
+ f32 tf = 1.0f / cast(f32)font->bitmap_height;
2425
+
2426
+ f32 ox, oy;
2427
+ f32 px, py;
2428
+
2429
+ isize glyph_count = 0, i;
2430
+ f32 font_height = font->size;
2431
+ i32 max_width = bs->text_params[gbglTextParam_MaxWidth].val_i32;
2432
+
2433
+ gbglJustifyType justify = cast(gbglJustifyType)bs->text_params[gbglTextParam_Justify].val_i32;
2434
+ if (justify == gbglJustify_Centre) {
2435
+ f32 width;
2436
+ gbgl_get_string_dimensions(font, ptr, &width, NULL);
2437
+ x = gbgl_round(x - width*0.5f);
2438
+ } else if (justify == gbglJustify_Right) {
2439
+ f32 width;
2440
+ gbgl_get_string_dimensions(font, ptr, &width, NULL);
2441
+ x = gbgl_round(x - width);
2442
+ }
2443
+
2444
+ line_count = 1;
2445
+
2446
+ ox = x;
2447
+ oy = y;
2448
+ px = ox;
2449
+ py = oy;
2450
+
2451
+ for (i = 0; i < char_count; i++) {
2452
+ char32 cp;
2453
+ isize byte_len, curr_index, draw_this_glyph_count = 1, j;
2454
+ gbglGlyphInfo *gi;
2455
+
2456
+ byte_len = gb_utf8_decode_len(ptr, len-(ptr-str), &cp);
2457
+ ptr += byte_len;
2458
+ if (ptr - str > len)
2459
+ break;
2460
+
2461
+ if (cp == '\t') {
2462
+ draw_this_glyph_count = GBGL_TAB_CHARACTER_WIDTH;
2463
+ cp = ' '; // TODO(bill): Set tab to be space
2464
+ }
2465
+
2466
+
2467
+ gi = gbgl_get_glyph_info(font, cp, &curr_index);
2468
+ if (!gi) {
2469
+ gi = gbgl_get_glyph_info(font, ' ', &curr_index);
2470
+ }
2471
+
2472
+ if (gi) {
2473
+ for (j = 0; j < draw_this_glyph_count; j++) {
2474
+ f32 s0, t0, s1, t1;
2475
+ f32 x0, y0, x1, y1;
2476
+ f32 kern = 0.0f;
2477
+
2478
+
2479
+ if (cp == '\r' || cp == '\n' ||
2480
+ (max_width > 0 && px - ox + gi->xadv >= cast(f32)max_width)) {
2481
+ px = ox;
2482
+
2483
+ py -= font_height;
2484
+ line_count += 2;
2485
+
2486
+ if (cp == '\r' || cp == '\n') {
2487
+ if (cp == '\r' && ptr[1] == '\n')
2488
+ ptr++;
2489
+ continue;
2490
+ }
2491
+ }
2492
+
2493
+ s0 = cast(f32)gi->s0 * sf;
2494
+ t0 = cast(f32)gi->t0 * tf;
2495
+ s1 = cast(f32)gi->s1 * sf;
2496
+ t1 = cast(f32)gi->t1 * tf;
2497
+
2498
+ x0 = px + gi->xoff;
2499
+ y0 = py - gi->yoff;
2500
+ x1 = x0 + (gi->s1 - gi->s0);
2501
+ y1 = y0 + (gi->t0 - gi->t1);
2502
+
2503
+ bs->font_vertices[glyph_count*4 + 0].x = x0;
2504
+ bs->font_vertices[glyph_count*4 + 0].y = y0;
2505
+ bs->font_vertices[glyph_count*4 + 0].u = s0;
2506
+ bs->font_vertices[glyph_count*4 + 0].v = t0;
2507
+
2508
+ bs->font_vertices[glyph_count*4 + 1].x = x1;
2509
+ bs->font_vertices[glyph_count*4 + 1].y = y0;
2510
+ bs->font_vertices[glyph_count*4 + 1].u = s1;
2511
+ bs->font_vertices[glyph_count*4 + 1].v = t0;
2512
+
2513
+ bs->font_vertices[glyph_count*4 + 2].x = x1;
2514
+ bs->font_vertices[glyph_count*4 + 2].y = y1;
2515
+ bs->font_vertices[glyph_count*4 + 2].u = s1;
2516
+ bs->font_vertices[glyph_count*4 + 2].v = t1;
2517
+
2518
+ bs->font_vertices[glyph_count*4 + 3].x = x0;
2519
+ bs->font_vertices[glyph_count*4 + 3].y = y1;
2520
+ bs->font_vertices[glyph_count*4 + 3].u = s0;
2521
+ bs->font_vertices[glyph_count*4 + 3].v = t1;
2522
+
2523
+ glyph_count++;
2524
+
2525
+ if (i < char_count-1) {
2526
+ isize next_index;
2527
+ char32 next_cp = 0;
2528
+ gbglGlyphInfo *ngi;
2529
+
2530
+ gb_utf8_decode_len(ptr, len-(ptr-str), &next_cp);
2531
+ ngi = gbgl_get_glyph_info(font, next_cp, &next_index);
2532
+ if (ngi) {
2533
+ kern = gbgl_get_font_kerning_from_glyph_indices(font, curr_index, next_index);
2534
+ }
2535
+ }
2536
+
2537
+ px += gi->xadv + kern;
2538
+ }
2539
+ }
2540
+ }
2541
+
2542
+
2543
+ if (glyph_count > 0) {
2544
+ isize sampler_index = 0;
2545
+
2546
+ gbgl_use_shader(&bs->font_shader);
2547
+ gbgl_set_uniform_mat4(&bs->font_shader, "u_ortho_mat", bs->ortho_mat);
2548
+ gbgl_set_uniform_colour(&bs->font_shader, "u_colour", col);
2549
+ GB_ASSERT(bs->text_params[gbglTextParam_TextureFilter].val_i32 < gb_count_of(bs->font_samplers));
2550
+ if (bs->text_params[gbglTextParam_TextureFilter].val_i32 < gb_count_of(bs->font_samplers))
2551
+ sampler_index = bs->text_params[gbglTextParam_TextureFilter].val_i32;
2552
+
2553
+ gbgl_bind_texture2d(&font->texture, 0, bs->font_samplers[sampler_index]);
2554
+ gbgl_vbo_copy(bs->font_vbo, bs->font_vertices, gb_size_of(bs->font_vertices[0]) * glyph_count * 4, 0);
2555
+
2556
+ gbgl_vert_ptr_aa(0, 2, gbglBasicVertex, x);
2557
+ gbgl_vert_ptr_aa(1, 2, gbglBasicVertex, u);
2558
+
2559
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bs->font_ebo);
2560
+
2561
+ glEnable(GL_BLEND);
2562
+ glBlendEquation(GL_FUNC_ADD);
2563
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2564
+ glDrawElements(GL_TRIANGLES, glyph_count*6, GL_UNSIGNED_SHORT, NULL);
2565
+
2566
+ }
2567
+ }
2568
+ return line_count;
2569
+ }
2570
+
2571
+ isize gbgl_bs_draw_string(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *fmt, ...) {
2572
+ isize len;
2573
+ va_list va;
2574
+ va_start(va, fmt);
2575
+ len = gbgl_bs_draw_string_va(bs, font, x, y, col, fmt, va);
2576
+ va_end(va);
2577
+ return len;
2578
+ }
2579
+
2580
+ gb_inline isize gbgl_bs_draw_string_va(gbglBasicState *bs, gbglFont *font, f32 x, f32 y, gbglColour col, char const *fmt, va_list va) {
2581
+ isize len = gb_snprintf_va(bs->font_text_buffer, gb_size_of(bs->font_text_buffer),
2582
+ fmt, va);
2583
+ isize char_count = gb_utf8_strnlen(bs->font_text_buffer, len);
2584
+ if (char_count <= 0)
2585
+ return 0;
2586
+ return gbgl_bs_draw_substring(bs, font, x, y, col, bs->font_text_buffer, len);
2587
+ }
2588
+
2589
+ #endif // !defined(GBGL_NO_FONTS)
2590
+ #endif // !defined(GBGL_NO_BASIC_STATE)
2591
+
2592
+ #endif