textures.c: fix regressions for loner textures

This commit is contained in:
2024-07-14 18:36:48 +03:00
parent bd53a931c0
commit 7218acb40b
4 changed files with 19 additions and 12 deletions

View File

@@ -125,6 +125,7 @@ static void upload_texture_from_surface(GLuint texture, SDL_Surface *surface) {
static void recreate_current_atlas_texture(struct texture_cache *cache) {
/* TODO: figure out if SDL_UpdateTexture alone is faster than blitting */
/* TODO: should surfaces be freed after they cannot be referenced in atlas builing? */
SDL_Surface *atlas_surface = cache->atlas_surfaces[cache->atlas_index];
/* clear */
@@ -132,9 +133,14 @@ static void recreate_current_atlas_texture(struct texture_cache *cache) {
/* blit the texture surfaces onto the atlas */
for (size_t i = 0; i < shlenu(cache->hash); ++i) {
/* skip all that aren't part of currently built one */
if (cache->hash[i].value.atlas_index != cache->atlas_index)
continue;
/* skip loners */
if (cache->hash[i].value.loner_texture != 0)
continue;
SDL_BlitSurface(cache->hash[i].value.data,
NULL,
atlas_surface,
@@ -155,7 +161,10 @@ static void recreate_current_atlas_texture(struct texture_cache *cache) {
static stbrp_rect *create_rects_from_cache(struct texture_cache *cache) {
stbrp_rect *rects = NULL;
for (size_t i = 0; i < shlenu(cache->hash); ++i) {
SDL_Surface *surface_data = cache->hash[i].value.data;
if (cache->hash[i].value.loner_texture != 0)
continue;
const SDL_Surface *surface_data = cache->hash[i].value.data;
stbrp_rect new_rect = {
.w = surface_data->w,
.h = surface_data->h,
@@ -293,7 +302,7 @@ static t_texture_key textures_load(struct texture_cache *cache, const char *path
new_texture.data = surface;
/* it's a "loner texture," it doesn't fit in an atlas so it's not in one */
if (surface->w > TEXTURE_ATLAS_SIZE || surface->h > TEXTURE_ATLAS_SIZE) {
if (surface->w >= TEXTURE_ATLAS_SIZE || surface->h >= TEXTURE_ATLAS_SIZE) {
new_texture.loner_texture = new_gl_texture();
upload_texture_from_surface(new_texture.loner_texture, surface);
new_texture.srcrect = (t_rect) { .w = surface->w, .h = surface->h };