Lot of bugfixes and improvements :)

- [input] Completely rewrote most of the code
- [input/camera] Renamed `*_draw` functions used in debug section to
`*_draw_debug`
- [main/input] Now use `_Bool` instead of `int` when appropriate
- [input] Added function `input_is_up`
- [player] Added function `player_draw_debug`
- [vec] Use `int` instead of `float`, new macro `VEC_PRECISION`
- [main/camera/player/vec] Updated vector related code to work with the
new system
This commit is contained in:
KikooDX 2020-09-18 11:12:34 +02:00
parent baa6071ebf
commit 65ceff3971
8 changed files with 68 additions and 36 deletions

View File

@ -14,6 +14,6 @@ typedef struct Camera
void camera_step(Camera *camera);
/* draw a dot corresponding to camera position */
void camera_draw(Camera *camera);
void camera_draw_debug(Camera *camera);
#endif /* _DEF_CAMERA */

View File

@ -13,10 +13,10 @@ enum {
};
enum {
S_PRESSED,
S_DOWN,
S_RELEASED,
S_UP
S_PRESSED, /* 0 */
S_DOWN, /* 1 */
S_RELEASED, /* 2 */
S_UP /* 3 */
};
typedef struct Input
@ -28,11 +28,12 @@ typedef struct Input
/* will check for new key inputs and update states[] */
void input_step(Input *input);
/* display all key states */
void input_draw(Input *input);
/* display all keys states and information */
void input_draw_debug(Input *input);
int input_is_pressed(Input input, uint8_t key);
int input_is_down(Input input, uint8_t key);
int input_is_released(Input input, uint8_t key);
_Bool input_is_pressed(Input *input, uint8_t key);
_Bool input_is_down(Input *input, uint8_t key);
_Bool input_is_released(Input *input, uint8_t key);
_Bool input_is_up(Input *input, uint8_t key);
#endif /* _DEF_INPUT */

View File

@ -14,5 +14,6 @@ typedef struct Player
void player_step(Player *player, Input *input);
void player_draw(Player *player, Camera *camera);
void player_draw_debug(Player *player);
#endif /* _DEF_PLAYER */

View File

@ -1,10 +1,12 @@
#ifndef _DEF_VEC
#define _DEF_VEC
#define VEC_PRECISION 512
typedef struct Vec
{
float x;
float y;
int x;
int y;
} Vec;
/* like memcpy but for vectors */

View File

@ -9,12 +9,12 @@ void camera_step(Camera *camera)
vec_lerp(&camera->pos, *camera->target, camera->speed);
}
void camera_draw(Camera *camera)
void camera_draw_debug(Camera *camera)
{
#ifdef FX9860G
dpixel((int)camera->pos.x, (int)camera->pos.y, C_BLACK);
dpixel(camera->pos.x / VEC_PRECISION, camera->pos.y / VEC_PRECISION, C_BLACK);
#endif /* FX9860G */
#ifdef FXCG50
dpixel((int)camera->pos.x, (int)camera->pos.y, C_RED);
dpixel(camera->pos.x / VEC_PRECISION, camera->pos.y / VEC_PRECISION, C_RED);
#endif /* FXCG50 */
}

View File

@ -11,38 +11,51 @@ void input_step(Input *input)
for (int i = 0; i < KEYS_COUNT; ++i)
{
uint8_t *state = &input->states[i];
uint8_t *key = &input->keys[i];
int pressed = keydown(*key);
uint8_t key = input->keys[i];
/* get if the key is pressed */
_Bool pressed = keydown(key);
/* update input status */
if (pressed)
{
*state = (input_is_down(*input, *key) ? S_DOWN : S_PRESSED);
*state = (*state == S_RELEASED || *state == S_UP) ? S_PRESSED : S_DOWN;
}
else
{
*state = (!input_is_down(*input, *key) ? S_UP : S_RELEASED);
*state = (*state == S_PRESSED || *state == S_DOWN) ? S_RELEASED : S_UP;
}
}
}
void input_draw(Input *input)
void input_draw_debug(Input *input)
{
for (int i = 0; i < KEYS_COUNT; ++i)
{
dprint(0, i * 10, C_BLACK, "%d", input->states[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));
dprint(48, i * 10, C_BLACK, "P%d", input_is_pressed(input, i));
dprint(64, i * 10, C_BLACK, "U%d", input_is_up(input, i));
dprint(80, i * 10, C_BLACK, "R%d", input_is_released(input, i));
dprint(96, i * 10, C_BLACK, "V%d", (int)input_is_down(input, i));
}
}
int input_is_pressed(Input input, uint8_t key)
_Bool input_is_pressed(Input *input, uint8_t key)
{
return input.states[key] == S_PRESSED;
return input->states[key] == S_PRESSED;
}
int input_is_down(Input input, uint8_t key)
_Bool input_is_down(Input *input, uint8_t key)
{
return input.states[key] <= S_DOWN;
return (input->states[key] == S_DOWN) || (input->states[key] == S_PRESSED);
}
int input_is_released(Input input, uint8_t key)
_Bool input_is_released(Input *input, uint8_t key)
{
return input.states[key] == S_RELEASED;
return input->states[key] == S_RELEASED;
}
_Bool input_is_up(Input *input, uint8_t key)
{
return (input->states[key] == S_UP) || (input->states[key] == S_RELEASED);
}

