From 470ac9b783271a492a0656f1b2d380acc6792215 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Mon, 21 Sep 2020 14:15:58 +0200 Subject: [PATCH] Camera kinda works. - created separated float and int vector functions for performance - simple camera related draw, should rework - new `vec_drect` function in `vec` --- include/conf.h | 2 ++ include/vec.h | 9 +++++++-- src/level.c | 17 +++++++++++++++-- src/player.c | 5 ++++- src/vec.c | 27 +++++++++++++++++++++++---- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/include/conf.h b/include/conf.h index 33567d7..c4a082d 100644 --- a/include/conf.h +++ b/include/conf.h @@ -1,6 +1,8 @@ #define VEC_PRECISION 1024 #define UPS 256 #define PXS (VEC_PRECISION / UPS) +#define TILE_SIZE 8 +#define VEC_DCENTER (Vec){DWIDTH / 2, DHEIGHT / 2} #ifdef FX9860G #define SCALE 1 diff --git a/include/vec.h b/include/vec.h index fcb9e96..61e85eb 100644 --- a/include/vec.h +++ b/include/vec.h @@ -17,13 +17,18 @@ void vec_add(Vec *vector, Vec force); void vec_sub(Vec *vector, Vec force); /* multiply a vector by a scale */ -void vec_mul(Vec *vector, float scale); +void vec_mul(Vec *vector, int scale); +void vec_mulf(Vec *vector, float scale); /* divise a vector by a scale */ -void vec_div(Vec *vector, float scale); +void vec_div(Vec *vector, int scale); +void vec_divf(Vec *vector, float scale); /* Linear interpolation between two vectors. * Require a scale ranging from 0 to 1 (0 is instant, 1 is completly still). */ void vec_lerp(Vec *from, Vec to, float scale); +/* Draw a rectangle using two Vec as coordinates */ +void vec_drect(Vec top_left, Vec bottom_right, int color); + #endif /* _DEF_VEC */ diff --git a/src/level.c b/src/level.c index 6fe5d57..4edeb85 100644 --- a/src/level.c +++ b/src/level.c @@ -1,5 +1,6 @@ #include +#include "conf.h" #include "level.h" #include "camera.h" @@ -21,11 +22,23 @@ void layer_draw(const Level *level, Camera *camera, uint layer_id) { const uint8_t cell = layer[x + y * level->width]; #ifdef FX9860G - dpixel(x, y, (cell == 1) ? C_LIGHT : C_BLACK); + const int color = C_BLACK; #endif /* FX9860G */ #ifdef FXCG50 - dpixel(x, y, (cell == 1) ? C_RED : C_BLACK); + const int color = C_GREEN; #endif /* FXCG50 */ + if (cell == 1) + { + Vec tl = {x, y}; + Vec br; + vec_mul(&tl, VEC_PRECISION * TILE_SIZE); + vec_sub(&tl, camera->pos); + vec_div(&tl, VEC_PRECISION); + vec_sub(&tl, VEC_DCENTER); + vec_cpy(&br, tl); + vec_add(&br, (Vec){TILE_SIZE - 1, TILE_SIZE - 1}); + vec_drect(tl, br, color); + } } } } diff --git a/src/player.c b/src/player.c index 3ff2e6d..472ba5c 100644 --- a/src/player.c +++ b/src/player.c @@ -21,13 +21,16 @@ void player_draw(Player *player, Camera *camera) Vec draw_pos_br; vec_cpy(&draw_pos_tl, player->pos); vec_sub(&draw_pos_tl, player->origin); + vec_sub(&draw_pos_tl, camera->pos); vec_cpy(&draw_pos_br, draw_pos_tl); vec_add(&draw_pos_br, player->hbox); vec_div(&draw_pos_tl, VEC_PRECISION); vec_div(&draw_pos_br, VEC_PRECISION); vec_mul(&draw_pos_tl, SCALE); vec_mul(&draw_pos_br, SCALE); - drect(draw_pos_tl.x, draw_pos_tl.y, draw_pos_br.x, draw_pos_br.y, C_BLACK); + vec_add(&draw_pos_tl, VEC_DCENTER); + vec_add(&draw_pos_br, VEC_DCENTER); + vec_drect(draw_pos_tl, draw_pos_br, C_BLACK); } void player_draw_debug(Player *player, uint step) diff --git a/src/vec.c b/src/vec.c index 1134c37..9529532 100644 --- a/src/vec.c +++ b/src/vec.c @@ -1,3 +1,5 @@ +#include + #include "vec.h" void vec_cpy(Vec *destination, Vec source) @@ -18,13 +20,25 @@ void vec_sub(Vec *vector, Vec force) vector->y -= force.y; } -void vec_mul(Vec *vector, float scale) +void vec_mul(Vec *vector, int scale) { vector->x *= scale; vector->y *= scale; } -void vec_div(Vec *vector, float scale) +void vec_mulf(Vec *vector, float scale) +{ + vector->x *= scale; + vector->y *= scale; +} + +void vec_div(Vec *vector, int scale) +{ + vector->x /= scale; + vector->y /= scale; +} + +void vec_divf(Vec *vector, float scale) { vector->x /= scale; vector->y /= scale; @@ -36,9 +50,14 @@ void vec_lerp(Vec *from, Vec to, float scale) Vec temp; vec_cpy(&temp, to); /* from * (1 - scale) */ - vec_mul(from, 1 - scale); + vec_mulf(from, 1 - scale); /* temp * scale */ - vec_mul(&temp, scale); + vec_mulf(&temp, scale); /* add */ vec_add(from, temp); } + +void vec_drect(Vec top_left, Vec bottom_right, int color) +{ + drect(top_left.x, top_left.y, bottom_right.x, bottom_right.y, color); +}