src/platform/qt/GameController.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "GameController.h"
7
8#include "AudioProcessor.h"
9#include "InputController.h"
10#include "MultiplayerController.h"
11#include "VFileDevice.h"
12
13#include <QDateTime>
14#include <QThread>
15
16#include <ctime>
17
18extern "C" {
19#include "gba/audio.h"
20#include "gba/gba.h"
21#include "gba/serialize.h"
22#include "gba/sharkport.h"
23#include "gba/renderers/video-software.h"
24#include "gba/supervisor/config.h"
25#include "util/vfs.h"
26}
27
28using namespace QGBA;
29using namespace std;
30
31const int GameController::LUX_LEVELS[10] = { 5, 11, 18, 27, 42, 62, 84, 109, 139, 183 };
32
33GameController::GameController(QObject* parent)
34 : QObject(parent)
35 , m_drawContext(new uint32_t[256 * 256])
36 , m_threadContext()
37 , m_activeKeys(0)
38 , m_inactiveKeys(0)
39 , m_logLevels(0)
40 , m_gameOpen(false)
41 , m_audioThread(new QThread(this))
42 , m_audioProcessor(AudioProcessor::create())
43 , m_pauseAfterFrame(false)
44 , m_videoSync(VIDEO_SYNC)
45 , m_audioSync(AUDIO_SYNC)
46 , m_fpsTarget(-1)
47 , m_turbo(false)
48 , m_turboForced(false)
49 , m_turboSpeed(-1)
50 , m_wasPaused(false)
51 , m_inputController(nullptr)
52 , m_multiplayer(nullptr)
53 , m_stateSlot(1)
54{
55 m_renderer = new GBAVideoSoftwareRenderer;
56 GBAVideoSoftwareRendererCreate(m_renderer);
57 m_renderer->outputBuffer = (color_t*) m_drawContext;
58 m_renderer->outputBufferStride = 256;
59
60 GBACheatDeviceCreate(&m_cheatDevice);
61
62 m_threadContext.state = THREAD_INITIALIZED;
63 m_threadContext.debugger = 0;
64 m_threadContext.frameskip = 0;
65 m_threadContext.bios = 0;
66 m_threadContext.renderer = &m_renderer->d;
67 m_threadContext.userData = this;
68 m_threadContext.rewindBufferCapacity = 0;
69 m_threadContext.cheats = &m_cheatDevice;
70 m_threadContext.logLevel = GBA_LOG_ALL;
71
72 m_lux.p = this;
73 m_lux.sample = [](GBALuminanceSource* context) {
74 GameControllerLux* lux = static_cast<GameControllerLux*>(context);
75 lux->value = 0xFF - lux->p->m_luxValue;
76 };
77
78 m_lux.readLuminance = [](GBALuminanceSource* context) {
79 GameControllerLux* lux = static_cast<GameControllerLux*>(context);
80 return lux->value;
81 };
82 setLuminanceLevel(0);
83
84 m_rtc.p = this;
85 m_rtc.override = GameControllerRTC::NO_OVERRIDE;
86 m_rtc.sample = [](GBARTCSource* context) {};
87 m_rtc.unixTime = [](GBARTCSource* context) -> time_t {
88 GameControllerRTC* rtc = static_cast<GameControllerRTC*>(context);
89 switch (rtc->override) {
90 case GameControllerRTC::NO_OVERRIDE:
91 default:
92 return time(nullptr);
93 case GameControllerRTC::FIXED:
94 return rtc->value;
95 case GameControllerRTC::FAKE_EPOCH:
96 return rtc->value + rtc->p->m_threadContext.gba->video.frameCounter * (int64_t) VIDEO_TOTAL_LENGTH / GBA_ARM7TDMI_FREQUENCY;
97 }
98 };
99
100 m_threadContext.startCallback = [](GBAThread* context) {
101 GameController* controller = static_cast<GameController*>(context->userData);
102 controller->m_audioProcessor->setInput(context);
103 context->gba->luminanceSource = &controller->m_lux;
104 context->gba->rtcSource = &controller->m_rtc;
105 context->gba->rumble = controller->m_inputController->rumble();
106 context->gba->rotationSource = controller->m_inputController->rotationSource();
107 controller->m_fpsTarget = context->fpsTarget;
108 controller->gameStarted(context);
109 };
110
111 m_threadContext.cleanCallback = [](GBAThread* context) {
112 GameController* controller = static_cast<GameController*>(context->userData);
113 controller->gameStopped(context);
114 };
115
116 m_threadContext.frameCallback = [](GBAThread* context) {
117 GameController* controller = static_cast<GameController*>(context->userData);
118 if (controller->m_pauseAfterFrame.testAndSetAcquire(true, false)) {
119 GBAThreadPauseFromThread(context);
120 controller->gamePaused(&controller->m_threadContext);
121 }
122 if (GBASyncDrawingFrame(&controller->m_threadContext.sync)) {
123 controller->frameAvailable(controller->m_drawContext);
124 } else {
125 controller->frameAvailable(nullptr);
126 }
127 };
128
129 m_threadContext.logHandler = [](GBAThread* context, enum GBALogLevel level, const char* format, va_list args) {
130 static const char* stubMessage = "Stub software interrupt";
131 if (!context) {
132 return;
133 }
134 GameController* controller = static_cast<GameController*>(context->userData);
135 if (level == GBA_LOG_STUB && strncmp(stubMessage, format, strlen(stubMessage)) == 0) {
136 va_list argc;
137 va_copy(argc, args);
138 int immediate = va_arg(argc, int);
139 controller->unimplementedBiosCall(immediate);
140 }
141 if (level == GBA_LOG_FATAL) {
142 QMetaObject::invokeMethod(controller, "crashGame", Q_ARG(const QString&, QString().vsprintf(format, args)));
143 } else if (!(controller->m_logLevels & level)) {
144 return;
145 }
146 QString message(QString().vsprintf(format, args));
147 if (level == GBA_LOG_STATUS) {
148 controller->statusPosted(message);
149 }
150 controller->postLog(level, message);
151 };
152
153 connect(&m_rewindTimer, &QTimer::timeout, [this]() {
154 GBARewind(&m_threadContext, 1);
155 emit frameAvailable(m_drawContext);
156 emit rewound(&m_threadContext);
157 });
158 m_rewindTimer.setInterval(100);
159
160 m_audioThread->setObjectName("Audio Thread");
161 m_audioThread->start(QThread::TimeCriticalPriority);
162 m_audioProcessor->moveToThread(m_audioThread);
163 connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
164 connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
165 connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
166 connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
167 connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(pollEvents()));
168}
169
170GameController::~GameController() {
171 m_audioThread->quit();
172 m_audioThread->wait();
173 disconnect();
174 clearMultiplayerController();
175 closeGame();
176 GBACheatDeviceDestroy(&m_cheatDevice);
177 delete m_renderer;
178 delete[] m_drawContext;
179}
180
181void GameController::setMultiplayerController(MultiplayerController* controller) {
182 if (controller == m_multiplayer) {
183 return;
184 }
185 clearMultiplayerController();
186 m_multiplayer = controller;
187 controller->attachGame(this);
188}
189
190void GameController::clearMultiplayerController() {
191 if (!m_multiplayer) {
192 return;
193 }
194 m_multiplayer->detachGame(this);
195 m_multiplayer = nullptr;
196}
197
198void GameController::setOverride(const GBACartridgeOverride& override) {
199 m_threadContext.override = override;
200 m_threadContext.hasOverride = true;
201}
202
203void GameController::setOptions(const GBAOptions* opts) {
204 setFrameskip(opts->frameskip);
205 setAudioSync(opts->audioSync);
206 setVideoSync(opts->videoSync);
207 setSkipBIOS(opts->skipBios);
208 setUseBIOS(opts->useBios);
209 setRewind(opts->rewindEnable, opts->rewindBufferCapacity, opts->rewindBufferInterval);
210 setVolume(opts->volume);
211 setMute(opts->mute);
212
213 threadInterrupt();
214 m_threadContext.idleOptimization = opts->idleOptimization;
215 threadContinue();
216}
217
218#ifdef USE_GDB_STUB
219ARMDebugger* GameController::debugger() {
220 return m_threadContext.debugger;
221}
222
223void GameController::setDebugger(ARMDebugger* debugger) {
224 threadInterrupt();
225 if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
226 GBADetachDebugger(m_threadContext.gba);
227 }
228 m_threadContext.debugger = debugger;
229 if (m_threadContext.debugger && GBAThreadIsActive(&m_threadContext)) {
230 GBAAttachDebugger(m_threadContext.gba, m_threadContext.debugger);
231 }
232 threadContinue();
233}
234#endif
235
236void GameController::loadGame(const QString& path, bool dirmode) {
237 closeGame();
238 if (!dirmode) {
239 QFile file(path);
240 if (!file.open(QIODevice::ReadOnly)) {
241 return;
242 }
243 file.close();
244 }
245
246 m_fname = path;
247 m_dirmode = dirmode;
248 openGame();
249}
250
251void GameController::bootBIOS() {
252 closeGame();
253 m_fname = QString();
254 m_dirmode = false;
255 openGame(true);
256}
257
258void GameController::openGame(bool biosOnly) {
259 if (biosOnly && (!m_useBios || m_bios.isNull())) {
260 return;
261 }
262
263 m_gameOpen = true;
264
265 m_pauseAfterFrame = false;
266
267 if (m_turbo) {
268 m_threadContext.sync.videoFrameWait = false;
269 m_threadContext.sync.audioWait = false;
270 } else {
271 m_threadContext.sync.videoFrameWait = m_videoSync;
272 m_threadContext.sync.audioWait = m_audioSync;
273 }
274
275 m_threadContext.gameDir = 0;
276 m_threadContext.bootBios = biosOnly;
277 if (biosOnly) {
278 m_threadContext.fname = nullptr;
279 } else {
280 m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
281 if (m_dirmode) {
282 m_threadContext.gameDir = VDirOpen(m_threadContext.fname);
283 m_threadContext.stateDir = m_threadContext.gameDir;
284 } else {
285 GBAThreadLoadROM(&m_threadContext, m_threadContext.fname);
286 }
287 }
288
289 if (!m_bios.isNull() && m_useBios) {
290 m_threadContext.bios = VFileDevice::open(m_bios, O_RDONLY);
291 } else {
292 m_threadContext.bios = nullptr;
293 }
294
295 if (!m_patch.isNull()) {
296 m_threadContext.patch = VFileDevice::open(m_patch, O_RDONLY);
297 }
298
299 m_inputController->recalibrateAxes();
300
301 if (!GBAThreadStart(&m_threadContext)) {
302 m_gameOpen = false;
303 emit gameFailed();
304 }
305}
306
307void GameController::loadBIOS(const QString& path) {
308 if (m_bios == path) {
309 return;
310 }
311 m_bios = path;
312 if (m_gameOpen) {
313 closeGame();
314 openGame();
315 }
316}
317
318void GameController::yankPak() {
319 if (!m_gameOpen) {
320 return;
321 }
322 threadInterrupt();
323 GBAYankROM(m_threadContext.gba);
324 threadContinue();
325}
326
327void GameController::replaceGame(const QString& path) {
328 if (!m_gameOpen) {
329 return;
330 }
331
332 m_fname = path;
333 threadInterrupt();
334 m_threadContext.fname = strdup(m_fname.toLocal8Bit().constData());
335 GBAThreadReplaceROM(&m_threadContext, m_threadContext.fname);
336 threadContinue();
337}
338
339void GameController::loadPatch(const QString& path) {
340 if (m_gameOpen) {
341 closeGame();
342 m_patch = path;
343 openGame();
344 } else {
345 m_patch = path;
346 }
347}
348
349void GameController::importSharkport(const QString& path) {
350 if (!m_gameOpen) {
351 return;
352 }
353 VFile* vf = VFileDevice::open(path, O_RDONLY);
354 if (!vf) {
355 return;
356 }
357 threadInterrupt();
358 GBASavedataImportSharkPort(m_threadContext.gba, vf, false);
359 threadContinue();
360 vf->close(vf);
361}
362
363void GameController::exportSharkport(const QString& path) {
364 if (!m_gameOpen) {
365 return;
366 }
367 VFile* vf = VFileDevice::open(path, O_WRONLY | O_CREAT | O_TRUNC);
368 if (!vf) {
369 return;
370 }
371 threadInterrupt();
372 GBASavedataExportSharkPort(m_threadContext.gba, vf);
373 threadContinue();
374 vf->close(vf);
375}
376
377void GameController::closeGame() {
378 if (!m_gameOpen) {
379 return;
380 }
381 m_rewindTimer.stop();
382 if (GBAThreadIsPaused(&m_threadContext)) {
383 GBAThreadUnpause(&m_threadContext);
384 }
385 GBAThreadEnd(&m_threadContext);
386 GBAThreadJoin(&m_threadContext);
387 if (m_threadContext.fname) {
388 free(const_cast<char*>(m_threadContext.fname));
389 m_threadContext.fname = nullptr;
390 }
391
392 m_patch = QString();
393
394 for (size_t i = 0; i < GBACheatSetsSize(&m_cheatDevice.cheats); ++i) {
395 GBACheatSet* set = *GBACheatSetsGetPointer(&m_cheatDevice.cheats, i);
396 GBACheatSetDeinit(set);
397 delete set;
398 }
399 GBACheatSetsClear(&m_cheatDevice.cheats);
400
401 m_gameOpen = false;
402 emit gameStopped(&m_threadContext);
403}
404
405void GameController::crashGame(const QString& crashMessage) {
406 closeGame();
407 emit gameCrashed(crashMessage);
408 emit gameStopped(&m_threadContext);
409}
410
411bool GameController::isPaused() {
412 if (!m_gameOpen) {
413 return false;
414 }
415 return GBAThreadIsPaused(&m_threadContext);
416}
417
418void GameController::setPaused(bool paused) {
419 if (!m_gameOpen || m_rewindTimer.isActive() || paused == GBAThreadIsPaused(&m_threadContext)) {
420 return;
421 }
422 if (paused) {
423 GBAThreadPause(&m_threadContext);
424 emit gamePaused(&m_threadContext);
425 } else {
426 GBAThreadUnpause(&m_threadContext);
427 emit gameUnpaused(&m_threadContext);
428 }
429}
430
431void GameController::reset() {
432 GBAThreadReset(&m_threadContext);
433}
434
435void GameController::threadInterrupt() {
436 if (m_gameOpen) {
437 GBAThreadInterrupt(&m_threadContext);
438 }
439}
440
441void GameController::threadContinue() {
442 if (m_gameOpen) {
443 GBAThreadContinue(&m_threadContext);
444 }
445}
446
447void GameController::frameAdvance() {
448 if (m_rewindTimer.isActive()) {
449 return;
450 }
451 if (m_pauseAfterFrame.testAndSetRelaxed(false, true)) {
452 setPaused(false);
453 }
454}
455
456void GameController::setRewind(bool enable, int capacity, int interval) {
457 if (m_gameOpen) {
458 threadInterrupt();
459 GBARewindSettingsChanged(&m_threadContext, enable ? capacity : 0, enable ? interval : 0);
460 threadContinue();
461 } else {
462 if (enable) {
463 m_threadContext.rewindBufferInterval = interval;
464 m_threadContext.rewindBufferCapacity = capacity;
465 } else {
466 m_threadContext.rewindBufferInterval = 0;
467 m_threadContext.rewindBufferCapacity = 0;
468 }
469 }
470}
471
472void GameController::rewind(int states) {
473 threadInterrupt();
474 if (!states) {
475 GBARewindAll(&m_threadContext);
476 } else {
477 GBARewind(&m_threadContext, states);
478 }
479 threadContinue();
480 emit frameAvailable(m_drawContext);
481 emit rewound(&m_threadContext);
482}
483
484void GameController::startRewinding() {
485 if (!m_gameOpen || m_rewindTimer.isActive()) {
486 return;
487 }
488 m_wasPaused = isPaused();
489 setPaused(true);
490 m_rewindTimer.start();
491}
492
493void GameController::stopRewinding() {
494 if (!m_rewindTimer.isActive()) {
495 return;
496 }
497 m_rewindTimer.stop();
498 setPaused(m_wasPaused);
499}
500
501void GameController::keyPressed(int key) {
502 int mappedKey = 1 << key;
503 m_activeKeys |= mappedKey;
504 if (!m_inputController->allowOpposing()) {
505 if ((m_activeKeys & 0x30) == 0x30) {
506 m_inactiveKeys |= mappedKey ^ 0x30;
507 m_activeKeys ^= mappedKey ^ 0x30;
508 }
509 if ((m_activeKeys & 0xC0) == 0xC0) {
510 m_inactiveKeys |= mappedKey ^ 0xC0;
511 m_activeKeys ^= mappedKey ^ 0xC0;
512 }
513 }
514 updateKeys();
515}
516
517void GameController::keyReleased(int key) {
518 int mappedKey = 1 << key;
519 m_activeKeys &= ~mappedKey;
520 if (!m_inputController->allowOpposing()) {
521 if (mappedKey & 0x30) {
522 m_activeKeys |= m_inactiveKeys & (0x30 ^ mappedKey);
523 m_inactiveKeys &= ~0x30;
524 }
525 if (mappedKey & 0xC0) {
526 m_activeKeys |= m_inactiveKeys & (0xC0 ^ mappedKey);
527 m_inactiveKeys &= ~0xC0;
528 }
529 }
530 updateKeys();
531}
532
533void GameController::clearKeys() {
534 m_activeKeys = 0;
535 m_inactiveKeys = 0;
536 updateKeys();
537}
538
539void GameController::setAudioBufferSamples(int samples) {
540 threadInterrupt();
541 redoSamples(samples);
542 threadContinue();
543 QMetaObject::invokeMethod(m_audioProcessor, "setBufferSamples", Q_ARG(int, samples));
544}
545
546void GameController::setFPSTarget(float fps) {
547 threadInterrupt();
548 m_fpsTarget = fps;
549 m_threadContext.fpsTarget = fps;
550 if (m_turbo && m_turboSpeed > 0) {
551 m_threadContext.fpsTarget *= m_turboSpeed;
552 }
553 redoSamples(m_audioProcessor->getBufferSamples());
554 threadContinue();
555}
556
557void GameController::setSkipBIOS(bool set) {
558 threadInterrupt();
559 m_threadContext.skipBios = set;
560 threadContinue();
561}
562
563void GameController::setUseBIOS(bool use) {
564 threadInterrupt();
565 m_useBios = use;
566 threadContinue();
567}
568
569void GameController::loadState(int slot) {
570 if (slot > 0) {
571 m_stateSlot = slot;
572 }
573 GBARunOnThread(&m_threadContext, [](GBAThread* context) {
574 GameController* controller = static_cast<GameController*>(context->userData);
575 if (GBALoadState(context, context->stateDir, controller->m_stateSlot)) {
576 controller->frameAvailable(controller->m_drawContext);
577 controller->stateLoaded(context);
578 }
579 });
580}
581
582void GameController::saveState(int slot) {
583 if (slot > 0) {
584 m_stateSlot = slot;
585 }
586 GBARunOnThread(&m_threadContext, [](GBAThread* context) {
587 GameController* controller = static_cast<GameController*>(context->userData);
588 GBASaveState(context, context->stateDir, controller->m_stateSlot, true);
589 });
590}
591
592void GameController::setVideoSync(bool set) {
593 m_videoSync = set;
594 if (!m_turbo) {
595 threadInterrupt();
596 m_threadContext.sync.videoFrameWait = set;
597 threadContinue();
598 }
599}
600
601void GameController::setAudioSync(bool set) {
602 m_audioSync = set;
603 if (!m_turbo) {
604 threadInterrupt();
605 m_threadContext.sync.audioWait = set;
606 threadContinue();
607 }
608}
609
610void GameController::setFrameskip(int skip) {
611 m_threadContext.frameskip = skip;
612}
613
614void GameController::setVolume(int volume) {
615 threadInterrupt();
616 m_threadContext.volume = volume;
617 if (m_gameOpen) {
618 m_threadContext.gba->audio.masterVolume = volume;
619 }
620 threadContinue();
621}
622
623void GameController::setMute(bool mute) {
624 threadInterrupt();
625 m_threadContext.mute = mute;
626 if (m_gameOpen) {
627 m_threadContext.gba->audio.masterVolume = mute ? 0 : m_threadContext.volume;
628 }
629 threadContinue();
630}
631
632void GameController::setTurbo(bool set, bool forced) {
633 if (m_turboForced && !forced) {
634 return;
635 }
636 if (m_turbo == set && m_turboForced == forced) {
637 // Don't interrupt the thread if we don't need to
638 return;
639 }
640 m_turbo = set;
641 m_turboForced = set && forced;
642 enableTurbo();
643}
644
645void GameController::setTurboSpeed(float ratio) {
646 m_turboSpeed = ratio;
647 enableTurbo();
648}
649
650void GameController::enableTurbo() {
651 threadInterrupt();
652 if (!m_turbo) {
653 m_threadContext.fpsTarget = m_fpsTarget;
654 m_threadContext.sync.audioWait = m_audioSync;
655 m_threadContext.sync.videoFrameWait = m_videoSync;
656 } else if (m_turboSpeed <= 0) {
657 m_threadContext.fpsTarget = m_fpsTarget;
658 m_threadContext.sync.audioWait = false;
659 m_threadContext.sync.videoFrameWait = false;
660 } else {
661 m_threadContext.fpsTarget = m_fpsTarget * m_turboSpeed;
662 m_threadContext.sync.audioWait = true;
663 m_threadContext.sync.videoFrameWait = false;
664 }
665 redoSamples(m_audioProcessor->getBufferSamples());
666 threadContinue();
667}
668
669void GameController::setAVStream(GBAAVStream* stream) {
670 threadInterrupt();
671 m_threadContext.stream = stream;
672 if (m_gameOpen) {
673 m_threadContext.gba->stream = stream;
674 }
675 threadContinue();
676}
677
678void GameController::clearAVStream() {
679 threadInterrupt();
680 m_threadContext.stream = nullptr;
681 if (m_gameOpen) {
682 m_threadContext.gba->stream = nullptr;
683 }
684 threadContinue();
685}
686
687#ifdef USE_PNG
688void GameController::screenshot() {
689 GBARunOnThread(&m_threadContext, GBAThreadTakeScreenshot);
690}
691#endif
692
693void GameController::reloadAudioDriver() {
694 QMetaObject::invokeMethod(m_audioProcessor, "pause", Qt::BlockingQueuedConnection);
695 int samples = m_audioProcessor->getBufferSamples();
696 delete m_audioProcessor;
697 m_audioProcessor = AudioProcessor::create();
698 m_audioProcessor->setBufferSamples(samples);
699 m_audioProcessor->moveToThread(m_audioThread);
700 connect(this, SIGNAL(gameStarted(GBAThread*)), m_audioProcessor, SLOT(start()));
701 connect(this, SIGNAL(gameStopped(GBAThread*)), m_audioProcessor, SLOT(pause()));
702 connect(this, SIGNAL(gamePaused(GBAThread*)), m_audioProcessor, SLOT(pause()));
703 connect(this, SIGNAL(gameUnpaused(GBAThread*)), m_audioProcessor, SLOT(start()));
704 if (isLoaded()) {
705 m_audioProcessor->setInput(&m_threadContext);
706 QMetaObject::invokeMethod(m_audioProcessor, "start");
707 }
708}
709
710void GameController::setLuminanceValue(uint8_t value) {
711 m_luxValue = value;
712 value = std::max<int>(value - 0x16, 0);
713 m_luxLevel = 10;
714 for (int i = 0; i < 10; ++i) {
715 if (value < LUX_LEVELS[i]) {
716 m_luxLevel = i;
717 break;
718 }
719 }
720 emit luminanceValueChanged(m_luxValue);
721}
722
723void GameController::setLuminanceLevel(int level) {
724 int value = 0x16;
725 level = std::max(0, std::min(10, level));
726 if (level > 0) {
727 value += LUX_LEVELS[level - 1];
728 }
729 setLuminanceValue(value);
730}
731
732void GameController::setRealTime() {
733 m_rtc.override = GameControllerRTC::NO_OVERRIDE;
734}
735
736void GameController::setFixedTime(const QDateTime& time) {
737 m_rtc.override = GameControllerRTC::FIXED;
738 m_rtc.value = time.toMSecsSinceEpoch() / 1000;
739}
740
741void GameController::setFakeEpoch(const QDateTime& time) {
742 m_rtc.override = GameControllerRTC::FAKE_EPOCH;
743 m_rtc.value = time.toMSecsSinceEpoch() / 1000;
744}
745
746void GameController::updateKeys() {
747 int activeKeys = m_activeKeys;
748 activeKeys |= m_activeButtons;
749 activeKeys &= ~m_inactiveKeys;
750 m_threadContext.activeKeys = activeKeys;
751}
752
753void GameController::redoSamples(int samples) {
754#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
755 float sampleRate = 0x8000;
756 float ratio;
757 if (m_threadContext.gba) {
758 sampleRate = m_threadContext.gba->audio.sampleRate;
759 }
760 ratio = GBAAudioCalculateRatio(sampleRate, m_threadContext.fpsTarget, 44100);
761 m_threadContext.audioBuffers = ceil(samples / ratio);
762#else
763 m_threadContext.audioBuffers = samples;
764#endif
765 if (m_threadContext.gba) {
766 GBAAudioResizeBuffer(&m_threadContext.gba->audio, m_threadContext.audioBuffers);
767 }
768 QMetaObject::invokeMethod(m_audioProcessor, "inputParametersChanged");
769}
770
771void GameController::setLogLevel(int levels) {
772 threadInterrupt();
773 m_logLevels = levels;
774 threadContinue();
775}
776
777void GameController::enableLogLevel(int levels) {
778 threadInterrupt();
779 m_logLevels |= levels;
780 threadContinue();
781}
782
783void GameController::disableLogLevel(int levels) {
784 threadInterrupt();
785 m_logLevels &= ~levels;
786 threadContinue();
787}
788
789void GameController::pollEvents() {
790 if (!m_inputController) {
791 return;
792 }
793
794 m_activeButtons = m_inputController->pollEvents();
795 updateKeys();
796}