all repos — mgba @ 5ee1dfcea7fb0de91b976e047053b8b1aace0d84

mGBA Game Boy Advance Emulator

src/gba/gui/gui-runner.c (view raw)

  1/* Copyright (c) 2013-2016 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#include "gui-runner.h"
  7
  8#include "core/core.h"
  9#include "gba/gui/gui-config.h"
 10#include "gba/input.h"
 11#include "gba/interface.h"
 12#include "gba/serialize.h"
 13#include "util/gui/file-select.h"
 14#include "util/gui/font.h"
 15#include "util/gui/menu.h"
 16#include "util/memory.h"
 17#include "util/png-io.h"
 18#include "util/vfs.h"
 19
 20#ifdef _3DS
 21#include <3ds.h>
 22#endif
 23
 24#include <sys/time.h>
 25
 26#define FPS_GRANULARITY 120
 27#define FPS_BUFFER_SIZE 3
 28
 29enum {
 30	RUNNER_CONTINUE = 1,
 31	RUNNER_EXIT,
 32	RUNNER_SAVE_STATE,
 33	RUNNER_LOAD_STATE,
 34	RUNNER_SCREENSHOT,
 35	RUNNER_CONFIG,
 36	RUNNER_RESET,
 37	RUNNER_COMMAND_MASK = 0xFFFF,
 38
 39	RUNNER_STATE_1 = 0x10000,
 40	RUNNER_STATE_2 = 0x20000,
 41	RUNNER_STATE_3 = 0x30000,
 42	RUNNER_STATE_4 = 0x40000,
 43	RUNNER_STATE_5 = 0x50000,
 44	RUNNER_STATE_6 = 0x60000,
 45	RUNNER_STATE_7 = 0x70000,
 46	RUNNER_STATE_8 = 0x80000,
 47	RUNNER_STATE_9 = 0x90000,
 48};
 49
 50static void _drawBackground(struct GUIBackground* background, void* context) {
 51	UNUSED(context);
 52	struct mGUIBackground* gbaBackground = (struct mGUIBackground*) background;
 53	if (gbaBackground->p->drawFrame) {
 54		gbaBackground->p->drawFrame(gbaBackground->p, true);
 55	}
 56}
 57
 58static void _drawState(struct GUIBackground* background, void* id) {
 59	struct mGUIBackground* gbaBackground = (struct mGUIBackground*) background;
 60	int stateId = ((int) id) >> 16;
 61	if (gbaBackground->p->drawScreenshot) {
 62		if (gbaBackground->screenshot && gbaBackground->screenshotId == (int) id) {
 63			gbaBackground->p->drawScreenshot(gbaBackground->p, gbaBackground->screenshot, true);
 64			return;
 65		}
 66		struct VFile* vf = mCoreGetState(gbaBackground->p->core, stateId, false);
 67		uint32_t* pixels = gbaBackground->screenshot;
 68		if (!pixels) {
 69			pixels = anonymousMemoryMap(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
 70			gbaBackground->screenshot = pixels;
 71		}
 72		bool success = false;
 73		if (vf && isPNG(vf) && pixels) {
 74			png_structp png = PNGReadOpen(vf, PNG_HEADER_BYTES);
 75			png_infop info = png_create_info_struct(png);
 76			png_infop end = png_create_info_struct(png);
 77			if (png && info && end) {
 78				success = PNGReadHeader(png, info);
 79				success = success && PNGReadPixels(png, info, pixels, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, VIDEO_HORIZONTAL_PIXELS);
 80				success = success && PNGReadFooter(png, end);
 81			}
 82			PNGReadClose(png, info, end);
 83		}
 84		if (vf) {
 85			vf->close(vf);
 86		}
 87		if (success) {
 88			gbaBackground->p->drawScreenshot(gbaBackground->p, pixels, true);
 89			gbaBackground->screenshotId = (int) id;
 90		} else if (gbaBackground->p->drawFrame) {
 91			gbaBackground->p->drawFrame(gbaBackground->p, true);
 92		}
 93	}
 94}
 95
 96static void _updateLux(struct GBALuminanceSource* lux) {
 97	UNUSED(lux);
 98}
 99
100static uint8_t _readLux(struct GBALuminanceSource* lux) {
101	struct mGUIRunnerLux* runnerLux = (struct mGUIRunnerLux*) lux;
102	int value = 0x16;
103	if (runnerLux->luxLevel > 0) {
104		value += GBA_LUX_LEVELS[runnerLux->luxLevel - 1];
105	}
106	return 0xFF - value;
107}
108
109void mGUIInit(struct mGUIRunner* runner, const char* port) {
110	GUIInit(&runner->params);
111	runner->port = port;
112	runner->core = NULL;
113	runner->luminanceSource.d.readLuminance = _readLux;
114	runner->luminanceSource.d.sample = _updateLux;
115	runner->luminanceSource.luxLevel = 0;
116	runner->background.d.draw = _drawBackground;
117	runner->background.p = runner;
118	runner->fps = 0;
119	runner->lastFpsCheck = 0;
120	runner->totalDelta = 0;
121	CircleBufferInit(&runner->fpsBuffer, FPS_BUFFER_SIZE * sizeof(uint32_t));
122}
123
124void mGUIDeinit(struct mGUIRunner* runner) {
125	if (runner->teardown) {
126		runner->teardown(runner);
127	}
128	CircleBufferDeinit(&runner->fpsBuffer);
129}
130
131void mGUIRun(struct mGUIRunner* runner, const char* path) {
132	struct mGUIBackground drawState = {
133		.d = {
134			.draw = _drawState
135		},
136		.p = runner,
137		.screenshot = 0,
138		.screenshotId = 0
139	};
140	struct GUIMenu pauseMenu = {
141		.title = "Game Paused",
142		.index = 0,
143		.background = &runner->background.d
144	};
145	struct GUIMenu stateSaveMenu = {
146		.title = "Save state",
147		.index = 0,
148		.background = &drawState.d
149	};
150	struct GUIMenu stateLoadMenu = {
151		.title = "Load state",
152		.index = 0,
153		.background = &drawState.d
154	};
155	GUIMenuItemListInit(&pauseMenu.items, 0);
156	GUIMenuItemListInit(&stateSaveMenu.items, 9);
157	GUIMenuItemListInit(&stateLoadMenu.items, 9);
158	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Unpause", .data = (void*) RUNNER_CONTINUE };
159	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Save state", .submenu = &stateSaveMenu };
160	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Load state", .submenu = &stateLoadMenu };
161
162	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 1", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_1) };
163	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 2", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_2) };
164	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 3", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_3) };
165	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 4", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_4) };
166	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 5", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_5) };
167	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 6", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_6) };
168	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 7", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_7) };
169	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 8", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_8) };
170	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 9", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_9) };
171
172	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 1", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_1) };
173	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 2", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_2) };
174	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 3", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_3) };
175	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 4", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_4) };
176	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 5", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_5) };
177	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 6", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_6) };
178	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 7", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_7) };
179	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 8", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_8) };
180	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 9", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_9) };
181
182	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Take screenshot", .data = (void*) RUNNER_SCREENSHOT };
183	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Configure", .data = (void*) RUNNER_CONFIG };
184	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Reset game", .data = (void*) RUNNER_RESET };
185	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Exit game", .data = (void*) RUNNER_EXIT };
186
187	// TODO: Message box API
188	runner->params.drawStart();
189	if (runner->params.guiPrepare) {
190		runner->params.guiPrepare();
191	}
192	GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, "Loading...");
193	if (runner->params.guiFinish) {
194		runner->params.guiFinish();
195	}
196	runner->params.drawEnd();
197
198	bool found = false;
199	runner->core = mCoreFind(path);
200	if (runner->core) {
201		runner->core->init(runner->core);
202		mInputMapInit(&runner->core->inputMap, &GBAInputInfo);
203		mCoreInitConfig(runner->core, runner->port);
204		found = mCoreLoadFile(runner->core, path);
205		if (!found) {
206			runner->core->deinit(runner->core);
207		}
208	}
209
210	if (!found) {
211		int i;
212		for (i = 0; i < 240; ++i) {
213			runner->params.drawStart();
214			if (runner->params.guiPrepare) {
215				runner->params.guiPrepare();
216			}
217			GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, "Load failed!");
218			if (runner->params.guiFinish) {
219				runner->params.guiFinish();
220			}
221			runner->params.drawEnd();
222		}
223		return;
224	}
225	if (runner->core->platform(runner->core) == PLATFORM_GBA) {
226		((struct GBA*) runner->core->board)->luminanceSource = &runner->luminanceSource.d;
227	}
228	if (runner->core->config.port && runner->keySources) {
229		size_t i;
230		for (i = 0; runner->keySources[i].id; ++i) {
231			mInputMapLoad(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->core->config));
232		}
233	}
234	// TODO: Do we need to load more defaults?
235	mCoreConfigSetDefaultIntValue(&runner->core->config, "volume", 0x100);
236	mCoreLoadConfig(runner->core);
237	mCoreAutoloadSave(runner->core);
238	if (runner->setup) {
239		runner->setup(runner);
240	}
241	runner->core->reset(runner->core);
242	bool running = true;
243	if (runner->gameLoaded) {
244		runner->gameLoaded(runner);
245	}
246	while (running) {
247		CircleBufferClear(&runner->fpsBuffer);
248		runner->totalDelta = 0;
249		runner->fps = 0;
250		struct timeval tv;
251		gettimeofday(&tv, 0);
252		runner->lastFpsCheck = 1000000LL * tv.tv_sec + tv.tv_usec;
253
254		while (true) {
255#ifdef _3DS
256			running = aptMainLoop();
257			if (!running) {
258				break;
259			}
260#endif
261			uint32_t guiKeys;
262			GUIPollInput(&runner->params, &guiKeys, 0);
263			if (guiKeys & (1 << GUI_INPUT_CANCEL)) {
264				break;
265			}
266			if (guiKeys & (1 << mGUI_INPUT_INCREASE_BRIGHTNESS)) {
267				if (runner->luminanceSource.luxLevel < 10) {
268					++runner->luminanceSource.luxLevel;
269				}
270			}
271			if (guiKeys & (1 << mGUI_INPUT_DECREASE_BRIGHTNESS)) {
272				if (runner->luminanceSource.luxLevel > 0) {
273					--runner->luminanceSource.luxLevel;
274				}
275			}
276			if (guiKeys & (1 << mGUI_INPUT_SCREEN_MODE) && runner->incrementScreenMode) {
277				runner->incrementScreenMode(runner);
278			}
279			uint16_t keys = runner->pollGameInput(runner);
280			if (runner->prepareForFrame) {
281				runner->prepareForFrame(runner);
282			}
283			runner->core->setKeys(runner->core, keys);
284			runner->core->runFrame(runner->core);
285			if (runner->drawFrame) {
286				int drawFps = false;
287				mCoreConfigGetIntValue(&runner->core->config, "fpsCounter", &drawFps);
288
289				runner->params.drawStart();
290				runner->drawFrame(runner, false);
291				if (drawFps) {
292					if (runner->params.guiPrepare) {
293						runner->params.guiPrepare();
294					}
295					GUIFontPrintf(runner->params.font, 0, GUIFontHeight(runner->params.font), GUI_ALIGN_LEFT, 0x7FFFFFFF, "%.2f fps", runner->fps);
296					if (runner->params.guiFinish) {
297						runner->params.guiFinish();
298					}
299				}
300				runner->params.drawEnd();
301
302				if (runner->core->frameCounter(runner->core) % FPS_GRANULARITY == 0) {
303					if (drawFps) {
304						struct timeval tv;
305						gettimeofday(&tv, 0);
306						uint64_t t = 1000000LL * tv.tv_sec + tv.tv_usec;
307						uint64_t delta = t - runner->lastFpsCheck;
308						runner->lastFpsCheck = t;
309						if (delta > 0x7FFFFFFFLL) {
310							CircleBufferClear(&runner->fpsBuffer);
311							runner->fps = 0;
312						}
313						if (CircleBufferSize(&runner->fpsBuffer) == CircleBufferCapacity(&runner->fpsBuffer)) {
314							int32_t last;
315							CircleBufferRead32(&runner->fpsBuffer, &last);
316							runner->totalDelta -= last;
317						}
318						CircleBufferWrite32(&runner->fpsBuffer, delta);
319						runner->totalDelta += delta;
320						runner->fps = (CircleBufferSize(&runner->fpsBuffer) * FPS_GRANULARITY * 1000000.0f) / (runner->totalDelta * sizeof(uint32_t));
321					}
322				}
323			}
324		}
325
326		if (runner->paused) {
327			runner->paused(runner);
328		}
329		GUIInvalidateKeys(&runner->params);
330		uint32_t keys = 0xFFFFFFFF; // Huge hack to avoid an extra variable!
331		struct GUIMenuItem* item;
332		enum GUIMenuExitReason reason = GUIShowMenu(&runner->params, &pauseMenu, &item);
333		if (reason == GUI_MENU_EXIT_ACCEPT) {
334			switch (((int) item->data) & RUNNER_COMMAND_MASK) {
335			case RUNNER_EXIT:
336				running = false;
337				keys = 0;
338				break;
339			case RUNNER_RESET:
340				runner->core->reset(runner->core);
341				break;
342			case RUNNER_SAVE_STATE:
343				mCoreSaveState(runner->core, ((int) item->data) >> 16, SAVESTATE_SCREENSHOT);
344				break;
345			case RUNNER_LOAD_STATE:
346				mCoreLoadState(runner->core, ((int) item->data) >> 16, SAVESTATE_SCREENSHOT);
347				break;
348			case RUNNER_SCREENSHOT:
349				mCoreTakeScreenshot(runner->core);
350				break;
351			case RUNNER_CONFIG:
352				mGUIShowConfig(runner, runner->configExtra, runner->nConfigExtra);
353				mCoreLoadConfig(runner->core);
354				break;
355			case RUNNER_CONTINUE:
356				break;
357			}
358		}
359		int frames = 0;
360		GUIPollInput(&runner->params, 0, &keys);
361		while (keys && frames < 30) {
362			++frames;
363			runner->params.drawStart();
364			runner->drawFrame(runner, true);
365			runner->params.drawEnd();
366			GUIPollInput(&runner->params, 0, &keys);
367		}
368		if (runner->unpaused) {
369			runner->unpaused(runner);
370		}
371	}
372	if (runner->gameUnloaded) {
373		runner->gameUnloaded(runner);
374	}
375	runner->core->unloadROM(runner->core);
376	drawState.screenshotId = 0;
377	if (drawState.screenshot) {
378		mappedMemoryFree(drawState.screenshot, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
379	}
380
381	if (runner->core->config.port) {
382		if (runner->keySources) {
383			size_t i;
384			for (i = 0; runner->keySources[i].id; ++i) {
385				mInputMapSave(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->core->config));
386			}
387		}
388		mCoreConfigSave(&runner->core->config);
389	}
390	runner->core->deinit(runner->core);
391
392	GUIMenuItemListDeinit(&pauseMenu.items);
393	GUIMenuItemListDeinit(&stateSaveMenu.items);
394	GUIMenuItemListDeinit(&stateLoadMenu.items);
395}
396
397void mGUIRunloop(struct mGUIRunner* runner) {
398	while (true) {
399		char path[PATH_MAX];
400		if (!GUISelectFile(&runner->params, path, sizeof(path), 0)) {
401			break;
402		}
403		mGUIRun(runner, path);
404	}
405}