add error gui

This commit is contained in:
PancakeTAS 2025-07-18 13:59:04 +02:00 committed by Pancake
parent a457ef5aff
commit b5ca50d3d8
6 changed files with 116 additions and 6 deletions

3
.gitmodules vendored
View file

@ -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

View file

@ -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})

14
include/utils/gui.hpp Normal file
View file

@ -0,0 +1,14 @@
#pragma once
#include <string>
namespace Utils {
///
/// Display error gui and exit
///
/// @param message The error message to display in the GUI.
///
[[noreturn]] void showErrorGui(const std::string& message);
}

View file

@ -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 <exception>
@ -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();

86
src/utils/gui.cpp Normal file
View file

@ -0,0 +1,86 @@
#include "utils/gui.hpp"
#include <raylib.h>
#include <cstdlib>
#include <cstddef>
#include <string>
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);
}

1
thirdparty/raylib vendored Submodule

@ -0,0 +1 @@
Subproject commit 78a06990c7c55ace00a8e6c5afe983457304e645