Fix main loop, add example and cleanup

This commit is contained in:
2026-05-04 21:28:22 +01:00
parent 55f4b4d933
commit a51eb903c4
8 changed files with 127 additions and 128 deletions
+45 -20
View File
@@ -29,10 +29,20 @@ Window::Window(Vec2<float> size, const char *title) {
SDL_UpdateTexture(white_tex, nullptr, &pixel, sizeof(pixel));
};
void Window::update(Vec2<float> new_size, const char *new_title) {
if (new_size.x > 0 && new_size.y > 0) {
virtual_size = new_size;
compute_transform();
}
if (new_title)
SDL_SetWindowTitle(window, new_title);
}
Window::~Window() {
for (auto tex : image_pool.all())
if (tex)
SDL_DestroyTexture(tex);
for (auto &tex : image_pool.all())
if (tex.texture)
SDL_DestroyTexture(tex.texture);
for (auto tex : text_pool.all())
if (tex)
@@ -48,7 +58,7 @@ Window::~Window() {
SDL_Quit();
}
int Window::load_image(const char *path, bool pixel_art) {
int Window::load_image(const char *path, float alpha, bool pixel_art) {
SDL_Texture *texture = IMG_LoadTexture(renderer, path);
if (pixel_art) {
@@ -60,7 +70,20 @@ int Window::load_image(const char *path, bool pixel_art) {
return UINT32_MAX;
}
return image_pool.acquire(texture);
SDL_SetTextureAlphaMod(texture, (uint8_t)(alpha * 255));
float w, h;
SDL_GetTextureSize(texture, &w, &h);
return image_pool.acquire({texture, {w, h}});
}
Vec2<float> Window::get_image_size(int image_id) {
if (!image_pool.is_valid(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};
}
void Window::unload_image(int image_id) {
@@ -69,7 +92,7 @@ void Window::unload_image(int image_id) {
return;
}
SDL_DestroyTexture(image_pool[image_id]);
SDL_DestroyTexture(image_pool[image_id].texture);
image_pool.release(image_id);
}
@@ -132,6 +155,16 @@ int Window::create_text(int font_id, const char *text, uint32_t length, Color co
return text_pool.acquire(texture);
}
Vec2<float> Window::get_text_size(int text_id) {
if (!text_pool.is_valid(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};
}
void Window::unload_text(int text_id) {
if (!text_pool.is_valid(text_id)) {
fprintf(stderr, "Invalid text ID: %d\n", text_id);
@@ -162,7 +195,7 @@ bool Window::write(int text_id, Vec2<float> position, float z, float angle, Vec2
return true;
};
bool Window::draw(int image_id, Vec2<float> position, float z, float angle, Vec2<float> scale, float alpha) {
bool Window::draw(int image_id, Vec2<float> position, float z, float angle, Vec2<float> scale) {
if (!image_pool.is_valid(image_id)) {
fprintf(stderr, "Invalid image ID: %d\n", image_id);
return false;
@@ -176,7 +209,6 @@ bool Window::draw(int image_id, Vec2<float> position, float z, float angle, Vec2
.draw_image = {
.image_id = image_id,
.position = position,
.alpha = alpha,
}}
);
@@ -236,9 +268,7 @@ void Window::render() {
std::stable_sort(
render_instructions.begin(),
render_instructions.end(),
[](const RenderInstruction &a, const RenderInstruction &b) {
return a.z < b.z;
}
[](const RenderInstruction &a, const RenderInstruction &b) { return a.z < b.z; }
);
for (const RenderInstruction &instruction : render_instructions) {
@@ -277,10 +307,7 @@ void Window::render() {
} break;
case RenderInstruction::DRAW_IMAGE: {
SDL_Texture *texture = image_pool[instruction.draw_image.image_id];
float w, h;
SDL_GetTextureSize(texture, &w, &h);
Image image = image_pool[instruction.draw_image.image_id];
SDL_FlipMode flip = SDL_FLIP_NONE;
@@ -295,14 +322,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 * w;
dst_rect.h = scale_y * h;
SDL_SetTextureAlphaMod(texture, static_cast<Uint8>(instruction.draw_image.alpha * 255));
dst_rect.w = scale_x * image.size.x;
dst_rect.h = scale_y * image.size.y;
SDL_RenderTextureRotated(
renderer,
texture,
image.texture,
nullptr,
&dst_rect,
instruction.angle,