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