all repos — mgba @ 8615defda763c928eadb4c0919081a74b6bd28fc

mGBA Game Boy Advance Emulator

src/gba/gui/gui-runner.c (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#include "gui-runner.h"
  7
  8#include "gba/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 = GBAGetState(gbaBackground->p->core->board, gbaBackground->p->core->dirs.state, 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->core = GBACoreCreate();
112	runner->core->init(runner->core);
113	mInputMapInit(&runner->core->inputMap, &GBAInputInfo);
114	mCoreInitConfig(runner->core, port);
115	runner->luminanceSource.d.readLuminance = _readLux;
116	runner->luminanceSource.d.sample = _updateLux;
117	runner->luminanceSource.luxLevel = 0;
118	((struct GBA*) runner->core->board)->luminanceSource = &runner->luminanceSource.d;
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	if (runner->setup) {
126		runner->setup(runner);
127	}
128
129	if (runner->core->config.port && runner->keySources) {
130		size_t i;
131		for (i = 0; runner->keySources[i].id; ++i) {
132			mInputMapLoad(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->core->config));
133		}
134	}
135}
136
137void mGUIDeinit(struct mGUIRunner* runner) {
138	if (runner->teardown) {
139		runner->teardown(runner);
140	}
141	if (runner->core->config.port) {
142		if (runner->keySources) {
143			size_t i;
144			for (i = 0; runner->keySources[i].id; ++i) {
145				mInputMapSave(&runner->core->inputMap, runner->keySources[i].id, mCoreConfigGetInput(&runner->core->config));
146			}
147		}
148		mCoreConfigSave(&runner->core->config);
149	}
150	CircleBufferDeinit(&runner->fpsBuffer);
151	runner->core->deinit(runner->core);
152}
153
154void mGUIRun(struct mGUIRunner* runner, const char* path) {
155	struct mGUIBackground drawState = {
156		.d = {
157			.draw = _drawState
158		},
159		.p = runner,
160		.screenshot = 0,
161		.screenshotId = 0
162	};
163	struct GUIMenu pauseMenu = {
164		.title = "Game Paused",
165		.index = 0,
166		.background = &runner->background.d
167	};
168	struct GUIMenu stateSaveMenu = {
169		.title = "Save state",
170		.index = 0,
171		.background = &drawState.d
172	};
173	struct GUIMenu stateLoadMenu = {
174		.title = "Load state",
175		.index = 0,
176		.background = &drawState.d
177	};
178	GUIMenuItemListInit(&pauseMenu.items, 0);
179	GUIMenuItemListInit(&stateSaveMenu.items, 9);
180	GUIMenuItemListInit(&stateLoadMenu.items, 9);
181	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Unpause", .data = (void*) RUNNER_CONTINUE };
182	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Save state", .submenu = &stateSaveMenu };
183	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Load state", .submenu = &stateLoadMenu };
184
185	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 1", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_1) };
186	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 2", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_2) };
187	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 3", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_3) };
188	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 4", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_4) };
189	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 5", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_5) };
190	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 6", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_6) };
191	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 7", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_7) };
192	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 8", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_8) };
193	*GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 9", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_9) };
194
195	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 1", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_1) };
196	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 2", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_2) };
197	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 3", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_3) };
198	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 4", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_4) };
199	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 5", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_5) };
200	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 6", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_6) };
201	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 7", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_7) };
202	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 8", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_8) };
203	*GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 9", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_9) };
204
205	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Take screenshot", .data = (void*) RUNNER_SCREENSHOT };
206	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Configure", .data = (void*) RUNNER_CONFIG };
207	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Reset game", .data = (void*) RUNNER_RESET };
208	*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Exit game", .data = (void*) RUNNER_EXIT };
209
210	// TODO: Message box API
211	runner->params.drawStart();
212	if (runner->params.guiPrepare) {
213		runner->params.guiPrepare();
214	}
215	GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, "Loading...");
216	if (runner->params.guiFinish) {
217		runner->params.guiFinish();
218	}
219	runner->params.drawEnd();
220
221	if (!mCoreLoadFile(runner->core, path)) {
222		int i;
223		for (i = 0; i < 240; ++i) {
224			runner->params.drawStart();
225			if (runner->params.guiPrepare) {
226				runner->params.guiPrepare();
227			}
228			GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, "Load failed!");
229			if (runner->params.guiFinish) {
230				runner->params.guiFinish();
231			}
232			runner->params.drawEnd();
233		}
234		return;
235	}
236	mCoreAutoloadSave(runner->core);
237	runner->core->reset(runner->core);
238	bool running = true;
239	if (runner->gameLoaded) {
240		runner->gameLoaded(runner);
241	}
242	while (running) {
243		CircleBufferClear(&runner->fpsBuffer);
244		runner->totalDelta = 0;
245		runner->fps = 0;
246		struct timeval tv;
247		gettimeofday(&tv, 0);
248		runner->lastFpsCheck = 1000000LL * tv.tv_sec + tv.tv_usec;
249
250		while (true) {
251#ifdef _3DS
252			running = aptMainLoop();
253			if (!running) {
254				break;
255			}
256#endif
257			uint32_t guiKeys;
258			GUIPollInput(&runner->params, &guiKeys, 0);
259			if (guiKeys & (1 << GUI_INPUT_CANCEL)) {
260				break;
261			}
262			if (guiKeys & (1 << mGUI_INPUT_INCREASE_BRIGHTNESS)) {
263				if (runner->luminanceSource.luxLevel < 10) {
264					++runner->luminanceSource.luxLevel;
265				}
266			}
267			if (guiKeys & (1 << mGUI_INPUT_DECREASE_BRIGHTNESS)) {
268				if (runner->luminanceSource.luxLevel > 0) {
269					--runner->luminanceSource.luxLevel;
270				}
271			}
272			if (guiKeys & (1 << mGUI_INPUT_SCREEN_MODE) && runner->incrementScreenMode) {
273				runner->incrementScreenMode(runner);
274			}
275			uint16_t keys = runner->pollGameInput(runner);
276			if (runner->prepareForFrame) {
277				runner->prepareForFrame(runner);
278			}
279			runner->core->setKeys(runner->core, keys);
280			runner->core->runFrame(runner->core);
281			if (runner->drawFrame) {
282				int drawFps = false;
283				mCoreConfigGetIntValue(&runner->core->config, "fpsCounter", &drawFps);
284
285				runner->params.drawStart();
286				runner->drawFrame(runner, false);
287				if (drawFps) {
288					if (runner->params.guiPrepare) {
289						runner->params.guiPrepare();
290					}
291					GUIFontPrintf(runner->params.font, 0, GUIFontHeight(runner->params.font), GUI_ALIGN_LEFT, 0x7FFFFFFF, "%.2f fps", runner->fps);
292					if (runner->params.guiFinish) {
293						runner->params.guiFinish();
294					}
295				}
296				runner->params.drawEnd();
297
298				if (runner->core->frameCounter(runner->core) % FPS_GRANULARITY == 0) {
299					if (drawFps) {
300						struct timeval tv;
301						gettimeofday(&tv, 0);
302						uint64_t t = 1000000LL * tv.tv_sec + tv.tv_usec;
303						uint64_t delta = t - runner->lastFpsCheck;
304						runner->lastFpsCheck = t;
305						if (delta > 0x7FFFFFFFLL) {
306							CircleBufferClear(&runner->fpsBuffer);
307							runner->fps = 0;
308						}
309						if (CircleBufferSize(&runner->fpsBuffer) == CircleBufferCapacity(&runner->fpsBuffer)) {
310							int32_t last;
311							CircleBufferRead32(&runner->fpsBuffer, &last);
312							runner->totalDelta -= last;
313						}
314						CircleBufferWrite32(&runner->fpsBuffer, delta);
315						runner->totalDelta += delta;
316						runner->fps = (CircleBufferSize(&runner->fpsBuffer) * FPS_GRANULARITY * 1000000.0f) / (runner->totalDelta * sizeof(uint32_t));
317					}
318				}
319			}
320		}
321
322		if (runner->paused) {
323			runner->paused(runner);
324		}
325		GUIInvalidateKeys(&runner->params);
326		uint32_t keys = 0xFFFFFFFF; // Huge hack to avoid an extra variable!
327		struct GUIMenuItem* item;
328		enum GUIMenuExitReason reason = GUIShowMenu(&runner->params, &pauseMenu, &item);
329		if (reason == GUI_MENU_EXIT_ACCEPT) {
330			switch (((int) item->data) & RUNNER_COMMAND_MASK) {
331			case RUNNER_EXIT:
332				running = false;
333				keys = 0;
334				break;
335			case RUNNER_RESET:
336				runner->core->reset(runner->core);
337				break;
338			case RUNNER_SAVE_STATE:
339				mCoreSaveState(runner->core, ((int) item->data) >> 16, SAVESTATE_SCREENSHOT);
340				break;
341			case RUNNER_LOAD_STATE:
342				mCoreLoadState(runner->core, ((int) item->data) >> 16, SAVESTATE_SCREENSHOT);
343				break;
344			case RUNNER_SCREENSHOT:
345				mCoreTakeScreenshot(runner->core);
346				break;
347			case RUNNER_CONFIG:
348				mGUIShowConfig(runner, runner->configExtra, runner->nConfigExtra);
349				mCoreConfigGetIntValue(&runner->core->config, "frameskip", &((struct GBA*) runner->core->board)->video.frameskip);
350				break;
351			case RUNNER_CONTINUE:
352				break;
353			}
354		}
355		int frames = 0;
356		GUIPollInput(&runner->params, 0, &keys);
357		while (keys && frames < 30) {
358			++frames;
359			runner->params.drawStart();
360			runner->drawFrame(runner, true);
361			runner->params.drawEnd();
362			GUIPollInput(&runner->params, 0, &keys);
363		}
364		if (runner->unpaused) {
365			runner->unpaused(runner);
366		}
367	}
368	if (runner->gameUnloaded) {
369		runner->gameUnloaded(runner);
370	}
371	runner->core->unloadROM(runner->core);
372	drawState.screenshotId = 0;
373	if (drawState.screenshot) {
374		mappedMemoryFree(drawState.screenshot, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
375	}
376	GUIMenuItemListDeinit(&pauseMenu.items);
377	GUIMenuItemListDeinit(&stateSaveMenu.items);
378	GUIMenuItemListDeinit(&stateLoadMenu.items);
379}
380
381void mGUIRunloop(struct mGUIRunner* runner) {
382	while (true) {
383		char path[PATH_MAX];
384		if (!GUISelectFile(&runner->params, path, sizeof(path), 0)) {
385			break;
386		}
387		mGUIRun(runner, path);
388	}
389}