This repository has been archived on 2022-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
jtmm2-old/src/camera.c

53 lines
1.6 KiB
C

#include <gint/display.h>
#include "camera.h"
#include "conf.h"
#include "vec.h"
#include "player.h"
#include "level.h"
void camera_step(Camera *camera)
{
/* lerp towards target */
vec_lerp(&camera->pos, *camera->target, camera->speed);
/* put the camera in the right place */
vec_clamp(&camera->pos, camera->min, camera->max);
/* calculate pixel offset */
Vec * const offset = &camera->offset; /* DRY */
vec_cpy(offset, camera->pos);
vec_div(offset, VEC_PRECISION);
vec_sub(offset, VEC_SCALED_DCENTER);
}
void camera_init(Camera *camera, Player *player, const Level *level)
{
/* calculate min and max */
vec_cpy(&camera->min, VEC_SCALED_DCENTER);
vec_mul(&camera->min, VEC_PRECISION);
vec_cpy(&camera->max, (Vec){0, 0});
vec_sub(&camera->max, VEC_SCALED_DCENTER);
vec_mul(&camera->max, VEC_PRECISION);
vec_add(&camera->max, (Vec){TILE_SIZE * level->width, TILE_SIZE * level->height});
vec_cpy(&camera->pos, player->pos);
vec_clamp(&camera->pos, camera->min, camera->max);
}
void camera_draw_debug(Camera *camera)
{
dprint(0, 0, C_BLACK, "-x: %d, -y: %d", camera->min.x, camera->min.y);
dprint(0, 10, C_BLACK, "+x: %d, +y: %d", camera->max.x, camera->max.y);
dprint(0, 20, C_BLACK, "cx: %d, cy: %d", camera->pos.x, camera->pos.y);
dprint(0, 30, C_BLACK, "px: %d, py: %d", camera->offset.x, camera->offset.y);
/*Vec draw_pos;
vec_cpy(&draw_pos, camera->pos);
vec_mul(&draw_pos, SCALE);
vec_div(&draw_pos, VEC_PRECISION);
#ifdef FX9860G
const int color = C_BLACK;
#endif /* FX9860G *//*
#ifdef FXCG50
const int color = C_RED;
#endif /* FXCG50 *//*
dpixel(draw_pos.x, draw_pos.y, color);*/
}