all repos — mgba @ 06657d9fde5a2e579ef55b72cfa7ed8269135b83

mGBA Game Boy Advance Emulator

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#pragma once
  7
  8#include <QByteArray>
  9#include <QList>
 10#include <QMutex>
 11#include <QObject>
 12#include <QSize>
 13
 14#include "VFileDevice.h"
 15
 16#include <functional>
 17#include <memory>
 18
 19#include <mgba/core/core.h>
 20#include <mgba/core/interface.h>
 21#include <mgba/core/thread.h>
 22#include <mgba/core/cache-set.h>
 23
 24#ifdef M_CORE_GB
 25#include <mgba/internal/gb/sio/printer.h>
 26#endif
 27
 28#ifdef M_CORE_GBA
 29#include <mgba/gba/interface.h>
 30#endif
 31
 32struct mCore;
 33
 34namespace QGBA {
 35
 36class ConfigController;
 37class InputController;
 38class LogController;
 39class MultiplayerController;
 40class Override;
 41
 42class CoreController : public QObject {
 43Q_OBJECT
 44
 45public:
 46	static const bool VIDEO_SYNC = false;
 47	static const bool AUDIO_SYNC = true;
 48
 49	enum class Feature {
 50		OPENGL = mCORE_FEATURE_OPENGL,
 51	};
 52
 53	class Interrupter {
 54	public:
 55		Interrupter(CoreController*, bool fromThread = false);
 56		Interrupter(std::shared_ptr<CoreController>, bool fromThread = false);
 57		Interrupter(const Interrupter&);
 58		~Interrupter();
 59
 60	private:
 61		CoreController* m_parent;
 62	};
 63
 64	CoreController(mCore* core, QObject* parent = nullptr);
 65	~CoreController();
 66
 67	mCoreThread* thread() { return &m_threadContext; }
 68
 69	const color_t* drawContext();
 70
 71	bool isPaused();
 72	bool hasStarted();
 73
 74	mPlatform platform() const;
 75	QSize screenDimensions() const;
 76	bool supportsFeature(Feature feature) const { return m_threadContext.core->supportsFeature(m_threadContext.core, static_cast<mCoreFeature>(feature)); }
 77	bool hardwareAccelerated() const { return m_hwaccel; }
 78
 79	void loadConfig(ConfigController*);
 80
 81	mCheatDevice* cheatDevice() { return m_threadContext.core->cheatDevice(m_threadContext.core); }
 82
 83#ifdef USE_DEBUGGERS
 84	mDebugger* debugger() { return m_threadContext.core->debugger; }
 85	void setDebugger(mDebugger*);
 86#endif
 87
 88	void setMultiplayerController(MultiplayerController*);
 89	void clearMultiplayerController();
 90	MultiplayerController* multiplayerController() { return m_multiplayer; }
 91
 92	mCacheSet* graphicCaches();
 93	int stateSlot() const { return m_stateSlot; }
 94
 95	void setOverride(std::unique_ptr<Override> override);
 96	Override* override() { return m_override.get(); }
 97
 98	void setInputController(InputController*);
 99	void setLogger(LogController*);
100
101	bool audioSync() const { return m_audioSync; }
102	bool videoSync() const { return m_videoSync; }
103
104public slots:
105	void start();
106	void stop();
107	void reset();
108	void setPaused(bool paused);
109	void frameAdvance();
110	void setSync(bool enable);
111
112	void setRewinding(bool);
113	void rewind(int count = 0);
114
115	void setFastForward(bool);
116	void forceFastForward(bool);
117
118	void loadState(int slot = 0);
119	void loadState(const QString& path);
120	void saveState(int slot = 0);
121	void saveState(const QString& path);
122	void loadBackupState();
123	void saveBackupState();
124
125	void loadSave(const QString&, bool temporary);
126	void loadPatch(const QString&);
127	void replaceGame(const QString&);
128	void yankPak();
129
130	void addKey(int key);
131	void clearKey(int key);
132	void setAutofire(int key, bool enable);
133
134#ifdef USE_PNG
135	void screenshot();
136#endif
137
138	void setRealTime();
139	void setFixedTime(const QDateTime& time);
140	void setFakeEpoch(const QDateTime& time);
141
142	void importSharkport(const QString& path);
143	void exportSharkport(const QString& path);
144
145#ifdef M_CORE_GB
146	void attachPrinter();
147	void detachPrinter();
148	void endPrint();
149#endif
150
151#ifdef M_CORE_GBA
152	void attachBattleChipGate();
153	void detachBattleChipGate();
154	void setBattleChipId(uint16_t id);
155	void setBattleChipFlavor(int flavor);
156#endif
157
158	void setAVStream(mAVStream*);
159	void clearAVStream();
160
161	void clearOverride();
162
163	void startVideoLog(const QString& path);
164	void endVideoLog();
165
166	void setFramebufferHandle(int fb);
167
168signals:
169	void started();
170	void paused();
171	void unpaused();
172	void stopping();
173	void crashed(const QString& errorMessage);
174	void failed();
175	void frameAvailable();
176	void didReset();
177	void stateLoaded();
178	void rewound();
179
180	void rewindChanged(bool);
181	void fastForwardChanged(bool);
182
183	void unimplementedBiosCall(int);
184	void statusPosted(const QString& message);
185	void logPosted(int level, int category, const QString& log);
186
187	void imagePrinted(const QImage&);
188
189private:
190	void updateKeys();
191	int updateAutofire();
192	void finishFrame();
193
194	void updateFastForward();
195
196	mCoreThread m_threadContext{};
197
198	bool m_patched = false;
199
200	QByteArray m_buffers[2];
201	QByteArray* m_activeBuffer;
202	QByteArray m_completeBuffer;
203	bool m_hwaccel = false;
204
205	std::unique_ptr<mCacheSet> m_cacheSet;
206	std::unique_ptr<Override> m_override;
207
208	QList<std::function<void()>> m_resetActions;
209	QList<std::function<void()>> m_frameActions;
210	QMutex m_mutex;
211
212	int m_activeKeys = 0;
213	bool m_autofire[32] = {};
214	int m_autofireStatus[32] = {};
215	int m_autofireThreshold = 1;
216
217	VFileDevice m_backupLoadState;
218	QByteArray m_backupSaveState{nullptr};
219	int m_stateSlot = 1;
220	QString m_statePath;
221	int m_loadStateFlags;
222	int m_saveStateFlags;
223
224	bool m_audioSync = AUDIO_SYNC;
225	bool m_videoSync = VIDEO_SYNC;
226
227	bool m_autosave;
228	bool m_autoload;
229	int m_autosaveCounter;
230
231	int m_fastForward = false;
232	int m_fastForwardForced = false;
233	int m_fastForwardVolume = -1;
234	int m_fastForwardMute = -1;
235	float m_fastForwardRatio = -1.f;
236	float m_fpsTarget;
237
238	InputController* m_inputController = nullptr;
239	LogController* m_log = nullptr;
240	MultiplayerController* m_multiplayer = nullptr;
241
242	mVideoLogContext* m_vl = nullptr;
243	VFile* m_vlVf = nullptr;
244
245#ifdef M_CORE_GB
246	struct QGBPrinter {
247		GBPrinter d;
248		CoreController* parent;
249	} m_printer;
250#endif
251
252#ifdef M_CORE_GBA
253	GBASIOBattlechipGate m_battlechip;
254#endif
255};
256
257}