sdl_effect: Fix palette issue when texture is created from sdl_dib

This commit is contained in:
kichikuou
2025-02-11 10:30:25 +09:00
parent c621a5dddb
commit 53f0a5ce2a
3 changed files with 16 additions and 17 deletions
-1
View File
@@ -82,7 +82,6 @@ extern void sdl_putRegion(void *src, int x, int y);
extern void sdl_restoreRegion(void *src, int x, int y);
extern void* sdl_saveRegion(int x, int y, int w, int h);
extern void sdl_delRegion(void *src);
extern SDL_Surface *com2surface(agsurface_t *s, int x, int y, int w, int h);
/* Effects */
struct sdl_effect;
-5
View File
@@ -306,11 +306,6 @@ SDL_Rect sdl_floodFill(int x, int y, int c) {
}
}
SDL_Surface *com2surface(agsurface_t *s, int x, int y, int w, int h) {
uint8_t *pixels = s->pixel + y * s->bytes_per_line + x * s->bytes_per_pixel;
return SDL_CreateRGBSurfaceFrom(pixels, w, h, s->depth, s->bytes_per_line, 0, 0, 0, 0);
}
int sdl_nearest_color(int r, int g, int b) {
int i, col, mind = INT_MAX;
for (i = 0; i < 256; i++) {
+16 -11
View File
@@ -126,28 +126,33 @@ enum sdl_effect_type from_sact_effect(enum sact_effect effect) {
}
}
static SDL_Surface *create_surface_view(agsurface_t *s, int x, int y, int w, int h) {
if (s == sdl_dibinfo) {
uint8_t *pixels = sdl_dib->pixels;
pixels += y * sdl_dib->pitch + x * sdl_dib->format->BytesPerPixel;
SDL_Surface *view = SDL_CreateRGBSurfaceWithFormatFrom(
pixels, w, h, sdl_dib->format->BitsPerPixel, sdl_dib->pitch, sdl_dib->format->format);
if (sdl_dib->format->palette)
SDL_SetSurfacePalette(view, sdl_dib->format->palette);
return view;
} else {
uint8_t *pixels = s->pixel + y * s->bytes_per_line + x * s->bytes_per_pixel;
return SDL_CreateRGBSurfaceFrom(pixels, w, h, s->depth, s->bytes_per_line, 0, 0, 0, 0);
}
}
typedef struct {
SDL_Texture *tx;
SDL_Rect rect;
} EffectTexture;
static SDL_Surface *create_surface_view(SDL_Surface *sf, int x, int y, int w, int h) {
uint8_t *pixels = sf->pixels;
pixels += y * sf->pitch + x * sf->format->BytesPerPixel;
SDL_Surface *view = SDL_CreateRGBSurfaceWithFormatFrom(
pixels, w, h, sf->format->BitsPerPixel, sf->pitch, sf->format->format);
return view;
}
static EffectTexture *create_effect_texture(agsurface_t *as, int x, int y, int w, int h) {
EffectTexture *t = calloc(1, sizeof(EffectTexture));
if (!as) {
t->tx = sdl_texture;
t->rect = (SDL_Rect){ x, y, w, h };
} else {
SDL_Surface *sf = (as == sdl_dibinfo)
? create_surface_view(sdl_dib, x, y, w, h)
: com2surface(as, x, y, w, h);
SDL_Surface *sf = create_surface_view(as, x, y, w, h);
t->tx = SDL_CreateTextureFromSurface(sdl_renderer, sf);
SDL_FreeSurface(sf);
t->rect = (SDL_Rect){ 0, 0, w, h };