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 "feature/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/display.h>
17#include <psp2/kernel/processmgr.h>
18#include <psp2/kernel/threadmgr.h>
19#include <psp2/moduleinfo.h>
20#include <psp2/power.h>
21#include <psp2/sysmodule.h>
22#include <psp2/touch.h>
23
24#include <vita2d.h>
25
26static void _drawStart(void) {
27 vita2d_set_vblank_wait(false);
28 vita2d_start_drawing();
29 vita2d_clear_screen();
30}
31
32static void _drawEnd(void) {
33 static int oldVCount = 0;
34 int vcount = oldVCount;
35 vita2d_end_drawing();
36 oldVCount = sceDisplayGetVcount();
37 vita2d_set_vblank_wait(oldVCount + 1 >= vcount);
38 vita2d_swap_buffers();
39}
40
41static uint32_t _pollInput(void) {
42 SceCtrlData pad;
43 sceCtrlPeekBufferPositive(0, &pad, 1);
44 int input = 0;
45 if (pad.buttons & SCE_CTRL_TRIANGLE) {
46 input |= 1 << GUI_INPUT_CANCEL;
47 }
48 if (pad.buttons & SCE_CTRL_SQUARE) {
49 input |= 1 << mGUI_INPUT_SCREEN_MODE;
50 }
51 if (pad.buttons & SCE_CTRL_CIRCLE) {
52 input |= 1 << GUI_INPUT_BACK;
53 }
54 if (pad.buttons & SCE_CTRL_CROSS) {
55 input |= 1 << GUI_INPUT_SELECT;
56 }
57
58 if (pad.buttons & SCE_CTRL_UP || pad.ly < 64) {
59 input |= 1 << GUI_INPUT_UP;
60 }
61 if (pad.buttons & SCE_CTRL_DOWN || pad.ly >= 192) {
62 input |= 1 << GUI_INPUT_DOWN;
63 }
64 if (pad.buttons & SCE_CTRL_LEFT || pad.lx < 64) {
65 input |= 1 << GUI_INPUT_LEFT;
66 }
67 if (pad.buttons & SCE_CTRL_RIGHT || pad.lx >= 192) {
68 input |= 1 << GUI_INPUT_RIGHT;
69 }
70
71 return input;
72}
73
74static enum GUICursorState _pollCursor(unsigned* x, unsigned* y) {
75 SceTouchData touch;
76 sceTouchPeek(0, &touch, 1);
77 if (touch.reportNum < 1) {
78 return GUI_CURSOR_NOT_PRESENT;
79 }
80 *x = touch.report[0].x / 2;
81 *y = touch.report[0].y / 2;
82 return GUI_CURSOR_DOWN;
83}
84
85static int _batteryState(void) {
86 int charge = scePowerGetBatteryLifePercent();
87 int adapter = scePowerIsPowerOnline();
88 int state = 0;
89 if (adapter) {
90 state |= BATTERY_CHARGING;
91 }
92 charge /= 25;
93 return state | charge;
94}
95
96int main() {
97 vita2d_init();
98 struct GUIFont* font = GUIFontCreate();
99 struct mGUIRunner runner = {
100 .params = {
101 PSP2_HORIZONTAL_PIXELS, PSP2_VERTICAL_PIXELS,
102 font, "ux0:", _drawStart, _drawEnd,
103 _pollInput, _pollCursor,
104 _batteryState,
105 0, 0,
106
107 GUI_PARAMS_TRAIL
108 },
109 .configExtra = (struct GUIMenuItem[]) {
110 {
111 .title = "Screen mode",
112 .data = "screenMode",
113 .submenu = 0,
114 .state = 0,
115 .validStates = (const char*[]) {
116 "With Background",
117 "Without Background",
118 "Stretched",
119 "Fit Aspect Ratio",
120 },
121 .nStates = 3
122 }
123 },
124 .keySources = (struct GUIInputKeys[]) {
125 {
126 .name = "Vita Input",
127 .id = PSP2_INPUT,
128 .keyNames = (const char*[]) {
129 "Select",
130 0,
131 0,
132 "Start",
133 "Up",
134 "Right",
135 "Down",
136 "Left",
137 "L",
138 "R",
139 0, // L2?
140 0, // R2?
141 "\1\xC",
142 "\1\xA",
143 "\1\xB",
144 "\1\xD"
145 },
146 .nKeys = 16
147 },
148 { .id = 0 }
149 },
150 .nConfigExtra = 1,
151 .setup = mPSP2Setup,
152 .teardown = mPSP2Teardown,
153 .gameLoaded = mPSP2LoadROM,
154 .gameUnloaded = mPSP2UnloadROM,
155 .prepareForFrame = mPSP2PrepareForFrame,
156 .drawFrame = mPSP2Draw,
157 .drawScreenshot = mPSP2DrawScreenshot,
158 .paused = mPSP2Paused,
159 .unpaused = mPSP2Unpaused,
160 .incrementScreenMode = mPSP2IncrementScreenMode,
161 .pollGameInput = mPSP2PollInput
162 };
163
164 sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG_WIDE);
165
166 mGUIInit(&runner, "psvita");
167 mGUIRunloop(&runner);
168
169 vita2d_fini();
170 mGUIDeinit(&runner);
171
172 int pgfLoaded = sceSysmoduleIsLoaded(SCE_SYSMODULE_PGF);
173 if (pgfLoaded != SCE_SYSMODULE_LOADED) {
174 sceSysmoduleLoadModule(SCE_SYSMODULE_PGF);
175 }
176 GUIFontDestroy(font);
177 sceSysmoduleUnloadModule(SCE_SYSMODULE_PGF);
178
179 sceKernelExitProcess(0);
180 return 0;
181}