formatting

This commit is contained in:
silverweed 2024-09-27 16:31:21 +02:00
parent 9ecc970aff
commit bb4e43a901
6 changed files with 133 additions and 133 deletions

View file

@ -12,7 +12,7 @@ struct Inspected_File {
String8 name; String8 name;
// @Platform: inotify file descriptor // @Platform: inotify file descriptor
int inot; int inot;
}; };
@ -29,8 +29,8 @@ struct App_State {
Viewer viewer; Viewer viewer;
#endif #endif
String8 ntpl_name; String8 ntpl_name;
u64 base_display_addr; u64 base_display_addr;
Delta_Time_Accum delta_time_accum; Delta_Time_Accum delta_time_accum;
@ -44,6 +44,6 @@ size_t file_size(FILE *f)
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
size_t res = ftell(f); size_t res = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
return res; return res;
} }

View file

@ -5,8 +5,8 @@ struct Defer_Guard {
F _f; F _f;
}; };
struct Defer_Guard_Helper { struct Defer_Guard_Helper {
template <typename F> template <typename F>
Defer_Guard<F> operator+(F&& f) { return Defer_Guard<F>(static_cast<F &&>(f)); } Defer_Guard<F> operator+(F&& f) { return Defer_Guard<F>(static_cast<F &&>(f)); }
}; };
#define CONCAT_STR_INTERNAL(A, B) A##B #define CONCAT_STR_INTERNAL(A, B) A##B

View file

