Skip to content

Commit 329eca7

Browse files
committed
Add ability to draw text background
1 parent 23184a0 commit 329eca7

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

src/gui.h

+60-53
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ int gl_save_frame_buffer(const int width, const int height, const char *fp);
233233

234234
typedef struct gl_entity_t {
235235
gl_float_t T[4 * 4];
236+
gl_float_t P[4 * 4];
236237

237238
gl_uint_t program_id;
238239
gl_uint_t VAO;
@@ -345,15 +346,9 @@ void gl_camera_zoom(gl_camera_t *camera,
345346

346347
typedef struct {
347348
gl_entity_t entity;
348-
gl_int_t x;
349-
gl_int_t y;
350-
gl_int_t width;
351-
gl_int_t height;
349+
gl_bounds_t bounds;
352350
} gl_rect_t;
353-
gl_rect_t *gl_rect_malloc(const gl_int_t x,
354-
const gl_int_t y,
355-
const gl_int_t width,
356-
const gl_int_t height);
351+
gl_rect_t *gl_rect_malloc(const gl_bounds_t bounds);
357352
void gl_rect_free(gl_rect_t *rect);
358353
void gl_rect_draw(const gl_rect_t *rect, const gl_camera_t *camera);
359354

@@ -452,16 +447,15 @@ typedef struct {
452447
} gl_char_t;
453448

454449
typedef struct {
450+
gl_entity_t entity;
455451
gl_char_t data[128];
456452
gl_color_t color;
457-
gl_float_t P[4 * 4];
458-
459-
gl_uint_t program_id;
460-
gl_uint_t vao;
461-
gl_uint_t vbo;
453+
gl_color_t bg_color;
462454
} gl_text_t;
463455

464456
void gl_char_print(const gl_char_t *ch);
457+
void gl_text_setup(gl_text_t *text);
458+
void gl_text_cleanup(gl_text_t *text);
465459
gl_text_t *gl_text_malloc(void);
466460
void gl_text_free(gl_text_t *text);
467461
void gl_text_draw(gl_text_t *text,
@@ -583,6 +577,7 @@ typedef struct {
583577
gl_color_t color_hover;
584578
gl_color_t color_press;
585579
gl_bounds_t bounds;
580+
gl_text_t text;
586581

587582
gl_uint_t program_id;
588583
gl_uint_t vao;
@@ -1159,6 +1154,7 @@ void gl_rot2quat(const gl_float_t C[3 * 3], gl_float_t q[4]) {
11591154

11601155
void gl_entity_setup(gl_entity_t *entity) {
11611156
gl_eye(entity->T, 4, 4);
1157+
gl_eye(entity->P, 4, 4);
11621158
entity->program_id = 0;
11631159
entity->VAO = 0;
11641160
entity->VBO = 0;
@@ -1648,17 +1644,11 @@ void gl_camera_zoom(gl_camera_t *camera,
16481644
" frag_color = vec4(0.5f, 0.5f, 01.0f, 1.0f);\n" \
16491645
"}\n"
16501646

1651-
gl_rect_t *gl_rect_malloc(const gl_int_t x,
1652-
const gl_int_t y,
1653-
const gl_int_t width,
1654-
const gl_int_t height) {
1647+
gl_rect_t *gl_rect_malloc(const gl_bounds_t bounds) {
16551648
// Malloc
16561649
gl_rect_t *rect = MALLOC(gl_rect_t, 1);
16571650
gl_entity_setup(&rect->entity);
1658-
rect->x = x;
1659-
rect->y = y;
1660-
rect->width = width;
1661-
rect->height = height;
1651+
rect->bounds = bounds;
16621652

16631653
// Shader program
16641654
rect->entity.program_id = gl_prog_setup(GL_RECT_VS, GL_RECT_FS, NULL);
@@ -1668,7 +1658,7 @@ gl_rect_t *gl_rect_malloc(const gl_int_t x,
16681658

16691659
// Vertices
16701660
// clang-format off
1671-
const float vertices[4 * 3] = {
1661+
const float vertices[4 * 2] = {
16721662
-0.5f, -0.5f, // Bottom left
16731663
0.5f, -0.5f, // Bottom right
16741664
0.5f, 0.5f, // Top right
@@ -2476,12 +2466,15 @@ void gl_line3d_draw(const gl_line3d_t *line, const gl_camera_t *camera) {
24762466
#define GL_TEXT_FS \
24772467
"#version 330 core\n" \
24782468
"in vec2 tex_coords;\n" \
2479-
"out vec4 color;\n" \
2469+
"out vec4 frag_color;\n" \
24802470
"uniform sampler2D text;\n" \
24812471
"uniform vec3 text_color;\n" \
2472+
"uniform vec3 bg_color;\n" \
24822473
"void main() {\n" \
2483-
" vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, tex_coords).r);\n" \
2484-
" color = vec4(text_color, 1.0) * sampled;\n" \
2474+
" float alpha = texture(text, tex_coords).r;\n" \
2475+
" vec4 glyph_rgba = vec4(text_color, alpha);\n" \
2476+
" vec4 bg_rgba = vec4(bg_color, 1.0);\n" \
2477+
" frag_color = mix(bg_rgba, glyph_rgba, alpha);\n" \
24852478
"}\n"
24862479

24872480
void gl_char_print(const gl_char_t *ch) {
@@ -2494,14 +2487,15 @@ void gl_char_print(const gl_char_t *ch) {
24942487
printf("\n");
24952488
}
24962489

2497-
gl_text_t *gl_text_malloc(void) {
2498-
// MALLOC
2499-
gl_text_t *text = MALLOC(gl_text_t, 1);
2500-
text->color = (gl_color_t){1.0, 1.0, 1.0};
2490+
void gl_text_setup(gl_text_t *text) {
2491+
// Initialize
2492+
gl_entity_setup(&text->entity);
2493+
text->color = (gl_color_t){1.0, 0.0, 0.0};
2494+
text->bg_color = (gl_color_t){1.0, 1.0, 1.0};
25012495

25022496
// Compile shader
2503-
text->program_id = gl_prog_setup(GL_TEXT_VS, GL_TEXT_FS, NULL);
2504-
if (text->program_id == GL_FALSE) {
2497+
text->entity.program_id = gl_prog_setup(GL_TEXT_VS, GL_TEXT_FS, NULL);
2498+
if (text->entity.program_id == GL_FALSE) {
25052499
FATAL("Failed to create shaders!");
25062500
}
25072501

@@ -2566,26 +2560,37 @@ gl_text_t *gl_text_malloc(void) {
25662560
FT_Done_FreeType(ft);
25672561

25682562
// VAO
2569-
glGenVertexArrays(1, &text->vao);
2570-
glBindVertexArray(text->vao);
2563+
glGenVertexArrays(1, &text->entity.VAO);
2564+
glBindVertexArray(text->entity.VAO);
25712565

25722566
// VBO
2573-
glGenBuffers(1, &text->vbo);
2574-
glBindBuffer(GL_ARRAY_BUFFER, text->vbo);
2567+
glGenBuffers(1, &text->entity.VBO);
2568+
glBindBuffer(GL_ARRAY_BUFFER, text->entity.VBO);
25752569
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
25762570
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
25772571
glEnableVertexAttribArray(0);
25782572

25792573
// Clean up
25802574
glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBO
25812575
glBindVertexArray(0); // Unbind VAO
2576+
}
2577+
2578+
void gl_text_cleanup(gl_text_t *text) {
2579+
// Clean up
2580+
gl_entity_cleanup(&text->entity);
2581+
}
25822582

2583+
gl_text_t *gl_text_malloc(void) {
2584+
gl_text_t *text = MALLOC(gl_text_t, 1);
2585+
gl_text_setup(text);
25832586
return text;
25842587
}
25852588

25862589
void gl_text_free(gl_text_t *text) {
2587-
glDeleteVertexArrays(1, &text->vao);
2588-
glDeleteBuffers(1, &text->vbo);
2590+
if (text == NULL) {
2591+
return;
2592+
}
2593+
gl_text_cleanup(text);
25892594
free(text);
25902595
}
25912596

@@ -2602,14 +2607,15 @@ void gl_text_draw(gl_text_t *text,
26022607
const gl_float_t top = *(camera->window_height);
26032608
const gl_float_t znear = -1.0f;
26042609
const gl_float_t zfar = 1.0f;
2605-
gl_ortho(left, right, bottom, top, znear, zfar, text->P);
2610+
gl_ortho(left, right, bottom, top, znear, zfar, text->entity.P);
26062611

26072612
// Activate shader
2608-
glUseProgram(text->program_id);
2609-
assert(gl_prog_set_mat4(text->program_id, "projection", text->P) == 0);
2610-
assert(gl_prog_set_color(text->program_id, "text_color", text->color) == 0);
2613+
glUseProgram(text->entity.program_id);
2614+
gl_prog_set_mat4(text->entity.program_id, "projection", text->entity.P);
2615+
gl_prog_set_color(text->entity.program_id, "text_color", text->color);
2616+
gl_prog_set_color(text->entity.program_id, "bg_color", text->bg_color);
26112617
glActiveTexture(GL_TEXTURE0);
2612-
glBindVertexArray(text->vao);
2618+
glBindVertexArray(text->entity.VAO);
26132619

26142620
// Render text
26152621
for (size_t i = 0; i < strlen(s); ++i) {
@@ -2635,7 +2641,7 @@ void gl_text_draw(gl_text_t *text,
26352641
glBindTexture(GL_TEXTURE_2D, ch->texture_id);
26362642

26372643
// Update content of VBO memory
2638-
glBindBuffer(GL_ARRAY_BUFFER, text->vbo);
2644+
glBindBuffer(GL_ARRAY_BUFFER, text->entity.VBO);
26392645
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
26402646
glBindBuffer(GL_ARRAY_BUFFER, 0);
26412647

@@ -3463,11 +3469,11 @@ void gui_setup(gui_t *gui) {
34633469
glEnable(GL_BLEND);
34643470
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
34653471

3466-
assert(glIsEnabled(GL_PROGRAM_POINT_SIZE) == 1);
3467-
assert(glIsEnabled(GL_LINE_SMOOTH) == 1);
3468-
assert(glIsEnabled(GL_DEPTH_TEST) == 1);
3469-
assert(glIsEnabled(GL_CULL_FACE) == 1);
3470-
assert(glIsEnabled(GL_BLEND) == 1);
3472+
assert(glIsEnabled(GL_PROGRAM_POINT_SIZE));
3473+
assert(glIsEnabled(GL_LINE_SMOOTH));
3474+
assert(glIsEnabled(GL_DEPTH_TEST));
3475+
assert(glIsEnabled(GL_CULL_FACE));
3476+
assert(glIsEnabled(GL_BLEND));
34713477

34723478
// Camera
34733479
gl_camera_setup(&gui->camera, &gui->window_width, &gui->window_height);
@@ -3508,7 +3514,7 @@ void gui_loop(gui_t *gui) {
35083514
gl_cube_malloc(cube_size, cube_pos, cube_color, outline_color);
35093515

35103516
// Rect
3511-
gl_rect_t *rect = gl_rect_malloc(0, 0, 0, 0);
3517+
gl_rect_t *rect = gl_rect_malloc((gl_bounds_t){0, 0, 10, 10});
35123518

35133519
// Camera frame
35143520
const gl_float_t cf_pos[3] = {0.0, 0.0, 0.0};
@@ -3582,7 +3588,8 @@ void gui_loop(gui_t *gui) {
35823588
// gl_grid3d_draw(grid, &gui->camera);
35833589
// gl_line3d_draw(line, &gui->camera);
35843590
// gl_points3d_draw(points, &gui->camera, num_points);
3585-
// gl_text_draw(text, &gui->camera, "Here!", 10.0f, 100.0f, 1.0f);
3591+
gl_text_draw(text, &gui->camera, "Here!", 150.0f, 100.0f, 1.0f);
3592+
// gl_text_draw(text, &gui->camera, "Here2!", 500.0f, 500.0f, 1.0f);
35863593
// gl_image_draw(image);
35873594
ui_button_draw(gui, &button);
35883595

@@ -3616,7 +3623,7 @@ void gui_loop(gui_t *gui) {
36163623
"uniform float button_width;\n" \
36173624
"uniform float button_height;\n" \
36183625
"void main() {\n" \
3619-
" gl_Position = vec4(in_pos, 0.0, 1.0);\n" \
3626+
" gl_Position = vec4(in_pos, 0.1, 1.0);\n" \
36203627
"}\n"
36213628

36223629
#define UI_BUTTON_FS \
@@ -3641,9 +3648,9 @@ void ui_button_setup(ui_button_t *button) {
36413648
button->color = (gl_color_t){1.0f, 1.0f, 1.0f};
36423649
button->color_hover = (gl_color_t){1.0f, 0.0f, 0.0f};
36433650
button->color_press = (gl_color_t){0.0f, 0.0f, 1.0f};
3644-
button->bounds.x = 20;
3651+
button->bounds.x = 100;
36453652
button->bounds.y = 600;
3646-
button->bounds.w = 200.0f;
3653+
button->bounds.w = 150.0f;
36473654
button->bounds.h = 100.0f;
36483655

36493656
button->program_id = -1;

0 commit comments

Comments
 (0)