Concepting out animations

This commit is contained in:
emb
2015-11-09 23:42:39 -06:00
parent afc59c22fd
commit 9ca9d022c0
6 changed files with 157 additions and 27 deletions

View File

@@ -94,6 +94,7 @@ set(RETROFE_HEADERS
"${RETROFE_DIR}/Source/Database/Configuration.h" "${RETROFE_DIR}/Source/Database/Configuration.h"
"${RETROFE_DIR}/Source/Graphics/Component/Image.h" "${RETROFE_DIR}/Source/Graphics/Component/Image.h"
"${RETROFE_DIR}/Source/Graphics/Component/Component.h" "${RETROFE_DIR}/Source/Graphics/Component/Component.h"
"${RETROFE_DIR}/Source/Graphics/Component/ComponentData.h"
"${RETROFE_DIR}/Source/Lua.h" "${RETROFE_DIR}/Source/Lua.h"
"${RETROFE_DIR}/Source/RetroFE.h" "${RETROFE_DIR}/Source/RetroFE.h"
"${RETROFE_DIR}/Source/SDL.h" "${RETROFE_DIR}/Source/SDL.h"

View File

@@ -17,4 +17,65 @@
#include "../../Utility/Log.h" #include "../../Utility/Log.h"
#include "../../SDL.h" #include "../../SDL.h"
Component::Component()
: startInfo(&info)
, endInfo(&info)
, elapsedTime(0)
, loop(false)
, start(false)
{
}
void Component::addAnimation(const ComponentData &newInfo)
{
animations.push_back(newInfo);
startInfo = &info;
endInfo = &animations[0];
elapsedTime = 0;
animationIndex = 0;
start = false;
}
void Component::animate(bool loop)
{
this->loop = loop;
this->start = true;
}
double linear(double t, double d, double b, double c)
{
if(d == 0) return b;
return c*t/d + b;
}
void Component::update(float dt)
{
elapsedTime += dt;
bool done = false;
while(elapsedTime >= endInfo->duration) {
elapsedTime -= endInfo->duration;
// don't animate if no tweens exist
if(animations.size() == 0) {
startInfo = &info;
endInfo = &info;
}
else if(loop) {
animationIndex = (animationIndex + 1) % animations.size();
unsigned int nextAnimationIndex = (animationIndex + 1) % animations.size();
done = (animationIndex + 1 >= animations.size());
startInfo = &animations[animationIndex];
endInfo = &animations[nextAnimationIndex];
}
}
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);
if(!loop && done) {
start = false;
}
}
}

View File

@@ -15,9 +15,25 @@
*/ */
#pragma once #pragma once
#include "ComponentData.h"
#include <vector>
class Component class Component
{ {
public: public:
virtual void update(float dt) = 0; Component();
virtual void update(float dt);
virtual void draw() = 0; virtual void draw() = 0;
virtual void addAnimation(const ComponentData &newInfo);
virtual void animate(bool loop);
ComponentData *startInfo;
ComponentData info;
ComponentData *endInfo;
private:
std::vector<ComponentData> animations;
unsigned int animationIndex;
float elapsedTime;
bool loop;
bool start;
}; };

View File

@@ -20,11 +20,6 @@
Image::Image(std::string file) Image::Image(std::string file)
: texture_(NULL) : texture_(NULL)
, file_(file) , file_(file)
, width(0)
, height(0)
, x(0)
, y(0)
, alpha(1)
{ {
} }
@@ -42,7 +37,7 @@ void Image::Initialize()
if (texture_ != NULL) if (texture_ != NULL)
{ {
SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND);
SDL_QueryTexture(texture_, NULL, NULL, &width, &height); SDL_QueryTexture(texture_, NULL, NULL, &info.width, &info.height);
} }
SDL_UnlockMutex(SDL::getMutex()); SDL_UnlockMutex(SDL::getMutex());
} }
@@ -59,6 +54,8 @@ void Image::DeInitialize()
void Image::update(float dt) void Image::update(float dt)
{ {
Component::update(dt);
if(!texture_) if(!texture_)
{ {
SDL_LockMutex(SDL::getMutex()); SDL_LockMutex(SDL::getMutex());
@@ -67,7 +64,7 @@ void Image::update(float dt)
if (texture_ != NULL) if (texture_ != NULL)
{ {
SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(texture_, SDL_BLENDMODE_BLEND);
SDL_QueryTexture(texture_, NULL, NULL, &width, &height); SDL_QueryTexture(texture_, NULL, NULL, &info.width, &info.height);
} }
SDL_UnlockMutex(SDL::getMutex()); SDL_UnlockMutex(SDL::getMutex());
} }
@@ -79,11 +76,11 @@ void Image::draw()
{ {
SDL_Rect rect; SDL_Rect rect;
rect.x = x; rect.x = info.x;
rect.y = y; rect.y = info.y;
rect.h = width; rect.h = info.width;
rect.w = height; rect.w = info.height;
SDL::renderCopy(texture_, (unsigned char)(alpha*255), NULL, &rect, 45); SDL::renderCopy(texture_, (unsigned char)(info.alpha*255), NULL, &rect, 45);
} }
} }

