src/platform/qt/CoreController.h (view raw)
1/* Copyright (c) 2013-2017 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#ifndef QGBA_CORE_CONTROLLER
7#define QGBA_CORE_CONTROLLER
8
9#include <QByteArray>
10#include <QList>
11#include <QMutex>
12#include <QObject>
13#include <QSize>
14
15#include "VFileDevice.h"
16
17#include <functional>
18#include <memory>
19
20#include <mgba/core/core.h>
21#include <mgba/core/interface.h>
22#include <mgba/core/thread.h>
23#include <mgba/core/tile-cache.h>
24
25struct mCore;
26
27namespace QGBA {
28
29class ConfigController;
30class InputController;
31class LogController;
32class MultiplayerController;
33class Override;
34
35class CoreController : public QObject {
36Q_OBJECT
37
38public:
39 static const bool VIDEO_SYNC = false;
40 static const bool AUDIO_SYNC = true;
41
42 class Interrupter {
43 public:
44 Interrupter(CoreController*, bool fromThread = false);
45 Interrupter(std::shared_ptr<CoreController>, bool fromThread = false);
46 Interrupter(const Interrupter&);
47 ~Interrupter();
48
49 private:
50 CoreController* m_parent;
51 };
52
53 CoreController(mCore* core, QObject* parent = nullptr);
54 ~CoreController();
55
56 mCoreThread* thread() { return &m_threadContext; }
57
58 color_t* drawContext();
59
60 bool isPaused();
61
62 mPlatform platform() const;
63 QSize screenDimensions() const;
64
65 void loadConfig(ConfigController*);
66
67 mCheatDevice* cheatDevice() { return m_threadContext.core->cheatDevice(m_threadContext.core); }
68
69#ifdef USE_DEBUGGERS
70 mDebugger* debugger() { return m_threadContext.core->debugger; }
71 void setDebugger(mDebugger*);
72#endif
73
74 void setMultiplayerController(MultiplayerController*);
75 void clearMultiplayerController();
76 MultiplayerController* multiplayerController() { return m_multiplayer; }
77
78 mTileCache* tileCache();
79 int stateSlot() const { return m_stateSlot; }
80
81 void setOverride(std::unique_ptr<Override> override);
82 Override* override() { return m_override.get(); }
83
84 void setInputController(InputController*);
85 void setLogger(LogController*);
86
87public slots:
88 void start();
89 void stop();
90 void reset();
91 void setPaused(bool paused);
92 void frameAdvance();
93 void setSync(bool enable);
94
95 void setRewinding(bool);
96 void rewind(int count = 0);
97
98 void setFastForward(bool);
99 void forceFastForward(bool);
100
101 void loadState(int slot = 0);
102 void saveState(int slot = 0);
103 void loadBackupState();
104 void saveBackupState();
105
106 void loadSave(const QString&, bool temporary);
107 void loadPatch(const QString&);
108 void replaceGame(const QString&);
109 void yankPak();
110
111 void addKey(int key);
112 void clearKey(int key);
113 void setAutofire(int key, bool enable);
114
115#ifdef USE_PNG
116 void screenshot();
117#endif
118
119 void setRealTime();
120 void setFixedTime(const QDateTime& time);
121 void setFakeEpoch(const QDateTime& time);
122
123 void importSharkport(const QString& path);
124 void exportSharkport(const QString& path);
125
126 void setAVStream(mAVStream*);
127 void clearAVStream();
128
129 void clearOverride();
130
131 void startVideoLog(const QString& path);
132 void endVideoLog();
133
134signals:
135 void started();
136 void paused();
137 void unpaused();
138 void stopping();
139 void crashed(const QString& errorMessage);
140 void failed();
141 void frameAvailable();
142 void stateLoaded();
143 void rewound();
144
145 void rewindChanged(bool);
146 void fastForwardChanged(bool);
147
148 void unimplementedBiosCall(int);
149 void statusPosted(const QString& message);
150 void logPosted(int level, int category, const QString& log);
151
152private:
153 void updateKeys();
154 int updateAutofire();
155 void finishFrame();
156
157 void updateFastForward();
158
159 mCoreThread m_threadContext{};
160
161 bool m_patched = false;
162
163 QByteArray m_buffers[2];
164 QByteArray* m_activeBuffer;
165 QByteArray* m_completeBuffer = nullptr;
166
167 std::unique_ptr<mTileCache> m_tileCache;
168 std::unique_ptr<Override> m_override;
169
170 QList<std::function<void()>> m_resetActions;
171 QList<std::function<void()>> m_frameActions;
172 QMutex m_mutex;
173
174 int m_activeKeys = 0;
175 bool m_autofire[32] = {};
176 int m_autofireStatus[32] = {};
177
178 VFileDevice m_backupLoadState;
179 QByteArray m_backupSaveState{nullptr};
180 int m_stateSlot = 1;
181 int m_loadStateFlags;
182 int m_saveStateFlags;
183
184 bool m_audioSync = AUDIO_SYNC;
185 bool m_videoSync = VIDEO_SYNC;
186
187 int m_fastForward = false;
188 int m_fastForwardForced = false;
189 float m_fastForwardRatio = -1.f;
190 float m_fpsTarget;
191
192 InputController* m_inputController = nullptr;
193 LogController* m_log = nullptr;
194 MultiplayerController* m_multiplayer = nullptr;
195
196 mVideoLogContext* m_vl = nullptr;
197 VFile* m_vlVf = nullptr;
198};
199
200}
201
202#endif