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