rewrite code for camera

This commit is contained in:
KikooDX 2021-08-25 01:01:43 +02:00
parent 4490187258
commit 0c5256d55c
13 changed files with 130 additions and 50 deletions

View File

@ -23,13 +23,15 @@ add_custom_command(
set(SOURCES
src/main.c
src/character.c
src/vec2.c
src/game.c
src/engine.c
src/animation.c
src/character.c
src/player.c
src/camera.c
src/map.c
src/player.c
src/util.c
src/animation.c
)
set(ASSETS_cg

View File

@ -1,5 +1,13 @@
#pragma once
#include "vec2.h"
struct Camera {
int x, y;
};
/* true position */
struct Vec2f pos;
/* used by draw functions as an offset */
struct Vec2 offset;
/* target to follow */
struct Vec2f *target;
};
struct Camera camera_new(struct Vec2f *target);

View File

@ -1,4 +1,5 @@
#pragma once
#include "camera.h"
#define ENGINE_TICK 35
@ -10,7 +11,7 @@ struct Game {
/*list of all the characters on the map*/
struct Character **characters;
/*the camera*/
struct Camera *camera;
struct Camera camera;
/*the background color*/
int background;
};
@ -31,4 +32,4 @@ void engine_set_background(struct Game *game, int color);
void engine_action(struct Game const *game, int action);
/*check the current position of the player. To perform action depends of his location*/
void engine_check_position(struct Game *game);
void vec_lerp(struct Camera *from, struct Player const *to, float scale);
void vec_lerp(struct Camera *from, struct Player const *to, float scale);

View File

@ -10,4 +10,4 @@ enum direction {
DIR_RIGHT = 3,
ACTION_SHIFT = 4,
ACTION_ALPHA = 5
};
};

View File

