rts/src/gfx/sdl-1.2/src/sprite.c

126 lines
2.5 KiB
C

#include <gfx.h>
#include <gfx/port.h>
#include <header.h>
#include <SDL/SDL.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
void sprite_free(struct sprite *const s)
{
if (s && s->s)
SDL_FreeSurface(s->s);
}
int sprite_clone(const struct sprite *const src, struct sprite *const dst)
{
*dst = *src;
return 0;
}
int sprite_screen_resize_ev(struct sprite *const s)
{
int ret = -1;
SDL_Surface *const old = s->s;
/* Magenta as transparent. */
if (s->transparent
&& SDL_SetColorKey(old, SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(old->format, 255, 0, 255)))
{
fprintf(stderr, "SDL_SetColorKey: %s\n", SDL_GetError());
goto end;
}
else if (!(s->s = SDL_DisplayFormat(old)))
{
fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError());
goto end;
}
ret = 0;
end:
SDL_FreeSurface(old);
return ret;
}
static int load_header(struct sprite *const s, FILE *const f)
{
if (header_load_bool(f, "transparent", &s->transparent))
return -1;
return 0;
}
static int load_bitmap(struct sprite *const s, FILE *const f)
{
int ret = -1;
SDL_RWops *ops = NULL;
SDL_Surface *ts = NULL;
if (!(ops = SDL_RWFromFP(f, 0)))
{
fprintf(stderr, "SDL_RWFromFP: %s\n", SDL_GetError());
goto end;
}
if (!(ts = SDL_LoadBMP_RW(ops, 0)))
{
fprintf(stderr, "SDL_LoadBMP_RW: %s\n", SDL_GetError());
goto end;
}
/* Magenta as transparent. */
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());
goto end;
}
else if (!(s->s = SDL_DisplayFormat(ts)))
{
fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError());
goto end;
}
gfx_register_sprite(s);
s->w = ts->w;
s->h = ts->h;
ret = 0;
end:
SDL_FreeRW(ops);
SDL_FreeSurface(ts);
return ret;
}
int sprite_from_fp(struct sprite *const s, FILE *const f)
{
memset(s, 0, sizeof *s);
if (load_header(s, f) || load_bitmap(s, f))
return -1;
return 0;
}
void sprite_sort(struct sprite *const s)
{
SDL_Rect r =
{
.x = s->x,
.y = s->y
};
SDL_Rect clip =
{
.x = s->u,
.y = s->v,
.w = s->w,
.h = s->h
};
if (SDL_BlitSurface(s->s, &clip, gfx_port_screen(), &r))
fprintf(stderr, "SDL_BlitSurface: %s\n", SDL_GetError());
}