From 6e8041a92606046422d1c1ba30a3f3cd3e0eeb6c Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 10 Nov 2021 06:15:41 +0100 Subject: [PATCH] i can't sleep fuck --- CMakeLists.txt | 1 + inc/conf.h | 5 ++++- inc/level.h | 4 ++++ inc/player.h | 12 ++++++++++++ inc/tile.h | 3 +++ inc/vec.h | 19 +++++++++++++++++++ src/level.c | 19 ++++++++++++++++++- src/main.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- src/player.c | 28 ++++++++++++++++++++++++++++ 9 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 inc/player.h create mode 100644 inc/tile.h create mode 100644 inc/vec.h create mode 100644 src/player.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f5c3c7..3f18424 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ set(SOURCES src/main.c src/input.c src/level.c + src/player.c ) set(LEVELS diff --git a/inc/conf.h b/inc/conf.h index 230cd9b..c077068 100644 --- a/inc/conf.h +++ b/inc/conf.h @@ -1,3 +1,6 @@ #pragma once -#define TILE_SIZE 16 +#define TARGET_FPS 60 +#define TILE_SIZE 16 +#define PLAYER_WIDTH 12 +#define PLAYER_HEIGHT 12 diff --git a/inc/level.h b/inc/level.h index 3c0ce81..4fc71d9 100644 --- a/inc/level.h +++ b/inc/level.h @@ -1,4 +1,6 @@ #pragma once +#include "tile.h" +#include "vec.h" struct LevelBin { unsigned char format; @@ -11,9 +13,11 @@ struct LevelBin { struct Level { int width; int height; + int size; char *data; }; struct Level level_load(struct LevelBin *restrict); void level_free(struct Level *restrict); void level_draw(struct Level *restrict); +struct Vec level_find(struct Level *restrict, enum Tile); diff --git a/inc/player.h b/inc/player.h new file mode 100644 index 0000000..48da2e0 --- /dev/null +++ b/inc/player.h @@ -0,0 +1,12 @@ +#pragma once +#include "vec.h" + +struct Player { + struct Vec pos; + struct VecF spd; + struct VecF rem; +}; + +struct Player player_new(struct Vec pos); +void player_update(struct Player *restrict); +void player_draw(struct Player *restrict); diff --git a/inc/tile.h b/inc/tile.h new file mode 100644 index 0000000..90e1a9b --- /dev/null +++ b/inc/tile.h @@ -0,0 +1,3 @@ +#pragma once + +enum Tile { TILE_VOID, TILE_SOLID }; diff --git a/inc/vec.h b/inc/vec.h new file mode 100644 index 0000000..ee541f4 --- /dev/null +++ b/inc/vec.h @@ -0,0 +1,19 @@ +#pragma once + +struct Vec { + int x; + int y; +}; + +#define VEC(x, y) \ + (struct Vec) { x, y } +#define VECZ VEC(0, 0) + +struct VecF { + float x; + float y; +}; + +#define VECF(x, y) \ + (struct VecF) { x, y } +#define VECFZ VECF(0.0, 0.0) diff --git a/src/level.c b/src/level.c index 180314e..5781634 100644 --- a/src/level.c +++ b/src/level.c @@ -1,5 +1,7 @@ #include "level.h" #include "conf.h" +#include "tile.h" +#include "vec.h" #include #include @@ -10,6 +12,7 @@ level_load(struct LevelBin *restrict s) int i = s->width * s->height; level.width = s->width; level.height = s->height; + level.size = i; level.data = malloc(i); while (i-- > 0) { level.data[i] = s->data[i]; @@ -32,7 +35,7 @@ level_draw(struct Level *restrict s) int x = 0; int y = 0; - for (i = 0; i < s->width * s->height; i++) { + for (i = 0; i < s->size; i++) { const int sx = x * TILE_SIZE; const int sy = y * TILE_SIZE; const int tile = s->data[i]; @@ -48,3 +51,17 @@ level_draw(struct Level *restrict s) } } } + +struct Vec +level_find(struct Level *restrict s, enum Tile seek) +{ + int i; + for (i = 0; i < s->size; i++) { + const enum Tile tile = s->data[i]; + if (tile == seek) { + return VEC(i % s->width * TILE_SIZE, + (int)(i / s->height) * TILE_SIZE); + } + } + return VEC(0, 0); +} diff --git a/src/main.c b/src/main.c index a2a49d1..95e9aa0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,30 +1,73 @@ +#include "conf.h" #include "input.h" #include "level.h" +#include "player.h" +#include "vec.h" +#include #include +#include extern struct LevelBin lvl_test; struct Level level; +struct Player player; +static void update(void); static void draw(void); +static int callback(volatile int *); int main(void) { + int timer; + int frameskip = 1; + volatile int has_ticked = 0; + + timer = timer_configure(TIMER_ANY, 1000000 / TARGET_FPS, + GINT_CALL(callback, &has_ticked)); + timer_start(timer); input_init(); level = level_load(&lvl_test); + player = player_new(VECZ); - draw(); - while (!input_pressed(K_EXIT)) - input_update(); + while (!input_pressed(K_EXIT)) { + int i; + for (i = 0; i < frameskip; i++) { + if (has_ticked > frameskip) { + frameskip = has_ticked; + } + while (!has_ticked) + sleep(); + while (has_ticked) + has_ticked = 0; + update(); + } + draw(); + } + timer_stop(timer); level_free(&level); return 1; } +static void +update(void) +{ + input_update(); + player_update(&player); +} + static void draw(void) { dclear(C_BLACK); level_draw(&level); + player_draw(&player); dupdate(); } + +static int +callback(volatile int *arg) +{ + *arg += 1; + return 0; +} diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..b419f89 --- /dev/null +++ b/src/player.c @@ -0,0 +1,28 @@ +#include "player.h" +#include "conf.h" +#include "vec.h" +#include + +struct Player +player_new(struct Vec pos) +{ + struct Player p; + p.pos = pos; + p.spd = VECFZ; + p.rem = VECFZ; + return p; +} + +void +player_update(struct Player *restrict p) +{ + p->pos.x++; +} + +void +player_draw(struct Player *restrict p) +{ + const int x2 = p->pos.x + PLAYER_WIDTH - 1; + const int y2 = p->pos.y + PLAYER_HEIGHT - 1; + drect(p->pos.x, p->pos.y, x2, y2, C_BLUE); +}