all repos — mgba @ 5c2dbc59a1df816dc836ef2c05abae02bd543dbd

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		if (runner->core->platform(runner->core) == PLATFORM_GBA) {
205			((struct GBA*) runner->core->board)->luminanceSource = &runner->luminanceSource.d;
206		}
207		if (runner->core->config.port && runner->keySources) {
208			size_t i;
209			for (i = 0; runner->keySources[i].id; ++i) {
210				mInputMapLoad(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->core->config));
211			}
212		}
213		found = mCoreLoadFile(runner->core, path);
214		if (!found) {
215			runner->core->deinit(runner->core);
216		}
217	}
218
219	if (!found) {
220		int i;
221		for (i = 0; i < 240; ++i) {
222			runner->params.drawStart();
223			if (runner->params.guiPrepare) {
224				runner->params.guiPrepare();
225			}
226			GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, "Load failed!");
227			if (runner->params.guiFinish) {
228				runner->params.guiFinish();
229			}
230			runner->params.drawEnd();
231		}
232		return;
233	}
234	mCoreAutoloadSave(runner->core);
235	if (runner->setup) {
236		runner->setup(runner);
237	}
238	runner->core->reset(runner->core);
239	bool running = true;
240	if (runner->gameLoaded) {
241		runner->gameLoaded(runner);
242	}
243	while (running) {
244		CircleBufferClear(&runner->fpsBuffer);
245		runner->totalDelta = 0;
246		runner->fps = 0;
247		struct timeval tv;
248		gettimeofday(&tv, 0);
249		runner->lastFpsCheck = 1000000LL * tv.tv_sec + tv.tv_usec;
250
251		while (true) {
252#ifdef _3DS
253			running = aptMainLoop();
254			if (!running) {
255				break;
256			}
257#endif
258			uint32_t guiKeys;
259			GUIPollInput(&runner->params, &guiKeys, 0);
260			if (guiKeys & (1 << GUI_INPUT_CANCEL)) {
261				break;
262			}
263			if (guiKeys & (1 << mGUI_INPUT_INCREASE_BRIGHTNESS)) {
264				if (runner->luminanceSource.luxLevel < 10) {
265					++runner->luminanceSource.luxLevel;
266				}
267			}
268			if (guiKeys & (1 << mGUI_INPUT_DECREASE_BRIGHTNESS)) {
269				if (runner->luminanceSource.luxLevel > 0) {
270					--runner->luminanceSource.luxLevel;
271				}
272			}
273			if (guiKeys & (1 << mGUI_INPUT_SCREEN_MODE) && runner->incrementScreenMode) {
274				runner->incrementScreenMode(runner);
275			}
276			uint16_t keys = runner->pollGameInput(runner);
277			if (runner->prepareForFrame) {
278				runner->prepareForFrame(runner);
279			}
280			runner->core->setKeys(runner->core, keys);
281			runner->core->runFrame(runner->core);
282			if (runner->drawFrame) {
283				int drawFps = false;
284				mCoreConfigGetIntValue(&runner->core->config, "fpsCounter", &drawFps);
285
286				runner->params.drawStart();
287				runner->drawFrame(runner, false);
288				if (drawFps) {
289					if (runner->params.guiPrepare) {
290						runner->params.guiPrepare();
291					}
292					GUIFontPrintf(runner->params.font, 0, GUIFontHeight(runner->params.font), GUI_ALIGN_LEFT, 0x7FFFFFFF, "%.2f fps", runner->fps);
293					if (runner->params.guiFinish) {
294						runner->params.guiFinish();
295					}
296				}
297				runner->params.drawEnd();
298
299				if (runner->core->frameCounter(runner->core) % FPS_GRANULARITY == 0) {
300					if (drawFps) {
301						struct timeval tv;
302						gettimeofday(&tv, 0);
303						uint64_t t = 1000000LL * tv.tv_sec + tv.tv_usec;
304						uint64_t delta = t - runner->lastFpsCheck;
305						runner->lastFpsCheck = t;
306						if (delta > 0x7FFFFFFFLL) {
307							CircleBufferClear(&runner->fpsBuffer);
308							runner->fps = 0;
309						}
310						if (CircleBufferSize(&runner->fpsBuffer) == CircleBufferCapacity(&runner->fpsBuffer)) {
311							int32_t last;
312							CircleBufferRead32(&runner->fpsBuffer, &last);
313							runner->totalDelta -= last;
314						}
315						CircleBufferWrite32(&runner->fpsBuffer, delta);
316						runner->totalDelta += delta;
317						runner->fps = (CircleBufferSize(&runner->fpsBuffer) * FPS_GRANULARITY * 1000000.0f) / (runner->totalDelta * sizeof(uint32_t));
318					}
319				}
320			}
321		}
322
323		if (runner->paused) {
324			runner->paused(runner);
325		}
326		GUIInvalidateKeys(&runner->params);
327		uint32_t keys = 0xFFFFFFFF; // Huge hack to avoid an extra variable!
328		struct GUIMenuItem* item;
329		enum GUIMenuExitReason reason = GUIShowMenu(&runner->params, &pauseMenu, &item);
330		if (reason == GUI_MENU_EXIT_ACCEPT) {
331			switch (((int) item->data) & RUNNER_COMMAND_MASK) {
332			case RUNNER_EXIT:
333				running = false;
334				keys = 0;
335				break;
336			case RUNNER_RESET:
337				runner->core->reset(runner->core);
338				break;
339			case RUNNER_SAVE_STATE:
340				mCoreSaveState(runner->core, ((int) item->data) >> 16, SAVESTATE_SCREENSHOT);
341				break;
342			case RUNNER_LOAD_STATE:
343				mCoreLoadState(runner->core, ((int) item->data) >> 16, SAVESTATE_SCREENSHOT);
344				break;
345			case RUNNER_SCREENSHOT:
346				mCoreTakeScreenshot(runner->core);
347				break;
348			case RUNNER_CONFIG:
349				mGUIShowConfig(runner, runner->configExtra, runner->nConfigExtra);
350				mCoreLoadConfig(runner->core);
351				break;
352			case RUNNER_CONTINUE:
353				break;
354			}
355		}
356		int frames = 0;
357		GUIPollInput(&runner->params, 0, &keys);
358		while (keys && frames < 30) {
359			++frames;
360			runner->params.drawStart();
361			runner->drawFrame(runner, true);
362			runner->params.drawEnd();
363			GUIPollInput(&runner->params, 0, &keys);
364		}
365		if (runner->unpaused) {
366			runner->unpaused(runner);
367		}
368	}
369	if (runner->gameUnloaded) {
370		runner->gameUnloaded(runner);
371	}
372	runner->core->unloadROM(runner->core);
373	drawState.screenshotId = 0;
374	if (drawState.screenshot) {
375		mappedMemoryFree(drawState.screenshot, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
376	}
377
378	if (runner->core->config.port) {
379		if (runner->keySources) {
380			size_t i;
381			for (i = 0; runner->keySources[i].id; ++i) {
382				mInputMapSave(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->core->config));
383			}
384		}
385		mCoreConfigSave(&runner->core->config);
386	}
387	runner->core->deinit(runner->core);
388
389	GUIMenuItemListDeinit(&pauseMenu.items);
390	GUIMenuItemListDeinit(&stateSaveMenu.items);
391	GUIMenuItemListDeinit(&stateLoadMenu.items);
392}
393
394void mGUIRunloop(struct mGUIRunner* runner) {
395	while (true) {
396		char path[PATH_MAX];
397		if (!GUISelectFile(&runner->params, path, sizeof(path), 0)) {
398			break;
399		}
400		mGUIRun(runner, path);
401	}
402}