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