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