all repos — mgba @ db2b56f418359e03f0505bac5e894cf821989cc3

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