[ADD] stick system
This commit is contained in:
36
src/main.c
36
src/main.c
@@ -9,12 +9,16 @@
|
||||
#include <vita2d.h>
|
||||
|
||||
#include "sprites/sprites.h"
|
||||
#include "timing/timing.h"
|
||||
#include "system/timing.h"
|
||||
#include "system/control_input.h"
|
||||
|
||||
// 14 april 2021: 11:00-15:00
|
||||
|
||||
#define printf psvDebugScreenPrintf
|
||||
|
||||
#define SCREEN_HEIGTH 544
|
||||
#define SCREEN_WIDTH 940
|
||||
|
||||
// extern unsigned char _binary_image_png_start;
|
||||
|
||||
/**
|
||||
@@ -34,6 +38,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
/* to enable analog sampling */
|
||||
sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG);
|
||||
stick_data left_stick = {0, 0}, right_stick = {0,0};
|
||||
SceCtrlData pad;
|
||||
vita2d_pgf *pgf;
|
||||
vita2d_pvf *pvf;
|
||||
@@ -73,22 +78,29 @@ int main(int argc, char *argv[])
|
||||
vita2d_start_drawing();
|
||||
vita2d_clear_screen();
|
||||
|
||||
int16_t lxpos = -128 + pad.lx; // full left is 0 so -128 and full right is 255 so 128
|
||||
char leftStick[50] = "left stick: ";
|
||||
sprintf(leftStick, "left stick: %d", lxpos);
|
||||
ctrl_input_get_leftstick(&pad, &left_stick);
|
||||
|
||||
vita2d_pgf_draw_text(pgf, 300, 30, RGBA8(255, 255, 0, 255), 1.0f, leftStick);
|
||||
if (abs(lxpos) > 15)
|
||||
x1 += (lxpos) * (deltaTime / 1000.0);
|
||||
char xtext[50];
|
||||
sprintf(xtext, "x: %f", x1);
|
||||
// char leftStick[50] = "left stick: ";
|
||||
// sprintf(leftStick, "left stick: %d", left_stick.x);
|
||||
// vita2d_pgf_draw_text(pgf, 300, 30, RGBA8(255, 255, 0, 255), 1.0f, leftStick);
|
||||
|
||||
vita2d_pgf_draw_text(pgf, 200, 80, RGBA8(255, 0, 0, 255), 1.0f, xtext);
|
||||
if (abs(left_stick.x) > 15)
|
||||
x1 += ctrl_input_calc_value(left_stick.x, deltaTime);
|
||||
if (abs(left_stick.y) > 15)
|
||||
y1 += ctrl_input_calc_value(left_stick.y, deltaTime);
|
||||
// char xtext[50];
|
||||
// sprintf(xtext, "x: %f", x1);
|
||||
|
||||
// vita2d_pgf_draw_text(pgf, 200, 80, RGBA8(255, 0, 0, 255), 1.0f, xtext);
|
||||
|
||||
if (x1 <= 0)
|
||||
x1 = 0;
|
||||
if (x1 > 700)
|
||||
x1 = 700;
|
||||
if (x1 >= SCREEN_WIDTH)
|
||||
x1 = SCREEN_WIDTH-1;
|
||||
if (y1 <= 0)
|
||||
y1 = 0;
|
||||
if (y1 >= SCREEN_HEIGTH)
|
||||
y1 = SCREEN_HEIGTH - 1;
|
||||
|
||||
vita2d_draw_rectangle(x1, y1, (float)10, (float)10, RGBA8(100, 100, 100, 255));
|
||||
vita2d_draw_rectangle(x2, y2, (float)10, (float)10, RGBA8(169, 60, 23, 255));
|
||||
|
||||
24
src/system/control_input.c
Normal file
24
src/system/control_input.c
Normal 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));
|
||||
}
|
||||
44
src/system/control_input.h
Normal file
44
src/system/control_input.h
Normal 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
|
||||
Reference in New Issue
Block a user