Make pools with generations and bind mouse position logic

This commit is contained in:
2026-05-06 16:40:36 +01:00
parent 08a642e07a
commit 3894b90398
8 changed files with 221 additions and 125 deletions
+30 -25
View File
@@ -56,7 +56,7 @@ Window::~Window() {
SDL_Quit();
}
int Window::load_image(const char *path, float alpha, bool pixel_art) {
uint64_t Window::load_image(const char *path, float alpha, bool pixel_art) {
SDL_Texture *texture = IMG_LoadTexture(renderer, path);
if (pixel_art) {
@@ -65,7 +65,7 @@ int Window::load_image(const char *path, float alpha, bool pixel_art) {
if (texture == nullptr) {
fprintf(stderr, "Failed to load image: %s\n", SDL_GetError());
return UINT32_MAX;
return UINT64_MAX;
}
SDL_SetTextureAlphaMod(texture, (uint8_t)(alpha * 255));
@@ -74,26 +74,26 @@ int Window::load_image(const char *path, float alpha, bool pixel_art) {
SDL_GetTextureSize(texture, &w, &h);
Image *img = new Image{texture, {w, h}};
int id = image_pool.acquire(img);
uint64_t id = image_pool.acquire(img);
image_pool.use(id);
return id;
}
Vec2<float> Window::get_image_size(int image_id) {
Vec2<float> Window::get_image_size(uint64_t image_id) {
if (!image_pool.is_valid(image_id)) {
fprintf(stderr, "Invalid image ID: %d\n", image_id);
fprintf(stderr, "Invalid image ID: %llu\n", (unsigned long long)image_id);
return {0, 0};
}
return image_pool[image_id]->size;
}
int Window::load_font(const char *path, int font_size, bool bold, bool italic, bool underline, bool pixel_art) {
uint64_t Window::load_font(const char *path, int font_size, bool bold, bool italic, bool underline, bool pixel_art) {
TTF_Font *font = TTF_OpenFont(path, font_size);
if (font == nullptr) {
fprintf(stderr, "Failed to load font: %s\n", SDL_GetError());
return UINT32_MAX;
return UINT64_MAX;
}
int style = TTF_STYLE_NORMAL;
@@ -107,16 +107,16 @@ int Window::load_font(const char *path, int font_size, bool bold, bool italic, b
TTF_SetFontStyle(font, style);
Font *f = new Font{font, pixel_art};
int id = font_pool.acquire(f);
uint64_t id = font_pool.acquire(f);
font_pool.use(id);
return id;
}
int Window::create_text(int font_id, const char *text, uint32_t length, Color color) {
uint64_t Window::create_text(uint64_t font_id, const char *text, uint32_t length, Color color) {
if (!font_pool.is_valid(font_id)) {
fprintf(stderr, "Invalid font ID: %d\n", font_id);
return UINT32_MAX;
fprintf(stderr, "Invalid font ID: %llu\n", (unsigned long long)font_id);
return UINT64_MAX;
}
SDL_Color sdl_color = {color.r, color.g, color.b, color.a};
@@ -124,7 +124,7 @@ int Window::create_text(int font_id, const char *text, uint32_t length, Color co
if (surface == nullptr) {
fprintf(stderr, "Failed to create text surface: %s\n", SDL_GetError());
return UINT32_MAX;
return UINT64_MAX;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
@@ -132,7 +132,7 @@ int Window::create_text(int font_id, const char *text, uint32_t length, Color co
if (texture == nullptr) {
fprintf(stderr, "Failed to create text texture: %s\n", SDL_GetError());
return UINT32_MAX;
return UINT64_MAX;
}
if (font_pool[font_id]->pixel_art)
@@ -142,23 +142,24 @@ int Window::create_text(int font_id, const char *text, uint32_t length, Color co
SDL_GetTextureSize(texture, &w, &h);
Text *t = new Text{texture, {w, h}};
int id = text_pool.acquire(t);
uint64_t id = text_pool.acquire(t);
text_pool.use(id);
return id;
}
Vec2<float> Window::get_text_size(int text_id) {
Vec2<float> Window::get_text_size(uint64_t text_id) {
if (!text_pool.is_valid(text_id)) {
fprintf(stderr, "Invalid text ID: %d\n", text_id);
fprintf(stderr, "(Size) Invalid text ID: %llu\n", (unsigned long long)text_id);
return {0, 0};
throw std::runtime_error("Invalid text ID");
}
return text_pool[text_id]->size;
}
bool Window::write(int text_id, Vec2<float> position, float z, float angle, Vec2<float> scale) {
bool Window::write(uint64_t text_id, Vec2<float> position, float z, float angle, Vec2<float> scale) {
if (!text_pool.is_valid(text_id)) {
fprintf(stderr, "Invalid text ID: %d\n", text_id);
fprintf(stderr, "(Write) Invalid text ID: %llu\n", (unsigned long long)text_id);
return false;
}
@@ -176,9 +177,9 @@ 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) {
bool Window::draw(uint64_t 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);
fprintf(stderr, "Invalid image ID: %llu\n", (unsigned long long)image_id);
return false;
}
@@ -382,6 +383,10 @@ bool Window::poll(Event &event) {
event.text_input.text[event.text_input.length] = '\0';
break;
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
event.type = Event::MOUSE_LEAVE;
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
event.type = Event::MOUSE_BUTTON_DOWN;
event.mouse_button.button = sdl_event.button.button;
@@ -389,7 +394,7 @@ bool Window::poll(Event &event) {
if (mouse_in_window({sdl_event.button.x, sdl_event.button.y}, virtual_position))
event.mouse_button.position = virtual_position;
else
return false;
event.type = Event::MOUSE_LEAVE;
} break;
case SDL_EVENT_MOUSE_BUTTON_UP: {
@@ -399,17 +404,17 @@ bool Window::poll(Event &event) {
if (mouse_in_window({sdl_event.button.x, sdl_event.button.y}, virtual_position))
event.mouse_button.position = virtual_position;
else
return false;
event.type = Event::MOUSE_LEAVE;
} break;
case SDL_EVENT_MOUSE_MOTION: {
event.type = Event::MOUSE_MOVE;
event.mouse_move.delta = {sdl_event.motion.xrel, sdl_event.motion.yrel};
Vec2<float> virtual_position;
if (mouse_in_window({sdl_event.button.x, sdl_event.button.y}, virtual_position))
event.mouse_button.position = virtual_position;
if (mouse_in_window({sdl_event.motion.x, sdl_event.motion.y}, virtual_position))
event.mouse_move.position = virtual_position;
else
return false;
event.type = Event::MOUSE_LEAVE;
} break;
case SDL_EVENT_MOUSE_WHEEL: