From e093a6d4925aed91f145327a688af1a3b96ef8b3 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Mon, 23 Sep 2024 14:35:46 +0300 Subject: [PATCH] twn_textures.c: handle RGB image files --- src/rendering/twn_gl_15_gpu_texture.c | 3 +++ src/twn_textures.c | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/rendering/twn_gl_15_gpu_texture.c b/src/rendering/twn_gl_15_gpu_texture.c index d8f5591..f4cad53 100644 --- a/src/rendering/twn_gl_15_gpu_texture.c +++ b/src/rendering/twn_gl_15_gpu_texture.c @@ -41,6 +41,9 @@ void upload_gpu_texture(gpu_texture texture, void *pixels, int channels, int wid if (channels == 4) { format_internal = GL_RGBA8; format = GL_RGBA; + } else if (channels == 3) { + format_internal = GL_RGBA8; + format = GL_RGB; } else if (channels == 1) { format_internal = GL_ALPHA; format = GL_ALPHA; diff --git a/src/twn_textures.c b/src/twn_textures.c index 5688a49..94c2873 100644 --- a/src/twn_textures.c +++ b/src/twn_textures.c @@ -34,7 +34,7 @@ static SDL_Surface *image_to_surface(const char *path) { goto ERR_CANNOT_ALLOCATE_MEM; int width, height, channels; - void *image_mem = stbi_load_from_memory(file_mem, (int)file_size, &width, &height, &channels, 4); + void *image_mem = stbi_load_from_memory(file_mem, (int)file_size, &width, &height, &channels, 0); if (!image_mem) goto ERR_CANNOT_READ_IMAGE; @@ -123,7 +123,7 @@ static void add_new_atlas(struct texture_cache *cache) { static void upload_texture_from_surface(gpu_texture texture, SDL_Surface *surface) { SDL_LockSurface(surface); - upload_gpu_texture(texture, surface->pixels, 4, surface->w, surface->h); + upload_gpu_texture(texture, surface->pixels, surface->format->BytesPerPixel, surface->w, surface->h); SDL_UnlockSurface(surface); }