all repos — mgba @ 2725d6ed2f0cafdf9c993a18d03e6ca85304af87

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