all repos — mgba @ cd0a352a33d1613196ad5c3fa7e2af055e0fbecc

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 = NULL;
 67		if (gbaBackground->p->core->platform(gbaBackground->p->core) == PLATFORM_GBA) {
 68			vf = GBAGetState(gbaBackground->p->core->board, gbaBackground->p->core->dirs.state, stateId, false);
 69		}
 70		uint32_t* pixels = gbaBackground->screenshot;
 71		if (!pixels) {
 72			pixels = anonymousMemoryMap(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
 73			gbaBackground->screenshot = pixels;
 74		}
 75		bool success = false;
 76		if (vf && isPNG(vf) && pixels) {
 77			png_structp png = PNGReadOpen(vf, PNG_HEADER_BYTES);
 78			png_infop info = png_create_info_struct(png);
 79			png_infop end = png_create_info_struct(png);
 80			if (png && info && end) {
 81				success = PNGReadHeader(png, info);
 82				success = success && PNGReadPixels(png, info, pixels, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, VIDEO_HORIZONTAL_PIXELS);
 83				success = success && PNGReadFooter(png, end);
 84			}
 85			PNGReadClose(png, info, end);
 86		}
 87		if (vf) {
 88			vf->close(vf);
 89		}
 90		if (success) {
 91			gbaBackground->p->drawScreenshot(gbaBackground->p, pixels, true);
 92			gbaBackground->screenshotId = (int) id;
 93		} else if (gbaBackground->p->drawFrame) {
 94			gbaBackground->p->drawFrame(gbaBackground->p, true);
 95		}
 96	}
 97}
 98
 99static void _updateLux(struct GBALuminanceSource* lux) {
100	UNUSED(lux);
101}
102
103static uint8_t _readLux(struct GBALuminanceSource* lux) {
104	struct mGUIRunnerLux* runnerLux = (struct mGUIRunnerLux*) lux;
105	int value = 0x16;
106	if (runnerLux->luxLevel > 0) {
107		value += GBA_LUX_LEVELS[runnerLux->luxLevel - 1];
108	}
109	return 0xFF - value;
110}
111
112void mGUIInit(struct mGUIRunner* runner, const char* port) {
113	GUIInit(&runner->params);
114	runner->port = port;
115	runner->core = NULL;
116	runner->luminanceSource.d.readLuminance = _readLux;
117	runner->luminanceSource.d.sample = _updateLux;
118	runner->luminanceSource.luxLevel = 0;
119	runner->background.d.draw = _drawBackground;
120	runner->background.p = runner;
121	runner->fps = 0;
122	runner->lastFpsCheck = 0;
123	runner->totalDelta = 0;
124	CircleBufferInit(&runner->fpsBuffer, FPS_BUFFER_SIZE * sizeof(uint32_t));
125}
126
127void mGUIDeinit(struct mGUIRunner* runner) {
128	if (runner->teardown) {
129		runner->teardown(runner);
130	}
131	CircleBufferDeinit(&runner->fpsBuffer);
132}
133
134void mGUIRun(struct mGUIRunner* runner, const char* path) {
135	struct mGUIBackground drawState = {
136		.d = {
137			.draw = _drawState
138		},
139		.p = runner,
140		.screenshot = 0,
141		.screenshotId = 0
142	};
143	struct GUIMenu pauseMenu = {
144		.title = "Game Paused",
145		.index = 0,
146		.background = &runner->background.d
147	};
148	struct GUIMenu stateSaveMenu = {
149		.title = "Save state",
150		.index = 0,
151		.background = &drawState.d
152	};
153	struct GUIMenu stateLoadMenu = {
154		.title = "Load state",
155		.index = 0,
156		.background = &drawState.d
157	};
158	GUIMenuItemListInit(&pauseMenu.items, 0);
159	GUIMenuItemListInit(&stateSaveMenu.items, 9);
160	GUIMenuItemListInit(&stateLoadMenu.items, 9);
161	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Unpause", .data = (void*) RUNNER_CONTINUE };
162	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Save state", .submenu = &stateSaveMenu };
163	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Load state", .submenu = &stateLoadMenu };
164
165	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 1", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_1) };
166	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 2", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_2) };
167	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 3", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_3) };
168	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 4", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_4) };
169	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 5", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_5) };
170	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 6", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_6) };
171	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 7", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_7) };
172	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 8", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_8) };
173	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 9", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_9) };
174
175	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 1", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_1) };
176	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 2", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_2) };
177	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 3", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_3) };
178	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 4", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_4) };
179	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 5", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_5) };
180	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 6", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_6) };
181	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 7", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_7) };
182	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 8", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_8) };
183	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 9", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_9) };
184
185	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Take screenshot", .data = (void*) RUNNER_SCREENSHOT };
186	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Configure", .data = (void*) RUNNER_CONFIG };
187	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Reset game", .data = (void*) RUNNER_RESET };
188	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Exit game", .data = (void*) RUNNER_EXIT };
189
190	// TODO: Message box API
191	runner->params.drawStart();
192	if (runner->params.guiPrepare) {
193		runner->params.guiPrepare();
194	}
195	GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, "Loading...");
196	if (runner->params.guiFinish) {
197		runner->params.guiFinish();
198	}
199	runner->params.drawEnd();
200
201	bool found = false;
202	runner->core = mCoreFind(path);
203	if (runner->core) {
204		runner->core->init(runner->core);
205		mInputMapInit(&runner->core->inputMap, &GBAInputInfo);
206		mCoreInitConfig(runner->core, runner->port);
207		if (runner->core->platform(runner->core) == PLATFORM_GBA) {
208			((struct GBA*) runner->core->board)->luminanceSource = &runner->luminanceSource.d;
209		}
210		if (runner->core->config.port && runner->keySources) {
211			size_t i;
212			for (i = 0; runner->keySources[i].id; ++i) {
213				mInputMapLoad(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->core->config));
214			}
215		}
216		found = mCoreLoadFile(runner->core, path);
217		if (!found) {
218			runner->core->deinit(runner->core);
219		}
220	}
221
222	if (!found) {
223		int i;
224		for (i = 0; i < 240; ++i) {
225			runner->params.drawStart();
226			if (runner->params.guiPrepare) {
227				runner->params.guiPrepare();
228			}
229			GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, "Load failed!");
230			if (runner->params.guiFinish) {
231				runner->params.guiFinish();
232			}
233			runner->params.drawEnd();
234		}
235		return;
236	}
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}