Camera kinda works.

- created separated float and int vector functions for performance
- simple camera related draw, should rework
- new `vec_drect` function in `vec`
This commit is contained in:
KikooDX 2020-09-21 14:15:58 +02:00
parent ac19ece235
commit 470ac9b783
5 changed files with 51 additions and 9 deletions

View File

@ -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

View File

@ -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 */

View File

@ -1,5 +1,6 @@
#include <gint/display.h>
#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);
}
}
}
}

View File

@ -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)

View File

@ -1,3 +1,5 @@
#include <gint/display.h>
#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);
}