Add stb_sprintf

This commit is contained in:
Phireh 2023-11-01 11:28:34 +01:00
commit a125c864cb
2 changed files with 1918 additions and 3 deletions

View file

@ -30,6 +30,11 @@
// STL // STL
#include <iostream> #include <iostream>
// STB
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
#undef STB_SPRINTF_IMPLEMENTATION
// POSIX // POSIX
#include <time.h> #include <time.h>
@ -231,6 +236,10 @@ uint32_t make_gl_program(const char *pathname_vertex, const char *pathname_fragm
void render_text(const char *text, float x, float y, float scale, glm::vec3 color) void render_text(const char *text, float x, float y, float scale, glm::vec3 color)
{ {
// TODO: Use triangle strip (4 vertices vs 6)
// TODO: Group draw calls together instead of doing 1 draw call/character. Use an atlas with GL_TEXTURE_ARRAY
// TODO: Do not make draw calls for invisible characters!
// activate corresponding render state // activate corresponding render state
glUseProgram(text_program); glUseProgram(text_program);
glUniform3f(glGetUniformLocation(text_program, "text_color"), color.x, color.y, color.z); glUniform3f(glGetUniformLocation(text_program, "text_color"), color.x, color.y, color.z);
@ -629,7 +638,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
static char debug_text_buf[256]; static char debug_text_buf[256];
{ {
TIME_BLOCK(text_render); TIME_BLOCK(text_render);
snprintf(debug_text_buf, 256, "Cursor position %.0f %.0f screen %.2f %.2f world", cursor_x, cursor_y, cursor_world.x, cursor_world.y); 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)); render_text(debug_text_buf, 25, 25, .5f, glm::vec3(1.0f, 1.0f, 1.0f));
for (int i = 0; i < grid.rows * grid.columns; ++i) for (int i = 0; i < grid.rows * grid.columns; ++i)
@ -642,13 +651,13 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
v.y += 1; v.y += 1;
v.y *= window_height/2.0f; v.y *= window_height/2.0f;
char number_string[11]; char number_string[11];
snprintf(number_string, 11, "%d", i); 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)); render_text(number_string, v.x, v.y, .2f + (0.3f / the_camera.size.x), glm::vec3(0.0f, 0.0f, 0.0f));
} }
if (hovered_hex != -1) if (hovered_hex != -1)
{ {
char buf[30]; char buf[30];
snprintf(buf, 30, "Cursor INSIDE hex %d", hovered_hex); 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)); render_text(buf, 25.0f, window_height - 25.0f, .5f, glm::vec3(1.0f, 1.0f, 1.0f));
} }
} }

1906
stb_sprintf.h Normal file

File diff suppressed because it is too large Load diff