-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
legacycore.cpp
125 lines (103 loc) · 2.88 KB
/
legacycore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "de/LegacyCore"
#include "de/LegacyNetwork"
#include "de/LogBuffer"
#include <QCoreApplication>
#include <QTimer>
#include <QDebug>
using namespace de;
LegacyCore* LegacyCore::_appCore;
/**
* @internal Private instance data for LegacyCore.
*/
struct LegacyCore::Instance
{
App* app;
LegacyNetwork network;
void (*loopFunc)(void);
LogBuffer logBuffer;
int loopInterval;
Instance() : app(0), loopFunc(0), loopInterval(1) {}
~Instance() {}
};
LegacyCore::LegacyCore(App* dengApp)
{
_appCore = this;
d = new Instance;
// Construct a new core application (must have one for the event loop).
d->app = dengApp;
// The global log buffer will be available for the entire runtime of deng2.
LogBuffer::setAppBuffer(d->logBuffer);
#ifdef DENG2_DEBUG
d->logBuffer.enable(Log::DEBUG);
#endif
}
LegacyCore::~LegacyCore()
{
stop();
delete d;
_appCore = 0;
}
LegacyCore& LegacyCore::instance()
{
DENG2_ASSERT(_appCore != 0);
DENG2_ASSERT(_appCore->d != 0);
return *_appCore;
}
LegacyNetwork& LegacyCore::network()
{
return instance().d->network;
}
void LegacyCore::setLoopFunc(void (*func)(void))
{
LOG_DEBUG("Loop function changed from %p set to %p.") << (dintptr)d->loopFunc << (dintptr)func;
// Set up a timer to periodically call the provided callback function.
d->loopFunc = func;
if(func)
{
// Start the periodic callback calls.
QTimer::singleShot(d->loopInterval, 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.
int code = d->app->exec();
LOG_MSG("Event loop exited with code %i.") << code;
return code;
}
void LegacyCore::setLoopRate(int freqHz)
{
d->loopInterval = qMax(1, 1000/freqHz);
}
void LegacyCore::stop(int exitCode)
{
d->app->exit(exitCode);
}
void LegacyCore::callback()
{
if(d->loopFunc)
{
d->loopFunc();
QTimer::singleShot(d->loopInterval, this, SLOT(callback()));
}
}