View File

@ -1,6 +1,7 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/std/string.h>
#include <stdbool.h>
#include "main.h"
#include "debug.h"
@ -26,9 +27,9 @@ int play_level(int level_id)
{
/* create player */
Player player = {
.pos = {16, 16},
.hbox = {7, 7},
.origin = {4, 4}
.pos = {16 * VEC_PRECISION, 16 * VEC_PRECISION},
.hbox = {7 * VEC_PRECISION, 7 * VEC_PRECISION},
.origin = {4 * VEC_PRECISION, 4 * VEC_PRECISION}
};
/* create level */
@ -39,7 +40,7 @@ int play_level(int level_id)
/* create camera */
Camera camera = {
.pos = {DWIDTH, DHEIGHT},
.pos = {DWIDTH * VEC_PRECISION, DHEIGHT * VEC_PRECISION},
.target = &player.pos,
#ifdef FX9860G
.speed = 0.0005
@ -57,7 +58,8 @@ int play_level(int level_id)
input.keys[K_DOWN] = KEY_DOWN;
//vec_cpy(&camera.pos, player.pos);
while ((int)camera.pos.x != (int)player.pos.x || (int)camera.pos.y != (int)player.pos.y)
while (camera.pos.x / VEC_PRECISION != player.pos.x / VEC_PRECISION ||
camera.pos.y / VEC_PRECISION != player.pos.y / VEC_PRECISION)
{
step_event(&player, &level, &camera, &input);
draw_event(&player, &level, &camera, &input);
@ -67,6 +69,7 @@ int play_level(int level_id)
void step_event(Player *player, Level *level, Camera *camera, Input *input)
{
//getkey();
input_step(input);
player_step(player, input);
level_step(level);
@ -79,8 +82,9 @@ void draw_event(Player *player, Level *level, Camera *camera, Input *input)
level_draw(level, camera);
player_draw(player, camera);
#ifdef DEBUG
camera_draw(camera);
input_draw(input);
camera_draw_debug(camera);
//input_draw_debug(input);
player_draw_debug(player);
#endif
dupdate();
}

View File

@ -7,8 +7,8 @@
void player_step(Player *player, Input *input)
{
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)
(input_is_down(input, K_RIGHT) - input_is_down(input, K_LEFT)) * VEC_PRECISION,
(input_is_down(input, K_DOWN) - input_is_down(input, K_UP)) * VEC_PRECISION
};
#ifdef FX9860G
vec_div(&move, 50);
@ -27,6 +27,8 @@ void player_draw(Player *player, Camera *camera)
vec_sub(&draw_pos_tl, player->origin);
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);
#ifdef FX9860G
drect(draw_pos_tl.x, draw_pos_tl.y, draw_pos_br.x, draw_pos_br.y, C_LIGHT);
#endif /* FX9860G */
@ -34,3 +36,12 @@ void player_draw(Player *player, Camera *camera)
drect(draw_pos_tl.x, draw_pos_tl.y, draw_pos_br.x, draw_pos_br.y, C_BLACK);
#endif /* FXCG50 */
}
void player_draw_debug(Player *player)
{
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, "tx: %d", player->pos.x / VEC_PRECISION);
dprint(0, 30, C_BLACK, "ty: %d", player->pos.y / VEC_PRECISION);
dprint(0, 40, C_BLACK, "vp: %d", VEC_PRECISION);
}