src/platform/psp2/main.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 "psp2-context.h"
7
8#include "gba/gba.h"
9#include "util/gui.h"
10#include "util/gui/font.h"
11#include "util/gui/file-select.h"
12
13#include <psp2/ctrl.h>
14#include <psp2/kernel/processmgr.h>
15#include <psp2/kernel/threadmgr.h>
16#include <psp2/moduleinfo.h>
17
18#include <vita2d.h>
19
20PSP2_MODULE_INFO(0, 0, "mGBA");
21
22static void _drawStart(void) {
23 vita2d_start_drawing();
24 vita2d_clear_screen();
25}
26
27static void _drawEnd(void) {
28 vita2d_end_drawing();
29 vita2d_swap_buffers();
30}
31
32static int _pollInput(void) {
33 SceCtrlData pad;
34 sceCtrlPeekBufferPositive(0, &pad, 1);
35 int input = 0;
36 if (pad.buttons & PSP2_CTRL_TRIANGLE) {
37 input |= 1 << GUI_INPUT_CANCEL;
38 }
39 if (pad.buttons & PSP2_CTRL_CIRCLE) {
40 input |= 1 << GUI_INPUT_BACK;
41 }
42 if (pad.buttons & PSP2_CTRL_CROSS) {
43 input |= 1 << GUI_INPUT_SELECT;
44 }
45
46 if (pad.buttons & PSP2_CTRL_UP) {
47 input |= 1 << GUI_INPUT_UP;
48 }
49 if (pad.buttons & PSP2_CTRL_DOWN) {
50 input |= 1 << GUI_INPUT_DOWN;
51 }
52 if (pad.buttons & PSP2_CTRL_LEFT) {
53 input |= 1 << GUI_INPUT_LEFT;
54 }
55 if (pad.buttons & PSP2_CTRL_RIGHT) {
56 input |= 1 << GUI_INPUT_RIGHT;
57 }
58 return input;
59}
60
61int main() {
62 printf("%s initializing", projectName);
63
64 vita2d_init();
65 struct GUIFont* font = GUIFontCreate();
66 GBAPSP2Setup();
67 struct GUIParams params = {
68 PSP2_HORIZONTAL_PIXELS, PSP2_VERTICAL_PIXELS,
69 font, "cache0:", _drawStart, _drawEnd, _pollInput, 0, 0,
70
71 GUI_PARAMS_TRAIL
72 };
73 GUIInit(¶ms);
74
75 while (true) {
76 bool running = true;
77 char path[256];
78 if (!GUISelectFile(¶ms, path, sizeof(path), GBAIsROM)) {
79 break;
80 }
81 if (!GBAPSP2LoadROM(path)) {
82 continue;
83 }
84 while (running) {
85 GBAPSP2Runloop();
86 GUIInvalidateKeys(¶ms);
87 while (true) {
88 int keys = 0;
89 _drawStart();
90 GBAPSP2Draw(0x80);
91 _drawEnd();
92 GUIPollInput(¶ms, &keys, 0);
93 if (keys & (1 << GUI_INPUT_CANCEL)) {
94 running = false;
95 break;
96 }
97 if (keys & (1 << GUI_INPUT_SELECT)) {
98 while (keys & (1 << GUI_INPUT_SELECT)) {
99 GUIPollInput(¶ms, 0, &keys);
100 }
101 break;
102 }
103 }
104 }
105 GBAPSP2UnloadROM();
106 }
107
108 GBAPSP2Teardown();
109
110 GUIFontDestroy(font);
111 vita2d_fini();
112
113 sceKernelExitProcess(0);
114 return 0;
115}