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 "gba/gui/gui-runner.h"
10#include "util/gui.h"
11#include "util/gui/font.h"
12#include "util/gui/file-select.h"
13#include "util/gui/menu.h"
14
15#include <psp2/ctrl.h>
16#include <psp2/kernel/processmgr.h>
17#include <psp2/kernel/threadmgr.h>
18#include <psp2/moduleinfo.h>
19#include <psp2/touch.h>
20
21#include <vita2d.h>
22
23PSP2_MODULE_INFO(0, 0, "mGBA");
24
25static void _drawStart(void) {
26 vita2d_start_drawing();
27 vita2d_clear_screen();
28}
29
30static void _drawEnd(void) {
31 vita2d_end_drawing();
32 vita2d_swap_buffers();
33}
34
35static uint32_t _pollInput(void) {
36 SceCtrlData pad;
37 sceCtrlPeekBufferPositive(0, &pad, 1);
38 int input = 0;
39 if (pad.buttons & PSP2_CTRL_TRIANGLE) {
40 input |= 1 << GUI_INPUT_CANCEL;
41 }
42 if (pad.buttons & PSP2_CTRL_SQUARE) {
43 input |= 1 << GBA_GUI_INPUT_SCREEN_MODE;
44 }
45 if (pad.buttons & PSP2_CTRL_CIRCLE) {
46 input |= 1 << GUI_INPUT_BACK;
47 }
48 if (pad.buttons & PSP2_CTRL_CROSS) {
49 input |= 1 << GUI_INPUT_SELECT;
50 }
51
52 if (pad.buttons & PSP2_CTRL_UP || pad.ly < 64) {
53 input |= 1 << GUI_INPUT_UP;
54 }
55 if (pad.buttons & PSP2_CTRL_DOWN || pad.ly >= 192) {
56 input |= 1 << GUI_INPUT_DOWN;
57 }
58 if (pad.buttons & PSP2_CTRL_LEFT || pad.lx < 64) {
59 input |= 1 << GUI_INPUT_LEFT;
60 }
61 if (pad.buttons & PSP2_CTRL_RIGHT || pad.lx >= 192) {
62 input |= 1 << GUI_INPUT_RIGHT;
63 }
64
65 return input;
66}
67
68static enum GUICursorState _pollCursor(int* x, int* y) {
69 SceTouchData touch;
70 sceTouchPeek(0, &touch, 1);
71 if (touch.reportNum < 1) {
72 return GUI_CURSOR_NOT_PRESENT;
73 }
74 *x = touch.report[0].x / 2;
75 *y = touch.report[0].y / 2;
76 return GUI_CURSOR_DOWN;
77}
78
79
80int main() {
81 vita2d_init();
82 struct GUIFont* font = GUIFontCreate();
83 struct GBAGUIRunner runner = {
84 .params = {
85 PSP2_HORIZONTAL_PIXELS, PSP2_VERTICAL_PIXELS,
86 font, "cache0:", _drawStart, _drawEnd,
87 _pollInput, _pollCursor,
88 0,
89 0, 0,
90
91 GUI_PARAMS_TRAIL
92 },
93 .configExtra = (struct GUIMenuItem[]) {
94 {
95 .title = "Screen mode",
96 .data = "screenMode",
97 .submenu = 0,
98 .state = 0,
99 .validStates = (const char*[]) {
100 "With Background",
101 "Without Background",
102 "Stretched",
103 0
104 }
105 }
106 },
107 .nConfigExtra = 1,
108 .setup = GBAPSP2Setup,
109 .teardown = GBAPSP2Teardown,
110 .gameLoaded = GBAPSP2LoadROM,
111 .gameUnloaded = GBAPSP2UnloadROM,
112 .prepareForFrame = GBAPSP2PrepareForFrame,
113 .drawFrame = GBAPSP2Draw,
114 .drawScreenshot = GBAPSP2DrawScreenshot,
115 .paused = 0,
116 .unpaused = 0,
117 .incrementScreenMode = GBAPSP2IncrementScreenMode,
118 .pollGameInput = GBAPSP2PollInput
119 };
120
121 GBAGUIInit(&runner, "psvita");
122 GBAGUIRunloop(&runner);
123 GBAGUIDeinit(&runner);
124
125 GUIFontDestroy(font);
126 vita2d_fini();
127
128 sceKernelExitProcess(0);
129 return 0;
130}