summaryrefslogtreecommitdiff
path: root/src/util/input.h
blob: 86d715c9e3dab3822c399e1afb7c2b1d2537236f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include <SDL_events.h>
#include <functional>
#include "vector.h"
#include "allocator.h"

const U32 MOUSEEV_MOVE = 0x0;
const U32 MOUSEEV_DOWN = 0x1;
const U32 MOUSEEV_UP   = 0x2;

const U32 MOUSE_LEFT   = 0x1;
const U32 MOUSE_RIGHT  = 0x2;
const U32 MOUSE_MIDDLE = 0x3;
const U32 MOUSE_WHEEL  = 0x4;

struct MOUSE_DATA {
  VEC2 pos;
  VEC2 pos_delta;
  U8   left;
  U8   right;
  U8   middle;
  U8   wheel;
};

struct INPUT_KEYBINDS {
  U8 fwd = 'w';
  U8 back = 's';
  U8 left = 'a';
  U8 right = 'd';

  U8 jump = ' ';
  U8 crouch = SDL_SCANCODE_LCTRL;
};

using ON_INPUT_FN = std::function<void( SDL_Event* )>;

struct INPUT_DATA {
  MOUSE_DATA mouse;
  U8         keys[0xff];
  bool       mouselock;
  F32        msens = .3f;
  F32        myaw = 1.f;
  F32        mpitch = 1.f;

  INPUT_KEYBINDS binds;
  LIST<ON_INPUT_FN> on_input;

};

extern INPUT_DATA input;

extern void input_frame_end();
extern void input_on_event( SDL_Event* e );
extern void input_on_mouse( I32 type, I32 x, I32 y );
extern U8   input_is_key_down( U32 key );
extern void input_capture_mouse( bool capture );
extern void input_reset_mouse_accumulator();

#define kb_down( key ) input_is_key_down( key )