all repos — mgba @ 178612a47196bb255b3a3554f2abf99548db51c4

mGBA Game Boy Advance Emulator

src/gba/context/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 "util/gui/file-select.h"
 9#include "util/gui/font.h"
10
11void GBAGUIInit(struct GBAGUIRunner* runner, const char* port) {
12	GUIInit(&runner->params);
13	GBAContextInit(&runner->context, port);
14	if (runner->setup) {
15		runner->setup(runner);
16	}
17}
18
19void GBAGUIDeinit(struct GBAGUIRunner* runner) {
20	if (runner->teardown) {
21		runner->teardown(runner);
22	}
23	GBAContextDeinit(&runner->context);
24}
25
26void GBAGUIRunloop(struct GBAGUIRunner* runner) {
27	while (true) {
28		if (runner->params.guiPrepare) {
29			runner->params.guiPrepare();
30		}
31		char path[256];
32		if (!GUISelectFile(&runner->params, path, sizeof(path), GBAIsROM)) {
33			if (runner->params.guiFinish) {
34				runner->params.guiFinish();
35			}
36			return;
37		}
38		if (runner->params.guiFinish) {
39			runner->params.guiFinish();
40		}
41
42		// TODO: Message box API
43		runner->params.drawStart();
44		GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
45		runner->params.drawEnd();
46		runner->params.drawStart();
47		GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Loading...");
48		runner->params.drawEnd();
49
50		if (!GBAContextLoadROM(&runner->context, path, true)) {
51			int i;
52			for (i = 0; i < 300; ++i) {
53				runner->params.drawStart();
54				GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_TEXT_CENTER, 0xFFFFFFFF, "Load failed!");
55				runner->params.drawEnd();
56			}
57		}
58		GBAContextStart(&runner->context);
59		if (runner->gameLoaded) {
60			runner->gameLoaded(runner);
61		}
62		while (true) {
63			int guiKeys = runner->params.pollInput();
64			if (guiKeys & (1 << GUI_INPUT_CANCEL)) {
65				break;
66			}
67			uint16_t keys = runner->pollGameInput(runner);
68			if (runner->prepareForFrame) {
69				runner->prepareForFrame(runner);
70			}
71			GBAContextFrame(&runner->context, keys);
72			if (runner->drawFrame) {
73				runner->drawFrame(runner, false);
74			}
75		}
76		GBAContextStop(&runner->context);
77		if (runner->gameUnloaded) {
78			runner->gameUnloaded(runner);
79		}
80		GBAContextUnloadROM(&runner->context);
81	}
82}