Fix textures/font lifetimes

This commit is contained in:
2026-05-05 21:29:44 +01:00
parent 0da950caf9
commit 306a053276
4 changed files with 61 additions and 67 deletions
+26 -7
View File
@@ -26,17 +26,26 @@ struct Element {
};
struct EImage : Element {
Animation animation;
int animation_id = -1;
EImage(Animation animation) : animation(std::move(animation)) {}
EImage(int animation_id) : animation_id(animation_id) {
animation_pool.use(animation_id);
}
~EImage() {
if (animation_id != -1)
animation_pool.release(animation_id);
}
Vec2<float> size(Window *window) override {
Animation &animation = *animation_pool[animation_id];
if (animation.frames.empty())
return {0, 0};
return window->get_image_size(animation.frames[0]);
}
void render(Window &window, uint64_t abs_time) override {
Animation &animation = *animation_pool[animation_id];
if (animation.frames.empty())
return;
@@ -47,11 +56,21 @@ struct EImage : Element {
struct EText : Element {
Localization::Key key;
int font_id;
int font_id = -1;
~EText() {
if (text_id != -1)
text_pool.release(text_id);
if (font_id != -1)
font_pool.release(font_id);
}
Color color = {255, 255, 255, 255};
EText(Localization::Key key, int font_id, Color color)
: key(key), font_id(font_id), color(color) {}
: key(key), font_id(font_id), color(color) {
font_pool.use(font_id);
}
Vec2<float> size(Window *window) override {
return window->get_text_size(text_id);
@@ -61,7 +80,7 @@ struct EText : Element {
if (last_language != Localization::current_language || last_key != key || text_id == -1) {
std::string localized_str = Localization::get(key);
if (text_id != -1)
window.unload_text(text_id);
text_pool.release(text_id);
text_id = window.create_text(font_id, localized_str.c_str(), localized_str.size(), color);
last_language = Localization::current_language;
last_key = key;
@@ -163,11 +182,11 @@ struct App {
current_scene_id = scene_id;
}
App(Vec2<float> size, const char *title, Animation loading_animation) {
App(Vec2<float> size, const char *title, int anim_loading) {
window.update(size, title);
#warning "Should try to center the loading image or scale it to fit the screen"
Scene *loading_scene = new Scene{};
EImage *image = new EImage(loading_animation);
EImage *image = new EImage(anim_loading);
int image_id = element_pool.acquire(image);
loading_scene->add_element(image_id);
current_scene_id = scene_pool.acquire(loading_scene);