Adding animations library (incomplete)

This commit is contained in:
emb
2015-11-25 07:08:02 -06:00
parent 9a4212907c
commit 7fa6c6209f
21 changed files with 321 additions and 535 deletions

View File

@@ -98,11 +98,16 @@ set(RETROFE_HEADERS
"${RETROFE_DIR}/Source/Database/DB.h" "${RETROFE_DIR}/Source/Database/DB.h"
"${RETROFE_DIR}/Source/Database/Metadata.h" "${RETROFE_DIR}/Source/Database/Metadata.h"
"${RETROFE_DIR}/Source/Database/MetadataDatabase.h" "${RETROFE_DIR}/Source/Database/MetadataDatabase.h"
"${RETROFE_DIR}/Source/Graphics/Animate/Animation.h"
"${RETROFE_DIR}/Source/Graphics/Animate/AnimationChain.h"
"${RETROFE_DIR}/Source/Graphics/Animate/AnimationManager.h"
"${RETROFE_DIR}/Source/Graphics/Animate/Tween.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/Graphics/Component/ComponentData.h"
"${RETROFE_DIR}/Source/Graphics/Component/ComponentFactory.h" "${RETROFE_DIR}/Source/Graphics/Component/ComponentFactory.h"
"${RETROFE_DIR}/Source/Lua/Lua.h" "${RETROFE_DIR}/Source/Lua/Lua.h"
"${RETROFE_DIR}/Source/Lua/LuaAnimate.h"
"${RETROFE_DIR}/Source/Lua/LuaCollection.h" "${RETROFE_DIR}/Source/Lua/LuaCollection.h"
"${RETROFE_DIR}/Source/Lua/LuaDisplay.h" "${RETROFE_DIR}/Source/Lua/LuaDisplay.h"
"${RETROFE_DIR}/Source/Lua/LuaEvent.h" "${RETROFE_DIR}/Source/Lua/LuaEvent.h"
@@ -121,12 +126,16 @@ set(RETROFE_SOURCES
"${RETROFE_DIR}/Source/Database/Configuration.cpp" "${RETROFE_DIR}/Source/Database/Configuration.cpp"
"${RETROFE_DIR}/Source/Database/DB.cpp" "${RETROFE_DIR}/Source/Database/DB.cpp"
"${RETROFE_DIR}/Source/Database/MetadataDatabase.cpp" "${RETROFE_DIR}/Source/Database/MetadataDatabase.cpp"
"${RETROFE_DIR}/Source/Graphics/Animate/Tween.cpp"
"${RETROFE_DIR}/Source/Graphics/Animate/AnimationChain.cpp"
"${RETROFE_DIR}/Source/Graphics/Animate/AnimationManager.cpp"
"${RETROFE_DIR}/Source/Graphics/Component/Image.cpp" "${RETROFE_DIR}/Source/Graphics/Component/Image.cpp"
"${RETROFE_DIR}/Source/Graphics/Component/Component.cpp" "${RETROFE_DIR}/Source/Graphics/Component/Component.cpp"
"${RETROFE_DIR}/Source/Graphics/Component/ComponentFactory.cpp" "${RETROFE_DIR}/Source/Graphics/Component/ComponentFactory.cpp"
"${RETROFE_DIR}/Source/Utility/Log.cpp" "${RETROFE_DIR}/Source/Utility/Log.cpp"
"${RETROFE_DIR}/Source/Utility/Utils.cpp" "${RETROFE_DIR}/Source/Utility/Utils.cpp"
"${RETROFE_DIR}/Source/Lua/Lua.cpp" "${RETROFE_DIR}/Source/Lua/Lua.cpp"
"${RETROFE_DIR}/Source/Lua/LuaAnimate.cpp"
"${RETROFE_DIR}/Source/Lua/LuaDisplay.cpp" "${RETROFE_DIR}/Source/Lua/LuaDisplay.cpp"
"${RETROFE_DIR}/Source/Lua/LuaImage.cpp" "${RETROFE_DIR}/Source/Lua/LuaImage.cpp"
"${RETROFE_DIR}/Source/Lua/LuaCollection.cpp" "${RETROFE_DIR}/Source/Lua/LuaCollection.cpp"

View File

@@ -1,68 +0,0 @@
/* This file is part of RetroFE.
*
* RetroFE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RetroFE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RetroFE. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Animation.h"
#include <string>
Animation::Animation()
{
}
Animation::Animation(Animation &copy)
{
for(std::vector<TweenSet *>::iterator it = copy.animationVector_.begin(); it != copy.animationVector_.end(); it++)
{
Push(new TweenSet(**it));
}
}
Animation::~Animation()
{
Clear();
}
void Animation::Push(TweenSet *set)
{
animationVector_.push_back(set);
}
void Animation::Clear()
{
std::vector<TweenSet *>::iterator it = animationVector_.begin();
while(it != animationVector_.end())
{
delete *it;
animationVector_.erase(it);
it = animationVector_.begin();
}
animationVector_.clear();
}
std::vector<TweenSet *> *Animation::tweenSets()
{
return &animationVector_;
}
TweenSet *Animation::tweenSet(unsigned int index)
{
return animationVector_[index];
}
unsigned int Animation::size()
{
return animationVector_.size();
}

View File

@@ -1,36 +1,15 @@
/* This file is part of RetroFE.
*
* RetroFE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RetroFE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RetroFE. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once #pragma once
#include "../Component/Component.h"
#include "TweenSet.h" #include "../Component/ComponentData.h"
#include "TweenTypes.h"
#include <string> #include <string>
#include <vector>
#include <map>
class Animation class Animation {
{
public: public:
Animation(); Component *component;
Animation(Animation &copy); ComponentData start;
~Animation(); ComponentData end;
void Push(TweenSet *set); float elapsedTime;
void Clear(); float duration;
std::vector<TweenSet *> *tweenSets(); TweenAlgorithm algorithm;
TweenSet *tweenSet(unsigned int index); };
unsigned int size();
private:
std::vector<TweenSet *> animationVector_;
};

View File

@@ -0,0 +1,17 @@
#include "AnimationChain.h"
AnimationChain::AnimationChain()
: index(0)
, loop(false)
, loopCount(0)
, autoDestroy(false)
{
}
AnimationChain::~AnimationChain() {
for (AnimationList_T::iterator it = animations.begin(); it != animations.end(); it++) {
delete *it;
}
animations.clear();
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include "Animation.h"
#include <vector>
class AnimationChain {
public:
AnimationChain();
virtual ~AnimationChain();
typedef std::vector<Animation *> AnimationList_T;
typedef AnimationList_T::iterator AnimationListIt_T;
AnimationList_T animations;
unsigned int index;
bool loop;
unsigned int loopCount;
bool autoDestroy;
};

View File

@@ -1,95 +0,0 @@
/* This file is part of RetroFE.
*
* RetroFE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RetroFE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RetroFE. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AnimationEvents.h"
#include <string>
AnimationEvents::AnimationEvents()
{
}
AnimationEvents::AnimationEvents(AnimationEvents &copy)
{
for(std::map<std::string, std::map<int , Animation *> >::iterator it = copy.animationMap_.begin(); it != copy.animationMap_.end(); it++)
{
for(std::map<int, Animation *>::iterator it2 = (it->second).begin(); it2 != (it->second).end(); it2++)
{
Animation *t = new Animation(*it2->second);
animationMap_[it->first][it2->first] = t;
}
}
}
AnimationEvents::~AnimationEvents()
{
clear();
}
Animation *AnimationEvents::getAnimation(std::string tween)
{
return getAnimation(tween, -1);
}
Animation *AnimationEvents::getAnimation(std::string tween, int index)
{
if(animationMap_.find(tween) == animationMap_.end())
{
animationMap_[tween][-1] = new Animation();
}
if(animationMap_[tween].find(index) == animationMap_[tween].end())
{
index = -1;
if(animationMap_[tween].find(index) == animationMap_[tween].end())
{
animationMap_[tween][index] = new Animation();
}
}
return animationMap_[tween][index];
}
void AnimationEvents::setAnimation(std::string tween, int index, Animation *animation)
{
animationMap_[tween][index] = animation;
}
void AnimationEvents::clear()
{
std::map<std::string, std::map<int, Animation *> >::iterator it = animationMap_.begin();
while(it != animationMap_.end())
{
std::map<int, Animation *>::iterator it2 = (it->second).begin();
while(it2 != (it->second).end())
{
delete it2->second;
(it->second).erase(it2);
}
(it->second).clear();
animationMap_.erase(it);
it = animationMap_.begin();
}
animationMap_.clear();
}

View File

@@ -1,38 +0,0 @@
/* This file is part of RetroFE.
*
* RetroFE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RetroFE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RetroFE. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Tween.h"
#include "Animation.h"
#include <string>
#include <vector>
#include <map>
class AnimationEvents
{
public:
AnimationEvents();
AnimationEvents(AnimationEvents &copy);
~AnimationEvents();
Animation *getAnimation(std::string tween);
Animation *getAnimation(std::string tween, int index);
void setAnimation(std::string tween, int index, Animation *animation);
void clear();
private:
std::map<std::string, std::map<int, Animation *> > animationMap_;
};

View File

@@ -0,0 +1,118 @@
#include "AnimationManager.h"
#include "Tween.h"
AnimationManager::~AnimationManager()
{
AnimationChainsIt_T it = chains.begin();
while(it != chains.end())
{
delete it->second;
chains.erase(it);
it = chains.begin();
}
}
void AnimationManager::destroyChain(AnimationChain *chain)
{
AnimationChainsIt_T it = chains.find(chain);
if(it != chains.end())
{
delete chain;
chains.erase(it);
}
}
void AnimationManager::update(float dt)
{
for(AnimationChainsIt_T it = chains.begin(); it != chains.end(); )
{
AnimationChain *chain = it->first;
AnimationChainsIt_T next = it;
next++;
if(updateChain(dt, chain))
{
chain->loopCount++;
// reset the counter to continue iterating
if(chain->loop) {
chain->index = 0;
}
if(chain->autoDestroy)
{
destroyChain(chain);
}
}
it = next;
}
}
bool AnimationManager::updateChain(float dt, AnimationChain *chain)
{
if(chain->index >= chain->animations.size()) return true;
Animation *a = chain->animations[chain->index];
if(a->elapsedTime == 0) {
a->start = a->component->info;
}
a->elapsedTime += dt;
if(a->elapsedTime < a->duration) {
ComponentData *endInfo = &a->end;
ComponentData *startInfo = &a->start;
if(endInfo->isMaskSet(COMPONENT_DATA_X_MASK)) {
endInfo->x = (int)Tween::calculate(a->algorithm, a->elapsedTime, a->duration, startInfo->x, endInfo->x - startInfo->x);
}
if(endInfo->isMaskSet(COMPONENT_DATA_Y_MASK)) {
endInfo->y = (int)Tween::calculate(a->algorithm, a->elapsedTime, a->duration, startInfo->y, endInfo->y - startInfo->y);
}
if(endInfo->isMaskSet(COMPONENT_DATA_ALPHA_MASK)) {
endInfo->alpha = (float)Tween::calculate(a->algorithm, a->elapsedTime, a->duration, startInfo->alpha, endInfo->alpha - startInfo->alpha);
}
if(endInfo->isMaskSet(COMPONENT_DATA_ROTATE_MASK)) {
endInfo->rotate = (float)Tween::calculate(a->algorithm, a->elapsedTime, a->duration, startInfo->rotate, endInfo->rotate - startInfo->rotate);
}
if(endInfo->isMaskSet(COMPONENT_DATA_WIDTH_MASK)) {
endInfo->width = (int)Tween::calculate(a->algorithm, a->elapsedTime, a->duration, startInfo->width, endInfo->width - startInfo->width);
}
if(endInfo->isMaskSet(COMPONENT_DATA_HEIGHT_MASK)) {
endInfo->height = (int)Tween::calculate(a->algorithm, a->elapsedTime, a->duration, startInfo->height, endInfo->height - startInfo->height);
}
}
else {
a->elapsedTime = 0;
chain->index++;
}
return (chain->index >= chain->animations.size());
}
AnimationChain *AnimationManager::start(Animation *a, bool loop, bool autoDestroy)
{
return startChain(a, 1, loop, autoDestroy);
}
AnimationChain *AnimationManager::startChain(Animation *list, unsigned int size, bool loop, bool autoDestroy)
{
AnimationChain *chain = new AnimationChain();
for(unsigned int i = 0; i < size; ++i) {
chain->animations.push_back(list);
list++;
}
chain->index = 0;
chain->loop = loop;
chain->autoDestroy = autoDestroy;
chains[chain] = chain;
return chain;
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include "Animation.h"
#include "AnimationChain.h"
#include <map>
class AnimationManager {
public:
virtual ~AnimationManager();
void destroyChain(AnimationChain *c);
void update(float dt);
bool updateChain(float dt, AnimationChain *chain);
AnimationChain *start(Animation *a, bool loop, bool autoDestroy);
AnimationChain *startChain(Animation *list, unsigned int size, bool loop, bool autoDestroy);
private:
typedef std::map<AnimationChain *, AnimationChain *> AnimationChains_T;
typedef AnimationChains_T::iterator AnimationChainsIt_T;
AnimationChains_T chains;
};

View File

@@ -20,48 +20,6 @@
#include <string> #include <string>
std::map<std::string, TweenAlgorithm> Tween::tweenTypeMap_; std::map<std::string, TweenAlgorithm> Tween::tweenTypeMap_;
std::map<std::string, TweenProperty> Tween::tweenPropertyMap_;
Tween::Tween(TweenProperty property, TweenAlgorithm type, double start, double end, double duration)
: property(property)
, duration(duration)
, type(type)
, start(start)
, end(end)
{
}
bool Tween::getTweenProperty(std::string name, TweenProperty &property)
{
bool retVal = false;
if(tweenPropertyMap_.size() == 0)
{
tweenPropertyMap_["x"] = TWEEN_PROPERTY_X;
tweenPropertyMap_["y"] = TWEEN_PROPERTY_Y;
tweenPropertyMap_["angle"] = TWEEN_PROPERTY_ANGLE;
tweenPropertyMap_["alpha"] = TWEEN_PROPERTY_ALPHA;
tweenPropertyMap_["width"] = TWEEN_PROPERTY_WIDTH;
tweenPropertyMap_["height"] = TWEEN_PROPERTY_HEIGHT;
tweenPropertyMap_["xorigin"] = TWEEN_PROPERTY_X_ORIGIN;
tweenPropertyMap_["yorigin"] = TWEEN_PROPERTY_Y_ORIGIN;
tweenPropertyMap_["xoffset"] = TWEEN_PROPERTY_X_OFFSET;
tweenPropertyMap_["yoffset"] = TWEEN_PROPERTY_Y_OFFSET;
tweenPropertyMap_["fontSize"] = TWEEN_PROPERTY_FONT_SIZE;
tweenPropertyMap_["backgroundalpha"] = TWEEN_PROPERTY_BACKGROUND_ALPHA;
}
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
if(tweenPropertyMap_.find(name) != tweenPropertyMap_.end())
{
property = tweenPropertyMap_[name];
retVal = true;
}
return retVal;
}
TweenAlgorithm Tween::getTweenType(std::string name) TweenAlgorithm Tween::getTweenType(std::string name)
@@ -105,13 +63,7 @@ TweenAlgorithm Tween::getTweenType(std::string name)
} }
float Tween::animate(double elapsedTime) float Tween::calculate(TweenAlgorithm type, double start, double end, double duration, double elapsedTime)
{
return animateSingle(type, start, end, duration, elapsedTime);
}
//todo: SDL likes floats, consider having casting being performed elsewhere
float Tween::animateSingle(TweenAlgorithm type, double start, double end, double duration, double elapsedTime)
{ {
double a = start; double a = start;
double b = end - start; double b = end - start;

View File

@@ -25,13 +25,8 @@ class Tween
{ {
public: public:
Tween(TweenProperty name, TweenAlgorithm type, double start, double end, double duration);
float animate(double elapsedTime);
static float animateSingle(TweenAlgorithm type, double start, double end, double duration, double elapsedTime);
static TweenAlgorithm getTweenType(std::string name); static TweenAlgorithm getTweenType(std::string name);
static bool getTweenProperty(std::string name, TweenProperty &property); static float calculate(TweenAlgorithm type, double start, double end, double duration, double elapsedTime);
TweenProperty property;
double duration;
private: private:
static double easeInQuadratic(double elapsedTime, double duration, double b, double c); static double easeInQuadratic(double elapsedTime, double duration, double b, double c);
@@ -56,10 +51,5 @@ private:
static double easeOutCircular(double elapsedTime, double duration, double b, double c); static double easeOutCircular(double elapsedTime, double duration, double b, double c);
static double easeInOutCircular(double elapsedTime, double duration, double b, double c); static double easeInOutCircular(double elapsedTime, double duration, double b, double c);
static double linear(double elapsedTime, double duration, double b, double c); static double linear(double elapsedTime, double duration, double b, double c);
static std::map<std::string, TweenAlgorithm> tweenTypeMap_; static std::map<std::string, TweenAlgorithm> tweenTypeMap_;
static std::map<std::string, TweenProperty> tweenPropertyMap_;
TweenAlgorithm type;
double start;
double end;
}; };

View File

@@ -1,65 +0,0 @@
/* This file is part of RetroFE.
*
* RetroFE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RetroFE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RetroFE. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TweenSet.h"
TweenSet::TweenSet()
{
}
TweenSet::TweenSet(TweenSet &copy)
{
for(std::vector<Tween *>::iterator it = copy.set_.begin(); it != copy.set_.end(); it++)
{
Tween *t = new Tween(**it);
set_.push_back(t);
}
}
TweenSet::~TweenSet()
{
clear();
}
void TweenSet::push(Tween *tween)
{
set_.push_back(tween);
}
void TweenSet::clear()
{
std::vector<Tween *>::iterator it = set_.begin();
while(it != set_.end())
{
delete *it;
set_.erase(it);
it = set_.begin();
}
}
std::vector<Tween *> *TweenSet::tweens()
{
return &set_;
}
Tween *TweenSet::getTween(unsigned int index)
{
return set_[index];
}
unsigned int TweenSet::size()
{
return set_.size();
}

View File

@@ -1,35 +0,0 @@
/* This file is part of RetroFE.
*
* RetroFE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RetroFE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RetroFE. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Tween.h"
#include <vector>
class TweenSet
{
public:
TweenSet();
TweenSet(TweenSet &copy);
~TweenSet();
void push(Tween * tween);
void clear();
std::vector<Tween *> *tweens();
Tween *getTween(unsigned int index);
unsigned int size();
private:
std::vector<Tween *> set_;
};

View File

@@ -18,85 +18,10 @@
#include "../../SDL.h" #include "../../SDL.h"
Component::Component() Component::Component()
: startInfo(&info)
, endInfo(&info)
, elapsedTime(0)
, loop(false)
, start(false)
{ {
} }
void Component::addAnimation(const ComponentData &newInfo)
{
start = false;
animations.push_back(newInfo);
elapsedTime = 0;
}
void Component::animate(bool loop)
{
this->loop = loop;
start = true;
elapsedTime = 0;
animationIndex = -1;
startInfo = &info;
endInfo = &info;
if(animations.size() > 0)
{
endInfo = &animations[0];
}
}
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) void Component::update(float dt)
{ {
elapsedTime += dt;
bool done = false;
while(elapsedTime && endInfo->duration && 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) {
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

@@ -24,16 +24,5 @@ public:
Component(); Component();
virtual void update(float dt); 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 info;
ComponentData *endInfo;
private:
std::vector<ComponentData> animations;
unsigned int animationIndex;
float elapsedTime;
bool loop;
bool start;
}; };

View File

@@ -0,0 +1,93 @@
#include "LuaAnimate.h"
#include "../Utility/Log.h"
#include "../Utility/Utils.h"
#include "../Graphics/Component/Component.h"
#include "../Graphics/Animate/Animation.h"
#include "../Graphics/Animate/TweenTypes.h"
#include "../Graphics/Animate/Tween.h"
AnimationManager *manager;
void LuaAnimate::initialize(AnimationManager *m)
{
manager = m;
}
template <class T>
static void BuildTableComponentInfo(lua_State *l, std::string name, unsigned char mask, ComponentData &newInfo, T &newParam) {
lua_pushstring (l, name.c_str());
lua_gettable(l, -2);
if(!lua_isnil(l, -1))
{
newInfo.setMask(mask);
newParam = (T)luaL_checknumber(l, -1);
}
lua_pop(l, 1);
}
Animation *LuaAnimate::create(lua_State *l)
{
if (!lua_istable(l, 3)) { return NULL; }
//todo: make sure it is a component
Animation *a = new Animation();
a->end.clearMask(COMPONENT_DATA_ALL_MASK);
BuildTableComponentInfo<int>(l, "x", COMPONENT_DATA_X_MASK, a->end, a->end.x);
BuildTableComponentInfo<int>(l, "y", COMPONENT_DATA_Y_MASK, a->end, a->end.y);
BuildTableComponentInfo<int>(l, "height", COMPONENT_DATA_Y_MASK, a->end, a->end.height);
BuildTableComponentInfo<int>(l, "width", COMPONENT_DATA_Y_MASK, a->end, a->end.width);
BuildTableComponentInfo<float>(l, "rotate", COMPONENT_DATA_ROTATE_MASK, a->end, a->end.rotate);
BuildTableComponentInfo<float>(l, "alpha", COMPONENT_DATA_ROTATE_MASK, a->end, a->end.alpha);
lua_pushstring (l, "duration");
lua_gettable(l, -2);
if(!lua_isnil(l, -1))
{
a->duration = (float)luaL_checknumber(l, -1);
}
lua_pop(l, 1);
a->algorithm = TweenAlgorithm::LINEAR;
lua_pushstring (l, "algorithm");
lua_gettable(l, -2);
if(!lua_isnil(l, -1))
{
a->algorithm = Tween::getTweenType(luaL_checkstring(l, -1));
}
lua_pop(l, 1);
return a;
}
//todo: NEED MEMORY TYPE CHECKING LIKE OTHER LUA CLASSES
int LuaAnimate::start(lua_State *l)
{
Component *component = (Component *)lua_tointeger(l, 1) ;
bool loop = (lua_toboolean(l, 2) != 0);
//todo: make sure it is a component
Animation *a = (Animation *)create(l);
a->elapsedTime = 0;
if(a) {
a->component = component;
AnimationChain *chain = manager->start(a, loop, false);
lua_pushinteger(l, (int)chain);
}
return 1;
}
int LuaAnimate::startChain(lua_State *l)
{
return 0;
}
int LuaAnimate::destroy(lua_State *l)
{
Animation *a = (Animation *)luaL_checkinteger(l, 1);
delete a;
return 1;
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include "Lua.h"
#include "../Graphics/Animate/AnimationManager.h"
namespace LuaAnimate
{
void initialize(AnimationManager *m);
Animation *create(lua_State *l);
int start(lua_State *l);
int startChain(lua_State *l);
int destroy(lua_State *l);
};

View File

@@ -322,51 +322,3 @@ int LuaImage::setLayer(lua_State *l)
return 0; 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;
}
template <class T>
static void BuildTableComponentInfo(lua_State *l, std::string name, unsigned char mask, ComponentData &newInfo, T &newParam) {
lua_pushstring (l, name.c_str());
lua_gettable(l, -2);
if(!lua_isnil(l, -1))
{
newInfo.setMask(mask);
newParam = (T)luaL_checknumber(l, -1);
}
lua_pop(l, 1);
}
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);
BuildTableComponentInfo<int>(l, "x", COMPONENT_DATA_X_MASK, newInfo, newInfo.x);
BuildTableComponentInfo<int>(l, "y", COMPONENT_DATA_Y_MASK, newInfo, newInfo.y);
BuildTableComponentInfo<int>(l, "height", COMPONENT_DATA_Y_MASK, newInfo, newInfo.height);
BuildTableComponentInfo<int>(l, "width", COMPONENT_DATA_Y_MASK, newInfo, newInfo.width);
BuildTableComponentInfo<float>(l, "rotate", COMPONENT_DATA_ROTATE_MASK, newInfo, newInfo.rotate);
BuildTableComponentInfo<float>(l, "alpha", COMPONENT_DATA_ROTATE_MASK, newInfo, newInfo.alpha);
newInfo.duration = duration;
i->addAnimation(newInfo);
return 0;
}

View File

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

View File

@@ -21,6 +21,7 @@
#include "SDL.h" #include "SDL.h"
#include "Graphics/Component/Image.h" #include "Graphics/Component/Image.h"
#include "Graphics/Component/Component.h" #include "Graphics/Component/Component.h"
#include "Lua/LuaAnimate.h"
#include "Lua/LuaCollection.h" #include "Lua/LuaCollection.h"
#include "Lua/LuaDisplay.h" #include "Lua/LuaDisplay.h"
#include "Lua/LuaImage.h" #include "Lua/LuaImage.h"
@@ -51,6 +52,13 @@ static int lua_registerOnInit(lua_State *l)
return 0; return 0;
} }
const luaL_Reg RetroFE::luaAnimateFuncs[] = {
{"start", LuaAnimate::start},
{"startChain", LuaAnimate::startChain},
{"destroy", LuaAnimate::destroy},
{NULL, NULL}
};
const luaL_Reg RetroFE::luaImageFuncs[] = { const luaL_Reg RetroFE::luaImageFuncs[] = {
// Creation // Creation
{"create", LuaImage::create}, {"create", LuaImage::create},
@@ -75,8 +83,6 @@ const luaL_Reg RetroFE::luaImageFuncs[] = {
{"setDimensions", LuaImage::setDimensions}, {"setDimensions", LuaImage::setDimensions},
{"setRotate", LuaImage::setRotate}, {"setRotate", LuaImage::setRotate},
{"setAlpha", LuaImage::setAlpha}, {"setAlpha", LuaImage::setAlpha},
{"addAnimation", LuaImage::addAnimation},
{"animate", LuaImage::animate},
{"destroy", LuaImage::destroy}, {"destroy", LuaImage::destroy},
{NULL, NULL} {NULL, NULL}
}; };
@@ -110,6 +116,11 @@ void RetroFE::initializeLua()
lua_.initialize(); lua_.initialize();
LuaImage::initialize(&config_, factory_); LuaImage::initialize(&config_, factory_);
LuaCollection::initialize(&config_, cib_, &luaEvent_); LuaCollection::initialize(&config_, cib_, &luaEvent_);
LuaAnimate::initialize(&animationManager_);
lua_newtable(lua_.state);
luaL_setfuncs (lua_.state, luaAnimateFuncs, 0);
lua_pushvalue(lua_.state, -1);
lua_setglobal(lua_.state, "animate");
lua_newtable(lua_.state); lua_newtable(lua_.state);
luaL_setfuncs (lua_.state, luaCollectionFuncs, 0); luaL_setfuncs (lua_.state, luaCollectionFuncs, 0);
@@ -200,6 +211,8 @@ void RetroFE::run()
SDL_SetRenderDrawColor(SDL::getRenderer(), 0x0, 0x0, 0x00, 0xFF); SDL_SetRenderDrawColor(SDL::getRenderer(), 0x0, 0x0, 0x00, 0xFF);
SDL_RenderClear(SDL::getRenderer()); SDL_RenderClear(SDL::getRenderer());
animationManager_.update((float)deltaTime);
for(std::map<Component *, Component *>::iterator it = factory_.components.begin(); it != factory_.components.end(); it++) for(std::map<Component *, Component *>::iterator it = factory_.components.begin(); it != factory_.components.end(); it++)
{ {
it->second->update((float)deltaTime); it->second->update((float)deltaTime);

View File

@@ -14,7 +14,7 @@
* along with RetroFE. If not, see <http://www.gnu.org/licenses/>. * along with RetroFE. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include "Graphics/Animate/AnimationManager.h"
#include "Collection/CollectionInfoBuilder.h" #include "Collection/CollectionInfoBuilder.h"
#include "Database/DB.h" #include "Database/DB.h"
#include "Database/Configuration.h" #include "Database/Configuration.h"
@@ -38,10 +38,12 @@ private:
Configuration &config_; Configuration &config_;
Lua lua_; Lua lua_;
LuaEvent luaEvent_; LuaEvent luaEvent_;
static const luaL_Reg luaDisplayFuncs[]; AnimationManager animationManager_;
static const luaL_Reg luaLogFuncs[]; static const luaL_Reg luaAnimateFuncs[];
static const luaL_Reg luaImageFuncs[];
static const luaL_Reg luaCollectionFuncs[]; static const luaL_Reg luaCollectionFuncs[];
static const luaL_Reg luaDisplayFuncs[];
static const luaL_Reg luaImageFuncs[];
static const luaL_Reg luaLogFuncs[];
DB db_; DB db_;
MetadataDatabase *mdb_; MetadataDatabase *mdb_;
CollectionInfoBuilder *cib_; CollectionInfoBuilder *cib_;