Support for image animations.

This commit is contained in:
emb
2015-11-17 21:57:52 -06:00
parent 4e7b696730
commit 2621fb7c5f
5 changed files with 105 additions and 65 deletions

View File

@@ -75,12 +75,24 @@ void Component::update(float dt)
}
if(start) {
info.x = (int)linear(elapsedTime, endInfo->duration, startInfo->x, endInfo->x - startInfo->x);
info.y = (int)linear(elapsedTime, endInfo->duration, startInfo->y, endInfo->y - startInfo->y);
info.alpha = (float)linear(elapsedTime, endInfo->duration, startInfo->alpha, endInfo->alpha - startInfo->alpha);
info.rotate = (float)linear(elapsedTime, endInfo->duration, startInfo->rotate, endInfo->rotate - startInfo->rotate);
info.width = (int)linear(elapsedTime, endInfo->duration, startInfo->width, endInfo->width - startInfo->width);
info.height = (int)linear(elapsedTime, endInfo->height, startInfo->height, endInfo->height - startInfo->height);
if(endInfo->isMaskSet(COMPONENT_DATA_X_MASK)) {
info.x = (int)linear(elapsedTime, endInfo->duration, startInfo->x, endInfo->x - startInfo->x);
}
if(endInfo->isMaskSet(COMPONENT_DATA_Y_MASK)) {
info.y = (int)linear(elapsedTime, endInfo->duration, startInfo->y, endInfo->y - startInfo->y);
}
if(endInfo->isMaskSet(COMPONENT_DATA_ALPHA_MASK)) {
info.alpha = (float)linear(elapsedTime, endInfo->duration, startInfo->alpha, endInfo->alpha - startInfo->alpha);
}
if(endInfo->isMaskSet(COMPONENT_DATA_ROTATE_MASK)) {
info.rotate = (float)linear(elapsedTime, endInfo->duration, startInfo->rotate, endInfo->rotate - startInfo->rotate);
}
if(endInfo->isMaskSet(COMPONENT_DATA_WIDTH_MASK)) {
info.width = (int)linear(elapsedTime, endInfo->duration, startInfo->width, endInfo->width - startInfo->width);
}
if(endInfo->isMaskSet(COMPONENT_DATA_HEIGHT_MASK)) {
info.height = (int)linear(elapsedTime, endInfo->duration, startInfo->height, endInfo->height - startInfo->height);
}
if(!loop && done) {
start = false;

View File

@@ -1,24 +1,43 @@
#pragma once
#define COMPONENT_DATA_X_MASK 0x1
#define COMPONENT_DATA_Y_MASK 0x2
#define COMPONENT_DATA_WIDTH_MASK 0x4
#define COMPONENT_DATA_HEIGHT_MASK 0x8
#define COMPONENT_DATA_ROTATE_MASK 0x10
#define COMPONENT_DATA_ALPHA_MASK 0x10
#define COMPONENT_DATA_DURATION_MASK 0x20
#define COMPONENT_DATA_LAYER_MASK 0x20
#define COMPONENT_DATA_ALL_MASK 0xFF
class ComponentData
{
public:
ComponentData()
: width(0)
, height(0)
, x(0)
: x(0)
, y(0)
, width(0)
, height(0)
, rotate(0)
, alpha(1)
, duration(0)
, layer(0)
, mask(COMPONENT_DATA_ALL_MASK)
{
}
int width;
int height;
public:
int x;
int y;
int width;
int height;
float rotate;
float alpha;
float duration;
float duration;
int layer;
};
void setMask(unsigned char flag) { mask |= flag; }
void clearMask(unsigned char flag) { mask &= ~flag; }
bool isMaskSet(unsigned char flag) { return ((mask & flag) != 0); }
private:
unsigned char mask;
};

View File

@@ -295,3 +295,59 @@ int LuaImage::setLayer(lua_State *l)
return 0;
}
int LuaImage::animate(lua_State *l)
{
Image *i = (Image *)lua_tointeger(l, 1);
bool loop = (lua_toboolean(l, 2) != 0);
i->animate(loop);
return 0;
}
int LuaImage::addAnimation(lua_State *l)
{
Image *i = (Image *)lua_tointeger(l, 1);
if(!i) return 0;
float duration = (float)lua_tonumber(l, 2);
if (!lua_istable(l, 3)) {
return 0;
}
ComponentData newInfo;
newInfo.clearMask(COMPONENT_DATA_ALL_MASK);
lua_pushstring (l, "x");
lua_gettable(l, -2);
if(!lua_isnil(l, -1))
{
newInfo.setMask(COMPONENT_DATA_X_MASK);
newInfo.x = (int)luaL_optinteger(l, -1, i->info.x);
}
lua_pop(l, 1);
lua_pushstring (l, "y");
lua_gettable(l, -2);
if(!lua_isnil(l, -1))
{
newInfo.setMask(COMPONENT_DATA_Y_MASK);
newInfo.y = (int)luaL_optinteger(l, -1, i->info.y);
}
lua_pop(l, 1);
lua_pushstring (l, "alpha");
lua_gettable(l, -2);
if(!lua_isnil(l, -1))
{
newInfo.setMask(COMPONENT_DATA_ALPHA_MASK);
newInfo.alpha = (float)luaL_optnumber(l, -1, i->info.alpha);
}
lua_pop(l, 1);
newInfo.duration = duration;
i->addAnimation(newInfo);
return 0;
}

