wilhelm_renderer 0.8.0

A minimalist 2D data rendering engine
Documentation
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
#include "glrenderer.h"

extern "C"
{

    void glfwErrorCallback(int error, const char* description) {
        std::cerr << "[GLFW ERROR] (" << error << "): " << description << std::endl;
    }


    GLFWwindow *_glfwCreateWindow(const char *title, int width, int height, GLFWframebuffersizefun callback)
    {
        glfwSetErrorCallback(glfwErrorCallback);
        
        if(!glfwInit()){
            return nullptr;
        }

        // Set MSAA samples for antialiasing
        glfwWindowHint(GLFW_SAMPLES, 4);

        // Enable DPI scaling on Windows - window resizes based on monitor content scale
        glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);

        // Tell GLFW what version of OpenGL we are using
        // In this case we are using OpenGL 3.3 to be compatible with Mac
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

        // Tell GLFW we are using the CORE profile
        // So that means we only have the modern functions
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

        //glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);        

#ifdef __APPLE__
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

        GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr);
        //glfwCreateWindow(100, 100, "Title", glfwGetPrimaryMonitor(), NULL);
        if (window == nullptr)
        {
            std::cerr << "Failed to create GLFW window" << std::endl;
            glfwTerminate();
            return nullptr;
        }
        glfwMakeContextCurrent(window);
        glfwSetFramebufferSizeCallback(window, callback);

        // glad: load all OpenGL function pointers
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        {
            std::cerr << "Failed to initialize GLAD" << std::endl;
            glfwDestroyWindow(window);
            glfwTerminate();
            return nullptr;
        }

        // Enable MSAA (glEnable must come AFTER context is current and GLAD is loaded)
        glEnable(GL_MULTISAMPLE);
        
        int fb_width, fb_height;
        glfwGetFramebufferSize(window, &fb_width, &fb_height);
        glViewport(0, 0, fb_width, fb_height);
        return window;
    }

    void _glfwGetWindowContentScale(GLFWwindow *window, float* xscale, float* yscale)
    {
        glfwGetWindowContentScale(window, xscale, yscale);
    }

    void _glfwWindowHint(int hint, int value)
    {
        glfwWindowHint(hint, value);
    }

    void _glfwSetWindowUserPointer(GLFWwindow *window, void *pointer)
    {
        glfwSetWindowUserPointer(window, pointer);
    }

    void *_glfwGetWindowUserPointer(GLFWwindow *window)
    {
        return glfwGetWindowUserPointer(window);
    }

    void _glfwSetWindowSizeCallback(GLFWwindow *window, GLFWwindowsizefun callback){
        glfwSetWindowSizeCallback(window, callback);
    }

    bool _glfwWindowShouldClose(GLFWwindow *window)
    {
        return glfwWindowShouldClose(window);
    }

    void _glfwDestroyWindow(GLFWwindow *window)
    {
        glfwDestroyWindow(window);
    }

    void _glfwTerminate()
    {
        glfwTerminate();
    }

    void _glfwSwapBuffers(GLFWwindow *window)
    {
        // Swap the back buffer with the front buffer
        glfwSwapBuffers(window);
    }

    void _glfwPollEvents()
    {
        // Take care of all GLFW events
        glfwPollEvents();
    }

    double _glfwGetTime()
    {
        return glfwGetTime();
    }

    void _glfwSetScrollCallback(GLFWwindow *window, GLFWscrollfun callback)
    {
        glfwSetScrollCallback(window, callback);
    }

    void _glfwSetCursorPosCallback(GLFWwindow *window, GLFWcursorposfun callback)
    {
        glfwSetCursorPosCallback(window, callback);
    }

    void _glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun callback)
    {
        glfwSetKeyCallback(window, callback);
    }

    void _glfwSetMouseButtonCallback(GLFWwindow *window, GLFWmousebuttonfun callback)
    {
        glfwSetMouseButtonCallback(window, callback);
    }

    void _glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
    {
        glfwGetWindowSize(window, width, height);
    }

    void _glClearColor(GLfloat x, GLfloat y, GLfloat z, GLfloat a)
    {
        glClearColor(x, y, z, a);
        // Clean the back buffer and assign the new color to it
        glClear(GL_COLOR_BUFFER_BIT);
    }

    void _glViewPort(GLint x, GLint y, GLsizei width, GLsizei height)
    {
        glViewport(x, y, width, height);
    }

    void _glGetIntegerv(GLenum pname, GLint *data)
    {
        glGetIntegerv(pname, data);
    }

    GLuint _glGenBuffer()
    {
        unsigned int VBO;
        glGenBuffers(1, &VBO);
        return VBO;
    }

    void _glGenBuffers(GLsizei n, GLuint *buffers)
    {
        glGenBuffers(n, buffers);
    }

    void _glDeleteBuffer(GLuint buffer)
    {
        glDeleteBuffers(1, &buffer);
    }

    void _glBindBuffer(GLenum target, GLuint buffer)
    {
        glBindBuffer(target, buffer);
    }

    void _glBufferData(GLenum mode, GLsizeiptr size, const GLvoid *data, GLenum usage)
    {
        glBufferData(mode, size, data, usage);
    }

    void _glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data)
    {
        glBufferSubData(target, offset, size, data);
    }

    GLuint _glGenVertexArray()
    {
        unsigned int VAO;
        glGenVertexArrays(1, &VAO);
        return VAO;
    }

    void _glDeleteVertexArray(GLuint vao)
    {
        glDeleteVertexArrays(1, &vao);
    }

    void _glBindVertexArray(GLuint array)
    {
        glBindVertexArray(array);
    }

    void _glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLsizei offset)
    {
        glVertexAttribPointer(index, size, type, normalized, stride, (void *)offset);
    }

    void _glEnableVertexAttribArray(GLuint index)
    {
        glEnableVertexAttribArray(index);
    }

    GLint _glGenTexture()
    {
        unsigned int texture;
        glGenTextures(1, &texture);
        return texture;
    }

    void _glActiveTexture(GLenum unit)
    {
        glActiveTexture(unit);
    }

    void _glBindTexture(GLenum target, GLuint texture)
    {
        glBindTexture(target, texture);
    }

    void _glTexParameteri(GLenum target, GLenum pname, GLint param)
    {
        glTexParameteri(target, pname, param);
    }

    void _glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *data)
    {
        glTexImage2D(target, level, internalformat, width, height, border, format, type, data);
        GLenum error = glGetError();
        if (error == GL_INVALID_OPERATION)
        {
            printf("OpenGL error: %d\n", error);
        }
        else
        {
            std::cout << "glTextImage2D called successfully" << std::endl;
        }
    }

    void _glTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *data)
    {
        glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, data);
    }

    void _glGenerateMipmap(GLenum target)
    {
        glGenerateMipmap(target);
    }

    void _glPixelStorei(GLenum pname, GLint param)
    {
        glPixelStorei(pname, param);
    }

    void _glDeleteTexture(GLuint texture)
    {
        glDeleteTextures(1, &texture);
    }

    GLuint _glCreateShader(GLenum shaderType)
    {
        return glCreateShader(shaderType);
    }

    void _glShaderSource(GLuint shader, GLchar *source)
    {
        glShaderSource(shader, 1, &source, NULL);
    }

    void _glCompileShader(GLuint shader)
    {
        glCompileShader(shader);
#ifndef NDEBUG
        int success;
        char infoLog[512];
        glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
        if (!success)
        {
            glGetShaderInfoLog(shader, 512, NULL, infoLog);
            std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n"
                      << infoLog << std::endl;
        }
        else
        {
            std::cout << "shader " << shader << " compiled successfully" << std::endl;
        }
#endif
    }

    void _glDeleteShader(GLuint shader)
    {
        glDeleteShader(shader);
    }

    void _glGetShaderiv(GLuint shader, GLenum pname, GLint *params)
    {
        glGetShaderiv(shader, pname, params);
    }

    GLuint _glCreateProgram()
    {
        return glCreateProgram();
    }

    void _glAttachShader(GLuint program, GLuint shader)
    {
        glAttachShader(program, shader);
    }

    void _glLinkProgram(GLuint program)
    {
        glLinkProgram(program);
    }

    void _glDeleteProgram(GLuint program)
    {
        glDeleteProgram(program);
    }

    void _glUseProgram(GLuint program)
    {
        glUseProgram(program);
    }

    void _glDrawArrays(GLenum mode, GLint first, GLsizei count)
    {
        glDrawArrays(mode, first, count);
    }

    void _glDrawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei instancecount)
    {
        glDrawArraysInstanced(mode, first, count, instancecount);
    }

    void _glVertexAttribDivisor(GLuint index, GLuint divisor)
    {
        glVertexAttribDivisor(index, divisor);
    }

    void _glVertexAttrib4f(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3)
    {
        glVertexAttrib4f(index, v0, v1, v2, v3);
    }

    void _glDrawElements(GLenum mode, GLsizei count, GLenum type, GLuint offset)
    {
        glDrawElements(mode, count, type, (void *)(offset));
    }

    GLint _glGetUniformLocation(GLuint program, GLchar *name)
    {
        return glGetUniformLocation(program, name);
    }

    void _glUniform1f(GLint location, GLfloat v0)
    {
        glUniform1f(location, v0);
    }

    void _glUniform2f(GLint location, GLfloat v0, GLfloat v1)
    {
        glUniform2f(location, v0, v1);
    }

    void _glUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2)
    {
        glUniform3f(location, v0, v1, v2);
    }

    void _glUniform4f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3)
    {
        glUniform4f(location, v0, v1, v2, v3);
    }

    void _glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
    {
        glUniformMatrix4fv(location, count, transpose, value);
    }

    void _glPointSize(GLfloat size)
    {
        glPointSize(size);
    }

    void _glEnable(GLenum cap)
    {
        glEnable(cap);
    }

    void _glBlendFunc(GLenum sfactor, GLenum dfactor)
    {
        glBlendFunc(sfactor, dfactor);
    }

    int _glfwGetPlatform()
    {
        return glfwGetPlatform();
    }

    // ============ FreeType ============

    int _ft_init_freetype(FT_Library *library)
    {
        FT_Error error = FT_Init_FreeType(library);
        if (error) {
            std::cerr << "[FreeType ERROR] Failed to initialize FreeType library: " << error << std::endl;
        }
        return error;
    }

    void _ft_done_freetype(FT_Library library)
    {
        FT_Done_FreeType(library);
    }

    int _ft_new_face(FT_Library library, const char *filepath, long face_index, FT_Face *face)
    {
        FT_Error error = FT_New_Face(library, filepath, face_index, face);
        if (error) {
            std::cerr << "[FreeType ERROR] Failed to load font '" << filepath << "': " << error << std::endl;
        }
        return error;
    }

    void _ft_done_face(FT_Face face)
    {
        FT_Done_Face(face);
    }

    int _ft_set_pixel_sizes(FT_Face face, unsigned int width, unsigned int height)
    {
        return FT_Set_Pixel_Sizes(face, width, height);
    }

    int _ft_load_char(FT_Face face, unsigned long char_code, int load_flags)
    {
        return FT_Load_Char(face, char_code, load_flags);
    }

    void _ft_get_glyph_metrics(FT_Face face, FT_GlyphMetrics *metrics)
    {
        FT_GlyphSlot glyph = face->glyph;
        metrics->width = glyph->bitmap.width;
        metrics->height = glyph->bitmap.rows;
        metrics->bearing_x = glyph->bitmap_left;
        metrics->bearing_y = glyph->bitmap_top;
        metrics->advance = glyph->advance.x;  // in 1/64th pixels
    }

    unsigned char *_ft_get_glyph_bitmap(FT_Face face)
    {
        return face->glyph->bitmap.buffer;
    }

    int _ft_get_glyph_bitmap_pitch(FT_Face face)
    {
        return face->glyph->bitmap.pitch;
    }
}