all repos — mgba @ d8c3306bf233812bc7487c2e1958d15bdabc3539

mGBA Game Boy Advance Emulator

PSP2: Gyro and accel support
Jeffrey Pfau jeffrey@endrift.com
Sat, 29 Aug 2015 15:41:09 -0700
commit

d8c3306bf233812bc7487c2e1958d15bdabc3539

parent

d690d3b1fbd3c108a0b1b160f59869ba33d49ad0

2 files changed, 41 insertions(+), 1 deletions(-)

jump to
M src/platform/psp2/CMakeLists.txtsrc/platform/psp2/CMakeLists.txt

@@ -2,7 +2,7 @@ file(GLOB PLATFORM_SRC ${CMAKE_SOURCE_DIR}/src/platform/psp2/*.c)

execute_process(COMMAND ${OBJCOPY} -I binary -O elf32-littlearm -B arm font.png ${CMAKE_BINARY_DIR}/font.o WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/res) -set(PLATFORM_LIBRARY -lvita2d -lSceCtrl_stub -lSceRtc_stub -lSceGxm_stub -lSceDisplay_stub -lSceAudio_stub -lpng -lz -l${M_LIBRARY}) +set(PLATFORM_LIBRARY -lvita2d -lSceCtrl_stub -lSceRtc_stub -lSceGxm_stub -lSceDisplay_stub -lSceAudio_stub -lSceMotion_stub -lpng -lz -l${M_LIBRARY}) add_executable(${BINARY_NAME}.elf ${PLATFORM_SRC} ${GUI_SRC} ${CMAKE_BINARY_DIR}/font.o) target_link_libraries(${BINARY_NAME}.elf ${BINARY_NAME} ${PLATFORM_LIBRARY})
M src/platform/psp2/psp2-context.csrc/platform/psp2/psp2-context.c

@@ -23,6 +23,7 @@ #include <psp2/ctrl.h>

#include <psp2/display.h> #include <psp2/gxm.h> #include <psp2/kernel/sysmem.h> +#include <psp2/motion.h> #include <vita2d.h>

@@ -30,6 +31,10 @@ static struct GBAContext context;

static struct GBAVideoSoftwareRenderer renderer; static vita2d_texture* tex; static Thread audioThread; +static struct GBASceRotationSource { + struct GBARotationSource d; + struct SceMotionSensorState state; +} rotation; static bool fullscreen = false;

@@ -74,6 +79,26 @@ sceAudioOutReleasePort(audioPort);

return 0; } +static void _sampleRotation(struct GBARotationSource* source) { + struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source; + sceMotionGetSensorState(&rotation->state, 1); +} + +static int32_t _readTiltX(struct GBARotationSource* source) { + struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source; + return rotation->state.accelerometer.x * 0x60000000; +} + +static int32_t _readTiltY(struct GBARotationSource* source) { + struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source; + return rotation->state.accelerometer.y * 0x60000000; +} + +static int32_t _readGyroZ(struct GBARotationSource* source) { + struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source; + return rotation->state.gyro.z * 0x10000000; +} + void GBAPSP2Setup() { GBAContextInit(&context, 0); struct GBAOptions opts = {

@@ -104,6 +129,13 @@ GBAVideoSoftwareRendererCreate(&renderer);

renderer.outputBuffer = vita2d_texture_get_datap(tex); renderer.outputBufferStride = 256; context.renderer = &renderer.d; + + rotation.d.sample = _sampleRotation; + rotation.d.readTiltX = _readTiltX; + rotation.d.readTiltY = _readTiltY; + rotation.d.readGyroZ = _readGyroZ; + context.gba->rotationSource = &rotation.d; + printf("%s starting", projectName); }

@@ -120,6 +152,10 @@ printf("%s started!", gameTitle);

double ratio = GBAAudioCalculateRatio(1, 60, 1); blip_set_rates(context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio); blip_set_rates(context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio); + + if (context.gba->memory.hw.devices & (HW_TILT | HW_GYRO)) { + sceMotionStartSampling(); + } CircleBufferInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample)); MutexInit(&audioContext.mutex);

@@ -194,6 +230,10 @@ }

} void GBAPSP2UnloadROM(void) { + if (context.gba->memory.hw.devices & (HW_TILT | HW_GYRO)) { + sceMotionStopSampling(); + } + GBAContextStop(&context); }