polyadvent

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

commit c1de338c059137296fb9ce905e5cd886a35ec61b
parent c6062a37b11deb6024ba2daa413a1c5e5feb44d5
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 27 Apr 2020 01:55:19 -0700

input/controller: axis move support

still need camera

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

Diffstat:
Msrc/input.c | 20+++++++++++++++++++-
Msrc/input.h | 7+++++++
Msrc/update.c | 7+++++++
3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/src/input.c b/src/input.c @@ -39,7 +39,7 @@ static void button_up(struct input *input, SDL_JoyButtonEvent *event, u64 curren static void button_down(struct input *input, SDL_JoyButtonEvent *event, u64 current_frame) { if (event->button >= SDL_CONTROLLER_BUTTON_MAX) return; - /* printf("button down %d\n", event->button); */ + printf("button down %d\n", event->button); struct input_edge *edge = &input->button_edge_states[event->button]; @@ -50,6 +50,18 @@ static void button_down(struct input *input, SDL_JoyButtonEvent *event, u64 curr edge->is_down = 1; } +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]); + 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]); +} + void process_events(struct input *input, u64 current_frame) { SDL_Event event; @@ -70,6 +82,9 @@ void process_events(struct input *input, u64 current_frame) { case SDL_KEYUP: key_up(input, event.key.keysym.scancode, current_frame); break; + case SDL_JOYAXISMOTION: + axis_motion(input, &event.jaxis); + break; case SDL_JOYBUTTONUP: button_up(input, &event.jbutton, current_frame); break; @@ -122,6 +137,9 @@ void input_init(struct input *input) { input->keystates = SDL_GetKeyboardState(NULL); assert(sizeof(input->key_edge_states) == SDL_NUM_SCANCODES * sizeof(input->key_edge_states[0])); memset(input->key_edge_states, 0, sizeof(input->key_edge_states)); + memset(input->button_edge_states, 0, sizeof(input->button_edge_states)); + memset(input->axis, 0, sizeof(input->axis)); + input->axis_min_input = 1024; input->mx = 0; input->my = 0; input->mdx = 0; diff --git a/src/input.h b/src/input.h @@ -19,6 +19,11 @@ #define FLAG_KEY_DOWN (1<<0) #define FLAG_KEY_UP (1<<1) +#define MAX_AXIS 2 +#define MAX_AXIS_VALUE 32767 +#define MIN_AXIS_VALUE -32767 +#define MAX_CONTROLLERS + struct input_edge { int is_down; u64 down_frame; @@ -31,6 +36,8 @@ struct input { SDL_Keymod modifiers; SDL_GameController *controller; int mx, my, last_mx, last_my; + int axis[MAX_AXIS]; + int axis_min_input; int mdx, mdy; float wheel_x, wheel_y; int mbuttons[MOUSE_BUTTONS]; diff --git a/src/update.c b/src/update.c @@ -39,8 +39,15 @@ static void movement(struct game *game, struct node *node, float speed_mult) float amt = 3.0 * game->dt; float turn = 1.0 * game->dt; + float x_axis = (float)game->input.axis[0] / (float)MAX_AXIS_VALUE; + float y_axis = (float)game->input.axis[1] / (float)MAX_AXIS_VALUE; + amt *= speed_mult; + // 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;