~jb55/polyadvent

c1de338c059137296fb9ce905e5cd886a35ec61b — William Casarin 1 year, 6 months ago c6062a3
input/controller: axis move support

still need camera

Signed-off-by: William Casarin <jb55@jb55.com>
3 files changed, 33 insertions(+), 1 deletions(-)

M src/input.c
M src/input.h
M src/update.c
M src/input.c => src/input.c +19 -1
@@ 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;

M src/input.h => src/input.h +7 -0
@@ 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];

M src/update.c => src/update.c +7 -0
@@ 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;