From b5ca50d3d84738c2f4094602a962ebe6bd6a9496 Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Fri, 18 Jul 2025 13:59:04 +0200 Subject: [PATCH] add error gui --- .gitmodules | 3 ++ CMakeLists.txt | 9 +++-- include/utils/gui.hpp | 14 +++++++ src/main.cpp | 9 +++-- src/utils/gui.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++ thirdparty/raylib | 1 + 6 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 include/utils/gui.hpp create mode 100644 src/utils/gui.cpp create mode 160000 thirdparty/raylib diff --git a/.gitmodules b/.gitmodules index d5ff308..331e7c8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "thirdparty/toml11"] path = thirdparty/toml11 url = https://github.com/ToruNiina/toml11 +[submodule "thirdparty/raylib"] + path = thirdparty/raylib + url = https://github.com/raysan5/raylib diff --git a/CMakeLists.txt b/CMakeLists.txt index c5b7e6e..42c59c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,14 +12,16 @@ endif() # subprojects add_compile_options(-fPIC -Wno-deprecated-declarations - -Wno-unused-template) + -Wno-unused-template + -Wno-tautological-compare + -Wno-undef) add_subdirectory(thirdparty/dxbc EXCLUDE_FROM_ALL) add_subdirectory(thirdparty/pe-parse/pe-parser-library EXCLUDE_FROM_ALL) add_subdirectory(thirdparty/toml11 EXCLUDE_FROM_ALL) +add_subdirectory(thirdparty/raylib EXCLUDE_FROM_ALL) add_subdirectory(framegen) - # main project project(lsfg-vk VERSION 0.0.1 @@ -43,7 +45,8 @@ set_target_properties(lsfg-vk PROPERTIES target_include_directories(lsfg-vk PRIVATE include) target_link_libraries(lsfg-vk PRIVATE - lsfg-vk-framegen pe-parse dxbc toml11 vulkan) + pe-parse dxbc toml11 raylib + lsfg-vk-framegen vulkan) get_target_property(TOML11_INCLUDE_DIRS toml11 INTERFACE_INCLUDE_DIRECTORIES) target_include_directories(lsfg-vk SYSTEM PRIVATE ${TOML11_INCLUDE_DIRS}) diff --git a/include/utils/gui.hpp b/include/utils/gui.hpp new file mode 100644 index 0000000..37295f3 --- /dev/null +++ b/include/utils/gui.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace Utils { + + /// + /// Display error gui and exit + /// + /// @param message The error message to display in the GUI. + /// + [[noreturn]] void showErrorGui(const std::string& message); + +} diff --git a/src/main.cpp b/src/main.cpp index 6d96f95..bb2a0ad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include "config/config.hpp" #include "extract/extract.hpp" #include "utils/benchmark.hpp" +#include "utils/gui.hpp" #include "utils/utils.hpp" #include @@ -20,7 +21,7 @@ namespace { } catch (const std::exception& e) { std::cerr << "lsfg-vk: An error occured while trying to parse the configuration, exiting:\n"; std::cerr << "- " << e.what() << '\n'; - exit(0); + Utils::showErrorGui(e.what()); } const std::string name = Utils::getProcessName(); @@ -29,7 +30,7 @@ namespace { } catch (const std::exception& e) { std::cerr << "lsfg-vk: The configuration for " << name << " is invalid, exiting:\n"; std::cerr << e.what() << '\n'; - exit(0); + Utils::showErrorGui(e.what()); } // exit silently if not enabled @@ -62,7 +63,7 @@ namespace { } catch (const std::exception& e) { std::cerr << "lsfg-vk: An error occurred while trying to extract the shaders, exiting:\n"; std::cerr << "- " << e.what() << '\n'; - exit(0); + Utils::showErrorGui(e.what()); } std::cerr << "lsfg-vk: Shaders extracted successfully.\n"; @@ -94,6 +95,7 @@ namespace { } catch (const std::exception& e) { std::cerr << "lsfg-vk: An error occurred while trying to parse the resolution, exiting:\n"; std::cerr << "- " << e.what() << '\n'; + exit(EXIT_FAILURE); } std::thread benchmark([width, height]() { @@ -102,6 +104,7 @@ namespace { } catch (const std::exception& e) { std::cerr << "lsfg-vk: An error occurred during the benchmark:\n"; std::cerr << "- " << e.what() << '\n'; + exit(EXIT_FAILURE); } }); benchmark.detach(); diff --git a/src/utils/gui.cpp b/src/utils/gui.cpp new file mode 100644 index 0000000..0f0dac5 --- /dev/null +++ b/src/utils/gui.cpp @@ -0,0 +1,86 @@ +#include "utils/gui.hpp" + +#include + +#include +#include +#include + +using namespace Utils; + +namespace { + + void DrawCenteredText(const std::string& message, int y, Color color) { + const int textWidth = MeasureText(message.c_str(), 20); + DrawText(message.c_str(), (GetScreenWidth() - textWidth) / 2, y, 20, color); + } + + int DrawTextBox(std::string message, int x, int y, int width, bool draw) { + while (!message.empty()) { + std::string current = message; + + // take one line at a time + const size_t pos = current.find('\n'); + if (pos != std::string::npos) + current = current.substr(0, pos); + + // if too long, remove word or character + while (MeasureText(message.c_str(), 20) > width) { + const size_t pos = current.find_last_of(' '); + if (pos == std::string::npos) + current = current.substr(0, current.size() - 1); + else + current = current.substr(0, pos); + } + + // remove the current line from the message text + message = message.substr(current.size()); + while (message.starts_with(' ')) + message = message.substr(1); // remove leading space + while (message.starts_with('\n')) + message = message.substr(1); // remove leading newline + + // draw the current line + if (draw) DrawText(current.c_str(), x, y, 20, Color { 198, 173, 173, 255 }); + y += 25; // move down for the next line + } + return y; // return height of the text box + } +} + +void Utils::showErrorGui(const std::string& message) { + const int height = DrawTextBox(message, 10, 60, 780, false); + InitWindow(800, height + 80, "lsfg-vk - Error"); + + SetTargetFPS(24); // cinema frame rate lol + + while (!WindowShouldClose()) { + BeginDrawing(); + + ClearBackground(Color{ 58, 23, 32, 255 }); + DrawCenteredText("lsfg-vk encountered an error", 10, Color{ 225, 115, 115, 255 }); + DrawLine(10, 35, 790, 35, Color{ 218, 87, 87, 255 }); + DrawTextBox(message, 10, 60, 780, true); + + const bool hover = GetMouseY() > height + 30 + && GetMouseY() < height + 70 + && GetMouseX() > 10 + && GetMouseX() < 790; + if (hover) { + DrawRectangleLines(10, height + 30, 780, 40, Color{ 250, 170, 151, 255 }); + DrawCenteredText("Press Escape or click here to close", height + 40, Color{ 253, 180, 170, 255 }); + } else { + DrawRectangleLines(10, height + 30, 780, 40, Color{ 218, 87, 87, 255 }); + DrawCenteredText("Press Escape or click here to close", height + 40, Color{ 225, 115, 115, 255 }); + } + + EndDrawing(); + + if (hover && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) + break; + } + + CloseWindow(); + + exit(EXIT_FAILURE); +} diff --git a/thirdparty/raylib b/thirdparty/raylib new file mode 160000 index 0000000..78a0699 --- /dev/null +++ b/thirdparty/raylib @@ -0,0 +1 @@ +Subproject commit 78a06990c7c55ace00a8e6c5afe983457304e645