diff options
author | rachytski <siarhei.rachytski@gmail.com> | 2012-08-16 12:18:01 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:42:07 +0300 |
commit | 96c0658a68e29d7b6671291c3c28b35dbd7d6086 (patch) | |
tree | 7b7694059c9f2043a7a9ae0ddc487d84e7cb6a5e /anim | |
parent | 6815bad255b40e1dd86d71e15c1ca2742b2ec1ed (diff) |
added animation library.
Diffstat (limited to 'anim')
-rw-r--r-- | anim/anim.pro | 19 | ||||
-rw-r--r-- | anim/controller.cpp | 83 | ||||
-rw-r--r-- | anim/controller.hpp | 50 | ||||
-rw-r--r-- | anim/task.hpp | 17 |
4 files changed, 169 insertions, 0 deletions
diff --git a/anim/anim.pro b/anim/anim.pro new file mode 100644 index 0000000000..0e328c422f --- /dev/null +++ b/anim/anim.pro @@ -0,0 +1,19 @@ +# Animation library + +TARGET = anim +TEMPLATE = lib +CONFIG += staticlib + +ROOT_DIR = .. +DEPENDENCIES = geometry coding base expat + +include($$ROOT_DIR/common.pri) + +HEADERS += \ + controller.hpp \ + task.hpp + +SOURCES += \ + controller.cpp \ + + diff --git a/anim/controller.cpp b/anim/controller.cpp new file mode 100644 index 0000000000..6737105e85 --- /dev/null +++ b/anim/controller.cpp @@ -0,0 +1,83 @@ +#include "controller.hpp" +#include "task.hpp" + +#include "../base/timer.hpp" +#include "../std/bind.hpp" + +namespace anim +{ + Controller::Controller() + { + m_animStep = 10; + m_thread.Create(this); + } + + Controller::~Controller() + { + m_tasks.Cancel(); + m_newTasks.Cancel(); + m_thread.Cancel(); + } + + void Controller::AddTask(shared_ptr<Task> const & task) + { + m_newTasks.PushBack(task); + } + + void Controller::CopyTasks(TTasks & from, TTasks & to) + { + to.clear(); + swap(from, to); + } + + void Controller::Do() + { + while (true) + { + // making synchronized copy of tasks to process + // them without intervention from other threads. + m_newTasks.ProcessList(bind(&Controller::CopyTasks, this, _1, ref(m_newTasksList))); + m_tasks.ProcessList(bind(&Controller::CopyTasks, this, _1, ref(m_tasksList))); + + // checking for thread cancellation + if (m_newTasks.IsCancelled() + || m_tasks.IsCancelled() + || IsCancelled()) + break; + + // current animation step timestamp + double timeStamp = my::Timer::LocalTime(); + + // starting new tasks and adding them to the pool. + // they we'll be processed in the next animation step + for (TTasks::const_iterator it = m_newTasksList.begin(); + it != m_newTasksList.end(); + ++it) + { + shared_ptr<Task> task = *it; + task->OnStart(timeStamp); + m_tasks.PushBack(task); + } + + m_newTasksList.clear(); + + // processing current tasks + for (TTasks::const_iterator it = m_tasksList.begin(); + it != m_tasksList.end(); + ++it) + { + shared_ptr<Task> task = *it; + task->OnStep(timeStamp); + if (!task->IsFinished()) + m_tasks.PushBack(task); + else + task->OnEnd(timeStamp); + } + + m_tasksList.clear(); + + // sleeping till the next animation step. + threads::Sleep(m_animStep); + } + } +} diff --git a/anim/controller.hpp b/anim/controller.hpp new file mode 100644 index 0000000000..bf77e7766e --- /dev/null +++ b/anim/controller.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include "../std/shared_ptr.hpp" + +#include "../base/thread.hpp" +#include "../base/threaded_list.hpp" + +namespace anim +{ + class Task; + + // Animation controller class. + // - Creates and manages the separate thread. + // - Using ThreadedList to manage the list of active commands. + // - CPU efficient, which means that when there are no commands + // the thread is sleeping and doesn't consume CPU + class Controller : public threads::IRoutine + { + private: + + // Container for tasks + typedef list<shared_ptr<Task> > TTasks; + + ThreadedList<shared_ptr<Task> > m_tasks; + // Task for the current step. + TTasks m_tasksList; + + ThreadedList<shared_ptr<Task> > m_newTasks; + // Added, but not started tasks. + // They'll be started in the next animation step. + TTasks m_newTasksList; + + // Animation thread. + threads::Thread m_thread; + // Animation step in miliseconds. + unsigned m_animStep; + // MainLoop method + void Do(); + + void CopyTasks(list<shared_ptr<Task> > & from, list<shared_ptr<Task> > & to); + + public: + // Constructor + Controller(); + // Destructor + ~Controller(); + // Adding animation task to the controller + void AddTask(shared_ptr<Task> const & task); + }; +} diff --git a/anim/task.hpp b/anim/task.hpp new file mode 100644 index 0000000000..16adf3231c --- /dev/null +++ b/anim/task.hpp @@ -0,0 +1,17 @@ +#pragma once + +namespace anim +{ + // Interface for single animation task + class Task + { + public: + virtual void OnStart(double ts) = 0; + virtual void OnStep(double ts) = 0; + virtual void OnEnd(double ts) = 0; + virtual bool IsFinished() = 0; + virtual void Finish() = 0; + + virtual ~Task() {}; + }; +} |