rntviewer/src/rntviewer.cpp
2024-07-23 09:48:43 +02:00

136 lines
3 KiB
C++

// ************************************
// *
// * rntviewer
// *
// * A graphical RNTuple visualizer
// *
// * @author silverweed, 2024
// *
// ***********************************
// #include <ROOT/RNTupleReader.hxx>
// #include <ROOT/RNTuple.hxx>
// #include <TFile.h>
#include <cstdio>
#include <cstdint>
#include <byteswap.h>
#include <chrono>
#ifdef DEBUG
#include <sanitizer/asan_interface.h>
#endif
#include <imgui/imgui.h>
#include <imgui/backends/imgui_impl_glfw.h>
#include <imgui/backends/imgui_impl_opengl3.h>
#include <imgui_club/imgui_memory_editor.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include "root/root_inc.h"
#include "root/RMicroFileReader.hxx"
#include "types.h"
#include "defer.hpp"
#include "prof.hpp"
#include "mem.h"
#include "str.h"
#include "rntuple.h"
#include "window.h"
#include "render.h"
#include "app_state.h"
// @Platform
#include "platform_linux.h"
namespace chr = std::chrono;
#include "mem.cpp"
#include "str.cpp"
#include "rntuple.cpp"
#include "render.cpp"
#include "mainloop.cpp"
internal
b8 init_imgui(GLFWwindow* window) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void) io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 400");
return true;
}
internal
void app_cleanup(App_State &app)
{
os_stop_file_watch(app);
os_unmap_file(app.inspected_file.mem, app.inspected_file.size);
if (app.inspected_file.stream) fclose(app.inspected_file.stream);
}
int main(int argc, char **argv)
{
Thread_Ctx tctx;
tctx_init(tctx);
defer { tctx_release(); };
if ((argc > 1 && argv[1][0] == '-') || argc < 3) {
fprintf(stderr, "Usage: %s <ntuple_name> <ntuple_file.root>\n", argv[0]);
return 1;
}
// Collect arguments
const char *ntpl_name = argc > 1 ? argv[1] : nullptr;
const char *fname = argc > 2 ? argv[2] : nullptr;
// Allocate program memory
Arena *arena = arena_alloc();
if (!arena) {
fprintf(stderr, "Failed to allocate memory\n");
return 1;
}
defer { arena_release(arena); };
// Init imgui and GLFW
GLFWwindow *window = init_glfw(800, 600);
if (!window) {
fprintf(stderr, "Failed to init GLFW\n");
return 1;
}
defer { glfwTerminate(); };
if (!init_imgui(window)) {
fprintf(stderr, "Failed to init Imgui\n");
return 1;
}
App_State app {};
defer { app_cleanup(app); };
// Open and map the file
if (fname) {
if (!os_open_and_map_file(fname, app))
return 1;
// Watch file for changes (to adapt the displayed file size - otherwise
// we may try to access invalid memory when the file gets shrunk)
os_start_file_watch(fname, app);
}
app.ntpl_name = str8(ntpl_name);
app.tfile_data = get_tfile_data(app.inspected_file);
app.rndata = get_rntuple_data(arena, app.inspected_file, app.ntpl_name);
make_viewer(app);
// Start main loop
run_main_loop(window, arena, app);
return 0;
}