diff --git a/CMakeLists.txt b/CMakeLists.txt index bd55a02..d5d0c60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,7 +52,8 @@ link_directories( add_executable(${PROJECT_NAME} src/main.c src/sprites/sprites.c - src/timing/timing.c + src/system/timing.c + src/system/control_input.c ../common/debugScreen.c ) diff --git a/Makefile b/Makefile index 94b974e..a9dcd60 100644 --- a/Makefile +++ b/Makefile @@ -230,32 +230,59 @@ src/sprites/sprites.c.s: $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/sprites/sprites.c.s .PHONY : src/sprites/sprites.c.s -src/timing/timing.obj: src/timing/timing.c.obj +src/system/control_input.obj: src/system/control_input.c.obj -.PHONY : src/timing/timing.obj +.PHONY : src/system/control_input.obj # target to build an object file -src/timing/timing.c.obj: - $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/timing/timing.c.obj -.PHONY : src/timing/timing.c.obj +src/system/control_input.c.obj: + $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/system/control_input.c.obj +.PHONY : src/system/control_input.c.obj -src/timing/timing.i: src/timing/timing.c.i +src/system/control_input.i: src/system/control_input.c.i -.PHONY : src/timing/timing.i +.PHONY : src/system/control_input.i # target to preprocess a source file -src/timing/timing.c.i: - $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/timing/timing.c.i -.PHONY : src/timing/timing.c.i +src/system/control_input.c.i: + $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/system/control_input.c.i +.PHONY : src/system/control_input.c.i -src/timing/timing.s: src/timing/timing.c.s +src/system/control_input.s: src/system/control_input.c.s -.PHONY : src/timing/timing.s +.PHONY : src/system/control_input.s # target to generate assembly for a file -src/timing/timing.c.s: - $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/timing/timing.c.s -.PHONY : src/timing/timing.c.s +src/system/control_input.c.s: + $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/system/control_input.c.s +.PHONY : src/system/control_input.c.s + +src/system/timing.obj: src/system/timing.c.obj + +.PHONY : src/system/timing.obj + +# target to build an object file +src/system/timing.c.obj: + $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/system/timing.c.obj +.PHONY : src/system/timing.c.obj + +src/system/timing.i: src/system/timing.c.i + +.PHONY : src/system/timing.i + +# target to preprocess a source file +src/system/timing.c.i: + $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/system/timing.c.i +.PHONY : src/system/timing.c.i + +src/system/timing.s: src/system/timing.c.s + +.PHONY : src/system/timing.s + +# target to generate assembly for a file +src/system/timing.c.s: + $(MAKE) -f CMakeFiles/cybershot.dir/build.make CMakeFiles/cybershot.dir/src/system/timing.c.s +.PHONY : src/system/timing.c.s # Help Target help: @@ -277,9 +304,12 @@ help: @echo "... src/sprites/sprites.obj" @echo "... src/sprites/sprites.i" @echo "... src/sprites/sprites.s" - @echo "... src/timing/timing.obj" - @echo "... src/timing/timing.i" - @echo "... src/timing/timing.s" + @echo "... src/system/control_input.obj" + @echo "... src/system/control_input.i" + @echo "... src/system/control_input.s" + @echo "... src/system/timing.obj" + @echo "... src/system/timing.i" + @echo "... src/system/timing.s" .PHONY : help diff --git a/src/main.c b/src/main.c index 809ea3b..8f34867 100644 --- a/src/main.c +++ b/src/main.c @@ -9,12 +9,16 @@ #include #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)); diff --git a/src/system/control_input.c b/src/system/control_input.c new file mode 100644 index 0000000..0bb415a --- /dev/null +++ b/src/system/control_input.c @@ -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)); +} \ No newline at end of file diff --git a/src/system/control_input.h b/src/system/control_input.h new file mode 100644 index 0000000..940a4a7 --- /dev/null +++ b/src/system/control_input.h @@ -0,0 +1,44 @@ +#ifndef CONTROL_INPUT_H +#define CONTROL_INPUT_H + +#include +#include + +/** + * @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 \ No newline at end of file diff --git a/src/timing/timing.c b/src/system/timing.c similarity index 100% rename from src/timing/timing.c rename to src/system/timing.c diff --git a/src/timing/timing.h b/src/system/timing.h similarity index 100% rename from src/timing/timing.h rename to src/system/timing.h