From 0477f1210c7c2915d64aebefea1bcdf3c2849b5c Mon Sep 17 00:00:00 2001 From: bgiraudr Date: Wed, 25 Aug 2021 15:46:05 +0200 Subject: [PATCH] speed up camera, camera stays on player even inside a house --- include/engine.h | 15 +-------------- include/game.h | 14 ++++++++++++++ include/map.h | 4 +--- src/camera.c | 2 +- src/engine.c | 29 +++++++---------------------- src/main.c | 4 ++-- src/map.c | 14 ++------------ src/vec2.c | 27 +++++++++------------------ 8 files changed, 37 insertions(+), 72 deletions(-) diff --git a/include/engine.h b/include/engine.h index 0c745ec..1a7a6d6 100644 --- a/include/engine.h +++ b/include/engine.h @@ -1,21 +1,8 @@ #pragma once -#include "camera.h" +#include "game.h" #define ENGINE_TICK 35 -struct Game { - /*the current map to display*/ - struct Map *map; - /*the player*/ - struct Player *player; - /*list of all the characters on the map*/ - struct Character **characters; - /*the camera*/ - struct Camera camera; - /*the background color*/ - int background; -}; - /*draw the current state of the game*/ void engine_draw(struct Game const *game); /*draw the map around the player*/ diff --git a/include/game.h b/include/game.h index ee090f6..adddade 100644 --- a/include/game.h +++ b/include/game.h @@ -1,4 +1,18 @@ #pragma once +#include "camera.h" + +struct Game { + /*the current map to display*/ + struct Map *map; + /*the player*/ + struct Player *player; + /*list of all the characters on the map*/ + struct Character **characters; + /*the camera*/ + struct Camera camera; + /*the background color*/ + int background; +}; /*get the input with a timeout*/ int get_inputs(void); diff --git a/include/map.h b/include/map.h index 8e2973a..1e84ad5 100644 --- a/include/map.h +++ b/include/map.h @@ -27,6 +27,4 @@ int map_walkable(struct Map const *map, int x, int y); /*get the tile under the player*/ int map_get_player_tile(struct Game const *game); -void generate_interior_map(struct Game *game); - -bool is_map_larger(struct Map *map); +void generate_interior_map(struct Game *game); \ No newline at end of file diff --git a/src/camera.c b/src/camera.c index bcd3ab2..a020b07 100644 --- a/src/camera.c +++ b/src/camera.c @@ -11,6 +11,6 @@ struct Camera camera_new(struct Vec2f *target) { } void camera_update(struct Camera *c) { - c->pos = vec2f_lerp(c->pos, *c->target, 0.05); + c->pos = vec2f_lerp(c->pos, *c->target, 0.08); c->offset = vec2f_vec2(c->pos); } diff --git a/src/engine.c b/src/engine.c index b4f1e9d..4ba9381 100644 --- a/src/engine.c +++ b/src/engine.c @@ -8,6 +8,7 @@ #include "define.h" #include "character.h" #include "camera.h" +#include "vec2.h" /*draw the current state of the game*/ void engine_draw(struct Game const *game) { @@ -40,16 +41,9 @@ void engine_draw_map(struct Game const *game) { unsigned int tile_x = TILE_SIZE * (tile_id % TILESET_WIDTH); unsigned int tile_y = TILE_SIZE * (tile_id / TILESET_WIDTH); - //provisoire le temps de trouver une manière propre - if(is_map_larger(game->map)) { - dsubimage(x * TILE_SIZE - x_offset%TILE_SIZE, - y * TILE_SIZE - y_offset%TILE_SIZE, game->map->tileset, - tile_x, tile_y, TILE_SIZE, TILE_SIZE, DIMAGE_NONE); - } else { - dsubimage(x * TILE_SIZE - x_offset%TILE_SIZE, - y * TILE_SIZE - y_offset%TILE_SIZE, game->map->tileset, - tile_x, tile_y, TILE_SIZE, TILE_SIZE, DIMAGE_NONE); - } + dsubimage(x * TILE_SIZE - x_offset%TILE_SIZE, + y * TILE_SIZE - y_offset%TILE_SIZE, game->map->tileset, + tile_x, tile_y, TILE_SIZE, TILE_SIZE, DIMAGE_NONE); } } } @@ -62,20 +56,11 @@ void engine_draw_player(struct Game const *g) { struct Vec2f draw_offset = vec2f_sub(g->player->pos_visual, vec2_vec2f(g->camera.offset)); draw_offset.x += g->player->anim.dx * 3; draw_offset.y += g->player->anim.dy * 3; - if(is_map_larger(g->map)) { - dframe(g->player->show_x * TILE_SIZE + draw_offset.x, - g->player->show_y * TILE_SIZE - 5 + draw_offset.y, - g->player->anim.img); //draw the player 5 pixel up - } else { - const int offset_map_x = (DWIDTH / TILE_SIZE - g->map->w + 1)/2; - const int offset_map_y = (DHEIGHT / TILE_SIZE - g->map->h + 1)/2; + dframe(g->player->show_x * TILE_SIZE + draw_offset.x + g->player->x_mid, + g->player->show_y * TILE_SIZE - 5 + draw_offset.y + g->player->y_mid, + g->player->anim.img); //draw the player 5 pixel up - dframe( - (g->player->pos.x + offset_map_x) * TILE_SIZE + g->player->anim.dx*3, - (g->player->pos.y + offset_map_y) * TILE_SIZE - 5 + g->player->anim.dy*3, - g->player->anim.img); //draw the player 5 pixel up - } dprint(1,1,C_BLACK,"%d:%d",g->player->pos.x, g->player->pos.y); } diff --git a/src/main.c b/src/main.c index c573b30..7b6165b 100644 --- a/src/main.c +++ b/src/main.c @@ -19,8 +19,8 @@ struct Map *maps[] = { }; static int callback_tick(volatile int *tick) { - *tick = 1; - return TIMER_CONTINUE; + *tick = 1; + return TIMER_CONTINUE; } int main(void) { diff --git a/src/map.c b/src/map.c index e99beb5..90ee4d2 100644 --- a/src/map.c +++ b/src/map.c @@ -9,28 +9,18 @@ int map_walkable(struct Map const *map, int x, int y) { int tile = map->info_map[x + map->w * y]; if(x < 0 || x > map->w-1 || y < 0 || y > map->h-1) return 0; return (tile != TILE_SOLID && tile != TILE_CHARACTER); - return 1; } /*get the tile under the player*/ int map_get_player_tile(struct Game const *game) { return game->map->info_map[game->player->pos.x + game->map->w * game->player->pos.y]; - return 0; } void generate_interior_map(struct Game *game) { extern struct Map in_1; game->map = &in_1; set_player_xy(game->player, 3,3); + //game->camera.target = &VEC2F(DWIDTH/2, DHEIGHT/2); game->camera.pos.x = in_1.w/2 * TILE_SIZE + game->player->x_mid; game->camera.pos.y = in_1.h/2 * TILE_SIZE + game->player->y_mid; -} - -/*return true if the map is larger than the screen. false otherwise*/ -bool is_map_larger(struct Map *map) { - if(map->w > DWIDTH / TILE_SIZE + 1 && map->h > DHEIGHT / TILE_SIZE + 1) { - return true; - } else { - return false; - } -} +} \ No newline at end of file diff --git a/src/vec2.c b/src/vec2.c index 9fc4459..973371e 100644 --- a/src/vec2.c +++ b/src/vec2.c @@ -1,55 +1,46 @@ #include "vec2.h" -struct Vec2 -vec2_add(struct Vec2 v1, struct Vec2 v2) +struct Vec2 vec2_add(struct Vec2 v1, struct Vec2 v2) { return VEC2(v1.x + v2.x, v1.y + v2.y); } -struct Vec2f -vec2f_add(struct Vec2f v1, struct Vec2f v2) +struct Vec2f vec2f_add(struct Vec2f v1, struct Vec2f v2) { return VEC2F(v1.x + v2.x, v1.y + v2.y); } -struct Vec2 -vec2_sub(struct Vec2 v1, struct Vec2 v2) +struct Vec2 vec2_sub(struct Vec2 v1, struct Vec2 v2) { return VEC2(v1.x - v2.x, v1.y - v2.y); } -struct Vec2f -vec2f_sub(struct Vec2f v1, struct Vec2f v2) +struct Vec2f vec2f_sub(struct Vec2f v1, struct Vec2f v2) { return VEC2F(v1.x - v2.x, v1.y - v2.y); } -struct Vec2 -vec2_mul(struct Vec2 v, int scale) +struct Vec2 vec2_mul(struct Vec2 v, int scale) { return VEC2(v.x * scale, v.y * scale); } -struct Vec2f -vec2f_mul(struct Vec2f v, int scale) +struct Vec2f vec2f_mul(struct Vec2f v, int scale) { return VEC2F(v.x * scale, v.y * scale); } -struct Vec2 -vec2f_vec2(struct Vec2f v) +struct Vec2 vec2f_vec2(struct Vec2f v) { return VEC2(v.x, v.y); } -struct Vec2f -vec2_vec2f(struct Vec2 v) +struct Vec2f vec2_vec2f(struct Vec2 v) { return VEC2F(v.x, v.y); } -struct Vec2f -vec2f_lerp(struct Vec2f from, struct Vec2f to, float scale) { +struct Vec2f vec2f_lerp(struct Vec2f from, struct Vec2f to, float scale) { /* Linear interpolation: can be used for camera and animations. * 'scale' is the transformation speed, 0 slow and 1 fast. */ return VEC2F(