all repos — mgba @ 7899e02db00c15e6f33c958d7c07f64f1047af96

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