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