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 },
120 .nStates = 3
121 }
122 },
123 .keySources = (struct GUIInputKeys[]) {
124 {
125 .name = "Vita Input",
126 .id = PSP2_INPUT,
127 .keyNames = (const char*[]) {
128 "Select",
129 0,
130 0,
131 "Start",
132 "Up",
133 "Right",
134 "Down",
135 "Left",
136 "L",
137 "R",
138 0, // L2?
139 0, // R2?
140 "\1\xC",
141 "\1\xA",
142 "\1\xB",
143 "\1\xD"
144 },
145 .nKeys = 16
146 },
147 { .id = 0 }
148 },
149 .nConfigExtra = 1,
150 .setup = mPSP2Setup,
151 .teardown = mPSP2Teardown,
152 .gameLoaded = mPSP2LoadROM,
153 .gameUnloaded = mPSP2UnloadROM,
154 .prepareForFrame = mPSP2PrepareForFrame,
155 .drawFrame = mPSP2Draw,
156 .drawScreenshot = mPSP2DrawScreenshot,
157 .paused = 0,
158 .unpaused = mPSP2Unpaused,
159 .incrementScreenMode = mPSP2IncrementScreenMode,
160 .pollGameInput = mPSP2PollInput
161 };
162
163 mGUIInit(&runner, "psvita");
164 mGUIRunloop(&runner);
165
166 vita2d_fini();
167 mGUIDeinit(&runner);
168
169 int pgfLoaded = sceSysmoduleIsLoaded(SCE_SYSMODULE_PGF);
170 if (pgfLoaded != SCE_SYSMODULE_LOADED) {
171 sceSysmoduleLoadModule(SCE_SYSMODULE_PGF);
172 }
173 GUIFontDestroy(font);
174 sceSysmoduleUnloadModule(SCE_SYSMODULE_PGF);
175
176 sceKernelExitProcess(0);
177 return 0;
178}