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