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 if (runner->setup) {
112 runner->setup(runner);
113 }
114}
115
116void GBAGUIDeinit(struct GBAGUIRunner* runner) {
117 if (runner->teardown) {
118 runner->teardown(runner);
119 }
120 if (runner->context.config.port) {
121 GBAConfigSave(&runner->context.config);
122 }
123 GBAContextDeinit(&runner->context);
124}
125
126void GBAGUIRunloop(struct GBAGUIRunner* runner) {
127 struct GBAGUIBackground drawState = {
128 .d = {
129 .draw = _drawState
130 },
131 .p = runner,
132 .screenshot = 0,
133 .screenshotId = 0
134 };
135 struct GUIMenu pauseMenu = {
136 .title = "Game Paused",
137 .index = 0,
138 .background = &runner->background.d
139 };
140 struct GUIMenu stateSaveMenu = {
141 .title = "Save state",
142 .index = 0,
143 .background = &drawState.d
144 };
145 struct GUIMenu stateLoadMenu = {
146 .title = "Load state",
147 .index = 0,
148 .background = &drawState.d
149 };
150 GUIMenuItemListInit(&pauseMenu.items, 0);
151 GUIMenuItemListInit(&stateSaveMenu.items, 9);
152 GUIMenuItemListInit(&stateLoadMenu.items, 9);
153 *GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Unpause", .data = (void*) RUNNER_CONTINUE };
154#if !(defined(__POWERPC__) || defined(__PPC__))
155 // PPC doesn't have working savestates yet
156 *GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Save state", .submenu = &stateSaveMenu };
157 *GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Load state", .submenu = &stateLoadMenu };
158
159 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 1", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_1) };
160 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 2", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_2) };
161 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 3", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_3) };
162 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 4", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_4) };
163 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 5", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_5) };
164 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 6", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_6) };
165 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 7", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_7) };
166 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 8", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_8) };
167 *GUIMenuItemListAppend(&stateSaveMenu.items) = (struct GUIMenuItem) { .title = "State 9", .data = (void*) (RUNNER_SAVE_STATE | RUNNER_STATE_9) };
168
169 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 1", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_1) };
170 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 2", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_2) };
171 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 3", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_3) };
172 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 4", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_4) };
173 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 5", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_5) };
174 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 6", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_6) };
175 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 7", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_7) };
176 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 8", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_8) };
177 *GUIMenuItemListAppend(&stateLoadMenu.items) = (struct GUIMenuItem) { .title = "State 9", .data = (void*) (RUNNER_LOAD_STATE | RUNNER_STATE_9) };
178#endif
179 *GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Take screenshot", .data = (void*) RUNNER_SCREENSHOT };
180 *GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Configure", .data = (void*) RUNNER_CONFIG };
181 *GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Exit game", .data = (void*) RUNNER_EXIT };
182
183 while (true) {
184 char path[256];
185 if (!GUISelectFile(&runner->params, path, sizeof(path), GBAIsROM)) {
186 break;
187 }
188
189 if (runner->params.guiPrepare) {
190 runner->params.guiPrepare();
191 }
192 // TODO: Message box API
193 runner->params.drawStart();
194 GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
195 runner->params.drawEnd();
196 runner->params.drawStart();
197 GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
198 runner->params.drawEnd();
199
200 if (!GBAContextLoadROM(&runner->context, path, true)) {
201 int i;
202 for (i = 0; i < 300; ++i) {
203 runner->params.drawStart();
204 GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Load failed!");
205 runner->params.drawEnd();
206 }
207 }
208 if (runner->params.guiFinish) {
209 runner->params.guiFinish();
210 }
211 GBAContextStart(&runner->context);
212 if (runner->gameLoaded) {
213 runner->gameLoaded(runner);
214 }
215 bool running = true;
216 while (running) {
217 while (true) {
218 uint32_t guiKeys;
219 GUIPollInput(&runner->params, &guiKeys, 0);
220 if (guiKeys & (1 << GUI_INPUT_CANCEL)) {
221 break;
222 }
223 if (guiKeys & (1 << GBA_GUI_INPUT_INCREASE_BRIGHTNESS)) {
224 if (runner->luminanceSource.luxLevel < 10) {
225 ++runner->luminanceSource.luxLevel;
226 }
227 }
228 if (guiKeys & (1 << GBA_GUI_INPUT_DECREASE_BRIGHTNESS)) {
229 if (runner->luminanceSource.luxLevel > 0) {
230 --runner->luminanceSource.luxLevel;
231 }
232 }
233 if (guiKeys & (1 << GBA_GUI_INPUT_SCREEN_MODE) && runner->incrementScreenMode) {
234 runner->incrementScreenMode(runner);
235 }
236 uint16_t keys = runner->pollGameInput(runner);
237 if (runner->prepareForFrame) {
238 runner->prepareForFrame(runner);
239 }
240 GBAContextFrame(&runner->context, keys);
241 if (runner->drawFrame) {
242 runner->params.drawStart();
243 runner->drawFrame(runner, false);
244 runner->params.drawEnd();
245 }
246 }
247
248 if (runner->paused) {
249 runner->paused(runner);
250 }
251 GUIInvalidateKeys(&runner->params);
252 uint32_t keys = 0xFFFFFFFF; // Huge hack to avoid an extra variable!
253 struct GUIMenuItem* item;
254 enum GUIMenuExitReason reason = GUIShowMenu(&runner->params, &pauseMenu, &item);
255 if (reason == GUI_MENU_EXIT_ACCEPT) {
256 struct VFile* vf;
257 switch (((int) item->data) & RUNNER_COMMAND_MASK) {
258 case RUNNER_EXIT:
259 running = false;
260 keys = 0;
261 break;
262 case RUNNER_SAVE_STATE:
263 vf = GBAGetState(runner->context.gba, 0, ((int) item->data) >> 16, true);
264 if (vf) {
265 GBASaveStateNamed(runner->context.gba, vf, true);
266 vf->close(vf);
267 }
268 break;
269 case RUNNER_LOAD_STATE:
270 vf = GBAGetState(runner->context.gba, 0, ((int) item->data) >> 16, false);
271 if (vf) {
272 GBALoadStateNamed(runner->context.gba, vf);
273 vf->close(vf);
274 }
275 break;
276 case RUNNER_SCREENSHOT:
277 GBATakeScreenshot(runner->context.gba, 0);
278 break;
279 case RUNNER_CONFIG:
280 GBAGUIShowConfig(runner, runner->configExtra, runner->nConfigExtra);
281 break;
282 case RUNNER_CONTINUE:
283 break;
284 }
285 }
286 int frames = 0;
287 GUIPollInput(&runner->params, 0, &keys);
288 while (keys && frames < 30) {
289 ++frames;
290 runner->params.drawStart();
291 runner->drawFrame(runner, true);
292 runner->params.drawEnd();
293 GUIPollInput(&runner->params, 0, &keys);
294 }
295 if (runner->unpaused) {
296 runner->unpaused(runner);
297 }
298 }
299 GBAContextStop(&runner->context);
300 if (runner->gameUnloaded) {
301 runner->gameUnloaded(runner);
302 }
303 GBAContextUnloadROM(&runner->context);
304 drawState.screenshotId = 0;
305 }
306 if (drawState.screenshot) {
307 mappedMemoryFree(drawState.screenshot, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
308 }
309 GUIMenuItemListDeinit(&pauseMenu.items);
310 GUIMenuItemListDeinit(&stateSaveMenu.items);
311 GUIMenuItemListDeinit(&stateLoadMenu.items);
312}