Introduce sdl_palette

It replaces the sdl_col array and is set as the palette of main_surface.
This commit is contained in:
kichikuou
2025-03-09 10:42:32 +09:00
parent 6bf6a5c86a
commit cb8a48cdec
4 changed files with 24 additions and 19 deletions
+6 -5
View File
@@ -132,6 +132,7 @@ SDL_Surface *font_get_glyph(const char *str_utf8, int r, int g, int b) {
// SDL can't blit ARGB to an indexed bitmap properly, so we do it ourselves.
static void sdl_drawAntiAlias_8bpp(int dstx, int dsty, SDL_Surface *src, uint8_t col)
{
const SDL_Color* palette = sdl_palette->colors;
Uint8 cache[256*7];
memset(cache, 0, 256);
@@ -154,9 +155,9 @@ static void sdl_drawAntiAlias_8bpp(int dstx, int dsty, SDL_Surface *src, uint8_t
// find nearest color in palette
cache[*dp] |= 1 << alpha;
int c = sdl_nearest_color(
(sdl_col[col].r * alpha + sdl_col[*dp].r * (7 - alpha)) / 7,
(sdl_col[col].g * alpha + sdl_col[*dp].g * (7 - alpha)) / 7,
(sdl_col[col].b * alpha + sdl_col[*dp].b * (7 - alpha)) / 7);
(palette[col].r * alpha + palette[*dp].r * (7 - alpha)) / 7,
(palette[col].g * alpha + palette[*dp].g * (7 - alpha)) / 7,
(palette[col].b * alpha + palette[*dp].b * (7 - alpha)) / 7);
cache[alpha << 8 | *dp] = c;
*dp = c;
}
@@ -185,9 +186,9 @@ SDL_Rect font_draw_glyph(int x, int y, const char *str_utf8, uint8_t cl) {
antialias = false;
if (antialias) {
fs = TTF_RenderUTF8_Blended(fontset->id, str_utf8, sdl_col[cl]);
fs = TTF_RenderUTF8_Blended(fontset->id, str_utf8, sdl_palette->colors[cl]);
} else {
fs = TTF_RenderUTF8_Solid(fontset->id, str_utf8, sdl_col[cl]);
fs = TTF_RenderUTF8_Solid(fontset->id, str_utf8, sdl_palette->colors[cl]);
}
if (!fs) {
WARNING("Text rendering failed: %s", TTF_GetError());
+12 -11
View File
@@ -53,7 +53,7 @@ static void sdl_pal_check(void) {
static Uint32 palette_color(uint8_t c) {
if (main_surface->format->BitsPerPixel == 8)
return c;
return SDL_MapRGB(main_surface->format, sdl_col[c].r, sdl_col[c].g, sdl_col[c].b);
return SDL_MapRGB(main_surface->format, sdl_palette->colors[c].r, sdl_palette->colors[c].g, sdl_palette->colors[c].b);
}
void sdl_updateScreen(void) {
@@ -128,13 +128,14 @@ void sdl_updateAll(MyRectangle *view_rect) {
/* Color の複数個指定 */
void sdl_setPalette(Color *pal, int first, int count) {
SDL_Color colors[256];
for (int i = 0; i < count; i++) {
sdl_col[first + i].r = pal[first + i].r;
sdl_col[first + i].g = pal[first + i].g;
sdl_col[first + i].b = pal[first + i].b;
colors[i].r = pal[first + i].r;
colors[i].g = pal[first + i].g;
colors[i].b = pal[first + i].b;
colors[i].a = 255;
}
if (main_surface->format->BitsPerPixel == 8)
SDL_SetPaletteColors(main_surface->format->palette, &sdl_col[first], first, count);
SDL_SetPaletteColors(sdl_palette, colors, first, count);
}
/* 矩形の描画 */
@@ -254,7 +255,7 @@ void sdl_drawImage8(cgdata *cg, int dx, int dy, int sprite_color) {
c++;
}
} else {
memcpy(s->format->palette->colors, sdl_col, sizeof(SDL_Color) * 256);
SDL_SetSurfacePalette(s, sdl_palette);
}
if (sprite_color != -1)
@@ -271,7 +272,7 @@ void sdl_drawLine(int x1, int y1, int x2, int y2, uint8_t c) {
sdl_pal_check();
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(main_surface);
SDL_SetRenderDrawColor(renderer, sdl_col[c].r, sdl_col[c].g, sdl_col[c].b, SDL_ALPHA_OPAQUE);
SDL_SetRenderDrawColor(renderer, sdl_palette->colors[c].r, sdl_palette->colors[c].g, sdl_palette->colors[c].b, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
SDL_DestroyRenderer(renderer);
}
@@ -305,9 +306,9 @@ SDL_Rect sdl_floodFill(int x, int y, int c) {
int sdl_nearest_color(int r, int g, int b) {
int i, col, mind = INT_MAX;
for (i = 0; i < 256; i++) {
int dr = r - sdl_col[i].r;
int dg = g - sdl_col[i].g;
int db = b - sdl_col[i].b;
int dr = r - sdl_palette->colors[i].r;
int dg = g - sdl_palette->colors[i].g;
int db = b - sdl_palette->colors[i].b;
int d = dr*dr*30 + dg*dg*59 + db*db*11;
if (d < mind) {
mind = d;
+1 -1
View File
@@ -33,7 +33,7 @@
extern SDL_Window *sdl_window;
extern SDL_Renderer *sdl_renderer;
extern SDL_Texture *sdl_texture;
extern SDL_Color sdl_col[256]; // color palette
extern SDL_Palette *sdl_palette;
extern surface_t *sdl_dibinfo;
extern int view_w;
extern int view_h;
+5 -2
View File
@@ -40,7 +40,7 @@ SDL_Window *sdl_window;
SDL_Renderer *sdl_renderer;
SDL_Texture *sdl_texture;
SDL_Surface *main_surface; // offscreen surface
SDL_Color sdl_col[256]; // color palette
SDL_Palette *sdl_palette;
surface_t *sdl_dibinfo;
int view_w;
int view_h;
@@ -104,6 +104,8 @@ int sdl_Initialize(const char *render_driver) {
}
void sdl_Remove(void) {
if (sdl_palette)
SDL_FreePalette(sdl_palette);
if (main_surface)
SDL_FreeSurface(main_surface);
if (sdl_renderer)
@@ -156,6 +158,7 @@ static void window_init(const char *render_driver) {
SYS35_DEFAULT_WIDTH, SYS35_DEFAULT_HEIGHT, flags);
sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0);
SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
sdl_palette = SDL_AllocPalette(256);
}
static void makeDIB(int width, int height, int depth) {
@@ -184,7 +187,7 @@ static void makeDIB(int width, int height, int depth) {
main_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, format);
if (main_surface->format->BitsPerPixel == 8) {
memset(main_surface->format->palette->colors, 0, sizeof(SDL_Color)*256);
SDL_SetSurfacePalette(main_surface, sdl_palette);
}
if (sdl_dibinfo) {