diff --git a/CMakeLists.txt b/CMakeLists.txt index 9203df2..bd55a02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ link_directories( add_executable(${PROJECT_NAME} src/main.c src/sprites/sprites.c + src/timing/timing.c ../common/debugScreen.c ) diff --git a/Makefile b/Makefile index 7f603bd..94b974e 100644 --- a/Makefile +++ b/Makefile @@ -230,6 +230,33 @@ 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 + +.PHONY : src/timing/timing.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/timing/timing.i: src/timing/timing.c.i + +.PHONY : src/timing/timing.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/timing/timing.s: src/timing/timing.c.s + +.PHONY : src/timing/timing.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 + # Help Target help: @echo "The following are some of the valid targets for this Makefile:" @@ -250,6 +277,9 @@ 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" .PHONY : help diff --git a/sce_sys/livearea/contents/bg.png b/sce_sys/livearea/contents/bg.png old mode 100755 new mode 100644 diff --git a/sce_sys/livearea/contents/startup.png b/sce_sys/livearea/contents/startup.png old mode 100755 new mode 100644 diff --git a/src/main.c b/src/main.c index eb8e000..20d0faf 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #include #include "sprites/sprites.h" +#include "timing/timing.h" // 14 april 2021: 11:00-15:00 @@ -16,6 +17,17 @@ // extern unsigned char _binary_image_png_start; +/** + * @brief should be called when an unhandlable exception or error occurs. Triggers coredump. + * + */ +__attribute__((__noreturn__)) +void shit_yourself(void){ + while(1){ + *(int *)(0xAA) = 0x55; // trigger coredump + } +} + int main(int argc, char *argv[]) { @@ -39,8 +51,15 @@ int main(int argc, char *argv[]) BULLET b1 = {1,200,200,RGBA8(255,0,255,255)}; + SceUInt64 deltaTime = 0; // delta time in ms + SceKernelSysClock sysclock; + while (1) { + deltaTime = timing_get_deltatime(&sysclock); + if (deltaTime < 0) + shit_yourself(); + sceCtrlPeekBufferPositive(0, &pad, 1); if (pad.buttons & SCE_CTRL_START) @@ -51,9 +70,15 @@ int main(int argc, char *argv[]) // vita2d_draw_texture(image, 100, 100); - vita2d_pgf_draw_text(pgf, 700, 30, RGBA8(0,255,0,255), 1.0f, "PGF Font sample!"); + char text[80] = "process time: "; + sprintf(text, "%lld ms", deltaTime); - vita2d_pvf_draw_text(pvf, 700, 80, RGBA8(0,255,0,255), 1.0f, "PVF Font sample!"); + char fps[15] = "fps: "; + sprintf(fps, "%d", timing_get_fps(deltaTime)); + + vita2d_pgf_draw_text(pgf, 700, 30, RGBA8(0, 255, 0, 255), 1.0f, text); + + vita2d_pvf_draw_text(pvf, 700, 80, RGBA8(0,255,0,255), 1.0f, fps); sprites_draw_bullet(&b1); diff --git a/src/sprites/sprites.h b/src/sprites/sprites.h index d68a4c4..0a6e856 100644 --- a/src/sprites/sprites.h +++ b/src/sprites/sprites.h @@ -23,6 +23,8 @@ typedef struct bullet_sprite_t unsigned int color; // color of the bullet } BULLET; + + /** * @brief function that draws the given bullet, if it is active * diff --git a/src/timing/timing.c b/src/timing/timing.c new file mode 100644 index 0000000..1a08542 --- /dev/null +++ b/src/timing/timing.c @@ -0,0 +1,19 @@ +#include + +#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)); +} diff --git a/src/timing/timing.h b/src/timing/timing.h new file mode 100644 index 0000000..f39802d --- /dev/null +++ b/src/timing/timing.h @@ -0,0 +1,21 @@ +#ifndef TIMING_H +#define TIMING_H + +#include + +/** + * @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 \ No newline at end of file