Add debug timers
This commit is contained in:
parent
6741e0ae9b
commit
ae874570c1
2 changed files with 325 additions and 182 deletions
489
main.cpp
489
main.cpp
|
|
@ -30,6 +30,46 @@
|
|||
// STL
|
||||
#include <iostream>
|
||||
|
||||
// POSIX
|
||||
#include <time.h>
|
||||
|
||||
struct debug_timer_t
|
||||
{
|
||||
const char *name;
|
||||
int64_t usec;
|
||||
};
|
||||
|
||||
extern debug_timer_t debug_timers[];
|
||||
extern int ntimers;
|
||||
|
||||
struct debug_clock_t
|
||||
{
|
||||
const char *name;
|
||||
int id;
|
||||
int64_t start;
|
||||
int64_t end;
|
||||
|
||||
debug_clock_t(const char *name, int id)
|
||||
{
|
||||
this->name = name;
|
||||
this->id = id;
|
||||
struct timespec spec;
|
||||
clock_gettime(CLOCK_MONOTONIC, &spec);
|
||||
start = spec.tv_sec * 1000 * 1000 + spec.tv_nsec / 1000;
|
||||
|
||||
}
|
||||
~debug_clock_t()
|
||||
{
|
||||
struct timespec spec;
|
||||
clock_gettime(CLOCK_MONOTONIC, &spec);
|
||||
end = spec.tv_sec * 1000 * 1000 + spec.tv_nsec / 1000;
|
||||
debug_timers[id].name = this->name;
|
||||
debug_timers[id].usec = end - start;
|
||||
}
|
||||
};
|
||||
|
||||
#define TIME_BLOCK(name) debug_clock_t _name_##timer(#name, __COUNTER__);
|
||||
|
||||
struct charglyph_t {
|
||||
uint32_t tex; // handle to glyph texture
|
||||
glm::ivec2 size; // 2d dimensions
|
||||
|
|
@ -38,6 +78,7 @@ struct charglyph_t {
|
|||
};
|
||||
|
||||
struct hex_t {
|
||||
int32_t id;
|
||||
glm::vec3 position;
|
||||
float radius;
|
||||
glm::vec3 vertices[7];
|
||||
|
|
@ -58,6 +99,11 @@ struct camera_t {
|
|||
glm::vec2 size;
|
||||
};
|
||||
|
||||
struct selection_t {
|
||||
int32_t *indices;
|
||||
int32_t nselected;
|
||||
};
|
||||
|
||||
charglyph_t glyphmap[128];
|
||||
uint32_t text_program;
|
||||
uint32_t textvao, textvbo;
|
||||
|
|
@ -79,6 +125,30 @@ double mouse_scroll = 0.0;
|
|||
|
||||
#define MAIN_ERROR(e) do { main_errcode = e; errcode_string = #e; goto exit_main_with_error; } while(0)
|
||||
|
||||
bool is_selected(int idx, selection_t *sel)
|
||||
{
|
||||
for (int i = 0; i < sel->nselected; ++i)
|
||||
if (sel->indices[i] == idx)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void select(int idx, selection_t *sel)
|
||||
{
|
||||
if (!is_selected(idx, sel))
|
||||
sel->indices[sel->nselected++] = idx;
|
||||
}
|
||||
|
||||
void deselect(int idx, selection_t *sel)
|
||||
{
|
||||
for (int i = 0; i < sel->nselected; ++i)
|
||||
if (sel->indices[i] == idx)
|
||||
{
|
||||
sel->indices[i] = -1;
|
||||
--sel->nselected;
|
||||
}
|
||||
}
|
||||
|
||||
void glfw_error_callback(int error, const char* description)
|
||||
{
|
||||
fprintf(stderr, "GLFW error %d: %s\n", error, description);
|
||||
|
|
@ -180,13 +250,13 @@ void render_text(const char *text, float x, float y, float scale, glm::vec3 colo
|
|||
float h = charglyph.size.y * scale;
|
||||
// update VBO for each character
|
||||
float vertices[6][4] = {
|
||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||
{ xpos, ypos, 0.0f, 1.0f },
|
||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||
{ xpos, ypos, 0.0f, 1.0f },
|
||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||
|
||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||
{ xpos + w, ypos + h, 1.0f, 0.0f }
|
||||
{ xpos, ypos + h, 0.0f, 0.0f },
|
||||
{ xpos + w, ypos, 1.0f, 1.0f },
|
||||
{ xpos + w, ypos + h, 1.0f, 0.0f }
|
||||
};
|
||||
// render glyph texture over quad
|
||||
glBindTexture(GL_TEXTURE_2D, charglyph.tex);
|
||||
|
|
@ -211,11 +281,77 @@ bool inside_hex(hex_t *hex, glm::vec2 point)
|
|||
glm::vec2 inner_vec = glm::vec2(point - glm::vec2(hex->vertices[i].x, hex->vertices[i].y));
|
||||
float angle = glm::orientedAngle(glm::normalize(outer_edge), glm::normalize(inner_vec));
|
||||
if (angle > 0.0f)
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void create_grid(grid_t *grid, int rows, int columns)
|
||||
{
|
||||
grid->rows = rows;
|
||||
grid->columns = columns;
|
||||
if (!grid->hexes)
|
||||
grid->hexes = (hex_t*)calloc(grid->rows * grid->columns, sizeof(hex_t));
|
||||
else
|
||||
{
|
||||
hex_t *new_ptr = (hex_t*)realloc(grid->hexes, grid->rows * grid->columns * sizeof(hex_t));
|
||||
if (new_ptr)
|
||||
grid->hexes = new_ptr;
|
||||
else
|
||||
{
|
||||
free(grid->hexes);
|
||||
grid->hexes = (hex_t*)calloc(grid->rows * grid->columns, sizeof(hex_t));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < grid->rows; ++i)
|
||||
for (int j = 0; j < grid->columns; ++j)
|
||||
{
|
||||
hex_t *the_hexagon = &grid->hexes[i*grid->columns+j];
|
||||
float radius = 0.2;
|
||||
the_hexagon->position = glm::vec3(0,0,0) + glm::vec3(j * radius * 3.0f * glm::cos(glm::radians(60.0))
|
||||
, (i * radius * 2.0f * glm::sin(glm::radians(60.0))) + (j % 2 ? 0 : radius * glm::sin(glm::radians(60.0))),
|
||||
0);
|
||||
the_hexagon->radius = radius;
|
||||
the_hexagon->id = i * grid->columns + j;
|
||||
the_hexagon->color = glm::vec4(1.0 - i * 0.1, 1.0 - j * 0.1, 1.0, 1.0);
|
||||
the_hexagon->vertices[0] = the_hexagon->position;
|
||||
the_hexagon->vertices[1] = the_hexagon->position + glm::vec3(-radius, 0, 0);
|
||||
the_hexagon->vertices[2] = the_hexagon->position + glm::vec3(-radius * glm::cos(glm::radians(60.0)), radius * glm::sin(glm::radians(60.0)), 0);
|
||||
the_hexagon->vertices[3] = the_hexagon->position + glm::vec3(radius * glm::cos(glm::radians(60.0)), radius * glm::sin(glm::radians(60.0)), 0);
|
||||
the_hexagon->vertices[4] = the_hexagon->position + glm::vec3(radius, 0, 0);
|
||||
the_hexagon->vertices[5] = the_hexagon->position + glm::vec3(radius * glm::cos(glm::radians(60.0)), -radius * glm::sin(glm::radians(60.0)), 0);
|
||||
the_hexagon->vertices[6] = the_hexagon->position + glm::vec3(-radius * glm::cos(glm::radians(60.0)), -radius * glm::sin(glm::radians(60.0)), 0);
|
||||
|
||||
glGenVertexArrays(1, &the_hexagon->vao);
|
||||
glGenBuffers(1, &the_hexagon->vbo);
|
||||
glBindVertexArray(the_hexagon->vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, the_hexagon->vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 7 * 3, the_hexagon->vertices, GL_STATIC_DRAW);
|
||||
|
||||
// EBO setup
|
||||
unsigned int indices[18];
|
||||
indices[0] = 0; indices[1] = 2; indices[2] = 1;
|
||||
indices[3] = 0; indices[4] = 3; indices[5] = 2;
|
||||
indices[6] = 0; indices[7] = 4; indices[8] = 3;
|
||||
indices[9] = 0; indices[10] = 5; indices[11] = 4;
|
||||
indices[12] = 0; indices[13] = 6; indices[14] = 5;
|
||||
indices[15] = 0; indices[16] = 1; indices[17] = 6;
|
||||
|
||||
glGenBuffers(1, &the_hexagon->ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, the_hexagon->ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*3 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
}
|
||||
|
||||
// global state
|
||||
GLFWwindow *window = nullptr;
|
||||
hex_t hexes[4];
|
||||
|
|
@ -238,6 +374,7 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
|
|||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSwapInterval(1);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
if (!gladLoadGL())
|
||||
|
|
@ -322,54 +459,9 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
|
|||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
grid_t grid;
|
||||
grid.rows = 10;
|
||||
grid.columns = 10;
|
||||
grid.hexes = (hex_t*)calloc(grid.rows * grid.columns, sizeof(hex_t));
|
||||
static grid_t grid = {};
|
||||
|
||||
|
||||
for (int i = 0; i < grid.rows; ++i)
|
||||
for (int j = 0; j < grid.columns; ++j)
|
||||
{
|
||||
hex_t *the_hexagon = &grid.hexes[i*grid.columns+j];
|
||||
float radius = 0.2;
|
||||
the_hexagon->position = glm::vec3(0,0,0) + glm::vec3(j * radius * 3.0f * glm::cos(glm::radians(60.0))
|
||||
, (i * radius * 2.0f * glm::sin(glm::radians(60.0))) + (j % 2 ? 0 : radius * glm::sin(glm::radians(60.0))),
|
||||
0);
|
||||
the_hexagon->radius = radius;
|
||||
the_hexagon->color = glm::vec4(1.0 - i * 0.1, 1.0 - j * 0.1, 1.0, 1.0);
|
||||
the_hexagon->vertices[0] = the_hexagon->position;
|
||||
the_hexagon->vertices[1] = the_hexagon->position + glm::vec3(-radius, 0, 0);
|
||||
the_hexagon->vertices[2] = the_hexagon->position + glm::vec3(-radius * glm::cos(glm::radians(60.0)), radius * glm::sin(glm::radians(60.0)), 0);
|
||||
the_hexagon->vertices[3] = the_hexagon->position + glm::vec3(radius * glm::cos(glm::radians(60.0)), radius * glm::sin(glm::radians(60.0)), 0);
|
||||
the_hexagon->vertices[4] = the_hexagon->position + glm::vec3(radius, 0, 0);
|
||||
the_hexagon->vertices[5] = the_hexagon->position + glm::vec3(radius * glm::cos(glm::radians(60.0)), -radius * glm::sin(glm::radians(60.0)), 0);
|
||||
the_hexagon->vertices[6] = the_hexagon->position + glm::vec3(-radius * glm::cos(glm::radians(60.0)), -radius * glm::sin(glm::radians(60.0)), 0);
|
||||
|
||||
glGenVertexArrays(1, &the_hexagon->vao);
|
||||
glGenBuffers(1, &the_hexagon->vbo);
|
||||
glBindVertexArray(the_hexagon->vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, the_hexagon->vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 7 * 3, the_hexagon->vertices, GL_STATIC_DRAW);
|
||||
|
||||
// EBO setup
|
||||
unsigned int indices[18];
|
||||
indices[0] = 0; indices[1] = 2; indices[2] = 1;
|
||||
indices[3] = 0; indices[4] = 3; indices[5] = 2;
|
||||
indices[6] = 0; indices[7] = 4; indices[8] = 3;
|
||||
indices[9] = 0; indices[10] = 5; indices[11] = 4;
|
||||
indices[12] = 0; indices[13] = 6; indices[14] = 5;
|
||||
indices[15] = 0; indices[16] = 1; indices[17] = 6;
|
||||
|
||||
glGenBuffers(1, &the_hexagon->ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, the_hexagon->ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*3 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
create_grid(&grid, 10, 10);
|
||||
|
||||
hex_program = make_gl_program("shaders/hex.vert", "shaders/hex.frag");
|
||||
|
||||
|
|
@ -387,164 +479,211 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
|
|||
camera_t the_camera;
|
||||
bool mouse_pressed;
|
||||
bool mouse_over_gui;
|
||||
static selection_t selection;
|
||||
selection.indices = (int32_t*)calloc(1, sizeof(int32_t) * grid.rows * grid.columns);
|
||||
selection.nselected = 0;
|
||||
|
||||
the_camera.position = glm::vec3(0.0f, 0.0f, 1.0f);
|
||||
the_camera.size = glm::vec2(1.0f, 1.0f);
|
||||
// main loop
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
TIME_BLOCK(full_frame);
|
||||
double cursor_dx = 0.0;
|
||||
double cursor_dy = 0.0;
|
||||
int hovered_hex = -1;
|
||||
int32_t window_width, window_height;
|
||||
glfwGetWindowSize(window, &window_width, &window_height);
|
||||
glViewport(0, 0, window_width, window_height);
|
||||
{
|
||||
TIME_BLOCK(window_size_polling);
|
||||
glfwGetWindowSize(window, &window_width, &window_height);
|
||||
glViewport(0, 0, window_width, window_height);
|
||||
}
|
||||
float aspect_ratio = (float)window_width/window_height;
|
||||
|
||||
/* Input handling */
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
the_camera.position.y += 0.1;
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
the_camera.position.y -= 0.1;
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
the_camera.position.x -= 0.1;
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
the_camera.position.x += 0.1;
|
||||
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
|
||||
the_camera.size -= glm::vec2(0.1);
|
||||
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS)
|
||||
the_camera.size += glm::vec2(0.1);
|
||||
if (mouse_scroll > 0.0)
|
||||
{
|
||||
the_camera.size -= glm::vec2(0.1);
|
||||
mouse_scroll = 0.0;
|
||||
}
|
||||
if (mouse_scroll < 0.0)
|
||||
{
|
||||
the_camera.size += glm::vec2(0.1);
|
||||
mouse_scroll = 0.0;
|
||||
}
|
||||
TIME_BLOCK(input_handling);
|
||||
/* Input handling */
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
the_camera.position.y += 0.1;
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
the_camera.position.y -= 0.1;
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
the_camera.position.x -= 0.1;
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
the_camera.position.x += 0.1;
|
||||
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
|
||||
the_camera.size -= glm::vec2(0.1);
|
||||
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS)
|
||||
the_camera.size += glm::vec2(0.1);
|
||||
if (mouse_scroll > 0.0)
|
||||
{
|
||||
the_camera.size -= glm::vec2(0.1);
|
||||
mouse_scroll = 0.0;
|
||||
}
|
||||
if (mouse_scroll < 0.0)
|
||||
{
|
||||
the_camera.size += glm::vec2(0.1);
|
||||
mouse_scroll = 0.0;
|
||||
}
|
||||
|
||||
mouse_pressed = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
|
||||
mouse_over_gui = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem);
|
||||
mouse_pressed = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
|
||||
mouse_over_gui = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem);
|
||||
|
||||
cursor_dx = cursor_x;
|
||||
cursor_dy = cursor_y;
|
||||
cursor_dx = cursor_x;
|
||||
cursor_dy = cursor_y;
|
||||
|
||||
glfwGetCursorPos(window, &cursor_x, &cursor_y);
|
||||
glfwGetCursorPos(window, &cursor_x, &cursor_y);
|
||||
|
||||
cursor_dx -= cursor_x;
|
||||
cursor_dy -= cursor_y;
|
||||
cursor_dx -= cursor_x;
|
||||
cursor_dy -= cursor_y;
|
||||
|
||||
// Mouse movement
|
||||
if (!mouse_over_gui && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
|
||||
{
|
||||
fprintf(stdout, "Moving camera by %f %f\n", cursor_dx, cursor_dx);
|
||||
// mouse delta!
|
||||
the_camera.position.x += cursor_dx * the_camera.size.x/window_width;
|
||||
// The Y coord is inverted
|
||||
the_camera.position.y -= cursor_dy * the_camera.size.y/window_height;
|
||||
// Mouse movement
|
||||
if (!mouse_over_gui && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
|
||||
{
|
||||
fprintf(stdout, "Moving camera by %f %f\n", cursor_dx, cursor_dx);
|
||||
// mouse delta!
|
||||
the_camera.position.x += cursor_dx * the_camera.size.x/window_width;
|
||||
// The Y coord is inverted
|
||||
the_camera.position.y -= cursor_dy * the_camera.size.y/window_height;
|
||||
}
|
||||
|
||||
if (the_camera.size.x < 0.1)
|
||||
the_camera.size.x = 0.1;
|
||||
if (the_camera.size.y < 0.1)
|
||||
the_camera.size.y = 0.1;
|
||||
}
|
||||
|
||||
if (the_camera.size.x < 0.1)
|
||||
the_camera.size.x = 0.1;
|
||||
if (the_camera.size.y < 0.1)
|
||||
the_camera.size.y = 0.1;
|
||||
|
||||
glm::mat4 model_matrix;
|
||||
glm::mat4 view_matrix;
|
||||
glm::mat4 proj_matrix;
|
||||
glm::vec2 cursor_world;
|
||||
|
||||
/* Rendering */
|
||||
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
{
|
||||
TIME_BLOCK(hex_render);
|
||||
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glm::mat4 model_matrix = glm::mat4(1.0f);
|
||||
glm::mat4 view_matrix = glm::mat4(1.0f);
|
||||
glm::mat4 proj_matrix = glm::ortho((the_camera.position.x - the_camera.size.x/2) * aspect_ratio, (the_camera.position.x + the_camera.size.x/2) * aspect_ratio, the_camera.position.y - the_camera.size.y/2, the_camera.position.y + the_camera.size.y/2);
|
||||
model_matrix = glm::mat4(1.0f);
|
||||
view_matrix = glm::mat4(1.0f);
|
||||
proj_matrix = glm::ortho((the_camera.position.x - the_camera.size.x/2) * aspect_ratio, (the_camera.position.x + the_camera.size.x/2) * aspect_ratio, the_camera.position.y - the_camera.size.y/2, the_camera.position.y + the_camera.size.y/2);
|
||||
|
||||
glm::vec4 aux((float)cursor_x, (float)cursor_y, 0.0f, 1.0f);
|
||||
glm::vec4 aux((float)cursor_x, (float)cursor_y, 0.0f, 1.0f);
|
||||
|
||||
// move screen coordinates to NDC
|
||||
aux.x -= window_width/2.0f;
|
||||
aux.x /= window_width/2.0f;
|
||||
aux.y -= window_height/2.0f;
|
||||
aux.y /= -window_height/2.0f;
|
||||
aux = glm::inverse(proj_matrix) * aux;
|
||||
glm::vec2 cursor_world = glm::vec2(aux.x, aux.y);
|
||||
// move screen coordinates to NDC
|
||||
aux.x -= window_width/2.0f;
|
||||
aux.x /= window_width/2.0f;
|
||||
aux.y -= window_height/2.0f;
|
||||
aux.y /= -window_height/2.0f;
|
||||
aux = glm::inverse(proj_matrix) * aux;
|
||||
cursor_world = glm::vec2(aux.x, aux.y);
|
||||
|
||||
glUseProgram(hex_program);
|
||||
glUniformMatrix4fv(glGetUniformLocation(hex_program, "model"), 1, GL_FALSE, &model_matrix[0][0]);
|
||||
glUniformMatrix4fv(glGetUniformLocation(hex_program, "view"), 1, GL_FALSE, &view_matrix[0][0]);
|
||||
glUniformMatrix4fv(glGetUniformLocation(hex_program, "proj"), 1, GL_FALSE, &proj_matrix[0][0]);
|
||||
|
||||
glUseProgram(hex_program);
|
||||
|
||||
glUniformMatrix4fv(glGetUniformLocation(hex_program, "model"), 1, GL_FALSE, &model_matrix[0][0]);
|
||||
glUniformMatrix4fv(glGetUniformLocation(hex_program, "view"), 1, GL_FALSE, &view_matrix[0][0]);
|
||||
glUniformMatrix4fv(glGetUniformLocation(hex_program, "proj"), 1, GL_FALSE, &proj_matrix[0][0]);
|
||||
for (int i = 0; i < grid.rows; ++i)
|
||||
for (int j = 0; j < grid.columns; j++)
|
||||
{
|
||||
int idx = i * grid.columns + j;
|
||||
if (!mouse_over_gui && inside_hex(&grid.hexes[idx], cursor_world)) // draw hovered hex differently
|
||||
{
|
||||
hovered_hex = i * grid.columns + j;
|
||||
if (mouse_pressed)
|
||||
{
|
||||
select(idx, &selection);
|
||||
}
|
||||
else
|
||||
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), 1.0f, 0.0f, 0.0f, 1.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
for (int i = 0; i < grid.rows; ++i)
|
||||
for (int j = 0; j < grid.columns; j++)
|
||||
{
|
||||
int idx = i * grid.columns + j;
|
||||
if (!mouse_over_gui && inside_hex(&grid.hexes[idx], cursor_world)) // draw hovered hex differently
|
||||
{
|
||||
hovered_hex = i * grid.columns + j;
|
||||
if (mouse_pressed)
|
||||
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), 1.0f, 1.0f, 0.0f, 1.0f);
|
||||
else
|
||||
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), 1.0f, 0.0f, 0.0f, 1.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), grid.hexes[idx].color.x, grid.hexes[idx].color.y, grid.hexes[idx].color.z, grid.hexes[idx].color.w);
|
||||
}
|
||||
if (is_selected(idx, &selection))
|
||||
{
|
||||
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), 1.0f, 1.0f, 0.0f, 1.0f);
|
||||
}
|
||||
else
|
||||
glUniform4f(glGetUniformLocation(hex_program, "hex_color"), grid.hexes[idx].color.x, grid.hexes[idx].color.y, grid.hexes[idx].color.z, grid.hexes[idx].color.w);
|
||||
}
|
||||
|
||||
glBindVertexArray(grid.hexes[idx].vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, grid.hexes[idx].vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, grid.hexes[idx].ebo);
|
||||
glEnable(GL_CULL_FACE);
|
||||
glDrawElements(GL_TRIANGLES, 3*6, GL_UNSIGNED_INT, 0);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
glBindVertexArray(grid.hexes[idx].vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, grid.hexes[idx].vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, grid.hexes[idx].ebo);
|
||||
glEnable(GL_CULL_FACE);
|
||||
glDrawElements(GL_TRIANGLES, 3*6, GL_UNSIGNED_INT, 0);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
glUseProgram(text_program);
|
||||
// text rendering
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glUseProgram(text_program);
|
||||
// text rendering
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
glm::mat4 text_proj_matrix = glm::ortho(0.0f, (float)window_width, 0.0f, (float)window_height);
|
||||
glUniformMatrix4fv(glGetUniformLocation(text_program, "projection"), 1, GL_FALSE, &text_proj_matrix[0][0]);
|
||||
|
||||
static char debug_text_buf[256];
|
||||
snprintf(debug_text_buf, 256, "Cursor position %.0f %.0f screen %.2f %.2f world", cursor_x, cursor_y, cursor_world.x, cursor_world.y);
|
||||
{
|
||||
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);
|
||||
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)
|
||||
{
|
||||
// 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;
|
||||
v.x += 1;
|
||||
v.x *= window_width/2.0f;
|
||||
v.y += 1;
|
||||
v.y *= window_height/2.0f;
|
||||
char number_string[11];
|
||||
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));
|
||||
}
|
||||
if (hovered_hex != -1)
|
||||
{
|
||||
char buf[30];
|
||||
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));
|
||||
for (int i = 0; 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;
|
||||
v.x += 1;
|
||||
v.x *= window_width/2.0f;
|
||||
v.y += 1;
|
||||
v.y *= window_height/2.0f;
|
||||
char number_string[11];
|
||||
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));
|
||||
}
|
||||
if (hovered_hex != -1)
|
||||
{
|
||||
char buf[30];
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
glDisable(GL_BLEND);
|
||||
glfwPollEvents();
|
||||
|
||||
static int grid_rows = grid.rows;
|
||||
static int grid_columns = grid.columns;
|
||||
|
||||
if (grid_rows != grid.rows || grid_columns != grid.columns)
|
||||
{
|
||||
create_grid(&grid, grid_rows, grid_columns);
|
||||
}
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
ImGui::Begin("Hi there");
|
||||
ImGui::End();
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
{
|
||||
TIME_BLOCK(gui_render);
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
ImGui::Begin("Hi there");
|
||||
ImGui::SliderInt("Rows", &grid_rows, 1, 100);
|
||||
ImGui::SliderInt("Columns", &grid_columns, 1, 100);
|
||||
if (ImGui::CollapsingHeader("Debug timers"))
|
||||
{
|
||||
for (int i = 0; i < ntimers; ++i)
|
||||
{
|
||||
ImGui::Text("%s: %d nsecs", debug_timers[i].name, debug_timers[i].usec);
|
||||
}
|
||||
}
|
||||
ImGui::End();
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
}
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
|
@ -557,8 +696,12 @@ int main([[maybe_unused]]int argc, [[maybe_unused]]char **argv)
|
|||
return 0;
|
||||
exit_main_with_error:
|
||||
if (window)
|
||||
glfwDestroyWindow(window);
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
fprintf(stderr, "Hexnando failed with errcode %d: %s\n", main_errcode, errcode_string);
|
||||
return main_errcode;
|
||||
}
|
||||
|
||||
#define NTIMERS __COUNTER__
|
||||
int ntimers = NTIMERS;
|
||||
debug_timer_t debug_timers[NTIMERS];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue