all repos — mgba @ 1bc8dfe42b7fb819a1e0585474356a11d0975972

mGBA Game Boy Advance Emulator

src/platform/qt/GameController.h (view raw)

  1/* Copyright (c) 2013-2015 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_GAME_CONTROLLER
  7#define QGBA_GAME_CONTROLLER
  8
  9#include <QAtomicInt>
 10#include <QFile>
 11#include <QImage>
 12#include <QObject>
 13#include <QString>
 14#include <QTimer>
 15
 16#include <memory>
 17
 18extern "C" {
 19#include "core/core.h"
 20#include "core/thread.h"
 21#include "gba/cheats.h"
 22#include "gba/hardware.h"
 23#include "gba/input.h"
 24#include "gba/overrides.h"
 25#ifdef BUILD_SDL
 26#include "sdl-events.h"
 27#endif
 28}
 29
 30struct GBAAudio;
 31struct mCoreConfig;
 32struct Configuration;
 33struct mDebugger;
 34
 35class QThread;
 36
 37namespace QGBA {
 38
 39class AudioProcessor;
 40class InputController;
 41class MultiplayerController;
 42
 43class GameController : public QObject {
 44Q_OBJECT
 45
 46public:
 47	static const bool VIDEO_SYNC = false;
 48	static const bool AUDIO_SYNC = true;
 49
 50	GameController(QObject* parent = nullptr);
 51	~GameController();
 52
 53	const uint32_t* drawContext() const { return m_drawContext; }
 54	mCoreThread* thread() { return &m_threadContext; }
 55	mCheatDevice* cheatDevice() { return m_threadContext.core ? m_threadContext.core->cheatDevice(m_threadContext.core) : nullptr; }
 56
 57	void threadInterrupt();
 58	void threadContinue();
 59
 60	bool isPaused();
 61	bool isLoaded() { return m_gameOpen && mCoreThreadIsActive(&m_threadContext); }
 62	mPlatform platform() const;
 63
 64	bool audioSync() const { return m_audioSync; }
 65	bool videoSync() const { return m_videoSync; }
 66	QSize screenDimensions() const;
 67
 68	void setInputController(InputController* controller) { m_inputController = controller; }
 69
 70	void setMultiplayerController(MultiplayerController* controller);
 71	MultiplayerController* multiplayerController() { return m_multiplayer; }
 72	void clearMultiplayerController();
 73
 74	void setOverride(const GBACartridgeOverride& override);
 75	void clearOverride() { /* TODO: Put back overrides */ }
 76
 77	void setConfig(const mCoreConfig*);
 78
 79	int stateSlot() const { return m_stateSlot; }
 80
 81#ifdef USE_GDB_STUB
 82	mDebugger* debugger();
 83	void setDebugger(mDebugger*);
 84#endif
 85
 86signals:
 87	void frameAvailable(const uint32_t*);
 88	void gameStarted(mCoreThread*, const QString& fname);
 89	void gameStopped(mCoreThread*);
 90	void gamePaused(mCoreThread*);
 91	void gameUnpaused(mCoreThread*);
 92	void gameCrashed(const QString& errorMessage);
 93	void gameFailed();
 94	void stateLoaded(mCoreThread*);
 95	void rewound(mCoreThread*);
 96	void unimplementedBiosCall(int);
 97
 98	void luminanceValueChanged(int);
 99
