polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit 9b033fe1f5d213ec2c17d4f649d9d3a9261e8929
parent c1de338c059137296fb9ce905e5cd886a35ec61b
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 27 Apr 2020 02:10:21 -0700

input: leftstick button to move fast

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Msrc/game.c | 3+--
Msrc/input.c | 16+++++++++++++---
Msrc/input.h | 6++----
Msrc/update.c | 8++++++--
4 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -195,8 +195,6 @@ void init_misc(struct game *game, int width, int height) { node_rotate(freecam, V3(100, 0, 0)); node_translate(freecam, V3(0,-40,20)); - input_init(&game->input); - // FBO STUFF init_fbo(&res->shadow_buffer); resize_fbos(player, &res->shadow_buffer, res->proj_ortho, width, height); @@ -241,6 +239,7 @@ void game_init(struct game *game, int width, int height) { game->wireframe = 0; + init_input(&game->input); init_controller(&game->input); init_misc(game, width, height); } diff --git a/src/input.c b/src/input.c @@ -54,12 +54,12 @@ static void axis_motion(struct input *input, SDL_JoyAxisEvent *event) { if (event->axis >= MAX_AXIS) return; input->axis[event->axis] = event->axis == 1 ? -event->value : event->value; - printf("axis %d %d", input->axis[0], input->axis[1]); + /* printf("axis %d %d", input->axis[0], input->axis[1]); */ for (int i = 0; i < MAX_AXIS; i++) { if (input->axis[i] >= -4000 && input->axis[i] <= 4000 ) input->axis[i] = 0; } - printf(" -> %d %d\n", input->axis[0], input->axis[1]); + /* printf(" -> %d %d\n", input->axis[0], input->axis[1]); */ } void process_events(struct input *input, u64 current_frame) { @@ -132,7 +132,7 @@ void process_events(struct input *input, u64 current_frame) { } -void input_init(struct input *input) { +void init_input(struct input *input) { /* memset(input->keys, 0, sizeof(input->keys[0]) * ARRAY_SIZE(input->keys)); */ input->keystates = SDL_GetKeyboardState(NULL); assert(sizeof(input->key_edge_states) == SDL_NUM_SCANCODES * sizeof(input->key_edge_states[0])); @@ -150,6 +150,7 @@ void input_init(struct input *input) { input->last_my = 0; input->resized_height = 0; input->resized_width = 0; + input->controller = 0; assert(input->keystates); } @@ -179,3 +180,12 @@ void input_reset(struct input *input) { int input_is_dragging(struct input *input, int mouse_button) { return input->mbuttons[mouse_button-1]; } + +bool is_button_down(struct input *input, SDL_GameControllerButton button) +{ + if (!input->controller) { + return false; + } + + return SDL_GameControllerGetButton(input->controller, button) == 1; +} diff --git a/src/input.h b/src/input.h @@ -55,15 +55,13 @@ struct input { int input_is_dragging(struct input *input, int mouse_button); +bool is_button_down(struct input *input, SDL_GameControllerButton button); bool is_key_down_on_frame(struct input *input, u8 scancode, u64 frame); bool is_button_down_on_frame(struct input *input, SDL_GameControllerButton button, u64 frame); -void input_init(struct input *input); - +void init_input(struct input *input); void input_reset(struct input *input); - void handle_key(struct input *input, SDL_KeyboardEvent); - void process_events(struct input *input, u64 current_frame); #endif /* POLYADVENT_INPUT_H */ diff --git a/src/update.c b/src/update.c @@ -44,12 +44,16 @@ static void movement(struct game *game, struct node *node, float speed_mult) amt *= speed_mult; + if ((game->input.modifiers & KMOD_SHIFT) || + is_button_down(&game->input, SDL_CONTROLLER_BUTTON_LEFTSTICK)) + { + amt *= 20; + } + // joystick movement node_forward(node, V3(0,amt*y_axis, 0)); node_forward(node, V3(amt*x_axis, 0, 0)); - if (game->input.modifiers & KMOD_SHIFT) - amt *= 20; if (game->input.keystates[SDL_SCANCODE_A]) node_forward(node, V3(-amt,0,0));