Winter cleaning

This commit is contained in:
KikooDX 2020-12-21 12:18:55 +01:00
parent 559c00a840
commit 833a1711c4
13 changed files with 71 additions and 133 deletions

View File

@ -3,8 +3,7 @@
#include "vec.h"
typedef struct Camera
{
typedef struct Camera {
Vec pos;
Vec offset; /* adjusted pixel offset based on pos */
Vec *target; /* the target position to lerp on */

View File

@ -21,8 +21,7 @@ enum {
S_UP /* 3 */
};
typedef struct Input
{
typedef struct Input {
uint8_t keys[KEYS_COUNT];
uint8_t states[KEYS_COUNT];
} Input;

View File

@ -3,8 +3,7 @@
#include <gint/defs/types.h>
typedef struct Level
{
typedef struct Level {
int width; /* in tiles */
int height; /* in tiles */
const uint8_t **layers; /* points toward the level content */

View File

@ -3,8 +3,7 @@
#include "vec.h"
typedef struct Player
{
typedef struct Player {
Vec pos;
Vec hbox; /* the bottom left corner of the player's hitbox */
Vec vbox; /* the bottom left corner of the player's visual box */

View File

@ -1,8 +1,7 @@
#ifndef _DEF_VEC
#define _DEF_VEC
typedef struct Vec
{
typedef struct {
int x;
int y;
} Vec;

View File

@ -6,8 +6,7 @@
#include "player.h"
#include "level.h"
void camera_step(Camera *camera)
{
void camera_step(Camera *camera) {
/* lerp towards target */
vec_lerp(&camera->pos, *camera->target, camera->speed);
/* put the camera in the right place */
@ -19,26 +18,23 @@ void camera_step(Camera *camera)
vec_sub(offset, VEC_SCALED_DCENTER);
}
void camera_init(Camera *camera, Player *player, const Level *level)
{
void camera_init(Camera *camera, Player *player, const Level *level) {
/* NOTE: This system doesn't totally works, but it's good enough for now. */
/* check level size */
const Vec level_dim = {level->width * (TILE_SIZE / VEC_PRECISION * SCALE),
level->height * (TILE_SIZE / VEC_PRECISION * SCALE)};
if (level_dim.x > DWIDTH || level_dim.y > DHEIGHT)
{
if (level_dim.x > DWIDTH || level_dim.y > DHEIGHT) {
/* level cannot be displayed on a single screen */
/* 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_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_add(&camera->max, (Vec){ TILE_SIZE * level->width, TILE_SIZE * level->height });
vec_cpy(&camera->pos, player->pos);
}
else
{
else {
/* level is single screen */
/* We calculate the offset and setup the camera to center everything. */
const Vec screen_center_precise = {level->width * TILE_SIZE / 2,
@ -49,8 +45,7 @@ void camera_init(Camera *camera, Player *player, const Level *level)
vec_clamp(&camera->pos, camera->min, camera->max);
}
void camera_draw_debug(Camera *camera)
{
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);

View File

@ -5,94 +5,78 @@
#include "conf.h"
#include "level.h"
uint8_t collide_point(Vec point, const Level *level, uint layer_id)
{
uint8_t collide_point(Vec point, const Level *level, uint layer_id) {
Vec cursor; /* the final position to test */
vec_cpy(&cursor, point);
vec_div(&cursor, TILE_SIZE); /* get the expected tile ID */
if (point.x < 0 || point.y < 0 ||
cursor.x >= level->width || cursor.y >= level->height)
{
cursor.x >= level->width || cursor.y >= level->height) {
return 0; /* the point is out of bounds */
}
else
{
else {
return level->layers[layer_id][cursor.x + cursor.y * level->width];
}
}
bool collide_right(Player *player, Vec position, const Level *level, uint layer_id)
{
bool collide_right(Player *player, Vec position, const Level *level, uint layer_id) {
Vec pos;
vec_cpy(&pos, position);
vec_sub(&pos, player->origin);
pos.x += player->hbox.x;
if (collide_point(pos, level, layer_id))
{
if (collide_point(pos, level, layer_id)) {
return true;
}
pos.y += player->hbox.y;
if (collide_point(pos, level, layer_id))
{
if (collide_point(pos, level, layer_id)) {
return true;
}
return false;
}
bool collide_left(Player *player, Vec position, const Level *level, uint layer_id)
{
bool collide_left(Player *player, Vec position, const Level *level, uint layer_id) {
Vec pos;
vec_cpy(&pos, position);
vec_sub(&pos, player->origin);
if (collide_point(pos, level, layer_id))
{
if (collide_point(pos, level, layer_id)) {
return true;
}
pos.y += player->hbox.y;
if (collide_point(pos, level, layer_id))
{
if (collide_point(pos, level, layer_id)) {
return true;
}
return false;
}
bool collide_down(Player *player, Vec position, const Level *level, uint layer_id)
{
bool collide_down(Player *player, Vec position, const Level *level, uint layer_id) {
Vec pos;
vec_cpy(&pos, position);
vec_sub(&pos, player->origin);
pos.y += player->hbox.y;
if (collide_point(pos, level, layer_id))
{
if (collide_point(pos, level, layer_id)) {
return true;
}
pos.x += player->hbox.x;
if (collide_point(pos, level, layer_id))
{
if (collide_point(pos, level, layer_id)) {
return true;
}
return false;
}
bool collide_up(Player *player, Vec position, const Level *level, uint layer_id)
{
bool collide_up(Player *player, Vec position, const Level *level, uint layer_id) {
Vec pos;
vec_cpy(&pos, position);
vec_sub(&pos, player->origin);
if (collide_point(pos, level, layer_id))
{
if (collide_point(pos, level, layer_id)) {
return true;
}
pos.x += player->hbox.x;
if (collide_point(pos, level, layer_id))
{
if (collide_point(pos, level, layer_id)) {
return true;
}
return false;
}
bool collide_full(Player *player, Vec position, const Level *level, uint layer_id)
{
bool collide_full(Player *player, Vec position, const Level *level, uint layer_id) {
return collide_up(player, position, level, layer_id) ||
collide_down(player, position, level, layer_id);
}

View File

@ -1,7 +1,6 @@
#include "init.h"
void init()
{
void init() {
#ifdef FX9860G
dgray(DGRAY_ON);
#endif /* FX9860G */

View File

@ -3,46 +3,38 @@
#include "input.h"
void input_step(Input *input)
{
void input_step(Input *input) {
/* read all inputs */
clearevents();
/* for each key, update it's state */
for (int i = 0; i < KEYS_COUNT; ++i)
{
for (int i = 0; i < KEYS_COUNT; ++i) {
uint8_t *state = &input->states[i];
uint8_t key = input->keys[i];
/* get if the key is pressed */
bool pressed = keydown(key);
/* update input status */
if (pressed)
{
if (pressed) {
*state = (*state == S_RELEASED || *state == S_UP) ? S_PRESSED : S_DOWN;
}
else
{
else {
*state = (*state == S_PRESSED || *state == S_DOWN) ? S_RELEASED : S_UP;
}
}
}
void input_init(Input *input)
{
void input_init(Input *input) {
input->keys[K_LEFT] = KEY_LEFT;
input->keys[K_RIGHT] = KEY_RIGHT;
input->keys[K_UP] = KEY_UP;
input->keys[K_DOWN] = KEY_DOWN;
input->keys[K_EXIT] = KEY_EXIT;
for (int i = 0; i < KEYS_COUNT; ++i)
{
for (int i = 0; i < KEYS_COUNT; ++i) {
input->states[i] = S_UP;
}
}
void input_draw_debug(Input *input)
{
for (int i = 0; i < KEYS_COUNT; ++i)
{
void input_draw_debug(Input *input) {
for (int i = 0; i < KEYS_COUNT; ++i) {
dprint(0, i * 10, C_BLACK, "%d", i);
dprint(16, i * 10, C_BLACK, "%d", input->states[i]);
dprint(32, i * 10, C_BLACK, "D%d", input_is_down(input, i));
@ -53,22 +45,18 @@ void input_draw_debug(Input *input)
}
}
bool input_is_pressed(Input *input, uint8_t key)
{
bool input_is_pressed(Input *input, uint8_t key) {
return input->states[key] == S_PRESSED;
}
bool input_is_down(Input *input, uint8_t key)
{
bool input_is_down(Input *input, uint8_t key) {
return (input->states[key] == S_DOWN) || (input->states[key] == S_PRESSED);
}
bool input_is_released(Input *input, uint8_t key)
{
bool input_is_released(Input *input, uint8_t key) {
return input->states[key] == S_RELEASED;
}
bool input_is_up(Input *input, uint8_t key)
{
bool input_is_up(Input *input, uint8_t key) {
return (input->states[key] == S_UP) || (input->states[key] == S_RELEASED);
}

View File

@ -6,17 +6,14 @@
#define VEC_PRECISE_HALF_DISP (Vec){DWIDTH * VEC_PRECISION / (2 * SCALE), DHEIGHT * VEC_PRECISION / (2 * SCALE)}
void level_step(const Level *level)
{
void level_step(const Level *level) {
}
void level_draw(const Level *level, Camera *camera)
{
void level_draw(const Level *level, Camera *camera) {
layer_draw(level, camera, 0);
}
void layer_draw(const Level *level, Camera *camera, uint layer_id)
{
void layer_draw(const Level *level, Camera *camera, uint layer_id) {
const uint8_t *layer = level->layers[layer_id];
Vec display_tl, display_br;
vec_cpy(&display_tl, camera->pos);
@ -29,10 +26,8 @@ void layer_draw(const Level *level, Camera *camera, uint layer_id)
int start_y = (display_tl.y > 0) ? display_tl.y : 0;
int end_x = (display_br.x < level->width) ? display_br.x + 1 : level->width;
int end_y = (display_br.y < level->height) ? display_br.y + 1 : level->height;
for (int y = start_y; y < end_y; ++y)
{
for (int x = start_x; x < end_x; ++x)
{
for (int y = start_y; y < end_y; ++y) {
for (int x = start_x; x < end_x; ++x) {
const uint8_t cell = layer[x + y * level->width];
#ifdef FX9860G
const int color = C_LIGHT;
@ -40,8 +35,7 @@ void layer_draw(const Level *level, Camera *camera, uint layer_id)
#ifdef FXCG50
const int color = C_GREEN;
#endif /* FXCG50 */
if (cell == 1)
{
if (cell == 1) {
Vec tl = {x, y};
Vec br;
vec_mul(&tl, TILE_SIZE);

View File

@ -16,8 +16,7 @@
#include "input.h"
#include "gen_levels.h"
int main(void)
{
int main(void) {
init(); /* initialize gint */
/* main game loop */
@ -27,8 +26,7 @@ int main(void)
return 1;
}
int play_level(uint level_id)
{
int play_level(uint level_id) {
/* create player */
Player player = {
//.pos = {TILE_SIZE, TILE_SIZE},
@ -36,7 +34,7 @@ int play_level(uint level_id)
.vbox = {6, 6},
.origin = {4 * VEC_PRECISION, 4 * VEC_PRECISION}
};
vec_cpy(&player.pos, player.origin); /* place the player at "0/0"
vec_cpy(&player.pos, player.origin); /* place the player at "0/0"*/
/* set level */
const Level *level;
@ -60,11 +58,9 @@ int play_level(uint level_id)
uint step = 0;
while (!input_is_down(&input, K_EXIT))
{
while (!input_is_down(&input, K_EXIT)) {
/* repeat step event so the UPS is constant regardless of FPS */
for (int i = 0; i < UPS / FPS; ++i)
{
for (int i = 0; i < UPS / FPS; ++i) {
/* UPS control */
while(!has_ticked) sleep();
has_ticked = 0;
@ -78,15 +74,13 @@ int play_level(uint level_id)
return 1;
}
int callback(volatile void *arg)
{
int callback(volatile void *arg) {
volatile int *has_ticked = arg;
*has_ticked = 1;
return 0;
}
void step_event(Player *player, const Level *level, Camera *camera, Input *input, uint step)
{
void step_event(Player *player, const Level *level, Camera *camera, Input *input, uint step) {
//getkey();
input_step(input);
player_step(player, input, level);
@ -94,8 +88,7 @@ void step_event(Player *player, const Level *level, Camera *camera, Input *input
camera_step(camera);
}
void draw_event(Player *player, const Level *level, Camera *camera, Input *input, uint step)
{
void draw_event(Player *player, const Level *level, Camera *camera, Input *input, uint step) {
dclear(C_WHITE);
level_draw(level, camera);
player_draw(player, camera);

View File

@ -6,8 +6,10 @@
#include "input.h"
#include "collide.h"
void player_step(Player *player, Input *input, const Level *level)
{
void player_move(Player *player, const Level *level) {
}
void player_step(Player *player, Input *input, const Level *level) {
Vec move = {
(input_is_down(input, K_RIGHT) - input_is_down(input, K_LEFT)),
(input_is_down(input, K_DOWN) - input_is_down(input, K_UP))
@ -23,8 +25,7 @@ void player_step(Player *player, Input *input, const Level *level)
}
}
void player_draw(Player *player, Camera *camera)
{
void player_draw(Player *player, Camera *camera) {
Vec tl;
Vec br;
vec_cpy(&tl, player->pos);
@ -41,8 +42,7 @@ void player_draw(Player *player, Camera *camera)
vec_drect(tl, br, C_BLACK);
}
void player_draw_debug(Player *player, uint step, const Level *level, uint layer_id)
{
void player_draw_debug(Player *player, uint step, const Level *level, uint layer_id) {
dprint(0, 0, C_BLACK, "x: %d", player->pos.x);
dprint(0, 10, C_BLACK, "y: %d", player->pos.y);
dprint(0, 20, C_BLACK, "vp: %d", VEC_PRECISION);

View File

@ -2,50 +2,42 @@
#include "vec.h"
void vec_cpy(Vec *destination, Vec source)
{
void vec_cpy(Vec *destination, Vec source) {
destination->x = source.x;
destination->y = source.y;
}
void vec_add(Vec *vector, Vec force)
{
void vec_add(Vec *vector, Vec force) {
vector->x += force.x;
vector->y += force.y;
}
void vec_sub(Vec *vector, Vec force)
{
void vec_sub(Vec *vector, Vec force) {
vector->x -= force.x;
vector->y -= force.y;
}
void vec_mul(Vec *vector, int scale)
{
void vec_mul(Vec *vector, int scale) {
vector->x *= scale;
vector->y *= scale;
}
void vec_mulf(Vec *vector, float scale)
{
void vec_mulf(Vec *vector, float scale) {
vector->x *= scale;
vector->y *= scale;
}
void vec_div(Vec *vector, int scale)
{
void vec_div(Vec *vector, int scale) {
vector->x /= scale;
vector->y /= scale;
}
void vec_divf(Vec *vector, float scale)
{
void vec_divf(Vec *vector, float scale) {
vector->x /= scale;
vector->y /= scale;
}
void vec_lerp(Vec *from, Vec to, float scale)
{
void vec_lerp(Vec *from, Vec to, float scale) {
/* from * (1 - scale) + temp * scale */
Vec temp;
vec_cpy(&temp, to);
@ -57,15 +49,13 @@ void vec_lerp(Vec *from, Vec to, float scale)
vec_add(from, temp);
}
void vec_clamp(Vec *to_limit, Vec min, Vec max)
{
void vec_clamp(Vec *to_limit, Vec min, Vec max) {
if (to_limit->x < min.x) to_limit->x = min.x;
if (to_limit->y < min.y) to_limit->y = min.y;
if (to_limit->x > max.x) to_limit->x = max.x;
if (to_limit->y > max.y) to_limit->y = max.y;
}
void vec_drect(Vec top_left, Vec bottom_right, int color)
{
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);
}