@ -1,63 +1,63 @@
internal internal
b8 init_imgui(GLFWwindow* window) { b8 init_imgui(GLFWwindow* window) {
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void) io; ImGuiIO& io = ImGui::GetIO(); (void) io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.IniFilename = nullptr; io.IniFilename = nullptr;
io.LogFilename = nullptr; io.LogFilename = nullptr;
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330"); ImGui_ImplOpenGL3_Init("#version 330");
return true; return true;
} }
internal internal
void glfw_error_callback(i32 error, const char *description) { 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 internal
GLFWwindow *init_glfw(i32 desired_win_width, i32 desired_win_height) GLFWwindow *init_glfw(i32 desired_win_width, i32 desired_win_height)
{ {
glfwSetErrorCallback(glfw_error_callback); glfwSetErrorCallback(glfw_error_callback);
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE); glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
glfwWindowHint(GLFW_DEPTH_BITS, 32); glfwWindowHint(GLFW_DEPTH_BITS, 32);
#ifndef NDEBUG #ifndef NDEBUG
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
#endif #endif
GLFWwindow *window = glfwCreateWindow( GLFWwindow *window = glfwCreateWindow(
desired_win_width, desired_win_height, desired_win_width, desired_win_height,
"RNTuple Viewer " V_MAJOR "." V_MINOR, "RNTuple Viewer " V_MAJOR "." V_MINOR,
nullptr, nullptr,
nullptr nullptr
); );
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
return window; return window;
} }
internal internal
void monitor_key(GLFWwindow *window, u8 *key_state, i32 glfw_key, Input_Key key) { void monitor_key(GLFWwindow *window, u8 *key_state, i32 glfw_key, Input_Key key) {
u8 &state = key_state[key]; u8 &state = key_state[key];
if (glfwGetKey(window, glfw_key) == GLFW_PRESS) { if (glfwGetKey(window, glfw_key) == GLFW_PRESS) {
if (!(state & KEY_STATE_IS_DOWN)) state |= KEY_STATE_JUST_PRESSED; if (!(state & KEY_STATE_IS_DOWN)) state |= KEY_STATE_JUST_PRESSED;
state |= KEY_STATE_IS_DOWN; state |= KEY_STATE_IS_DOWN;
} else if (glfwGetKey(window, glfw_key) == GLFW_RELEASE) { } else if (glfwGetKey(window, glfw_key) == GLFW_RELEASE) {
if (state & KEY_STATE_IS_DOWN) state |= KEY_STATE_JUST_RELEASED; if (state & KEY_STATE_IS_DOWN) state |= KEY_STATE_JUST_RELEASED;
state &= ~KEY_STATE_IS_DOWN; state &= ~KEY_STATE_IS_DOWN;
} }
} }
internal internal
@ -85,75 +85,75 @@ void run_main_loop(GLFWwindow *window, Arena *arena, App_State &app)
app.delta_time_accum.base = arena_push_array<f32>(arena, app.delta_time_accum.max); app.delta_time_accum.base = arena_push_array<f32>(arena, app.delta_time_accum.max);
while (!app.should_quit) { while (!app.should_quit) {
chr::time_point frame_start = chr::high_resolution_clock::now(); 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(); 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; delta_time_ms = time_since_prev_frame_us * 0.001f;
last_saved_time = frame_start; last_saved_time = frame_start;
for (u32 i = 0; i < MOUSE_BTN_COUNT; ++i) 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); app.user_input.mouse_btn_state[i] &= ~(MOUSE_BTN_STATE_JUST_PRESSED|MOUSE_BTN_STATE_JUST_RELEASED);
glfwPollEvents(); glfwPollEvents();
// Update window size // Update window size
{ {
Window_Data &wdata = app.win_data; Window_Data &wdata = app.win_data;
i32 prev_width = wdata.width; i32 prev_width = wdata.width;
i32 prev_height = wdata.height; 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_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
// Check if the inspected file changed // Check if the inspected file changed
{ {
char buf[sizeof(inotify_event) + NAME_MAX + 1]; char buf[sizeof(inotify_event) + NAME_MAX + 1];
ssize_t nbytes = read(app.inspected_file.inot, buf, sizeof(buf)); ssize_t nbytes = read(app.inspected_file.inot, buf, sizeof(buf));
if (nbytes) if (nbytes)
app.inspected_file.size = file_size(app.inspected_file.stream); app.inspected_file.size = file_size(app.inspected_file.stream);
} }
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(window)) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(window)) {
app.should_quit = true; app.should_quit = true;
break; break;
} }
b32 focused = glfwGetWindowAttrib(window, GLFW_FOCUSED); b32 focused = glfwGetWindowAttrib(window, GLFW_FOCUSED);
if (focused) { if (focused) {
// Update keyboard // Update keyboard
u8 *key_state = app.user_input.key_state; 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_Q, KEY_Q);
monitor_key(window, key_state, GLFW_KEY_UP, KEY_UP); 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_DOWN, KEY_DOWN);
monitor_key(window, key_state, GLFW_KEY_LEFT, KEY_LEFT); 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_RIGHT, KEY_RIGHT);
monitor_key(window, key_state, GLFW_KEY_LEFT_ALT, KEY_ALT); 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_TAB, KEY_TAB);
monitor_key(window, key_state, GLFW_KEY_LEFT_SHIFT, KEY_SHIFT); monitor_key(window, key_state, GLFW_KEY_LEFT_SHIFT, KEY_SHIFT);
// Update mouse // Update mouse
f64 mposx, mposy; f64 mposx, mposy;
glfwGetCursorPos(window, &mposx, &mposy); glfwGetCursorPos(window, &mposx, &mposy);
u8 *mouse_btn_state = app.user_input.mouse_btn_state; 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_LEFT, MOUSE_BTN_LEFT);
monitor_mouse_btn(window, mouse_btn_state, GLFW_MOUSE_BUTTON_RIGHT, MOUSE_BTN_RIGHT); monitor_mouse_btn(window, mouse_btn_state, GLFW_MOUSE_BUTTON_RIGHT, MOUSE_BTN_RIGHT);
app.user_input.mouse_pos.x = static_cast<i32>(mposx); app.user_input.mouse_pos.x = static_cast<i32>(mposx);
app.user_input.mouse_pos.y = static_cast<i32>(mposy); app.user_input.mouse_pos.y = static_cast<i32>(mposy);
} }
update_and_render(arena, app, delta_time_ms); update_and_render(arena, app, delta_time_ms);
ImGui::Render(); ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
} }

