From 9ca4fd1d37bd2414a383b42b1d22a9e07cd29824 Mon Sep 17 00:00:00 2001 From: Darks Date: Sat, 24 Oct 2020 10:54:54 +0200 Subject: [PATCH] WIP --- include/animation.h | 32 +++++++++++++ include/config.h | 13 ++++++ include/engine.h | 7 +-- include/player.h | 13 ++++++ src/engine.c | 110 ++++++++++++++++++++++++++++++++++++++++++++ src/images.c | 40 ++++++++++++++++ src/main.c | 36 ++------------- 7 files changed, 217 insertions(+), 34 deletions(-) create mode 100644 include/animation.h create mode 100644 include/player.h create mode 100644 src/images.c diff --git a/include/animation.h b/include/animation.h new file mode 100644 index 0000000..e8e16b6 --- /dev/null +++ b/include/animation.h @@ -0,0 +1,32 @@ +#ifndef _ANIMATION_H +#define _ANIMATION_H + +// Objects +typedef struct { + uint16_t x, y, r; // Position and radius + struct { + uint8_t enabled :1; // Object enabled (visible, updated) + uint8_t col_nor :1; // Collisions in normal mode + uint8_t col_foc :1; // Collisions in focused mode + uint8_t color :4; // Color. 3 bits used, maybe 4 in the future + uint8_t type :8; // Type of object. Linked to image_t + } meta; +} object_t; + +// Animations +typedef struct animation_s animation_t; +typedef struct { + uint32_t timestamp; // Timestamp of the animation + uint32_t n_objects; // Number of objects + object_t *objects; // Table of object_t + void (*updater)(animation_t*, uint32_t); // Updater function +} animation_s; + +// Utils +animation_t *new_animation(uint32_t objects, void (*updater)(animation_t*)); +void del_animation(animation_t *animation); + +// List of animation functions +void dummy_updater(animation_t *animation, uint32_t us_elapsed); + +#endif // _ANIMATION_H diff --git a/include/config.h b/include/config.h index a69cc32..b46792e 100644 --- a/include/config.h +++ b/include/config.h @@ -1,6 +1,11 @@ #ifndef _CONFIG_H #define _CONFIG_H +#define SCREEN_X 270 +#define SCREEN_Y 224 + +#define NULL (void*)(0) + enum { WITCH_1 = 0, WITCH_2, @@ -13,4 +18,12 @@ enum { N_WITCHES }; +// Profiling +enum { + PROFCTX_FPS = 0, + PROFCTX_ENGINE, + PROFCTX_DISPLAY, + PROFCTX_COUNT +}; + #endif diff --git a/include/engine.h b/include/engine.h index 73e5ca0..9e95c9d 100644 --- a/include/engine.h +++ b/include/engine.h @@ -1,9 +1,10 @@ #ifndef _ENGINE_H #define _ENGINE_H -struct player_s { - char witch_id; +#include -}; +void engine(void); +void physics(void); +void graphics(void); #endif diff --git a/include/player.h b/include/player.h new file mode 100644 index 0000000..e0269de --- /dev/null +++ b/include/player.h @@ -0,0 +1,13 @@ +#ifndef _PLAYER_H +#define _PLAYER_H + +#include "utils/vector.h" + +typedef struct { + float x, y; + char power; + char lives; + char bombs; +} player_t; + +#endif diff --git a/src/engine.c b/src/engine.c index 4d28e17..60ab8e7 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,3 +1,113 @@ #include #include #include +#include + +#include "config.h" +#include "engine.h" + +// Speed in px/us +#define SPEED_FAST 0.000208 +#define SPEED_SLOW 0.000104 + + +extern image_t *img_witches[8]; +extern image_t *img_battlefields[3]; + + +float speeds[2] = {SPEED_FAST, SPEED_SLOW}; + +void engine(void) +{ + uint32_t frame = 0; + uint32_t us_elapsed = 1; + + // Profiling + prof_t prof_fps = prof_make(); + prof_t prof_engine = prof_make(); + prof_t prof_display = prof_make(); + + char witch = 0; + char mode = 0; // 0 = normal, 1 = focused + float px = SCREEN_X / 2; + float py = SCREEN_Y / 2; + + char battlefield = 0; + + while(!keydown(KEY_EXIT)) + { + // Profiling + prof_enter(prof_fps); + + // Get keyboard events + clearevents(); + + + //*** Some debug inputs *********************************************** + if(keydown(KEY_F1)) battlefield = 0; + if(keydown(KEY_F2)) battlefield = 1; + if(keydown(KEY_F3)) battlefield = 2; + if(keydown(KEY_1)) witch = 0; + if(keydown(KEY_2)) witch = 1; + if(keydown(KEY_3)) witch = 2; + if(keydown(KEY_4)) witch = 3; + //*** End debug ******************************************************* + + prof_enter(prof_engine); + // Fast or slow witch + mode = keydown(KEY_OPTN); + float base_speed = us_elapsed * speeds[mode] * ( + keydown_any(KEY_LEFT, KEY_RIGHT, 0) && + keydown_any(KEY_UP, KEY_DOWN, 0) ? 0.707 : 1.0); + + // Move witch + if(keydown(KEY_UP)) + py -= base_speed; + if(keydown(KEY_DOWN)) + py += base_speed; + if(keydown(KEY_LEFT)) + px -= base_speed; + if(keydown(KEY_RIGHT)) + px += base_speed; + + prof_leave(prof_engine); + + + prof_enter(prof_display); + // Clear VRAM + dclear(C_BLACK); + + // Draw battlefield + dimage(0, 0, img_battlefields[battlefield]); + + // Draw our cute witch + image_t *w = img_witches[witch * 2 + mode]; + dimage(px - w->width / 2, py - w->height / 2, w); + + prof_leave(prof_display); + + // Print FPS + dprint(273, 184, C_WHITE, "%i FPS", 1000000 / us_elapsed); + dprint(273, 194, C_WHITE, "Tot: %i us", us_elapsed); + dprint(273, 204, C_WHITE, "Eng: %i us", prof_time(prof_engine)); + dprint(273, 214, C_WHITE, "Dsp: %i us", prof_time(prof_display)); + + // Update VRAM + dupdate(); + + // Update FPS & frame + prof_leave(prof_fps); + us_elapsed = prof_time(prof_fps); + frame++; + } +} + +void physics(void) +{ + +} + +void graphics(void) +{ + +} diff --git a/src/images.c b/src/images.c new file mode 100644 index 0000000..a693243 --- /dev/null +++ b/src/images.c @@ -0,0 +1,40 @@ +#include + +// TODO: generate those tables in `fxconv` instead of this dirty file + +extern image_t img_witch1_back; +extern image_t img_witch2_back; +extern image_t img_witch3_back; +extern image_t img_witch4_back; +extern image_t img_witch5_back; +extern image_t img_witch6_back; +extern image_t img_witch7_back; +extern image_t img_witch8_back; +image_t *img_witches[8] = { + &img_witch1_back, &img_witch2_back, &img_witch3_back, &img_witch4_back, + &img_witch5_back, &img_witch6_back, &img_witch7_back, &img_witch8_back +}; + + +extern image_t img_battlefield1; +extern image_t img_battlefield2; +extern image_t img_battlefield3; +image_t *img_battlefields[3] = { + &img_battlefield1, &img_battlefield2, &img_battlefield3 +}; + + +extern image_t img_bullet1_1; +extern image_t img_bullet1_2; +extern image_t img_bullet1_3; +extern image_t img_bullet2_1; +extern image_t img_bullet2_2; +extern image_t img_bullet2_3; +extern image_t img_bullet3_1; +extern image_t img_bullet3_2; +extern image_t img_bullet3_3; +image_t *img_bullets[3][3] = { + {&img_bullet1_1, &img_bullet1_2, &img_bullet1_3}, + {&img_bullet2_1, &img_bullet2_2, &img_bullet2_3}, + {&img_bullet3_1, &img_bullet3_2, &img_bullet3_3}, +}; diff --git a/src/main.c b/src/main.c index 8d84c02..944bf3b 100644 --- a/src/main.c +++ b/src/main.c @@ -2,41 +2,15 @@ #include #include -void draw() { - extern image_t img_battlefield1; - extern image_t img_battlefield2; - extern image_t img_battlefield3; - extern image_t img_bullet3_2; +#include "config.h" +#include "engine.h" - prof_clear(0); - prof_enter(0); - - dclear(C_BLACK); - // dimage(0, 0, &img_battlefield1); - // dimage(0, 0, &img_battlefield2); - dimage(0, 0, &img_battlefield3); - - for(int i = 0; i < 2000; i++) { - dimage((i*79) % 216, (i*41) % 216, &img_bullet3_2); - } - - prof_leave(0); - - dprint(300, 10, C_NONE, C_NONE, "%i us", prof_time(0)); - dprint(300, 40, C_WHITE, C_NONE, "%i us", prof_time(0)); - dprint(300, 70, C_BLACK, C_NONE, "%i us", prof_time(0)); - dupdate(); -} int main(void) -{ - prof_init(1, 0); +{ + prof_init(); - for(int i = 0; i < 100; i++) { - draw(); - } - - getkey(); + engine(); prof_quit();