SDL-1.2: Implement X mirroring for quads

This commit is contained in:
Xavier Del Campo Romero 2022-07-15 00:59:44 +02:00
parent 2d22b88695
commit f753ede740
3 changed files with 48 additions and 20 deletions

View File

@ -11,7 +11,7 @@ extern "C"
struct sprite struct sprite
{ {
SDL_Surface *s; SDL_Surface *s, *s_x;
short x, y, w, h; short x, y, w, h;
unsigned char u, v; unsigned char u, v;
bool transparent; bool transparent;
@ -24,8 +24,9 @@ struct quad
short y0, y1, y2, y3; short y0, y1, y2, y3;
unsigned char u0, u1, u2, u3; unsigned char u0, u1, u2, u3;
unsigned char v0, v1, v2, v3; unsigned char v0, v1, v2, v3;
short w, h;
bool transparent; bool transparent;
SDL_Surface *s; SDL_Surface *s, *s_x;
}; };
struct rect struct rect

View File

@ -7,29 +7,38 @@
int quad_from_sprite(const struct sprite *const s, struct quad *const q) int quad_from_sprite(const struct sprite *const s, struct quad *const q)
{ {
q->s = s->s; q->s = s->s;
q->s_x = s->s_x;
q->u0 = q->u2 = s->u; q->u0 = q->u2 = s->u;
q->v0 = q->v1 = s->v; q->v0 = q->v1 = s->v;
q->u1 = q->u3 = s->u + s->w - 1; q->u1 = q->u3 = s->u + s->w - 1;
q->v2 = q->v3 = s->v + s->h - 1; q->v2 = q->v3 = s->v + s->h - 1;
q->w = s->w;
q->h = s->h;
return 0; return 0;
} }
void quad_sort(struct quad *const q) void quad_sort(struct quad *const q)
{ {
const bool xflip = q->x0 > q->x1;
SDL_Rect r = SDL_Rect r =
{ {
.x = q->x0, .x = xflip ? q->x1 : q->x0,
.y = q->y0 .y = q->y0
}; };
const short w = q->u1 - q->u0 + 1, h = q->v2 - q->v0 + 1;
SDL_Rect clip = SDL_Rect clip =
{ {
.x = q->u0, .x = xflip ? q->w - q->u0 - w: q->u0,
.y = q->v0, .y = q->v0,
.w = q->u1 - q->u0 + 1, .w = w,
.h= q->v2 - q->v0 + 1 .h = h
}; };
if (SDL_BlitSurface(q->s, &clip, gfx_screen(), &r)) SDL_Surface *const s = xflip ? q->s_x : q->s;
if (SDL_BlitSurface(s, &clip, gfx_screen(), &r))
fprintf(stderr, "SDL_BlitSurface: %s\n", SDL_GetError()); fprintf(stderr, "SDL_BlitSurface: %s\n", SDL_GetError());
} }

View File

@ -3,6 +3,7 @@
#include <header.h> #include <header.h>
#include <sdl-1.2/gfx_private.h> #include <sdl-1.2/gfx_private.h>
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_rotozoom.h>
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
@ -19,20 +20,32 @@ int sprite_clone(const struct sprite *const src, struct sprite *const dst)
return 0; return 0;
} }
static int set_transparent(SDL_Surface *const s)
{
/* Magenta as transparent. */
const Uint32 map = SDL_MapRGB(s->format, 255, 0, 255);
const int ret = SDL_SetColorKey(s, SDL_SRCCOLORKEY | SDL_RLEACCEL, map);
if (ret)
fprintf(stderr, "SDL_SetColorKey: %s\n", SDL_GetError());
return ret;
}
int sprite_screen_resize_ev(struct sprite *const s) int sprite_screen_resize_ev(struct sprite *const s)
{ {
int ret = -1; int ret = -1;
SDL_Surface *const old = s->s; SDL_Surface *const old = s->s, *const old_x = s->s_x;
/* Magenta as transparent. */ /* Magenta as transparent. */
if (s->transparent if (s->transparent && (set_transparent(old) || set_transparent(old_x)))
&& SDL_SetColorKey(old, SDL_SRCCOLORKEY | SDL_RLEACCEL, goto end;
SDL_MapRGB(old->format, 255, 0, 255))) else if (!(s->s = SDL_DisplayFormat(old)))
{ {
fprintf(stderr, "SDL_SetColorKey: %s\n", SDL_GetError()); fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError());
goto end; goto end;
} }
else if (!(s->s = SDL_DisplayFormat(old))) else if (!(s->s_x = SDL_DisplayFormat(old_x)))
{ {
fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError()); fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError());
goto end; goto end;
@ -42,6 +55,7 @@ int sprite_screen_resize_ev(struct sprite *const s)
end: end:
SDL_FreeSurface(old); SDL_FreeSurface(old);
SDL_FreeSurface(old_x);
return ret; return ret;
} }
@ -57,31 +71,35 @@ static int load_bitmap(struct sprite *const s, FILE *const f)
{ {
int ret = -1; int ret = -1;
SDL_RWops *ops = NULL; SDL_RWops *ops = NULL;
SDL_Surface *ts = NULL; SDL_Surface *ts = NULL, *zs = NULL;
if (!(ops = SDL_RWFromFP(f, 0))) if (!(ops = SDL_RWFromFP(f, 0)))
{ {
fprintf(stderr, "SDL_RWFromFP: %s\n", SDL_GetError()); fprintf(stderr, "SDL_RWFromFP: %s\n", SDL_GetError());
goto end; goto end;
} }
if (!(ts = SDL_LoadBMP_RW(ops, 0))) else if (!(ts = SDL_LoadBMP_RW(ops, 0)))
{ {
fprintf(stderr, "SDL_LoadBMP_RW: %s\n", SDL_GetError()); fprintf(stderr, "SDL_LoadBMP_RW: %s\n", SDL_GetError());
goto end; goto end;
} }
/* Magenta as transparent. */ else if (!(zs = zoomSurface(ts, -1, 1, 0)))
else if (s->transparent
&& SDL_SetColorKey(ts, SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(ts->format, 255, 0, 255)))
{ {
fprintf(stderr, "SDL_SetColorKey: %s\n", SDL_GetError()); fprintf(stderr, "zoomSurface: %s\n", SDL_GetError());
goto end; goto end;
} }
else if (s->transparent && (set_transparent(ts) || set_transparent(zs)))
goto end;
else if (!(s->s = SDL_DisplayFormat(ts))) else if (!(s->s = SDL_DisplayFormat(ts)))
{ {
fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError()); fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError());
goto end; goto end;
} }
else if (!(s->s_x = SDL_DisplayFormat(zs)))
{
fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError());
goto end;
}
gfx_register_sprite(s); gfx_register_sprite(s);
s->w = ts->w; s->w = ts->w;