From 9ca9d022c0287136fc356594e339e21d71c5f30c Mon Sep 17 00:00:00 2001 From: emb <> Date: Mon, 9 Nov 2015 23:42:39 -0600 Subject: [PATCH] Concepting out animations --- RetroFE/Source/CMakeLists.txt | 1 + .../Source/Graphics/Component/Component.cpp | 61 +++++++++++++++ RetroFE/Source/Graphics/Component/Component.h | 18 ++++- RetroFE/Source/Graphics/Component/Image.cpp | 21 +++-- RetroFE/Source/Graphics/Component/Image.h | 6 -- RetroFE/Source/RetroFE.cpp | 77 +++++++++++++++++-- 6 files changed, 157 insertions(+), 27 deletions(-) diff --git a/RetroFE/Source/CMakeLists.txt b/RetroFE/Source/CMakeLists.txt index 2216b0e..a186f3d 100644 --- a/RetroFE/Source/CMakeLists.txt +++ b/RetroFE/Source/CMakeLists.txt @@ -94,6 +94,7 @@ set(RETROFE_HEADERS "${RETROFE_DIR}/Source/Database/Configuration.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/Lua.h" "${RETROFE_DIR}/Source/RetroFE.h" "${RETROFE_DIR}/Source/SDL.h" diff --git a/RetroFE/Source/Graphics/Component/Component.cpp b/RetroFE/Source/Graphics/Component/Component.cpp index e0d55a7..0729084 100644 --- a/RetroFE/Source/Graphics/Component/Component.cpp +++ b/RetroFE/Source/Graphics/Component/Component.cpp @@ -17,4 +17,65 @@ #include "../../Utility/Log.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; + } + } +} diff --git a/RetroFE/Source/Graphics/Component/Component.h b/RetroFE/Source/Graphics/Component/Component.h index 83f282b..7db7163 100644 --- a/RetroFE/Source/Graphics/Component/Component.h +++ b/RetroFE/Source/Graphics/Component/Component.h @@ -15,9 +15,25 @@ */ #pragma once +#include "ComponentData.h" + +#include class Component { public: - virtual void update(float dt) = 0; + 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/Graphics/Component/Image.cpp b/RetroFE/Source/Graphics/Component/Image.cpp index 5464dd8..6f5f71a 100644 --- a/RetroFE/Source/Graphics/Component/Image.cpp +++ b/RetroFE/Source/Graphics/Component/Image.cpp @@ -20,11 +20,6 @@ Image::Image(std::string file) : texture_(NULL) , file_(file) - , width(0) - , height(0) - , x(0) - , y(0) - , alpha(1) { } @@ -42,7 +37,7 @@ void Image::Initialize() if (texture_ != NULL) { 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()); } @@ -59,6 +54,8 @@ void Image::DeInitialize() void Image::update(float dt) { + Component::update(dt); + if(!texture_) { SDL_LockMutex(SDL::getMutex()); @@ -67,7 +64,7 @@ void Image::update(float dt) if (texture_ != NULL) { 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()); } @@ -79,11 +76,11 @@ void Image::draw() { SDL_Rect rect; - rect.x = x; - rect.y = y; - rect.h = width; - rect.w = height; + rect.x = info.x; + rect.y = info.y; + rect.h = info.width; + 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); } } diff --git a/RetroFE/Source/Graphics/Component/Image.h b/RetroFE/Source/Graphics/Component/Image.h index c8edffd..1f493ec 100644 --- a/RetroFE/Source/Graphics/Component/Image.h +++ b/RetroFE/Source/Graphics/Component/Image.h @@ -28,12 +28,6 @@ public: void DeInitialize(); void update(float dt); void draw(); - int width; - int height; - int x; - int y; - int rotate; - float alpha; protected: SDL_Texture *texture_; diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index 0eb1158..63f38aa 100644 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -82,29 +82,72 @@ static int lua_imageDelete(lua_State *l) static int lua_imageSetSize(lua_State *l) { Image *i = (Image *)lua_tointeger(l, 1); - i->width = (int)lua_tointeger(l, 2); - i->height = (int)lua_tointeger(l, 3); + i->info.width = (int)lua_tointeger(l, 2); + i->info.height = (int)lua_tointeger(l, 3); return 0; } static int lua_imageSetPosition(lua_State *l) { Image *i = (Image *)lua_tointeger(l, 1); - i->x = (int)lua_tointeger(l, 2); - i->y = (int)lua_tointeger(l, 3); + i->info.x = (int)lua_tointeger(l, 2); + i->info.y = (int)lua_tointeger(l, 3); return 0; } static int lua_imageSetRotate(lua_State *l) { Image *i = (Image *)lua_tointeger(l, 1); - i->rotate = (int)lua_tointeger(l, 2); + i->info.rotate = (float)lua_tonumber(l, 2); return 0; } static int lua_imageSetAlpha(lua_State *l) { 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; } @@ -124,6 +167,8 @@ static const luaL_Reg luaImageFuncs[] = { {"setRotate", lua_imageSetRotate}, {"setPosition", lua_imageSetPosition}, {"setAlpha", lua_imageSetAlpha}, + {"addAnimation", lua_imageAddAnimation}, + {"animate", lua_imageAnimate}, {NULL, NULL} }; @@ -171,7 +216,9 @@ void RetroFE::run() reloadLuaScripts(); bool quit = false; - + double currentTime = 0; + double lastTime = 0; + double deltaTime = 0; while(!quit) { SDL_LockMutex(SDL::getMutex()); @@ -180,7 +227,7 @@ void RetroFE::run() for(std::map::iterator it = components.begin(); it != components.end(); it++) { - it->second->update(0); + it->second->update((float)deltaTime); } for(std::map::iterator it = components.begin(); it != components.end(); it++) @@ -190,7 +237,21 @@ void RetroFE::run() SDL_RenderPresent(SDL::getRenderer()); SDL_UnlockMutex(SDL::getMutex()); + + lastTime = currentTime; + currentTime = static_cast(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(sleepTime)); + } }