@ -1,8 +1,7 @@
#pragma once
#include <gint/display.h>
#include "engine.h"
#include <stdbool.h>
#include "engine.h"
struct Map {
/*width, height and the number of layer of the map (max 2)*/
@ -30,4 +29,4 @@ int map_get_player_tile(struct Game const *game);
void generate_interior_map(struct Game *game);
bool is_map_larger(struct Map *map);
bool is_map_larger(struct Map *map);

View File

@ -1,11 +1,13 @@
#pragma once
#include "animation.h"
#include "engine.h"
#include "vec2.h"
struct Player {
/*current position of the player on the map - Tile*/
int x, y;
struct Vec2 pos;
/*current position of the player on the map - pixels */
struct Vec2f pos_visual;
/*player mid - offset pixels*/
int x_mid, y_mid;
/*the direction the player facing to*/
@ -21,4 +23,4 @@ struct Player {
/*return the info tile value the player is facing to*/
int player_facing(struct Game const *game);
void set_player_xy(struct Player *player, int x, int y);
void set_player_xy(struct Player *player, int x, int y);

25
include/vec2.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
struct Vec2 {
int x;
int y;
};
struct Vec2f {
float x;
float y;
};
#define VEC2(x, y) \
(struct Vec2) { x, y }
#define VEC2F(x, y) \
(struct Vec2f) { x, y }
#define VEC2Z VEC2(0, 0)
#define VEC2FZ VEC2F(0.0, 0.0)
struct Vec2 vec2_add(struct Vec2, struct Vec2);
struct Vec2f vec2f_add(struct Vec2f, struct Vec2f);
struct Vec2 vec2_sub(struct Vec2, struct Vec2);
struct Vec2 vec2_mul(struct Vec2, int scale);
struct Vec2 vec2f_vec2(struct Vec2f);
struct Vec2f vec2_vec2f(struct Vec2);

10
src/camera.c Normal file
View File

@ -0,0 +1,10 @@
#include "vec2.h"
#include "camera.h"
struct Camera camera_new(struct Vec2f *target) {
return (struct Camera) {
.pos = *target,
.offset = vec2f_vec2(*target),
.target = target,
};
}

View File

@ -14,13 +14,13 @@ void engine_draw(struct Game const *game) {
dclear(game->background);
engine_draw_map(game);
engine_draw_player(game);
dprint(1,15,C_BLACK,"%d:%d",game->camera->x, game->camera->y);
dprint(1,15,C_BLACK,"%d:%d",game->camera.offset.x, game->camera.offset.y);
dprint(1,30,C_BLACK,"%d:%d",game->map->w, game->map->h);
}
void engine_draw_map(struct Game const *game) {
int x_offset = (game->camera->x - DWIDTH/2);
int y_offset = (game->camera->y - DHEIGHT/2);
int x_offset = (game->camera.offset.x - DWIDTH/2);
int y_offset = (game->camera.offset.y - DHEIGHT/2);
//currently -1 to avoid white during transition
for (int layer = 0 ; layer < game->map->nb_layers; layer++) {
@ -31,10 +31,10 @@ void engine_draw_map(struct Game const *game) {
int indexY = (y + y_offset / TILE_SIZE);
int indexX = (x + x_offset / TILE_SIZE);
if(indexX >= 0 && indexX < game->map->w
&& indexY >= 0 && indexY < game->map->h)
&& indexY >= 0 && indexY < game->map->h)
tile_id = game->map->layers[layer][indexX + indexY * game->map->w];
//tile_id = game->map->layers[layer][(x + x_offset / TILE_SIZE) + (y + y_offset / TILE_SIZE) * game->map->w];
if (tile_id != 0) {
tile_id--;
unsigned int tile_x = TILE_SIZE * (tile_id % TILESET_WIDTH);
@ -69,11 +69,11 @@ void engine_draw_player(struct Game const *game) {
const int offset_map_y = (DHEIGHT / TILE_SIZE - game->map->h + 1)/2;
dframe(
(game->player->x + offset_map_x) * TILE_SIZE + game->player->anim.dx*3,
(game->player->y + offset_map_y) * TILE_SIZE - 5 + game->player->anim.dy*3,
(game->player->pos.x + offset_map_x) * TILE_SIZE + game->player->anim.dx*3,
(game->player->pos.y + offset_map_y) * TILE_SIZE - 5 + game->player->anim.dy*3,
game->player->anim.img); //draw the player 5 pixel up
}
dprint(1,1,C_BLACK,"%d:%d",game->player->x, game->player->y);
dprint(1,1,C_BLACK,"%d:%d",game->player->pos.x, game->player->pos.y);
}
/*move the player to the direction*/
@ -84,13 +84,13 @@ int engine_move(struct Game *game, int direction) {
if(!game->player->idle) return 0;
if(game->player->direction == direction) {
if(map_walkable(game->map, game->player->x + dx, game->player->y + dy)) {
game->player->x += dx;
game->player->y += dy;
if(map_walkable(game->map, game->player->pos.x + dx, game->player->pos.y + dy)) {
game->player->pos.x += dx;
game->player->pos.y += dy;
if(is_map_larger(game->map)) {
game->camera->x += dx*16;
game->camera->y += dy*16;
game->camera.offset.x += dx*16;
game->camera.offset.y += dy*16;
}
game->player->idle = !anim_player_walking(&game->player->anim, 1);
@ -125,7 +125,7 @@ void engine_action(struct Game const *game, int action) {
int direction = game->player->direction;
int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT);
int dy = (direction == DIR_DOWN) - (direction == DIR_UP);
draw_dialog(get_character_xy(game->characters, game->player->x + dx, game->player->y + dy));
draw_dialog(get_character_xy(game->characters, game->player->pos.x + dx, game->player->pos.y + dy));
}
}
}
@ -140,4 +140,4 @@ void engine_check_position(struct Game *game) {
if(player_curr_tile == TILE_DOOR_OUT) {
engine_set_background(game, C_WHITE);
}
}
}

View File

@ -26,8 +26,8 @@ static int callback_tick(volatile int *tick) {
int main(void) {
/*Structure definition*/
struct Player player = {
.x = 32,
.y = 30,
.pos = VEC2(32, 30),
.pos_visual = VEC2F(32*TILE_SIZE, 30*TILE_SIZE),
.x_mid = 6,
.y_mid = 1,
.show_x = 12,
@ -38,15 +38,10 @@ int main(void) {
};
player.idle = !anim_player_idle(&player.anim, 1);
struct Camera camera = {
.x = player.x * TILE_SIZE + player.x_mid,
.y = player.y * TILE_SIZE + player.y_mid
};
struct Game game = {
.map = maps[0],
.player = &player,
.camera = &camera,
.camera = camera_new(&player.pos_visual),
.background = C_WHITE
};
game.characters = get_map_characters(1);
@ -67,7 +62,7 @@ int main(void) {
dupdate();
int action = get_inputs();
if(action >= 0 && action <= 3)
if(action >= 0 && action <= 3)
engine_move(&game, action);
else if(action >= 4) {
engine_action(&game, action);

View File

@ -14,7 +14,7 @@ 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) {
return game->map->info_map[game->player->x + game->map->w * game->player->y];
return game->map->info_map[game->player->pos.x + game->map->w * game->player->pos.y];
return 0;
}
@ -22,8 +22,8 @@ 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->x = in_1.w/2 * TILE_SIZE + game->player->x_mid;
game->camera->y = in_1.h/2 * TILE_SIZE + game->player->y_mid;
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*/
@ -33,4 +33,4 @@ bool is_map_larger(struct Map *map) {
} else {
return false;
}
}
}

View File

@ -12,17 +12,18 @@ int player_facing(struct Game const *game) {
int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT);
int dy = (direction == DIR_DOWN) - (direction == DIR_UP);
int index = game->player->x + dx + game->map->w * (game->player->y + dy);
if(game->player->x + dx >= 0 &&
game->player->x + dx <= game->map->w &&
game->player->y + dy >= 0 &&
game->player->y + dy <= game->map->h) {
int index = game->player->pos.x + dx + game->map->w * (game->player->pos.y + dy);
if(game->player->pos.x + dx >= 0 &&
game->player->pos.x + dx <= game->map->w &&
game->player->pos.y + dy >= 0 &&
game->player->pos.y + dy <= game->map->h) {
return game->map->info_map[index];
}
return TILE_SOLID;
}
/* lol */
void set_player_xy(struct Player *player, int x, int y) {
player->x = x;
player->y = y;
}
player->pos.x = x;
player->pos.y = y;
}

37
src/vec2.c Normal file
View File

@ -0,0 +1,37 @@
#include "vec2.h"
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)
{
return VEC2F(v1.x + v2.x, v1.y + v2.y);
}
struct Vec2
vec2_sub(struct Vec2 v1, struct Vec2 v2)
{
return VEC2(v1.x - v2.x, v1.y - v2.y);
}
struct Vec2
vec2_mul(struct Vec2 v, int scale)
{
return VEC2(v.x * scale, v.y * scale);
}
struct Vec2
vec2f_vec2(struct Vec2f v)
{
return VEC2(v.x, v.y);
}
struct Vec2f
vec2_vec2f(struct Vec2 v)
{
return VEC2F(v.x, v.y);
}