View File

@@ -28,12 +28,6 @@ public:
void DeInitialize(); void DeInitialize();
void update(float dt); void update(float dt);
void draw(); void draw();
int width;
int height;
int x;
int y;
int rotate;
float alpha;
protected: protected:
SDL_Texture *texture_; SDL_Texture *texture_;

View File

@@ -82,29 +82,72 @@ static int lua_imageDelete(lua_State *l)
static int lua_imageSetSize(lua_State *l) static int lua_imageSetSize(lua_State *l)
{ {
Image *i = (Image *)lua_tointeger(l, 1); Image *i = (Image *)lua_tointeger(l, 1);
i->width = (int)lua_tointeger(l, 2); i->info.width = (int)lua_tointeger(l, 2);
i->height = (int)lua_tointeger(l, 3); i->info.height = (int)lua_tointeger(l, 3);
return 0; return 0;
} }
static int lua_imageSetPosition(lua_State *l) static int lua_imageSetPosition(lua_State *l)
{ {
Image *i = (Image *)lua_tointeger(l, 1); Image *i = (Image *)lua_tointeger(l, 1);
i->x = (int)lua_tointeger(l, 2); i->info.x = (int)lua_tointeger(l, 2);
i->y = (int)lua_tointeger(l, 3); i->info.y = (int)lua_tointeger(l, 3);
return 0; return 0;
} }
static int lua_imageSetRotate(lua_State *l) static int lua_imageSetRotate(lua_State *l)
{ {
Image *i = (Image *)lua_tointeger(l, 1); Image *i = (Image *)lua_tointeger(l, 1);
i->rotate = (int)lua_tointeger(l, 2); i->info.rotate = (float)lua_tonumber(l, 2);
return 0; return 0;
} }
static int lua_imageSetAlpha(lua_State *l) static int lua_imageSetAlpha(lua_State *l)
{ {
Image *i = (Image *)lua_tointeger(l, 1); Image *i = (Image *)lua_tointeger(l, 1);
i->alpha = (float)lua_tonumber(l, 2); i->info.alpha = (float)lua_tonumber(l, 2);
return 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; return 0;
} }
@@ -124,6 +167,8 @@ static const luaL_Reg luaImageFuncs[] = {
{"setRotate", lua_imageSetRotate}, {"setRotate", lua_imageSetRotate},
{"setPosition", lua_imageSetPosition}, {"setPosition", lua_imageSetPosition},
{"setAlpha", lua_imageSetAlpha}, {"setAlpha", lua_imageSetAlpha},
{"addAnimation", lua_imageAddAnimation},
{"animate", lua_imageAnimate},
{NULL, NULL} {NULL, NULL}
}; };
@@ -171,7 +216,9 @@ void RetroFE::run()
reloadLuaScripts(); reloadLuaScripts();
bool quit = false; bool quit = false;
double currentTime = 0;
double lastTime = 0;
double deltaTime = 0;
while(!quit) { while(!quit) {
SDL_LockMutex(SDL::getMutex()); SDL_LockMutex(SDL::getMutex());
@@ -180,7 +227,7 @@ void RetroFE::run()
for(std::map<Component *, Component *>::iterator it = components.begin(); it != components.end(); it++) for(std::map<Component *, Component *>::iterator it = components.begin(); it != components.end(); it++)
{ {
it->second->update(0); it->second->update((float)deltaTime);
} }
for(std::map<Component *, Component *>::iterator it = components.begin(); it != components.end(); it++) for(std::map<Component *, Component *>::iterator it = components.begin(); it != components.end(); it++)
@@ -191,6 +238,20 @@ void RetroFE::run()
SDL_RenderPresent(SDL::getRenderer()); SDL_RenderPresent(SDL::getRenderer());
SDL_UnlockMutex(SDL::getMutex()); SDL_UnlockMutex(SDL::getMutex());
lastTime = currentTime;
currentTime = static_cast<float>(SDL_GetTicks()) / 1000;
if (currentTime < lastTime)
{
currentTime = lastTime;
}
deltaTime = currentTime - lastTime;
double sleepTime = 1000.0/60.0 - deltaTime*1000;
if(sleepTime > 0)
{
SDL_Delay(static_cast<unsigned int>(sleepTime));
}
} }