diff --git a/RetroFE/Source/CMakeLists.txt b/RetroFE/Source/CMakeLists.txt index d350ede..5a749b5 100644 --- a/RetroFE/Source/CMakeLists.txt +++ b/RetroFE/Source/CMakeLists.txt @@ -98,11 +98,16 @@ set(RETROFE_HEADERS "${RETROFE_DIR}/Source/Database/DB.h" "${RETROFE_DIR}/Source/Database/Metadata.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/Component.h" "${RETROFE_DIR}/Source/Graphics/Component/ComponentData.h" "${RETROFE_DIR}/Source/Graphics/Component/ComponentFactory.h" "${RETROFE_DIR}/Source/Lua/Lua.h" + "${RETROFE_DIR}/Source/Lua/LuaAnimate.h" "${RETROFE_DIR}/Source/Lua/LuaCollection.h" "${RETROFE_DIR}/Source/Lua/LuaDisplay.h" "${RETROFE_DIR}/Source/Lua/LuaEvent.h" @@ -121,12 +126,16 @@ set(RETROFE_SOURCES "${RETROFE_DIR}/Source/Database/Configuration.cpp" "${RETROFE_DIR}/Source/Database/DB.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/Component.cpp" "${RETROFE_DIR}/Source/Graphics/Component/ComponentFactory.cpp" "${RETROFE_DIR}/Source/Utility/Log.cpp" "${RETROFE_DIR}/Source/Utility/Utils.cpp" "${RETROFE_DIR}/Source/Lua/Lua.cpp" + "${RETROFE_DIR}/Source/Lua/LuaAnimate.cpp" "${RETROFE_DIR}/Source/Lua/LuaDisplay.cpp" "${RETROFE_DIR}/Source/Lua/LuaImage.cpp" "${RETROFE_DIR}/Source/Lua/LuaCollection.cpp" diff --git a/RetroFE/Source/Graphics/Animate/Animation.cpp b/RetroFE/Source/Graphics/Animate/Animation.cpp deleted file mode 100644 index c55c302..0000000 --- a/RetroFE/Source/Graphics/Animate/Animation.cpp +++ /dev/null @@ -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 . - */ - -#include "Animation.h" -#include - -Animation::Animation() -{ -} - -Animation::Animation(Animation ©) -{ - for(std::vector::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::iterator it = animationVector_.begin(); - while(it != animationVector_.end()) - { - delete *it; - animationVector_.erase(it); - it = animationVector_.begin(); - } - - animationVector_.clear(); -} - -std::vector *Animation::tweenSets() -{ - return &animationVector_; -} - -TweenSet *Animation::tweenSet(unsigned int index) -{ - return animationVector_[index]; -} - - -unsigned int Animation::size() -{ - return animationVector_.size(); -} diff --git a/RetroFE/Source/Graphics/Animate/Animation.h b/RetroFE/Source/Graphics/Animate/Animation.h index d2f2254..dec1c16 100644 --- a/RetroFE/Source/Graphics/Animate/Animation.h +++ b/RetroFE/Source/Graphics/Animate/Animation.h @@ -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 . - */ #pragma once - -#include "TweenSet.h" +#include "../Component/Component.h" +#include "../Component/ComponentData.h" +#include "TweenTypes.h" #include -#include -#include -class Animation -{ +class Animation { public: - Animation(); - Animation(Animation ©); - ~Animation(); - void Push(TweenSet *set); - void Clear(); - std::vector *tweenSets(); - TweenSet *tweenSet(unsigned int index); - unsigned int size(); -private: - std::vector animationVector_; -}; + Component *component; + ComponentData start; + ComponentData end; + float elapsedTime; + float duration; + TweenAlgorithm algorithm; +}; \ No newline at end of file diff --git a/RetroFE/Source/Graphics/Animate/AnimationChain.cpp b/RetroFE/Source/Graphics/Animate/AnimationChain.cpp new file mode 100644 index 0000000..3990031 --- /dev/null +++ b/RetroFE/Source/Graphics/Animate/AnimationChain.cpp @@ -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(); +} \ No newline at end of file diff --git a/RetroFE/Source/Graphics/Animate/AnimationChain.h b/RetroFE/Source/Graphics/Animate/AnimationChain.h new file mode 100644 index 0000000..82c45a9 --- /dev/null +++ b/RetroFE/Source/Graphics/Animate/AnimationChain.h @@ -0,0 +1,17 @@ +#pragma once +#include "Animation.h" +#include + +class AnimationChain { +public: + AnimationChain(); + virtual ~AnimationChain(); + typedef std::vector AnimationList_T; + typedef AnimationList_T::iterator AnimationListIt_T; + + AnimationList_T animations; + unsigned int index; + bool loop; + unsigned int loopCount; + bool autoDestroy; +}; diff --git a/RetroFE/Source/Graphics/Animate/AnimationEvents.cpp b/RetroFE/Source/Graphics/Animate/AnimationEvents.cpp deleted file mode 100644 index 5719b12..0000000 --- a/RetroFE/Source/Graphics/Animate/AnimationEvents.cpp +++ /dev/null @@ -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 . - */ - -#include "AnimationEvents.h" -#include - - - -AnimationEvents::AnimationEvents() -{ -} - -AnimationEvents::AnimationEvents(AnimationEvents ©) -{ - for(std::map >::iterator it = copy.animationMap_.begin(); it != copy.animationMap_.end(); it++) - { - for(std::map::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 >::iterator it = animationMap_.begin(); - while(it != animationMap_.end()) - { - std::map::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(); -} - - - - diff --git a/RetroFE/Source/Graphics/Animate/AnimationEvents.h b/RetroFE/Source/Graphics/Animate/AnimationEvents.h deleted file mode 100644 index 7775fb2..0000000 --- a/RetroFE/Source/Graphics/Animate/AnimationEvents.h +++ /dev/null @@ -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 . - */ -#pragma once - -#include "Tween.h" -#include "Animation.h" -#include -#include -#include - -class AnimationEvents -{ -public: - AnimationEvents(); - AnimationEvents(AnimationEvents ©); - ~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 > animationMap_; -}; diff --git a/RetroFE/Source/Graphics/Animate/AnimationManager.cpp b/RetroFE/Source/Graphics/Animate/AnimationManager.cpp new file mode 100644 index 0000000..efdc28f --- /dev/null +++ b/RetroFE/Source/Graphics/Animate/AnimationManager.cpp @@ -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; +} \ No newline at end of file diff --git a/RetroFE/Source/Graphics/Animate/AnimationManager.h b/RetroFE/Source/Graphics/Animate/AnimationManager.h new file mode 100644 index 0000000..13be32b --- /dev/null +++ b/RetroFE/Source/Graphics/Animate/AnimationManager.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Animation.h" +#include "AnimationChain.h" +#include + +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 AnimationChains_T; + typedef AnimationChains_T::iterator AnimationChainsIt_T; + AnimationChains_T chains; +}; \ No newline at end of file diff --git a/RetroFE/Source/Graphics/Animate/Tween.cpp b/RetroFE/Source/Graphics/Animate/Tween.cpp index 2b4d4b3..bce4d53 100644 --- a/RetroFE/Source/Graphics/Animate/Tween.cpp +++ b/RetroFE/Source/Graphics/Animate/Tween.cpp @@ -20,48 +20,6 @@ #include std::map Tween::tweenTypeMap_; -std::map 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) @@ -105,13 +63,7 @@ TweenAlgorithm Tween::getTweenType(std::string name) } -float Tween::animate(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) +float Tween::calculate(TweenAlgorithm type, double start, double end, double duration, double elapsedTime) { double a = start; double b = end - start; diff --git a/RetroFE/Source/Graphics/Animate/Tween.h b/RetroFE/Source/Graphics/Animate/Tween.h index b38bd8e..7dc40da 100644 --- a/RetroFE/Source/Graphics/Animate/Tween.h +++ b/RetroFE/Source/Graphics/Animate/Tween.h @@ -25,13 +25,8 @@ class Tween { 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 bool getTweenProperty(std::string name, TweenProperty &property); - TweenProperty property; - double duration; + static float calculate(TweenAlgorithm type, double start, double end, double duration, double elapsedTime); private: 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 easeInOutCircular(double elapsedTime, double duration, double b, double c); static double linear(double elapsedTime, double duration, double b, double c); - static std::map tweenTypeMap_; - static std::map tweenPropertyMap_; - TweenAlgorithm type; - double start; - double end; }; diff --git a/RetroFE/Source/Graphics/Animate/TweenSet.cpp b/RetroFE/Source/Graphics/Animate/TweenSet.cpp deleted file mode 100644 index b338177..0000000 --- a/RetroFE/Source/Graphics/Animate/TweenSet.cpp +++ /dev/null @@ -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 . - */ -#include "TweenSet.h" - -TweenSet::TweenSet() -{ -} - -TweenSet::TweenSet(TweenSet ©) -{ - for(std::vector::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::iterator it = set_.begin(); - while(it != set_.end()) - { - delete *it; - set_.erase(it); - it = set_.begin(); - } -} -std::vector *TweenSet::tweens() -{ - return &set_; -} - -Tween *TweenSet::getTween(unsigned int index) -{ - return set_[index]; -} - - -unsigned int TweenSet::size() -{ - return set_.size(); -} diff --git a/RetroFE/Source/Graphics/Animate/TweenSet.h b/RetroFE/Source/Graphics/Animate/TweenSet.h deleted file mode 100644 index a56e615..0000000 --- a/RetroFE/Source/Graphics/Animate/TweenSet.h +++ /dev/null @@ -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 . - */ -#pragma once - -#include "Tween.h" -#include - -class TweenSet -{ -public: - TweenSet(); - TweenSet(TweenSet ©); - ~TweenSet(); - void push(Tween * tween); - void clear(); - std::vector *tweens(); - Tween *getTween(unsigned int index); - unsigned int size(); - -private: - std::vector set_; -}; diff --git a/RetroFE/Source/Graphics/Component/Component.cpp b/RetroFE/Source/Graphics/Component/Component.cpp index 30b0e15..660cff4 100644 --- a/RetroFE/Source/Graphics/Component/Component.cpp +++ b/RetroFE/Source/Graphics/Component/Component.cpp @@ -18,85 +18,10 @@ #include "../../SDL.h" 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) { - 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; - } - } } diff --git a/RetroFE/Source/Graphics/Component/Component.h b/RetroFE/Source/Graphics/Component/Component.h index 7db7163..4507c6e 100644 --- a/RetroFE/Source/Graphics/Component/Component.h +++ b/RetroFE/Source/Graphics/Component/Component.h @@ -24,16 +24,5 @@ public: Component(); virtual void update(float dt); virtual void draw() = 0; - virtual void addAnimation(const ComponentData &newInfo); - virtual void animate(bool loop); - ComponentData *startInfo; ComponentData info; - ComponentData *endInfo; - -private: - std::vector animations; - unsigned int animationIndex; - float elapsedTime; - bool loop; - bool start; }; diff --git a/RetroFE/Source/Lua/LuaAnimate.cpp b/RetroFE/Source/Lua/LuaAnimate.cpp new file mode 100644 index 0000000..8eae58c --- /dev/null +++ b/RetroFE/Source/Lua/LuaAnimate.cpp @@ -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 +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(l, "x", COMPONENT_DATA_X_MASK, a->end, a->end.x); + BuildTableComponentInfo(l, "y", COMPONENT_DATA_Y_MASK, a->end, a->end.y); + BuildTableComponentInfo(l, "height", COMPONENT_DATA_Y_MASK, a->end, a->end.height); + BuildTableComponentInfo(l, "width", COMPONENT_DATA_Y_MASK, a->end, a->end.width); + BuildTableComponentInfo(l, "rotate", COMPONENT_DATA_ROTATE_MASK, a->end, a->end.rotate); + BuildTableComponentInfo(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; +} + diff --git a/RetroFE/Source/Lua/LuaAnimate.h b/RetroFE/Source/Lua/LuaAnimate.h new file mode 100644 index 0000000..4ab7d36 --- /dev/null +++ b/RetroFE/Source/Lua/LuaAnimate.h @@ -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); +}; diff --git a/RetroFE/Source/Lua/LuaImage.cpp b/RetroFE/Source/Lua/LuaImage.cpp index 49bc7bc..c71819c 100644 --- a/RetroFE/Source/Lua/LuaImage.cpp +++ b/RetroFE/Source/Lua/LuaImage.cpp @@ -322,51 +322,3 @@ 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; -} - -template -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(l, "x", COMPONENT_DATA_X_MASK, newInfo, newInfo.x); - BuildTableComponentInfo(l, "y", COMPONENT_DATA_Y_MASK, newInfo, newInfo.y); - BuildTableComponentInfo(l, "height", COMPONENT_DATA_Y_MASK, newInfo, newInfo.height); - BuildTableComponentInfo(l, "width", COMPONENT_DATA_Y_MASK, newInfo, newInfo.width); - BuildTableComponentInfo(l, "rotate", COMPONENT_DATA_ROTATE_MASK, newInfo, newInfo.rotate); - BuildTableComponentInfo(l, "alpha", COMPONENT_DATA_ROTATE_MASK, newInfo, newInfo.alpha); - - newInfo.duration = duration; - i->addAnimation(newInfo); - - return 0; -} - diff --git a/RetroFE/Source/Lua/LuaImage.h b/RetroFE/Source/Lua/LuaImage.h index f3a3e62..0819af9 100644 --- a/RetroFE/Source/Lua/LuaImage.h +++ b/RetroFE/Source/Lua/LuaImage.h @@ -35,6 +35,4 @@ 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); }; diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index bc0a7c6..5d470cf 100644 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -21,6 +21,7 @@ #include "SDL.h" #include "Graphics/Component/Image.h" #include "Graphics/Component/Component.h" +#include "Lua/LuaAnimate.h" #include "Lua/LuaCollection.h" #include "Lua/LuaDisplay.h" #include "Lua/LuaImage.h" @@ -51,6 +52,13 @@ static int lua_registerOnInit(lua_State *l) return 0; } +const luaL_Reg RetroFE::luaAnimateFuncs[] = { + {"start", LuaAnimate::start}, + {"startChain", LuaAnimate::startChain}, + {"destroy", LuaAnimate::destroy}, + {NULL, NULL} +}; + const luaL_Reg RetroFE::luaImageFuncs[] = { // Creation {"create", LuaImage::create}, @@ -75,8 +83,6 @@ const luaL_Reg RetroFE::luaImageFuncs[] = { {"setDimensions", LuaImage::setDimensions}, {"setRotate", LuaImage::setRotate}, {"setAlpha", LuaImage::setAlpha}, - {"addAnimation", LuaImage::addAnimation}, - {"animate", LuaImage::animate}, {"destroy", LuaImage::destroy}, {NULL, NULL} }; @@ -110,6 +116,11 @@ void RetroFE::initializeLua() lua_.initialize(); LuaImage::initialize(&config_, factory_); 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); luaL_setfuncs (lua_.state, luaCollectionFuncs, 0); @@ -200,6 +211,8 @@ void RetroFE::run() SDL_SetRenderDrawColor(SDL::getRenderer(), 0x0, 0x0, 0x00, 0xFF); SDL_RenderClear(SDL::getRenderer()); + animationManager_.update((float)deltaTime); + for(std::map::iterator it = factory_.components.begin(); it != factory_.components.end(); it++) { it->second->update((float)deltaTime); diff --git a/RetroFE/Source/RetroFE.h b/RetroFE/Source/RetroFE.h index d06a9fc..ad0164a 100644 --- a/RetroFE/Source/RetroFE.h +++ b/RetroFE/Source/RetroFE.h @@ -14,7 +14,7 @@ * along with RetroFE. If not, see . */ #pragma once - +#include "Graphics/Animate/AnimationManager.h" #include "Collection/CollectionInfoBuilder.h" #include "Database/DB.h" #include "Database/Configuration.h" @@ -38,10 +38,12 @@ private: Configuration &config_; Lua lua_; LuaEvent luaEvent_; - static const luaL_Reg luaDisplayFuncs[]; - static const luaL_Reg luaLogFuncs[]; - static const luaL_Reg luaImageFuncs[]; + AnimationManager animationManager_; + static const luaL_Reg luaAnimateFuncs[]; static const luaL_Reg luaCollectionFuncs[]; + static const luaL_Reg luaDisplayFuncs[]; + static const luaL_Reg luaImageFuncs[]; + static const luaL_Reg luaLogFuncs[]; DB db_; MetadataDatabase *mdb_; CollectionInfoBuilder *cib_;