[new system] Input manager! plus misc cleanup
This commit is contained in:
parent
62d637969d
commit
c95d92f6d1
|
@ -12,9 +12,8 @@ typedef struct Camera
|
|||
} Camera;
|
||||
|
||||
void camera_step(Camera *camera);
|
||||
#ifdef DEBUG
|
||||
|
||||
/* draw a dot corresponding to camera position */
|
||||
void camera_draw(Camera *camera);
|
||||
#endif
|
||||
|
||||
#endif /* _DEF_CAMERA */
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef _DEF_INPUT
|
||||
#define _DEF_INPUT
|
||||
|
||||
#include <gint/defs/types.h>
|
||||
|
||||
#define KEYS_COUNT 4
|
||||
|
||||
enum {
|
||||
K_LEFT,
|
||||
K_RIGHT,
|
||||
K_UP,
|
||||
K_DOWN
|
||||
};
|
||||
|
||||
enum {
|
||||
S_PRESSED,
|
||||
S_DOWN,
|
||||
S_RELEASED,
|
||||
S_UP
|
||||
};
|
||||
|
||||
typedef struct Input
|
||||
{
|
||||
uint8_t keys[KEYS_COUNT];
|
||||
uint8_t states[KEYS_COUNT];
|
||||
} Input;
|
||||
|
||||
/* will check for new key inputs and update states[] */
|
||||
void input_step(Input *input);
|
||||
|
||||
/* display all key states */
|
||||
void input_draw(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);
|
||||
|
||||
#endif /* _DEF_INPUT */
|
|
@ -1,13 +1,15 @@
|
|||
#ifndef _DEF_LEVEL
|
||||
#define _DEF_LEVEL
|
||||
|
||||
#include <gint/defs/types.h>
|
||||
|
||||
#include "camera.h"
|
||||
|
||||
typedef struct Level
|
||||
{
|
||||
int width; /* in tiles */
|
||||
int height; /* in tiles */
|
||||
const unsigned char **layers; /* points toward the level content */
|
||||
const uint8_t **layers; /* points toward the level content */
|
||||
} Level;
|
||||
|
||||
void level_step(Level *level);
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
#include "player.h"
|
||||
#include "level.h"
|
||||
|
||||
#ifndef _DEF_MAIN
|
||||
#define _DEF_MAIN
|
||||
#include "camera.h"
|
||||
#include "input.h"
|
||||
|
||||
int play_level(int level_id);
|
||||
void step_event(Player *player, Level *level, Camera *camera);
|
||||
void draw_event(Player *player, Level *level, Camera *camera);
|
||||
|
||||
#endif
|
||||
void step_event(Player *player, Level *level, Camera *camera, Input *input);
|
||||
void draw_event(Player *player, Level *level, Camera *camera, Input *input);
|
||||
|
|
|
@ -35,7 +35,7 @@ local function create_structure_c(id)
|
|||
if content then
|
||||
-- layers
|
||||
for i, layer in ipairs(content.layers) do
|
||||
io.write("const unsigned char tiles_", id, "_", i, "[] = {\n\t")
|
||||
io.write("const uint8_t tiles_", id, "_", i, "[] = {\n\t")
|
||||
for i, v in ipairs(layer) do
|
||||
io.write(v, ", ")
|
||||
if i % 14 == 0 and i ~= #layer then
|
||||
|
@ -45,7 +45,7 @@ local function create_structure_c(id)
|
|||
io.write("\n};\n");
|
||||
end
|
||||
-- array
|
||||
io.write("const unsigned char *layers_", id, "[] = {")
|
||||
io.write("const uint8_t *layers_", id, "[] = {")
|
||||
for i = 1, #content.layers, 1 do
|
||||
io.write("tiles_", id, "_", i, ", ")
|
||||
end
|
||||
|
|
|
@ -9,7 +9,6 @@ void camera_step(Camera *camera)
|
|||
vec_lerp(&camera->pos, *camera->target, camera->speed);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
void camera_draw(Camera *camera)
|
||||
{
|
||||
#ifdef FX9860G
|
||||
|
@ -19,4 +18,3 @@ void camera_draw(Camera *camera)
|
|||
dpixel((int)camera->pos.x, (int)camera->pos.y, C_RED);
|
||||
#endif /* FXCG50 */
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
#include <gint/keyboard.h>
|
||||
#include <gint/display.h>
|
||||
|
||||
#include "input.h"
|
||||
|
||||
void input_step(Input *input)
|
||||
{
|
||||
/* read all inputs */
|
||||
clearevents();
|
||||
/* for each key, update it's state */
|
||||
for (int i = 0; i < KEYS_COUNT; ++i)
|
||||
{
|
||||
uint8_t *state = &input->states[i];
|
||||
uint8_t *key = &input->keys[i];
|
||||
int pressed = keydown(*key);
|
||||
if (pressed)
|
||||
{
|
||||
*state = (input_is_down(*input, *key) ? S_DOWN : S_PRESSED);
|
||||
}
|
||||
else
|
||||
{
|
||||
*state = (!input_is_down(*input, *key) ? S_UP : S_RELEASED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void input_draw(Input *input)
|
||||
{
|
||||
for (int i = 0; i < KEYS_COUNT; ++i)
|
||||
{
|
||||
dprint(0, i * 10, C_BLACK, "%d", input->states[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int input_is_pressed(Input input, uint8_t key)
|
||||
{
|
||||
return input.states[key] == S_PRESSED;
|
||||
}
|
||||
|
||||
int input_is_down(Input input, uint8_t key)
|
||||
{
|
||||
return input.states[key] <= S_DOWN;
|
||||
}
|
||||
|
||||
int input_is_released(Input input, uint8_t key)
|
||||
{
|
||||
return input.states[key] == S_RELEASED;
|
||||
}
|
19
src/main.c
19
src/main.c
|
@ -9,6 +9,7 @@
|
|||
#include "player.h"
|
||||
#include "level.h"
|
||||
#include "camera.h"
|
||||
#include "input.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
@ -47,29 +48,39 @@ int play_level(int level_id)
|
|||
.speed = 0.05
|
||||
#endif /* FXCG50 */
|
||||
};
|
||||
|
||||
/* create input manager */
|
||||
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;
|
||||
|
||||
//vec_cpy(&camera.pos, player.pos);
|
||||
while ((int)camera.pos.x != (int)player.pos.x)
|
||||
{
|
||||
step_event(&player, &level, &camera);
|
||||
draw_event(&player, &level, &camera);
|
||||
step_event(&player, &level, &camera, &input);
|
||||
draw_event(&player, &level, &camera, &input);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void step_event(Player *player, Level *level, Camera *camera)
|
||||
void step_event(Player *player, Level *level, Camera *camera, Input *input)
|
||||
{
|
||||
input_step(input);
|
||||
player_step(player);
|
||||
level_step(level);
|
||||
camera_step(camera);
|
||||
}
|
||||
|
||||
void draw_event(Player *player, Level *level, Camera *camera)
|
||||
void draw_event(Player *player, Level *level, Camera *camera, Input *input)
|
||||
{
|
||||
dclear(C_WHITE);
|
||||
level_draw(level, camera);
|
||||
player_draw(player, camera);
|
||||
#ifdef DEBUG
|
||||
camera_draw(camera);
|
||||
input_draw(input);
|
||||
#endif
|
||||
dupdate();
|
||||
}
|
||||
|
|
Reference in New Issue