#include #include #include #include "MainWindow.hpp" #include "exceptions.hpp" #include "../format.hpp" #include "../Resources/fonts.hpp" #include "../Resources/textures.hpp" #include "../math.hpp" namespace ougge::GUI { f32 MainWindow::getDPI(){ int w=0, h=0; SDL_GetRendererOutputSize(sdl_renderer, &w, &h); int sim_w=0, sim_h=0; SDL_GetWindowSize(sdl_window, &sim_w, &sim_h); f32 wdpi=(f32)w / sim_w; f32 hdpi=(f32)h / sim_h; f32 dpi=SDL_sqrtf(wdpi*wdpi + hdpi*hdpi); return dpi; } void MainWindow::open(const char* window_title, UpdateFunc_t _update){ update = _update; SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING)); SDL_version v; SDL_GetVersion(&v); std::cout< textures; Resources::Texture& tutel = textures.getOrCreate("tutel.png", sdl_renderer); Resources::SDL_RenderCopyExF_Params rp; rp.target_section = SDL_FRectConstruct(100, 100, 400, 400); static int si = 1; rp.rotation_angle = M_PI_4 * (si++ / fps_max); tutel.render(rp); ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer); // Swap buffers SDL_RenderPresent(sdl_renderer); } void MainWindow::startUpdateLoop(){ if(loop_running) throw UsefulException("loop is running already"); nsec_t prev_update_time_ns = getMonotonicTimeNsec(); loop_running=true; // main loop while(loop_running){ poll_events(false); nsec_t update_time_ns = getMonotonicTimeNsec(); if(update_time_ns < prev_update_time_ns) throw UsefulException("monotonic clock returned unexpected value"); f64 delta_time_s = (f64)(update_time_ns - prev_update_time_ns) / 1e9; prev_update_time_ns = update_time_ns; update(delta_time_s); draw_frame(); nsec_t after_update_time_ns = getMonotonicTimeNsec(); nsec_t frame_delay_ns = (nsec_t)1e9 / fps_max - (after_update_time_ns - update_time_ns); if(frame_delay_ns > 0){ SDL_Delay(frame_delay_ns / 1e6); } } destroy(); } void MainWindow::destroy(){ ImGui_ImplSDLRenderer2_Shutdown(); ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(); SDL_DestroyRenderer(sdl_renderer); SDL_DestroyWindow(sdl_window); SDL_Quit(); } void MainWindow::close(){ loop_running = false; } void MainWindow::draw_bg_window(){ const ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode; const ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground; // not dockable window that always bound to viewport ImGuiViewport* viewport = ImGui::GetWindowViewport(); ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always); ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); ImGui::Begin("bg_window", nullptr, window_flags); ImGui::PopStyleVar(3); // DockSpace ImGuiID dockspace_id = ImGui::GetID("bg_dockspace"); ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); // MenuBar if(ImGui::BeginMainMenuBar()){ if(ImGui::BeginMenu("test")){ if(ImGui::MenuItem("throw exception")){ ImGui::EndMenu(); ImGui::EndMainMenuBar(); ImGui::End(); throw UsefulException("example exception"); } if(ImGui::MenuItem("throw const char*")){ ImGui::EndMenu(); ImGui::EndMainMenuBar(); ImGui::End(); throw "cptr"; } if(ImGui::MenuItem("throw std::string")){ ImGui::EndMenu(); ImGui::EndMainMenuBar(); ImGui::End(); throw std::string("str"); } if(ImGui::MenuItem("throw unknown")){ ImGui::EndMenu(); ImGui::EndMainMenuBar(); ImGui::End(); throw 111; } ImGui::EndMenu(); } ImGui::EndMainMenuBar(); } ImGui::End(); } void MainWindow::draw_debug_window(){ ImGuiIO& io = ImGui::GetIO(); ImGui::Begin("Debug Options"); ImGui::ColorEdit3("clear_color", (float*)&clear_color); ImGui::InputInt("fps_max", &fps_max); ImGui::Text("Application average %.3f ms/frame (%.2f FPS)", 1000.0f / io.Framerate, io.Framerate); ImGui::Checkbox("Demo Window", &show_demo_window); ImGui::Checkbox("Metrics/Debug Window", &show_metrics_window); ImGui::End(); if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); if (show_metrics_window) ImGui::ShowMetricsWindow(&show_metrics_window); } }