all repos — mgba @ 56722324efdf8d5c9675f7658a35b0d05e9f8594

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 = -1);
139	void setAVStream(mAVStream*);
140	void clearAVStream();
141	void reloadAudioDriver();
142	void setSaveStateExtdata(int flags);
143	void setLoadStateExtdata(int flags);
144
145#ifdef USE_PNG
146	void screenshot();
147#endif
148
149	void setLuminanceValue(uint8_t value);
150	uint8_t luminanceValue() const { return m_luxValue; }
151	void setLuminanceLevel(int level);
152	void increaseLuminanceLevel() { setLuminanceLevel(m_luxLevel + 1); }
153	void decreaseLuminanceLevel() { setLuminanceLevel(m_luxLevel - 1); }
154
155	void setRealTime();
156	void setFixedTime(const QDateTime& time);
157	void setFakeEpoch(const QDateTime& time);
158
159	void setLogLevel(int);
160	void enableLogLevel(int);
161	void disableLogLevel(int);
162
163private slots:
164	void openGame(bool bios = false);
165	void crashGame(const QString& crashMessage);
166
167	void pollEvents();
168	void updateAutofire();
169
170private:
171	void updateKeys();
172	void redoSamples(int samples);
173	void enableTurbo();
174
175	uint32_t* m_drawContext;
176	uint32_t* m_frontBuffer;
177	mCoreThread m_threadContext;
178	const mCoreConfig* m_config;
179	mCheatDevice* m_cheatDevice;
180	int m_activeKeys;
181	int m_activeButtons;
182	int m_inactiveKeys;
183	int m_logLevels;
184
185	bool m_gameOpen;
186
187	QString m_fname;
188	VFile* m_vf;
189	QString m_bios;
190	bool m_useBios;
191	QString m_patch;
192
193	QThread* m_audioThread;
194	AudioProcessor* m_audioProcessor;
195
196	QAtomicInt m_pauseAfterFrame;
197	QList<std::function<void ()>> m_resetActions;
198
199	bool m_videoSync;
200	bool m_audioSync;
201	float m_fpsTarget;
202	bool m_turbo;
203	bool m_turboForced;
204	float m_turboSpeed;
205	bool m_wasPaused;
206
207	bool m_audioChannels[6];
208	bool m_videoLayers[5];
209
210	bool m_autofire[GBA_KEY_MAX];
211	int m_autofireStatus[GBA_KEY_MAX];
212
213	int m_stateSlot;
214	struct VFile* m_backupLoadState;
215	QByteArray m_backupSaveState;
216	int m_saveStateFlags;
217	int m_loadStateFlags;
218
219	InputController* m_inputController;
220	MultiplayerController* m_multiplayer;
221
222	mAVStream* m_stream;
223
224	struct GameControllerLux : GBALuminanceSource {
225		GameController* p;
226		uint8_t value;
227	} m_lux;
228	uint8_t m_luxValue;
229	int m_luxLevel;
230
231	mRTCGenericSource m_rtc;
232};
233
234}
235
236#endif