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