rotate added

This commit is contained in:
p.kosyh 2011-04-12 07:42:01 +00:00
parent 0acb47a575
commit 7ff58bebf7
4 changed files with 82 additions and 0 deletions

View file

@ -1293,6 +1293,42 @@ img_t gfx_scale(img_t src, float xscale, float yscale)
}
return (img_t)zoomSurface((SDL_Surface *)src, xscale, yscale, 1);
}
img_t gfx_rotate(img_t src, float angle)
{
anigif_t ag;
float rangle = angle * (M_PI / 180.0);
if ((ag = is_anigif(Surf(src)))) {
int i;
int w,h;
float x, y, x1, y1;
w = gfx_img_w(src);
h = gfx_img_h(src);
for (i = 0; i < ag->nr_frames; i ++) {
SDL_Surface *s = rotozoomSurface(ag->frames[i].surface, angle, 1.0, 11);
if (i)
SDL_FreeSurface(ag->frames[i].surface);
ag->frames[i].surface = s;
x = (float)(ag->frames[i].x) - w / 2;
y = (float)(ag->frames[i].y) - h / 2;
x1 = x*cosf(rangle) - y*sinf(rangle);
y1 = y*cosf(rangle) + x*sinf(rangle);
ag->frames[i].x = x1 + w / 2;
ag->frames[i].y = y1 + h / 2;
}
return ag->frames[0].surface;
}
return (img_t)rotozoomSurface(Surf(src), angle, 1.0, 1);
}
#define FN_REG 0
#define FN_BOLD 1
#define FN_ITALIC 2

View file

@ -86,6 +86,8 @@ extern void gfx_set_alpha(img_t src, int alpha);
extern img_t gfx_alpha_img(img_t src, int alpha);
extern img_t gfx_display_alpha(img_t src);
extern img_t gfx_scale(img_t src, float xscale, float yscale);
extern img_t gfx_rotate(img_t src, float angle);
extern void gfx_draw_bg(img_t p, int x, int y, int width, int height);
extern void gfx_draw_from(img_t p, int x, int y, int width, int height, img_t to, int xx, int yy);
extern void gfx_cursor(int *xp, int *yp);

View file

@ -972,6 +972,7 @@ static int luaB_scale_sprite(lua_State *L) {
return 0;
img2 = gfx_scale(s, xs, ys);
if (!img2)
return 0;
@ -992,6 +993,45 @@ err:
}
static int luaB_rotate_sprite(lua_State *L) {
_spr_t *sp;
img_t s;
img_t img2 = NULL;
const char *key;
char sname[sizeof(unsigned long) * 2 + 16];
const char *src = luaL_optstring(L, 1, NULL);
float angle = luaL_optnumber(L, 2, 1.0f);
const char *desc = luaL_optstring(L, 3, NULL);
if (!src)
return 0;
s = cache_lookup(gfx_image_cache(), src);
if (!s)
return 0;
img2 = gfx_rotate(s, angle);
if (!img2)
return 0;
if (!desc || sprite_lookup(desc)) {
key = sname;
sprite_name(src, sname, sizeof(sname));
} else
key = desc;
sp = sprite_new(key, img2);
if (!sp)
goto err;
lua_pushstring(L, sname);
return 1;
err:
gfx_free_image(img2);
return 0;
}
static int luaB_fill_sprite(lua_State *L) {
img_t d;
float v;
@ -1092,6 +1132,7 @@ static const luaL_Reg base_funcs[] = {
{"sprite_alpha", luaB_alpha_sprite},
{"sprite_size", luaB_sprite_size},
{"sprite_scale", luaB_scale_sprite},
{"sprite_rotate", luaB_rotate_sprite},
{"sprite_text_size", luaB_text_size},
{NULL, NULL}
};

View file

@ -29,6 +29,9 @@ sprite = {
scale = function(name, xs, ys)
return sprite_scale(name, xs, ys);
end;
rotate = function(name, angle)
return sprite_rotate(name, angle);
end;
text = function(font, text, col, style)
return sprite_text(font, text, col, style);
end;