Skip to content

Commit

Permalink
Refactor|LegacyCore: Loop callback function set separately
Browse files Browse the repository at this point in the history
Now the main loop can be first started without a callback.
  • Loading branch information
skyjake committed Mar 11, 2012
1 parent fdf1acf commit 137d701
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
3 changes: 2 additions & 1 deletion doomsday/libdeng2/include/de/c_wrapper.h
Expand Up @@ -41,7 +41,8 @@ extern "C" {
DENG2_OPAQUE(LegacyCore)

DENG2_PUBLIC LegacyCore* LegacyCore_New(void* dengApp);
DENG2_PUBLIC int LegacyCore_RunEventLoop(LegacyCore* lc, void (*loopFunc)(void));
DENG2_PUBLIC void LegacyCore_SetLoopFunc(LegacyCore* lc, void (*callback)(void));
DENG2_PUBLIC int LegacyCore_RunEventLoop(LegacyCore* lc);
DENG2_PUBLIC void LegacyCore_Stop(LegacyCore* lc, int exitCode);
DENG2_PUBLIC void LegacyCore_Delete(LegacyCore* lc);

Expand Down
13 changes: 9 additions & 4 deletions doomsday/libdeng2/include/de/legacy/legacycore.h
Expand Up @@ -51,12 +51,17 @@ class LegacyCore : public QObject
* Starts the deng2 event loop in the current thread. Does not return until
* the loop is stopped.
*
* @param callback Function to call periodically from the loop, as
* often as possible.
*
* @return Exit code.
*/
int runEventLoop(void (*callback)(void));
int runEventLoop();

/**
* Sets the callback function that gets called periodically from the main
* loop. The calls are made as often as possible without blocking the loop.
*
* @param callback Loop callback function.
*/
void setLoopFunc(void (*callback)(void));

/**
* Stops the event loop. This is automatically called when the core is
Expand Down
1 change: 1 addition & 0 deletions doomsday/libdeng2/libdeng2.pro
Expand Up @@ -65,6 +65,7 @@ macx {
fixInstallName("QtCore.framework/Versions/4/QtCore")
fixInstallName("QtNetwork.framework/Versions/4/QtNetwork")
fixInstallName("QtGui.framework/Versions/4/QtGui")
fixInstallName("QtOpenGL.framework/Versions/4/QtOpenGL")

# Update the library included in the main app bundle.
doPostLink("cp -fRp libdeng2*dylib ../engine/doomsday.app/Contents/Frameworks")
Expand Down
10 changes: 8 additions & 2 deletions doomsday/libdeng2/src/c_wrapper.cpp
Expand Up @@ -32,10 +32,16 @@ LegacyCore* LegacyCore_New(void* dengApp)
return reinterpret_cast<LegacyCore*>(new de::LegacyCore(reinterpret_cast<QApplication*>(dengApp)));
}

int LegacyCore_RunEventLoop(LegacyCore* lc, void (*loopFunc)(void))
int LegacyCore_RunEventLoop(LegacyCore* lc)
{
DENG2_SELF(LegacyCore, lc);
return self->runEventLoop(loopFunc);
return self->runEventLoop();
}

void LegacyCore_SetLoopFunc(LegacyCore* lc, void (*callback)(void))
{
DENG2_SELF(LegacyCore, lc);
return self->setLoopFunc(callback);
}

void LegacyCore_Stop(LegacyCore *lc, int exitCode)
Expand Down
18 changes: 13 additions & 5 deletions doomsday/libdeng2/src/legacy/legacycore.cpp
Expand Up @@ -74,13 +74,21 @@ LegacyNetwork& LegacyCore::network()
return instance().d->network;
}

int LegacyCore::runEventLoop(void (*func)(void))
void LegacyCore::setLoopFunc(void (*func)(void))
{
LOG_MSG("Starting LegacyCore event loop...");

// Set up a timer to periodically call the provided callback function.
d->loopFunc = func;
QTimer::singleShot(1, this, SLOT(callback()));

if(func)
{
// Start the periodic callback calls.
QTimer::singleShot(1, this, SLOT(callback()));
}
}

int LegacyCore::runEventLoop()
{
LOG_MSG("Starting LegacyCore event loop...");

// Run the Qt event loop. In the future this will be replaced by the
// application's main Qt event loop, where deng2 will hook into.
Expand All @@ -100,6 +108,6 @@ void LegacyCore::callback()
if(d->loopFunc)
{
d->loopFunc();
QTimer::singleShot(1, this, SLOT(callback()));
}
QTimer::singleShot(1, this, SLOT(callback()));
}

0 comments on commit 137d701

Please sign in to comment.