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/controller.cpp | |
parent | 6815bad255b40e1dd86d71e15c1ca2742b2ec1ed (diff) |
added animation library.
Diffstat (limited to 'anim/controller.cpp')
-rw-r--r-- | anim/controller.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
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); + } + } +} |