gui error handler

This commit is contained in:
Timerix22 2024-05-06 17:02:50 +05:00
parent da1dfd4c13
commit 461b92ccc3
4 changed files with 90 additions and 15 deletions

View File

@ -6,7 +6,7 @@ UsefulException_::UsefulException_(const std::string& _message, const std::strin
{ {
std::stringstream ss; std::stringstream ss;
ss<<message<<'\n'; ss<<message<<'\n';
ss<<file<<':'<<_line_n<<" in "<<function; ss<<" at "<<file<<':'<<_line_n<<" in "<<function;
complete_text = ss.str(); complete_text = ss.str();
} }

View File

@ -43,7 +43,11 @@ void GUI::init(const char* window_title){
sdl_window = SDL_CreateWindow(window_title, sdl_window = SDL_CreateWindow(window_title,
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1280, 720, window_flags); 1280, 720, window_flags);
if(sdl_window == nullptr)
throw SDLException();
gl_context = SDL_GL_CreateContext(sdl_window); gl_context = SDL_GL_CreateContext(sdl_window);
if(gl_context == nullptr)
throw SDLException();
SDL_TRY_ZERO( SDL_GL_MakeCurrent(sdl_window, gl_context)); SDL_TRY_ZERO( SDL_GL_MakeCurrent(sdl_window, gl_context));
SDL_TRY_ZERO( SDL_GL_SetSwapInterval(1)); // Enable vsync SDL_TRY_ZERO( SDL_GL_SetSwapInterval(1)); // Enable vsync
@ -108,20 +112,58 @@ void GUI::poll_events(u16& frame_updates_requested, bool wait){
} while (SDL_PollEvent(&event)); // if there are more events, handles them } while (SDL_PollEvent(&event)); // if there are more events, handles them
} }
void ImGui_drawErrorWindow(bool* draw_error_window, std::string& msg){
ImGui::Begin("ERROR", draw_error_window);
ImGui::Text("%s", msg.c_str());
ImGui::End();
}
void GUI::draw_ui(){
static std::string error_message;
static bool draw_error_window = false;
try {
// Draw UI
if(draw_error_window)
ImGui_drawErrorWindow(&draw_error_window, error_message);
else {
draw_bg_window();
draw_debug_window();
node_editor->draw();
}
}
catch(const std::exception& e){
error_message = "Catched exception: " + std::string(e.what());
draw_error_window = true;
std::cerr<<error_message<<std::endl;
}
catch(const char* cstr){
error_message = "Catched error message (const char*): " + std::string(cstr);
draw_error_window = true;
std::cerr<<error_message<<std::endl;
}
catch(const std::string& str){
error_message = "Catched error message (std::string): " + str;
draw_error_window = true;
std::cerr<<error_message<<std::endl;
}
catch(...){
error_message = "Catched unknown error";
draw_error_window = true;
std::cerr<<error_message<<std::endl;
}
}
void GUI::draw_frame(){ void GUI::draw_frame(){
// Start the Dear ImGui frame // Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(); ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
ImGuiIO& io = ImGui::GetIO();
// Draw UI draw_ui();
draw_bg_window();
draw_debug_window(io);
node_editor->draw();
// Rendering // Rendering
ImGui::Render(); ImGui::Render();
ImGuiIO& io = ImGui::GetIO();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@ -230,12 +272,43 @@ void GUI::draw_bg_window(){
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
// MenuBar // MenuBar
//TODO 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(); ImGui::End();
} }
void GUI::draw_debug_window(ImGuiIO& io){ void GUI::draw_debug_window(){
ImGuiIO& io = ImGui::GetIO();
ImGui::Begin("Debug Options"); ImGui::Begin("Debug Options");
ImGui::ColorEdit3("clear_color", (float*)&clear_color); ImGui::ColorEdit3("clear_color", (float*)&clear_color);
ImGui::Checkbox("main_loop_wait_for_input", &main_loop_wait_for_input); ImGui::Checkbox("main_loop_wait_for_input", &main_loop_wait_for_input);

View File

@ -48,7 +48,8 @@ private:
void destroy(); void destroy();
void poll_events(u16& frame_updates_requested, bool wait); void poll_events(u16& frame_updates_requested, bool wait);
void draw_frame(); void draw_frame();
void draw_debug_window(ImGuiIO &io); void draw_ui();
void draw_debug_window();
void draw_bg_window(); void draw_bg_window();
}; };

View File

@ -1,5 +1,6 @@
#include "gui/gui.hpp"
#include <iostream> #include <iostream>
#include "gui/gui.hpp"
// SDL for some reason redefines main // SDL for some reason redefines main
#ifdef main #ifdef main
@ -23,16 +24,16 @@ int main(const int argc, const char* const* argv){
std::cerr<<"Catched exception: "<<e.what()<<std::endl; std::cerr<<"Catched exception: "<<e.what()<<std::endl;
return -1; return -1;
} }
catch(const char* msg){ catch(const char* cstr){
std::cerr<<"Catched exception message: "<<msg<<std::endl; std::cerr<<"Catched error message (const char*): "<<cstr<<std::endl;
return -1; return -1;
} }
catch(const std::string& msg){ catch(const std::string& str){
std::cerr<<"Catched exception message: "<<msg<<std::endl; std::cerr<<"Catched error message (std::string): "<<str<<std::endl;
return -1; return -1;
} }
catch(...){ catch(...){
std::cerr<<"Catched unknown exception"<<std::endl; std::cerr<<"Catched unknown error"<<std::endl;
return -1; return -1;
} }