[ADD] stick system

This commit is contained in:
SemvdH
2021-05-20 11:21:06 +02:00
parent 0de0766cbc
commit 748c3416b4
7 changed files with 142 additions and 31 deletions

View File

@@ -0,0 +1,24 @@
#include "control_input.h"
void ctrl_input_get_leftstick(SceCtrlData *pad, stick_data *stickdata)
{
int16_t lxpos = -128 + pad->lx; // full left is 0 so -128 and full right is 255 so 128
int16_t lypos = -128 + pad->ly;
stickdata->x = lxpos;
stickdata->y = lypos;
}
void ctrl_input_get_rightstick(SceCtrlData *pad, stick_data *stickdata)
{
int16_t rxpos = -128 + pad->rx; // full left is 0 so -128 and full right is 255 so 128
int16_t rypos = -128 + pad->ry;
stickdata->x = rxpos;
stickdata->y = rypos;
}
float ctrl_input_calc_value(int16_t pos, SceUInt64 deltaTime)
{
return (float)((pos) * (deltaTime / 1000.0));
}

View File

@@ -0,0 +1,44 @@
#ifndef CONTROL_INPUT_H
#define CONTROL_INPUT_H
#include <stdio.h>
#include <psp2/ctrl.h>
/**
* @brief a struct that holds the x and y pos for a stick. < 0 is left or down, > 0 is right or up. Value goes from 0 to 128
*
*/
typedef struct
{
int16_t x;
int16_t y;
} stick_data;
/**
* @brief gets the value for the left stick
* NOTE: should be called after calling sceCtrlPeekBufferPositive
*
* @param pad the sce control data pointer
* @param stickdata the struct that will hold the result
*/
void ctrl_input_get_leftstick(SceCtrlData *pad, stick_data* stickdata);
/**
* @brief gets the value for the right stick
* NOTE: should be called after calling sceCtrlPeekBufferPositive
*
* @param pad the sce control data pointer
* @param stickdata the struct that will hold the result
* @return stick_data the data of the stick in the struct format
*/
void ctrl_input_get_rightstick(SceCtrlData *pad, stick_data* stickdata);
/**
* @brief calculates the value of the stick based on the input value
*
* @param pos the position to calculate with
* @param deltaTime the deltatime
* @return float the value that can be used to change an entity.
*/
float ctrl_input_calc_value(int16_t pos, SceUInt64 deltaTime);
#endif

19
src/system/timing.c Normal file
View File

@@ -0,0 +1,19 @@
#include <psp2/kernel/processmgr.h>
#include "timing.h"
static SceUInt64 lastFrameTime = 0;
SceUInt64 timing_get_deltatime(SceKernelSysClock *sysclock)
{
lastFrameTime = *sysclock; // put the last measured process time in lastFrameTime
uint8_t ret = sceKernelGetProcessTime(sysclock); // sysclock now holds the current process time
if (ret < 0)
return -1;
return (*sysclock - lastFrameTime) / 1000.0; // delta is the new time - the last time
}
uint8_t timing_get_fps(SceUInt64 dt)
{
return (1 / (dt / 1000.0));
}

21
src/system/timing.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef TIMING_H
#define TIMING_H
#include <psp2/kernel/processmgr.h>
/**
* @brief gets the delta time in ms. (the duration since this function was last called)
*
* @param sysclock the system kernel clock pointer to use
* @return SceUInt64 the delta time in ms
*/
SceUInt64 timing_get_deltatime(SceKernelSysClock *sysclock);
/**
* @brief calculates the fps based on the given delta time
*
* @param dt the delta time in ms
* @return uint8_t the fps
*/
uint8_t timing_get_fps(SceUInt64 dt);
#endif