fix: opengl resource delete error

This commit is contained in:
2026-07-17 15:18:06 +08:00
parent 199735609f
commit 7244534ac4
5 changed files with 27 additions and 6 deletions

View File

@@ -5,6 +5,7 @@
#include "Cubed/ui/widget.hpp" #include "Cubed/ui/widget.hpp"
#include <format> #include <format>
#include <memory>
#include <unordered_map> #include <unordered_map>
namespace Cubed { namespace Cubed {
@@ -12,6 +13,7 @@ namespace Cubed {
class DebugCollector { class DebugCollector {
public: public:
static DebugCollector& get(); static DebugCollector& get();
static void distory();
DebugCollector(); DebugCollector();
void report(const std::string& name, std::string_view content); void report(const std::string& name, std::string_view content);
@@ -22,6 +24,7 @@ public:
private: private:
ColumnLayout m_widget; ColumnLayout m_widget;
std::unordered_map<std::string, Label*> m_component; std::unordered_map<std::string, Label*> m_component;
static std::unique_ptr<DebugCollector>& get_ptr();
}; };
template <typename... Args> template <typename... Args>

View File

@@ -51,6 +51,8 @@ public:
Font(); Font();
~Font(); ~Font();
static Font& get(); static Font& get();
static void destroy();
TextMesh vertices(const std::string& text); TextMesh vertices(const std::string& text);
const Texture* text_texture(); const Texture* text_texture();
static const std::string& font_path(); static const std::string& font_path();
@@ -70,6 +72,7 @@ private:
int m_max_layers = 0; int m_max_layers = 0;
Glyph& load_glyph(uint32_t glyph_index); Glyph& load_glyph(uint32_t glyph_index);
void upload_glyph(Glyph& glyph, const unsigned char* buffer); void upload_glyph(Glyph& glyph, const unsigned char* buffer);
static std::unique_ptr<Font>& get_ptr();
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -6,6 +6,7 @@
#include "Cubed/localization.hpp" #include "Cubed/localization.hpp"
#include "Cubed/tools/arg_parser.hpp" #include "Cubed/tools/arg_parser.hpp"
#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/font.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
#include "Cubed/tools/system_info.hpp" #include "Cubed/tools/system_info.hpp"
#include "Cubed/tools/system_locate.hpp" #include "Cubed/tools/system_locate.hpp"
@@ -13,6 +14,7 @@
#include <exception> #include <exception>
#include <imgui_impl_sdl3.h> #include <imgui_impl_sdl3.h>
namespace Cubed { namespace Cubed {
App::App() App::App()
@@ -21,7 +23,11 @@ App::App()
m_texture_manager(m_game_config), m_audio(m_game_config), m_texture_manager(m_game_config), m_audio(m_game_config),
m_renderer(m_texture_manager, m_game_config), m_scene_manager(*this) {} m_renderer(m_texture_manager, m_game_config), m_scene_manager(*this) {}
App::~App() { stop_text_input(); } App::~App() {
stop_text_input();
Font::destroy();
DebugCollector::distory();
}
void App::init(int argc, char** argv) { void App::init(int argc, char** argv) {
handle_argument(argc, argv); handle_argument(argc, argv);

View File

@@ -4,16 +4,19 @@
#include "version.hpp" #include "version.hpp"
#include <SDL3/SDL_video.h> #include <SDL3/SDL_video.h>
#include <memory>
namespace Cubed { namespace Cubed {
DebugCollector::DebugCollector() : m_widget(nullptr) {} DebugCollector::DebugCollector() : m_widget(nullptr) {}
DebugCollector& DebugCollector::get() { DebugCollector& DebugCollector::get() { return *get_ptr(); }
static DebugCollector instance; std::unique_ptr<DebugCollector>& DebugCollector::get_ptr() {
static std::unique_ptr<DebugCollector> instance =
std::make_unique<DebugCollector>();
return instance; return instance;
} }
void DebugCollector::distory() { get_ptr().reset(); }
void DebugCollector::init(int width, int height) { void DebugCollector::init(int width, int height) {
constexpr float SCALE = 0.6f; constexpr float SCALE = 0.6f;

View File

@@ -2,6 +2,9 @@
#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
#include <memory>
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace Cubed { namespace Cubed {
@@ -72,10 +75,13 @@ void Font::upload_glyph(Glyph& glyph, const unsigned char* buffer) {
glyph.size.y / float(CELL_SIZE)}; glyph.size.y / float(CELL_SIZE)};
} }
Font& Font::get() { Font& Font::get() { return *get_ptr(); }
static Font font; std::unique_ptr<Font>& Font::get_ptr() {
static std::unique_ptr<Font> font = std::make_unique<Font>();
return font; return font;
} }
void Font::destroy() { get_ptr().reset(); }
TextMesh Font::vertices(const std::string& text) { TextMesh Font::vertices(const std::string& text) {
hb_buffer_t* buffer = hb_buffer_create(); hb_buffer_t* buffer = hb_buffer_create();