Subversion Repositories DIN Is Noise

Rev

Rev 2300 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1528 jag 1
/*
2
* input.h
2302 jag 3
* DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath
1713 jag 4
* DIN Is Noise is released under GNU Public License 2.0
1528 jag 5
* For more information, please visit https://dinisnoise.org/
6
*/
7
 
8
 
9
#ifndef __input
10
#define __input
11
 
12
#include "chrono.h"
13
#include "log.h"
1916 jag 14
 
1528 jag 15
#include <SDL/SDL.h>
16
 
17
struct keyboard {
18
 
19
    int nkeys; // number of keys
20
    Uint8* now; // key state now (1 - down, 0 - up)
21
    Uint8* last; // last state of keys
22
 
23
    unsigned int size;
24
 
25
    double* tstamp;
26
    double* repeat;
27
 
28
    keyboard () {
29
 
30
      SDL_GetKeyState (&nkeys);
31
 
32
      size = nkeys * sizeof (Uint8);
33
 
34
      last = new Uint8 [nkeys];
35
      memset (last, 0, size);
36
 
37
      tstamp = new double [nkeys];
38
      repeat = new double [nkeys];
39
 
40
      dlog << "+++ initialised computer keyboard input +++" << endl;
41
 
42
    }
43
 
1729 jag 44
    void read () {
1528 jag 45
      now = SDL_GetKeyState (0);
46
    }
47
 
48
    void save () {
49
      memcpy (last, now, size);
50
    }
51
 
52
    ~keyboard () {
53
      delete[] last;
54
      delete[] tstamp;
55
      delete[] repeat;
56
      dlog << "--- destroyed computer keyboard input ---" << endl;
57
    }
58
 
59
};
60
 
61
extern keyboard keybd;
62
 
1592 jag 63
 
1528 jag 64
inline int nkeys () {
65
  return keybd.nkeys;
66
}
67
 
68
inline int keydown (unsigned int k) {
69
  return keybd.now[k];
70
}
71
 
72
inline int keyup (unsigned int k) {
73
  int wkd = keybd.last[k];
74
  int kd = keybd.now[k];
75
  return (!kd && wkd);
76
}
77
 
78
inline int waskeydown (unsigned int k) {
79
  return keybd.last[k];
80
}
81
 
82
inline int keypressed (unsigned int k) {
83
  int wkd = waskeydown (k), kd = keydown (k);
84
  if (kd && !wkd) return 1; else return 0;
85
}
86
 
1916 jag 87
inline int keypressedd (unsigned int k, double firstt = 1./3, double constt = 1./20) {
1528 jag 88
  /* keypressed with key repeat */
89
  int wkd = waskeydown(k), kd = keydown(k);
90
  if (kd) {
91
    if (!wkd) {
92
      keybd.repeat[k] = firstt + ui_clk();
93
      return 1;
94
    } else {
95
      if (ui_clk() > keybd.repeat[k]) {
96
        keybd.repeat[k] = constt + ui_clk();
97
        return 1;
98
      }
99
    }
1916 jag 100
  }
1528 jag 101
  return 0;
102
}
103
 
104
inline int shift_down () {
105
  return (keydown (SDLK_LSHIFT) || keydown (SDLK_RSHIFT));
106
}
107
 
108
inline int ctrl_down () {
109
  return (keydown (SDLK_LCTRL) || keydown (SDLK_RCTRL));
110
}
111
 
112
inline int alt_down () {
113
  return (keydown (SDLK_LALT) || keydown (SDLK_RALT));
114
}
115
 
116
extern int lmb, rmb, mmb;
117
extern int mousex, mousey, mouseyy;
118
extern int wheel;
119
extern int SHIFT, CTRL, ALT;
120
#endif