more scripting features (dynamic fields)

This commit is contained in:
Eduardo Bart
2011-05-02 01:13:57 -03:00
parent f7bb044f48
commit 9e90ae0ee4
19 changed files with 378 additions and 146 deletions

View File

@@ -31,11 +31,15 @@ Dispatcher g_dispatcher;
void Dispatcher::poll()
{
while(!m_taskList.empty()) {
Task *task = m_taskList.top();
m_taskList.front()();
m_taskList.pop_front();
}
while(!m_scheduledTaskList.empty()) {
ScheduledTask *task = m_scheduledTaskList.top();
if(g_engine.getCurrentFrameTicks() < task->ticks)
break;
m_taskList.pop();
m_scheduledTaskList.pop();
task->callback();
delete task;
}
@@ -43,10 +47,13 @@ void Dispatcher::poll()
void Dispatcher::scheduleTask(const SimpleCallback& callback, int delay)
{
m_taskList.push(new Task(g_engine.getCurrentFrameTicks() + delay, callback));
m_scheduledTaskList.push(new ScheduledTask(g_engine.getCurrentFrameTicks() + delay, callback));
}
void Dispatcher::addTask(const SimpleCallback& callback)
void Dispatcher::addTask(const SimpleCallback& callback, bool pushFront)
{
m_taskList.push(new Task(callback));
if(pushFront)
m_taskList.push_front(callback);
else
m_taskList.push_back(callback);
}

View File

@@ -27,18 +27,18 @@
#include <prerequisites.h>
class Task {
class ScheduledTask {
public:
inline Task(const SimpleCallback& _callback) : ticks(0), callback(_callback) { }
inline Task(int _ticks, const SimpleCallback& _callback) : ticks(_ticks), callback(_callback) { }
inline bool operator<(const Task& other) const { return ticks > other.ticks; }
inline ScheduledTask(const SimpleCallback& _callback) : ticks(0), callback(_callback) { }
inline ScheduledTask(int _ticks, const SimpleCallback& _callback) : ticks(_ticks), callback(_callback) { }
inline bool operator<(const ScheduledTask& other) const { return ticks > other.ticks; }
int ticks;
SimpleCallback callback;
};
class lessTask : public std::binary_function<Task*&, Task*&, bool> {
class lessScheduledTask : public std::binary_function<ScheduledTask*&, ScheduledTask*&, bool> {
public:
bool operator()(Task*& t1,Task*& t2) { return (*t1) < (*t2); }
bool operator()(ScheduledTask*& t1,ScheduledTask*& t2) { return (*t1) < (*t2); }
};
class Dispatcher
@@ -50,13 +50,14 @@ public:
void poll();
/// Add an event
void addTask(const SimpleCallback& callback);
void addTask(const SimpleCallback& callback, bool pushFront = false);
/// Schedula an event
void scheduleTask(const SimpleCallback& callback, int delay);
private:
std::priority_queue<Task*, std::vector<Task*>, lessTask> m_taskList;
std::list<SimpleCallback> m_taskList;
std::priority_queue<ScheduledTask*, std::vector<ScheduledTask*>, lessScheduledTask> m_scheduledTaskList;
};
extern Dispatcher g_dispatcher;