all repos — mgba @ 331d92d36388ae2e2007b3be34c0889a246504a5

mGBA Game Boy Advance Emulator

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				},
121				.nStates = 3
122			}
123		},
124		.keySources = (struct GUIInputKeys[]) {
125			{
126				.name = "Vita Input",
127				.id = PSP2_INPUT,
128				.keyNames = (const char*[]) {
129					"Select",
130					0,
131					0,
132					"Start",
133					"Up",
134					"Right",
135					"Down",
136					"Left",
137					"L",
138					"R",
139					0, // L2?
140					0, // R2?
141					"Triangle",
142					"Circle",
143					"Cross",
144					"Square"
145				},
146				.nKeys = 16
147			},
148			{ .id = 0 }
149		},
150		.nConfigExtra = 1,
151		.setup = GBAPSP2Setup,
152		.teardown = GBAPSP2Teardown,
153		.gameLoaded = GBAPSP2LoadROM,
154		.gameUnloaded = GBAPSP2UnloadROM,
155		.prepareForFrame = GBAPSP2PrepareForFrame,
156		.drawFrame = GBAPSP2Draw,
157		.drawScreenshot = GBAPSP2DrawScreenshot,
158		.paused = 0,
159		.unpaused = 0,
160		.incrementScreenMode = GBAPSP2IncrementScreenMode,
161		.pollGameInput = GBAPSP2PollInput
162	};
163
164	GBAGUIInit(&runner, "psvita");
165	GBAGUIRunloop(&runner);
166	GBAGUIDeinit(&runner);
167
168	GUIFontDestroy(font);
169	vita2d_fini();
170
171	sceKernelExitProcess(0);
172	return 0;
173}