From 4d4cbd334f82e19cae3256c060ae7cb868f329b5 Mon Sep 17 00:00:00 2001 From: Phireh Date: Mon, 8 Jan 2024 13:49:38 +0100 Subject: [PATCH] Enable debug-friendly optimizations --- Makefile | 5 +++-- main.cpp | 50 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 4a5097a..b0d5145 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ LINK_FLAGS=-lm -lpthread -ldl -lm -lGLEW -lEGL -lGL -lGLU -lOpenGL -lglfw -lfreetype -CFLAGS=-I./include -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-4 -Iimgui -pthread +CFLAGS=-I./include -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-4 -Iimgui -pthread -fno-exceptions .PHONY: hexnando + hexnando: main.cpp - g++ -std=c++20 -Wall -g -Wextra -pedantic -Werror main.cpp ${LINK_FLAGS} ${CFLAGS} -o hexnando + g++ -std=c++20 -Wall -g -Wextra -pedantic -Werror -Og main.cpp ${LINK_FLAGS} ${CFLAGS} -o hexnando diff --git a/main.cpp b/main.cpp index 36b71de..615f4ab 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,13 @@ #include #include "glad.c" +// math types +#include +#include +#include +#include + + #define IMGUI_IMPL_OPENGL_LOADER_CUSTOM #include "imgui/imgui.cpp" @@ -21,12 +28,6 @@ #include #include FT_FREETYPE_H -// math types -#include -#include -#include -#include - // STL #include @@ -279,9 +280,7 @@ void render_text(const char *text, float x, float y, float scale, glm::vec3 colo { // Avoid drawing more than ARRAY_LIMIT chars at a time if (text_frame_idx == ARRAY_LIMIT) - { render_queued_text(color); - } char c = *p; charglyph_t charglyph = glyphmap[(size_t)c]; @@ -325,6 +324,9 @@ bool inside_hex(hex_t *hex, glm::vec2 point) return true; } +char *hex_labels = nullptr; +const uint32_t label_size = 8; + void create_grid(grid_t *grid, int rows, int columns) { grid->rows = rows; @@ -343,6 +345,26 @@ void create_grid(grid_t *grid, int rows, int columns) } } + if (!hex_labels) + { + hex_labels = (char*)calloc(grid->rows * grid->columns * label_size, 1); + } + else + { + char *new_ptr = (char*)realloc(hex_labels, grid->rows * grid->columns * label_size); + if (new_ptr) + hex_labels = new_ptr; + else + { + free(hex_labels); + hex_labels = (char*)calloc(grid->rows * grid->columns * label_size, 1); + } + } + + // Fill labels buffer + for (int n = 0; n < grid->rows * grid->columns; n++) + stbsp_snprintf(&hex_labels[n*label_size], label_size, "%d", n); + for (int i = 0; i < grid->rows; ++i) for (int j = 0; j < grid->columns; ++j) { @@ -506,7 +528,6 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv) glBindVertexArray(0); static grid_t grid = {}; - create_grid(&grid, 10, 10); hex_program = make_gl_program("shaders/hex.vert", "shaders/hex.frag"); @@ -686,11 +707,12 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv) stbsp_snprintf(debug_text_buf, 256, "Cursor position %.0f %.0f screen %.2f %.2f world", cursor_x, cursor_y, cursor_world.x, cursor_world.y); render_text(debug_text_buf, 25, 25, .5f, glm::vec3(1.0f, 1.0f, 1.0f), true); + glm::mat4 camera_transform = proj_matrix * view_matrix * model_matrix; + for (int i = 0; show_hex_numbers && i < grid.rows * grid.columns; ++i) { // Get position of hexes in screen - glm::vec4 v = glm::vec4(grid.hexes[i].position.x, grid.hexes[i].position.y, grid.hexes[i].position.z, 1.0); - v = proj_matrix * view_matrix * model_matrix * v; + glm::vec4 v = camera_transform * glm::vec4(grid.hexes[i].position.x, grid.hexes[i].position.y, grid.hexes[i].position.z, 1.0); v.x += 1; v.x *= window_width/2.0f; v.y += 1; @@ -698,14 +720,12 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv) // Do not draw text outside visible window if (v.x > window_width || v.x < 0 || v.y > window_height || v.y < 0) continue; - char number_string[11]; - stbsp_snprintf(number_string, 11, "%d", i); - render_text(number_string, v.x, v.y, .2f + (0.3f / the_camera.size.x), glm::vec3(0.0f, 0.0f, 0.0f), false); + render_text(&hex_labels[i*label_size], v.x, v.y, .2f + (0.3f / the_camera.size.x), glm::vec3(0.0f, 0.0f, 0.0f), false); } render_queued_text(glm::vec3(0.0f, 0.0f, 0.0f)); if (hovered_hex != -1) { - char buf[30]; + static char buf[30]; stbsp_snprintf(buf, 30, "Cursor INSIDE hex %d", hovered_hex); render_text(buf, 25.0f, window_height - 25.0f, .5f, glm::vec3(1.0f, 1.0f, 1.0f), true); }