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 -14
View File
@@ -7,18 +7,39 @@
struct Image {
SDL_Texture *texture;
Vec2<float> size;
~Image() {
SDL_DestroyTexture(texture);
}
};
struct Font {
TTF_Font *font;
bool pixel_art;
~Font() {
TTF_CloseFont(font);
}
};
struct Text {
SDL_Texture *texture;
Vec2<float> size;
~Text() {
SDL_DestroyTexture(texture);
}
};
// Image pool
inline Pool<Image> image_pool;
// Font pool
inline Pool<Font> font_pool;
// Text pool
inline Pool<Text> text_pool;
struct Event {
enum Type {
NONE,
@@ -63,6 +84,11 @@ struct Animation {
float frame_rate;
bool loop;
int reference_count = 0;
~Animation() {
for (int frame_id : frames)
image_pool.release(frame_id);
}
};
inline Pool<Animation> animation_pool;
@@ -78,17 +104,12 @@ public:
// Rendering functions
int load_image(const char *path, float alpha = 1.0f, bool pixel_art = false);
void use_image(int image_id);
void unload_image(int image_id);
Vec2<float> get_image_size(int image_id);
int load_font(const char *path, int font_size, bool bold = false, bool italic = false, bool underline = false, bool pixel_art = false);
void use_font(int font_id);
void unload_font(int font_id);
// text is copied internally, so it can be free'd by the caller after the text is created.
int create_text(int font_id, const char *text, uint32_t length, Color color);
void unload_text(int text_id);
Vec2<float> get_text_size(int text_id);
bool write(int text_id, Vec2<float> position, float z, float angle = 0.0f, Vec2<float> scale = {1.0f, 1.0f});
@@ -138,15 +159,6 @@ private:
// White pixel texture for drawing colored rectangles
SDL_Texture *white_tex;
// Image pool
Pool<Image> image_pool;
// Font pool
Pool<Font> font_pool;
// Text pool
Pool<Text> text_pool;
struct RenderInstruction {
enum Type {
DRAW_IMAGE,