Skip to content

Commit

Permalink
[Web] Pad NPOT theme textures to POT.
Browse files Browse the repository at this point in the history
  • Loading branch information
skylersaleh committed Sep 5, 2024
1 parent 5e209f7 commit 0d7310d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<im_h)pad_pow2<<=1;
if(pad_pow2!=im_w ||pad_pow2!=im_h){
uint8_t * old = im;
im = se_pad_image_to_size(im,im_w,im_h,pad_pow2,pad_pow2);
printf("Padding npot texture from {%d, %d} -> {%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;
Expand Down Expand Up @@ -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<num_mips;++m){
free((uint8_t*)im_data.subimage[0][m].ptr);
}
Expand Down

0 comments on commit 0d7310d

Please sign in to comment.