View file

@ -1,14 +1,14 @@
struct Prof_Scope_Print { 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(const char *name) : scope_name { name }, scope_start_t { std::chrono::high_resolution_clock::now() } {}
~Prof_Scope_Print() { ~Prof_Scope_Print() {
using namespace std::chrono; using namespace std::chrono;
time_point scope_end_t = high_resolution_clock::now(); time_point scope_end_t = high_resolution_clock::now();
duration scope_duration = duration_cast<microseconds>(scope_end_t - scope_start_t); duration scope_duration = duration_cast<microseconds>(scope_end_t - scope_start_t);
fprintf(stderr, "[profile] %s took %.4f ms\n", scope_name, scope_duration.count() * 0.001f); fprintf(stderr, "[profile] %s took %.4f ms\n", scope_name, scope_duration.count() * 0.001f);
} }
const char *scope_name; const char *scope_name;
std::chrono::time_point<std::chrono::high_resolution_clock> scope_start_t; std::chrono::time_point<std::chrono::high_resolution_clock> scope_start_t;
}; };
#define TIMED_SCOPE() const Prof_Scope_Print CONCAT_STR(_prof, __LINE__) { __func__ } #define TIMED_SCOPE() const Prof_Scope_Print CONCAT_STR(_prof, __LINE__) { __func__ }

View file

@ -32,10 +32,10 @@ String8 str8_from_c(const char *str)
internal internal
String8 to_pretty_size(Arena *arena, u64 bytes) String8 to_pretty_size(Arena *arena, u64 bytes)
{ {
if (bytes >= GiB(1)) return push_str8f(arena, "%.1f GiB", (f32)bytes / GiB(1)); 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 >= 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)); if (bytes >= KiB(1)) return push_str8f(arena, "%.1f KiB", (f32)bytes / KiB(1));
return push_str8f(arena, "%zu B", bytes); return push_str8f(arena, "%zu B", bytes);
} }
internal internal

View file

@ -1,26 +1,26 @@
enum Input_Key { enum Input_Key {
KEY_UP, KEY_UP,
KEY_DOWN, KEY_DOWN,
KEY_LEFT, KEY_LEFT,
KEY_RIGHT, KEY_RIGHT,
KEY_Q, KEY_Q,
KEY_ALT, KEY_ALT,
KEY_TAB, KEY_TAB,
KEY_SHIFT, KEY_SHIFT,
KEY_COUNT KEY_COUNT
}; };
enum Key_State : u8 { enum Key_State : u8 {
KEY_STATE_IS_DOWN = 0x1, KEY_STATE_IS_DOWN = 0x1,
KEY_STATE_JUST_PRESSED = 0x2, KEY_STATE_JUST_PRESSED = 0x2,
KEY_STATE_JUST_RELEASED = 0x4, KEY_STATE_JUST_RELEASED = 0x4,
}; };
enum Mouse_Button { enum Mouse_Button {
MOUSE_BTN_LEFT, MOUSE_BTN_LEFT,
MOUSE_BTN_RIGHT, MOUSE_BTN_RIGHT,
MOUSE_BTN_COUNT MOUSE_BTN_COUNT
}; };
enum Mouse_Button_State : u8 { enum Mouse_Button_State : u8 {
@ -30,18 +30,18 @@ enum Mouse_Button_State : u8 {
}; };
struct User_Input { struct User_Input {
u8 key_state[KEY_COUNT]; u8 key_state[KEY_COUNT];
u8 mouse_btn_state[MOUSE_BTN_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 { struct Window_Data {
// Real width and height of the window // Real width and height of the window
i32 width; i32 width;
i32 height; i32 height;
bool size_just_changed; bool size_just_changed;
f32 desired_aspect_ratio; f32 desired_aspect_ratio;
}; };