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