Fix major incorrect usage of mrb class definitions causing segfaults
This commit is contained in:
+45
-28
@@ -42,16 +42,13 @@ void Window::update(Vec2<float> new_size, const char *new_title) {
|
||||
|
||||
Window::~Window() {
|
||||
for (auto &tex : image_pool.all())
|
||||
if (tex.texture)
|
||||
SDL_DestroyTexture(tex.texture);
|
||||
SDL_DestroyTexture(tex->texture);
|
||||
|
||||
for (auto tex : text_pool.all())
|
||||
if (tex)
|
||||
SDL_DestroyTexture(tex);
|
||||
SDL_DestroyTexture(tex->texture);
|
||||
|
||||
for (auto &f : font_pool.all())
|
||||
if (f.font)
|
||||
TTF_CloseFont(f.font);
|
||||
TTF_CloseFont(f->font);
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
@@ -76,7 +73,15 @@ int Window::load_image(const char *path, float alpha, bool pixel_art) {
|
||||
float w, h;
|
||||
SDL_GetTextureSize(texture, &w, &h);
|
||||
|
||||
return image_pool.acquire({texture, {w, h}});
|
||||
Image *img = new Image{texture, {w, h}};
|
||||
int id = image_pool.acquire(img);
|
||||
image_pool.use(id);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void Window::use_image(int image_id) {
|
||||
image_pool.use(image_id);
|
||||
}
|
||||
|
||||
Vec2<float> Window::get_image_size(int image_id) {
|
||||
@@ -84,7 +89,7 @@ Vec2<float> Window::get_image_size(int image_id) {
|
||||
fprintf(stderr, "Invalid image ID: %d\n", image_id);
|
||||
return {0, 0};
|
||||
}
|
||||
return {image_pool[image_id].size.x, image_pool[image_id].size.y};
|
||||
return image_pool[image_id]->size;
|
||||
}
|
||||
|
||||
void Window::unload_image(int image_id) {
|
||||
@@ -93,7 +98,7 @@ void Window::unload_image(int image_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_DestroyTexture(image_pool[image_id].texture);
|
||||
SDL_DestroyTexture(image_pool[image_id]->texture);
|
||||
image_pool.release(image_id);
|
||||
}
|
||||
|
||||
@@ -115,7 +120,15 @@ int Window::load_font(const char *path, int font_size, bool bold, bool italic, b
|
||||
|
||||
TTF_SetFontStyle(font, style);
|
||||
|
||||
return font_pool.acquire({font, pixel_art});
|
||||
Font *f = new Font{font, pixel_art};
|
||||
int id = font_pool.acquire(f);
|
||||
font_pool.use(id);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void Window::use_font(int font_id) {
|
||||
font_pool.use(font_id);
|
||||
}
|
||||
|
||||
void Window::unload_font(int font_id) {
|
||||
@@ -124,7 +137,7 @@ void Window::unload_font(int font_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
TTF_CloseFont(font_pool[font_id].font);
|
||||
TTF_CloseFont(font_pool[font_id]->font);
|
||||
font_pool.release(font_id);
|
||||
}
|
||||
|
||||
@@ -135,7 +148,7 @@ int Window::create_text(int font_id, const char *text, uint32_t length, Color co
|
||||
}
|
||||
|
||||
SDL_Color sdl_color = {color.r, color.g, color.b, color.a};
|
||||
SDL_Surface *surface = TTF_RenderText_Solid(font_pool[font_id].font, text, length, sdl_color);
|
||||
SDL_Surface *surface = TTF_RenderText_Solid(font_pool[font_id]->font, text, length, sdl_color);
|
||||
|
||||
if (surface == nullptr) {
|
||||
fprintf(stderr, "Failed to create text surface: %s\n", SDL_GetError());
|
||||
@@ -150,10 +163,17 @@ int Window::create_text(int font_id, const char *text, uint32_t length, Color co
|
||||
return UINT32_MAX;
|
||||
}
|
||||
|
||||
if (font_pool[font_id].pixel_art)
|
||||
if (font_pool[font_id]->pixel_art)
|
||||
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
|
||||
|
||||
return text_pool.acquire(texture);
|
||||
float w, h;
|
||||
SDL_GetTextureSize(texture, &w, &h);
|
||||
|
||||
Text *t = new Text{texture, {w, h}};
|
||||
int id = text_pool.acquire(t);
|
||||
text_pool.use(id);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
Vec2<float> Window::get_text_size(int text_id) {
|
||||
@@ -161,9 +181,7 @@ Vec2<float> Window::get_text_size(int text_id) {
|
||||
fprintf(stderr, "Invalid text ID: %d\n", text_id);
|
||||
return {0, 0};
|
||||
}
|
||||
float w, h;
|
||||
SDL_GetTextureSize(text_pool[text_id], &w, &h);
|
||||
return {w, h};
|
||||
return text_pool[text_id]->size;
|
||||
}
|
||||
|
||||
void Window::unload_text(int text_id) {
|
||||
@@ -172,7 +190,7 @@ void Window::unload_text(int text_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_DestroyTexture(text_pool[text_id]);
|
||||
SDL_DestroyTexture(text_pool[text_id]->texture);
|
||||
text_pool.release(text_id);
|
||||
}
|
||||
|
||||
@@ -275,7 +293,8 @@ void Window::render() {
|
||||
for (const RenderInstruction &instruction : render_instructions) {
|
||||
switch (instruction.type) {
|
||||
case RenderInstruction::DRAW_TEXT: {
|
||||
SDL_Texture *texture = text_pool[instruction.draw_text.text_id];
|
||||
Text *text = text_pool[instruction.draw_text.text_id];
|
||||
SDL_Texture *texture = text->texture;
|
||||
|
||||
SDL_FlipMode flip = SDL_FLIP_NONE;
|
||||
|
||||
@@ -287,14 +306,12 @@ void Window::render() {
|
||||
float scale_x = std::abs(instruction.scale.x) * scale;
|
||||
float scale_y = std::abs(instruction.scale.y) * scale;
|
||||
|
||||
float w, h;
|
||||
SDL_GetTextureSize(texture, &w, &h);
|
||||
|
||||
SDL_FRect dst_rect;
|
||||
dst_rect.x = instruction.draw_text.position.x * scale + offset.x;
|
||||
dst_rect.y = instruction.draw_text.position.y * scale + offset.y;
|
||||
dst_rect.w = scale_x * w;
|
||||
dst_rect.h = scale_y * h;
|
||||
|
||||
dst_rect.w = scale_x * text->size.x;
|
||||
dst_rect.h = scale_y * text->size.y;
|
||||
|
||||
SDL_RenderTextureRotated(
|
||||
renderer,
|
||||
@@ -308,7 +325,7 @@ void Window::render() {
|
||||
} break;
|
||||
|
||||
case RenderInstruction::DRAW_IMAGE: {
|
||||
Image image = image_pool[instruction.draw_image.image_id];
|
||||
Image *image = image_pool[instruction.draw_image.image_id];
|
||||
|
||||
SDL_FlipMode flip = SDL_FLIP_NONE;
|
||||
|
||||
@@ -323,12 +340,12 @@ void Window::render() {
|
||||
SDL_FRect dst_rect;
|
||||
dst_rect.x = instruction.draw_image.position.x * scale + offset.x;
|
||||
dst_rect.y = instruction.draw_image.position.y * scale + offset.y;
|
||||
dst_rect.w = scale_x * image.size.x;
|
||||
dst_rect.h = scale_y * image.size.y;
|
||||
dst_rect.w = scale_x * image->size.x;
|
||||
dst_rect.h = scale_y * image->size.y;
|
||||
|
||||
SDL_RenderTextureRotated(
|
||||
renderer,
|
||||
image.texture,
|
||||
image->texture,
|
||||
nullptr,
|
||||
&dst_rect,
|
||||
instruction.angle,
|
||||
|
||||
Reference in New Issue
Block a user