rntviewer/src/mainloop.cpp

121 lines
3.5 KiB
C++
Raw Normal View History

2024-07-11 12:59:59 +00:00
internal
void glfw_error_callback(i32 error, const char *description) {
fprintf(stderr, "GLFW error %d: %s\n", error, description);
}
internal
GLFWwindow *init_glfw(i32 desired_win_width, i32 desired_win_height)
{
glfwSetErrorCallback(glfw_error_callback);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
glfwWindowHint(GLFW_DEPTH_BITS, 32);
#ifndef NDEBUG
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
#endif
GLFWwindow *window = glfwCreateWindow(
desired_win_width, desired_win_height,
"RNTuple Viewer",
nullptr,
nullptr
);
glfwMakeContextCurrent(window);
return window;
}
2024-07-10 19:47:37 +00:00
internal
2024-07-16 15:58:01 +00:00
void monitor_key(GLFWwindow *window, u8 *key_state, i32 glfw_key, Input_Key key) {
u8 &state = key_state[key];
2024-07-10 17:38:16 +00:00
if (glfwGetKey(window, glfw_key) == GLFW_PRESS) {
if (!(state & KEY_STATE_IS_DOWN)) state |= KEY_STATE_JUST_PRESSED;
state |= KEY_STATE_IS_DOWN;
} else if (glfwGetKey(window, glfw_key) == GLFW_RELEASE) {
if (state & KEY_STATE_IS_DOWN) state |= KEY_STATE_JUST_RELEASED;
state &= ~KEY_STATE_IS_DOWN;
}
}
2024-07-10 19:47:37 +00:00
internal
void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app)
2024-07-10 17:38:16 +00:00
{
2024-07-11 12:59:59 +00:00
f32 delta_time_ms;
2024-07-10 17:38:16 +00:00
chr::time_point last_saved_time = chr::high_resolution_clock::now();
2024-07-11 12:59:59 +00:00
app.delta_time_accum.max = 100;
app.delta_time_accum.base = arena_push_array<f32>(arena, app.delta_time_accum.max);
2024-07-10 17:38:16 +00:00
b8 running = true;
while (running) {
chr::time_point frame_start = chr::high_resolution_clock::now();
u64 time_since_prev_frame_us = chr::duration_cast<chr::microseconds>(frame_start - last_saved_time).count();
2024-07-11 12:59:59 +00:00
delta_time_ms = time_since_prev_frame_us * 0.001f;
2024-07-10 17:38:16 +00:00
last_saved_time = frame_start;
glfwPollEvents();
// Update window size
{
Window_Data &wdata = app.win_data;
i32 prev_width = wdata.width;
i32 prev_height = wdata.height;
glfwGetWindowSize(window, &wdata.width, &wdata.height);
wdata.size_just_changed = prev_width != wdata.width || prev_height != wdata.height;
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
2024-07-10 20:30:49 +00:00
// Check if the inspected file changed
{
char buf[sizeof(inotify_event) + NAME_MAX + 1];
2024-07-16 12:34:51 +00:00
ssize_t nbytes = read(app.inspected_file.inot, buf, sizeof(buf));
2024-07-10 20:30:49 +00:00
if (nbytes)
2024-07-16 12:34:51 +00:00
app.inspected_file.size = file_size(app.inspected_file.stream);
2024-07-10 20:30:49 +00:00
}
2024-07-16 15:58:01 +00:00
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(window)) {
2024-07-10 17:38:16 +00:00
running = false;
break;
}
b32 focused = glfwGetWindowAttrib(window, GLFW_FOCUSED);
if (focused) {
// Update keyboard
2024-07-16 15:58:01 +00:00
u8 *key_state = app.user_input.key_state;
2024-07-10 17:38:16 +00:00
monitor_key(window, key_state, GLFW_KEY_Q, KEY_Q);
monitor_key(window, key_state, GLFW_KEY_UP, KEY_UP);
monitor_key(window, key_state, GLFW_KEY_DOWN, KEY_DOWN);
monitor_key(window, key_state, GLFW_KEY_LEFT, KEY_LEFT);
monitor_key(window, key_state, GLFW_KEY_RIGHT, KEY_RIGHT);
monitor_key(window, key_state, GLFW_KEY_LEFT_ALT, KEY_ALT);
monitor_key(window, key_state, GLFW_KEY_TAB, KEY_TAB);
// Update mouse
f64 mposx, mposy;
glfwGetCursorPos(window, &mposx, &mposy);
2024-07-16 15:58:01 +00:00
app.user_input.mouse_pos.x = static_cast<i32>(mposx);
app.user_input.mouse_pos.y = static_cast<i32>(mposy);
2024-07-10 17:38:16 +00:00
}
2024-07-11 12:59:59 +00:00
update_and_render(arena, app, delta_time_ms);
2024-07-10 17:38:16 +00:00
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
}