Skip to content

Commit

Permalink
Scripting|libcore: Scheduler's Clock can override the default context
Browse files Browse the repository at this point in the history
Each Clock running the scheduled scripts may use its own context for
the processes.
  • Loading branch information
skyjake committed Aug 16, 2015
1 parent 1c43362 commit 1620f75
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/scriptsys/scheduler.h
Expand Up @@ -68,7 +68,7 @@ class DENG2_PUBLIC Scheduler
class DENG2_PUBLIC Clock
{
public:
Clock(Scheduler const &schedule);
Clock(Scheduler const &schedule, Record *context = nullptr);

/**
* Returns the current time of the clock.
Expand Down
79 changes: 41 additions & 38 deletions doomsday/sdk/libcore/src/scriptsys/scheduler.cpp
Expand Up @@ -84,42 +84,6 @@ DENG2_PIMPL(Scheduler)
}
};

DENG2_PIMPL_NOREF(Scheduler::Clock)
{
typedef Scheduler::Instance::Event Event;
typedef Scheduler::Instance::Events Events; // Events not owned

Scheduler const *scheduler = nullptr;
TimeDelta at = 0.0;
Events events;

void rewind()
{
at = 0.0;

// Restore all events in the queue.
events = scheduler->d->events; // copied
}

void advanceTime(TimeDelta const &elapsed)
{
at += elapsed;

while(!events.empty())
{
Event const *ev = events.top();
if(ev->at > at) break;

events.pop();

// Execute the script in the specified context.
Process process(scheduler->d->context);
process.run(ev->script);
process.execute();
}
}
};

Scheduler::Scheduler()
: d(new Instance(this))
{}
Expand Down Expand Up @@ -147,7 +111,6 @@ void Scheduler::addFromInfo(Record const &timelineRecord)
for(String key : ScriptedInfo::sortRecordsBySource(scripts))
{
auto const &def = *scripts[key];
qDebug() << def.asText();
try
{
addScript(def.getd("at", 0.0),
Expand All @@ -163,10 +126,50 @@ void Scheduler::addFromInfo(Record const &timelineRecord)
}
}

Scheduler::Clock::Clock(Scheduler const &schedule)
//----------------------------------------------------------------------------

DENG2_PIMPL_NOREF(Scheduler::Clock)
{
typedef Scheduler::Instance::Event Event;
typedef Scheduler::Instance::Events Events; // Events not owned

Record *context = nullptr;
Scheduler const *scheduler = nullptr;
TimeDelta at = 0.0;
Events events;

void rewind()
{
at = 0.0;

// Restore all events in the queue.
events = scheduler->d->events; // copied
}

void advanceTime(TimeDelta const &elapsed)
{
at += elapsed;

while(!events.empty())
{
Event const *ev = events.top();
if(ev->at > at) break;

events.pop();

// Execute the script in the specified context.
Process process(context? context : scheduler->d->context);
process.run(ev->script);
process.execute();
}
}
};

Scheduler::Clock::Clock(Scheduler const &schedule, Record *context)
: d(new Instance)
{
d->scheduler = &schedule;
d->context = context;
d->rewind();
}

Expand Down

0 comments on commit 1620f75

Please sign in to comment.