View File

@@ -35,4 +35,6 @@ namespace LuaImage
int setAlpha(lua_State *l);
int setLayer(lua_State *l);
int setHeight(lua_State *l);
int animate(lua_State *l);
int addAnimation(lua_State *l);
};

View File

@@ -47,50 +47,6 @@ static int lua_registerOnInit(lua_State *l)
// events.registerOnInit(function);
return 0;
}
#if 0
static int lua_imageAnimate(lua_State *l)
{
Image *i = (Image *)lua_tointeger(l, 1);
bool loop = (lua_toboolean(l, 2) != 0);
i->animate(loop);
return 0;
}
static int lua_imageAddAnimation(lua_State *l)
{
// int argc = lua_gettop(l);
Image *i = (Image *)lua_tointeger(l, 1);
if(!i) return 0;
float duration = (float)lua_tonumber(l, 2);
if (!lua_istable(l, 3)) {
return 0;
}
ComponentData newInfo;
lua_pushstring (l, "x");
lua_gettable(l, -2);
newInfo.x = (int)luaL_optinteger(l, -1, i->info.x);
lua_pop(l, 1);
lua_pushstring (l, "y");
lua_gettable(l, -2);
newInfo.y = (int)luaL_optinteger(l, -1, i->info.y);
lua_pop(l, 1);
lua_pushstring (l, "alpha");
lua_gettable(l, -2);
newInfo.alpha = (float)luaL_optnumber(l, -1, i->info.alpha);
lua_pop(l, 1);
newInfo.duration = duration;
i->addAnimation(newInfo);
return 0;
}
#endif
const luaL_Reg RetroFE::luaImageFuncs[] = {
// Creation
@@ -115,15 +71,10 @@ const luaL_Reg RetroFE::luaImageFuncs[] = {
{"setDimensions", LuaImage::setDimensions},
{"setRotate", LuaImage::setRotate},
{"setAlpha", LuaImage::setAlpha},
{"addAnimation", LuaImage::addAnimation},
{"animate", LuaImage::animate},
#if 0
{"delete", lua_imageDelete},
{"delete", lua_imageDelete},
{"setSize", lua_imageSetSize},
{"setRotate", lua_imageSetRotate},
{"setPosition", lua_imageSetPosition},
{"setAlpha", lua_imageSetAlpha},
{"addAnimation", lua_imageAddAnimation},
{"animate", lua_imageAnimate},
#endif
{NULL, NULL}
};