100	void statusPosted(const QString& message);
101	void postLog(int level, int category, const QString& log);
102
103public slots:
104	void loadGame(const QString& path);
105	void loadGame(VFile* vf, const QString& base = QString());
106	void loadBIOS(const QString& path);
107	void loadSave(const QString& path, bool temporary = true);
108	void yankPak();
109	void replaceGame(const QString& path);
110	void setUseBIOS(bool);
111	void loadPatch(const QString& path);
112	void importSharkport(const QString& path);
113	void exportSharkport(const QString& path);
114	void bootBIOS();
115	void closeGame();
116	void setPaused(bool paused);
117	void reset();
118	void frameAdvance();
119	void setRewind(bool enable, int capacity);
120	void rewind(int states = 0);
121	void startRewinding();
122	void stopRewinding();
123	void keyPressed(int key);
124	void keyReleased(int key);
125	void clearKeys();
126	void setAutofire(int key, bool enable);
127	void setAudioBufferSamples(int samples);
128	void setAudioSampleRate(unsigned rate);
129	void setAudioChannelEnabled(int channel, bool enable = true);
130	void startAudio();
131	void setVideoLayerEnabled(int layer, bool enable = true);
132	void setFPSTarget(float fps);
133	void loadState(int slot = 0);
134	void saveState(int slot = 0);
135	void loadBackupState();
136	void saveBackupState();
137	void setTurbo(bool, bool forced = true);
138	void setTurboSpeed(float ratio);
139	void setSync(bool);
140	void setAVStream(mAVStream*);
141	void clearAVStream();
142	void reloadAudioDriver();
143	void setSaveStateExtdata(int flags);
144	void setLoadStateExtdata(int flags);
145
146#ifdef USE_PNG
147	void screenshot();
148#endif
149
150	void setLuminanceValue(uint8_t value);
151	uint8_t luminanceValue() const { return m_luxValue; }
152	void setLuminanceLevel(int level);
153	void increaseLuminanceLevel() { setLuminanceLevel(m_luxLevel + 1); }
154	void decreaseLuminanceLevel() { setLuminanceLevel(m_luxLevel - 1); }
155
156	void setRealTime();
157	void setFixedTime(const QDateTime& time);
158	void setFakeEpoch(const QDateTime& time);
159
160	void setLogLevel(int);
161	void enableLogLevel(int);
162	void disableLogLevel(int);
163
164private slots:
165	void openGame(bool bios = false);
166	void crashGame(const QString& crashMessage);
167	void cleanGame();
168
169	void pollEvents();
170	void updateAutofire();
171
172private:
173	void updateKeys();
174	void redoSamples(int samples);
175	void enableTurbo();
176
177	uint32_t* m_drawContext;
178	uint32_t* m_frontBuffer;
179	mCoreThread m_threadContext;
180	const mCoreConfig* m_config;
181	mCheatDevice* m_cheatDevice;
182	int m_activeKeys;
183	int m_activeButtons;
184	int m_inactiveKeys;
185	int m_logLevels;
186
187	bool m_gameOpen;
188
189	QString m_fname;
190	VFile* m_vf;
191	QString m_bios;
192	bool m_useBios;
193	QString m_patch;
194
195	QThread* m_audioThread;
196	AudioProcessor* m_audioProcessor;
197
198	QAtomicInt m_pauseAfterFrame;
199	QList<std::function<void ()>> m_resetActions;
200
201	bool m_sync;
202	bool m_videoSync;
203	bool m_audioSync;
204	float m_fpsTarget;
205	bool m_turbo;
206	bool m_turboForced;
207	float m_turboSpeed;
208	bool m_wasPaused;
209
210	bool m_audioChannels[6];
211	bool m_videoLayers[5];
212
213	bool m_autofire[GBA_KEY_MAX];
214	int m_autofireStatus[GBA_KEY_MAX];
215
216	int m_stateSlot;
217	struct VFile* m_backupLoadState;
218	QByteArray m_backupSaveState;
219	int m_saveStateFlags;
220	int m_loadStateFlags;
221
222	InputController* m_inputController;
223	MultiplayerController* m_multiplayer;
224
225	mAVStream* m_stream;
226
227	struct GameControllerLux : GBALuminanceSource {
228		GameController* p;
229		uint8_t value;
230	} m_lux;
231	uint8_t m_luxValue;
232	int m_luxLevel;
233
234	mRTCGenericSource m_rtc;
235};
236
237}
238
239#endif