From 0d7310db6c34c9752eb7354473c61f532d9815b8 Mon Sep 17 00:00:00 2001 From: Sky Date: Wed, 4 Sep 2024 21:57:54 -0700 Subject: [PATCH] [Web] Pad NPOT theme textures to POT. --- src/main.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main.c b/src/main.c index a147d2598..710e686bb 100644 --- a/src/main.c +++ b/src/main.c @@ -8273,6 +8273,18 @@ static bool se_load_theme_from_image_format_mini(uint8_t* im, uint32_t im_w, uin } return true; } +static uint8_t * se_pad_image_to_size(uint8_t* im, uint32_t im_w, uint32_t im_h, uint32_t new_w, uint32_t new_h){ + uint8_t * new_image = malloc(new_w*new_h*4); + memset(new_image,0,new_w*new_h*4); + for(int y = 0; y< im_h;++y) + for(int x = 0; x< im_w;++x){ + new_image[(y*new_w + x)*4+0] = im[(y*im_w+x)*4+0]; + new_image[(y*new_w + x)*4+1] = im[(y*im_w+x)*4+1]; + new_image[(y*new_w + x)*4+2] = im[(y*im_w+x)*4+2]; + new_image[(y*new_w + x)*4+3] = im[(y*im_w+x)*4+3]; + } + return new_image; +} static bool se_load_theme_from_image(uint8_t* im, uint32_t im_w, uint32_t im_h, bool invert){ if(!im){return false; } bool loaded = false; @@ -8413,6 +8425,22 @@ static bool se_load_theme_from_image(uint8_t* im, uint32_t im_w, uint32_t im_h, theme->palettes[i*4+2]= 255-theme->palettes[i*4+2]; } } + //WebGL doesn't support mip mapping of NPOT, so pad them to a power of two. + bool free_mip0 = false; + #if defined(EMSCRIPTEN) + int pad_pow2 = 1; + while(pad_pow2 < im_w ||pad_pow2 {%d %d}\n",im_w,im_h,pad_pow2,pad_pow2); + im_w = pad_pow2; + im_h = pad_pow2; + free_mip0 = true; + } + #endif + theme->im_h=im_h; + theme->im_w=im_w; sg_image_data im_data={0}; im_data.subimage[0][0].ptr = im; @@ -8459,6 +8487,7 @@ static bool se_load_theme_from_image(uint8_t* im, uint32_t im_w, uint32_t im_h, .data= im_data, }; gui_state.theme.image= sg_make_image(&desc); + if(free_mip0)free((uint8_t*)im_data.subimage[0][0].ptr); for(int m = 1; m