polyadvent

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

input.h (1737B)


      1 
      2 
      3 #ifndef POLYADVENT_INPUT_H
      4 #define POLYADVENT_INPUT_H
      5 
      6 #include <SDL2/SDL.h>
      7 #include "common.h"
      8 
      9 /* enum key_state { */
     10 /*   KEY_NONE, */
     11 /*   KEY_DOWN, */
     12 /*   KEY_UP */
     13 /* }; */
     14 
     15 #define MOUSE_BUTTONS 5
     16 
     17 /* #define KEY_BUFFER_SIZE 32 */
     18 
     19 #define FLAG_KEY_DOWN (1<<0)
     20 #define FLAG_KEY_UP   (1<<1)
     21 
     22 #define MAX_AXIS 2
     23 #define MAX_AXIS_VALUE 32767
     24 #define MIN_AXIS_VALUE -32767
     25 #define MAX_CONTROLLERS
     26 
     27 struct input_edge {
     28     int is_down;
     29     u64 down_frame;
     30     u64 up_frame;
     31 };
     32 
     33 struct input {
     34   /* enum key_state keys[0x7F-0x1F]; */
     35     u8 const *keystates;
     36     SDL_Keymod modifiers;
     37     SDL_GameController *controller;
     38     int mx, my, last_mx, last_my;
     39     int axis[MAX_AXIS];
     40     int axis_min_input;
     41     int mdx, mdy;
     42     float wheel_x, wheel_y;
     43     int mbuttons[MOUSE_BUTTONS];
     44     int n_frame_down_keys;
     45     int n_frame_up_keys;
     46     int resized_width;
     47     int resized_height;
     48     /* u64 down_key_frames[KEY_BUFFER_SIZE]; */
     49     /* u64 up_key_frames[KEY_BUFFER_SIZE]; */
     50     /* u8 frame_down_keys[KEY_BUFFER_SIZE]; */
     51     /* u8 frame_up_keys[KEY_BUFFER_SIZE]; */
     52     struct input_edge key_edge_states[SDL_NUM_SCANCODES];
     53     struct input_edge button_edge_states[SDL_CONTROLLER_BUTTON_MAX];
     54 };
     55 
     56 int input_is_dragging(struct input *input, int mouse_button);
     57 
     58 bool is_button_down(struct input *input, SDL_GameControllerButton button);
     59 bool is_key_down_on_frame(struct input *input, u8 scancode, u64 frame);
     60 bool is_button_down_on_frame(struct input *input, SDL_GameControllerButton button, u64 frame);
     61 
     62 void init_input(struct input *input);
     63 void input_reset(struct input *input);
     64 void handle_key(struct input *input, SDL_KeyboardEvent);
     65 void process_events(struct input *input, u64 current_frame);
     66 
     67 #endif /* POLYADVENT_INPUT_H */