diff --git a/src/app_state.h b/src/app_state.h index 086580d..0cdf7ae 100644 --- a/src/app_state.h +++ b/src/app_state.h @@ -12,7 +12,7 @@ struct Inspected_File { String8 name; - // @Platform: inotify file descriptor + // @Platform: inotify file descriptor int inot; }; @@ -29,8 +29,8 @@ struct App_State { Viewer viewer; #endif - String8 ntpl_name; - u64 base_display_addr; + String8 ntpl_name; + u64 base_display_addr; Delta_Time_Accum delta_time_accum; @@ -44,6 +44,6 @@ size_t file_size(FILE *f) fseek(f, 0, SEEK_END); size_t res = ftell(f); fseek(f, 0, SEEK_SET); - return res; + return res; } diff --git a/src/defer.h b/src/defer.h index 5a29ee2..3f896d1 100644 --- a/src/defer.h +++ b/src/defer.h @@ -5,8 +5,8 @@ struct Defer_Guard { F _f; }; struct Defer_Guard_Helper { - template - Defer_Guard operator+(F&& f) { return Defer_Guard(static_cast(f)); } + template + Defer_Guard operator+(F&& f) { return Defer_Guard(static_cast(f)); } }; #define CONCAT_STR_INTERNAL(A, B) A##B diff --git a/src/mainloop.cpp b/src/mainloop.cpp index bed0f64..49579d5 100644 --- a/src/mainloop.cpp +++ b/src/mainloop.cpp @@ -1,63 +1,63 @@ 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_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void) io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.IniFilename = nullptr; + io.LogFilename = nullptr; - ImGui::StyleColorsDark(); + ImGui::StyleColorsDark(); - ImGui_ImplGlfw_InitForOpenGL(window, true); - ImGui_ImplOpenGL3_Init("#version 330"); + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 330"); - return true; + return true; } internal void glfw_error_callback(i32 error, const char *description) { - fprintf(stderr, "GLFW error %d: %s\n", error, 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); + 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); + glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true); #endif - GLFWwindow *window = glfwCreateWindow( - desired_win_width, desired_win_height, - "RNTuple Viewer " V_MAJOR "." V_MINOR, - nullptr, - nullptr - ); + GLFWwindow *window = glfwCreateWindow( + desired_win_width, desired_win_height, + "RNTuple Viewer " V_MAJOR "." V_MINOR, + nullptr, + nullptr + ); - glfwMakeContextCurrent(window); + glfwMakeContextCurrent(window); - return 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; - } + 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 @@ -85,75 +85,75 @@ void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app) app.delta_time_accum.base = arena_push_array(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(frame_start - last_saved_time).count(); - delta_time_ms = time_since_prev_frame_us * 0.001f; - last_saved_time = frame_start; + chr::time_point frame_start = chr::high_resolution_clock::now(); + u64 time_since_prev_frame_us = chr::duration_cast(frame_start - last_saved_time).count(); + delta_time_ms = time_since_prev_frame_us * 0.001f; + last_saved_time = frame_start; - for (u32 i = 0; i < MOUSE_BTN_COUNT; ++i) - app.user_input.mouse_btn_state[i] &= ~(MOUSE_BTN_STATE_JUST_PRESSED|MOUSE_BTN_STATE_JUST_RELEASED); + for (u32 i = 0; i < MOUSE_BTN_COUNT; ++i) + app.user_input.mouse_btn_state[i] &= ~(MOUSE_BTN_STATE_JUST_PRESSED|MOUSE_BTN_STATE_JUST_RELEASED); - glfwPollEvents(); + glfwPollEvents(); - // Update window size - { - Window_Data &wdata = app.win_data; + // Update window size + { + Window_Data &wdata = app.win_data; - i32 prev_width = wdata.width; - i32 prev_height = wdata.height; + i32 prev_width = wdata.width; + i32 prev_height = wdata.height; - glfwGetWindowSize(window, &wdata.width, &wdata.height); + glfwGetWindowSize(window, &wdata.width, &wdata.height); - wdata.size_just_changed = prev_width != wdata.width || prev_height != wdata.height; - } + wdata.size_just_changed = prev_width != wdata.width || prev_height != wdata.height; + } - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); + 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); - } + // 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; - } + 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); - monitor_key(window, key_state, GLFW_KEY_LEFT_SHIFT, KEY_SHIFT); + 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); + monitor_key(window, key_state, GLFW_KEY_LEFT_SHIFT, KEY_SHIFT); - // Update mouse - f64 mposx, mposy; - glfwGetCursorPos(window, &mposx, &mposy); + // Update mouse + f64 mposx, mposy; + glfwGetCursorPos(window, &mposx, &mposy); - u8 *mouse_btn_state = app.user_input.mouse_btn_state; - monitor_mouse_btn(window, mouse_btn_state, GLFW_MOUSE_BUTTON_LEFT, MOUSE_BTN_LEFT); - monitor_mouse_btn(window, mouse_btn_state, GLFW_MOUSE_BUTTON_RIGHT, MOUSE_BTN_RIGHT); + u8 *mouse_btn_state = app.user_input.mouse_btn_state; + monitor_mouse_btn(window, mouse_btn_state, GLFW_MOUSE_BUTTON_LEFT, MOUSE_BTN_LEFT); + monitor_mouse_btn(window, mouse_btn_state, GLFW_MOUSE_BUTTON_RIGHT, MOUSE_BTN_RIGHT); - app.user_input.mouse_pos.x = static_cast(mposx); - app.user_input.mouse_pos.y = static_cast(mposy); - } + app.user_input.mouse_pos.x = static_cast(mposx); + app.user_input.mouse_pos.y = static_cast(mposy); + } - update_and_render(arena, app, delta_time_ms); + update_and_render(arena, app, delta_time_ms); - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - glfwSwapBuffers(window); + glfwSwapBuffers(window); } } diff --git a/src/prof.h b/src/prof.h index 9a40d38..3634d56 100644 --- a/src/prof.h +++ b/src/prof.h @@ -1,14 +1,14 @@ struct Prof_Scope_Print { - Prof_Scope_Print(const char *name) : scope_name { name }, scope_start_t { std::chrono::high_resolution_clock::now() } {} - ~Prof_Scope_Print() { - using namespace std::chrono; - time_point scope_end_t = high_resolution_clock::now(); - duration scope_duration = duration_cast(scope_end_t - scope_start_t); - fprintf(stderr, "[profile] %s took %.4f ms\n", scope_name, scope_duration.count() * 0.001f); - } + Prof_Scope_Print(const char *name) : scope_name { name }, scope_start_t { std::chrono::high_resolution_clock::now() } {} + ~Prof_Scope_Print() { + using namespace std::chrono; + time_point scope_end_t = high_resolution_clock::now(); + duration scope_duration = duration_cast(scope_end_t - scope_start_t); + fprintf(stderr, "[profile] %s took %.4f ms\n", scope_name, scope_duration.count() * 0.001f); + } - const char *scope_name; - std::chrono::time_point scope_start_t; + const char *scope_name; + std::chrono::time_point scope_start_t; }; #define TIMED_SCOPE() const Prof_Scope_Print CONCAT_STR(_prof, __LINE__) { __func__ } diff --git a/src/str.cpp b/src/str.cpp index 5ab4ace..9bf82d0 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -32,10 +32,10 @@ String8 str8_from_c(const char *str) internal String8 to_pretty_size(Arena *arena, u64 bytes) { - if (bytes >= GiB(1)) return push_str8f(arena, "%.1f GiB", (f32)bytes / GiB(1)); - if (bytes >= MiB(1)) return push_str8f(arena, "%.1f MiB", (f32)bytes / MiB(1)); - if (bytes >= KiB(1)) return push_str8f(arena, "%.1f KiB", (f32)bytes / KiB(1)); - return push_str8f(arena, "%zu B", bytes); + if (bytes >= GiB(1)) return push_str8f(arena, "%.1f GiB", (f32)bytes / GiB(1)); + if (bytes >= MiB(1)) return push_str8f(arena, "%.1f MiB", (f32)bytes / MiB(1)); + if (bytes >= KiB(1)) return push_str8f(arena, "%.1f KiB", (f32)bytes / KiB(1)); + return push_str8f(arena, "%zu B", bytes); } internal diff --git a/src/window.h b/src/window.h index eaf14fb..284bbb8 100644 --- a/src/window.h +++ b/src/window.h @@ -1,26 +1,26 @@ enum Input_Key { - KEY_UP, - KEY_DOWN, - KEY_LEFT, - KEY_RIGHT, - KEY_Q, - KEY_ALT, - KEY_TAB, - KEY_SHIFT, + KEY_UP, + KEY_DOWN, + KEY_LEFT, + KEY_RIGHT, + KEY_Q, + KEY_ALT, + KEY_TAB, + KEY_SHIFT, - KEY_COUNT + KEY_COUNT }; enum Key_State : u8 { - KEY_STATE_IS_DOWN = 0x1, - KEY_STATE_JUST_PRESSED = 0x2, - KEY_STATE_JUST_RELEASED = 0x4, + KEY_STATE_IS_DOWN = 0x1, + KEY_STATE_JUST_PRESSED = 0x2, + KEY_STATE_JUST_RELEASED = 0x4, }; enum Mouse_Button { - MOUSE_BTN_LEFT, - MOUSE_BTN_RIGHT, - MOUSE_BTN_COUNT + MOUSE_BTN_LEFT, + MOUSE_BTN_RIGHT, + MOUSE_BTN_COUNT }; enum Mouse_Button_State : u8 { @@ -30,18 +30,18 @@ enum Mouse_Button_State : u8 { }; struct User_Input { - u8 key_state[KEY_COUNT]; - u8 mouse_btn_state[MOUSE_BTN_COUNT]; + u8 key_state[KEY_COUNT]; + u8 mouse_btn_state[MOUSE_BTN_COUNT]; - struct { i32 x; i32 y; } mouse_pos; + struct { i32 x; i32 y; } mouse_pos; }; struct Window_Data { - // Real width and height of the window - i32 width; - i32 height; - bool size_just_changed; + // Real width and height of the window + i32 width; + i32 height; + bool size_just_changed; - f32 desired_aspect_ratio; + f32 desired_aspect_ratio; };