rntviewer/src/mainloop.cpp
2024-09-04 14:38:28 +02:00

136 lines
3.9 KiB
C++

internal
b8 init_imgui(GLFWwindow* window) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void) io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.IniFilename = nullptr;
io.LogFilename = nullptr;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 400");
return true;
}
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, 3);
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 " V_MAJOR "." V_MINOR,
nullptr,
nullptr
);
glfwMakeContextCurrent(window);
return window;
}
internal
void monitor_key(GLFWwindow *window, u8 *key_state, i32 glfw_key, Input_Key key) {
u8 &state = key_state[key];
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;
}
}
internal
void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app)
{
f32 delta_time_ms;
chr::time_point last_saved_time = chr::high_resolution_clock::now();
app.delta_time_accum.max = 100;
app.delta_time_accum.base = arena_push_array<f32>(arena, app.delta_time_accum.max);
while (!app.should_quit) {
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();
delta_time_ms = time_since_prev_frame_us * 0.001f;
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();
// Check if the inspected file changed
{
char buf[sizeof(inotify_event) + NAME_MAX + 1];
ssize_t nbytes = read(app.inspected_file.inot, buf, sizeof(buf));
if (nbytes)
app.inspected_file.size = file_size(app.inspected_file.stream);
}
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(window)) {
app.should_quit = true;
break;
}
b32 focused = glfwGetWindowAttrib(window, GLFW_FOCUSED);
if (focused) {
// Update keyboard
u8 *key_state = app.user_input.key_state;
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);
app.user_input.mouse_pos.x = static_cast<i32>(mposx);
app.user_input.mouse_pos.y = static_cast<i32>(mposy);
}
update_and_render(arena, app, delta_time_ms);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
}