Adonthell  0.4
input.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999/2000/2001 Alexandre Courbot.
3  Copyright (C) 2016 Kai Sterker
4  Part of the Adonthell Project <http://adonthell.nongnu.org>
5 
6  Adonthell is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  Adonthell is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 /**
22  * @file input.cc
23  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
24  * @author Kai Sterker
25  *
26  * @brief Defines the input class.
27  *
28  *
29  */
30 
31 
32 #include <iostream>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <string.h>
36 #include <SDL.h>
37 #include "input.h"
38 
39 u_int8 * input::keystate=NULL;
40 u_int8 * input::p_keystate=NULL;
41 bool input::text_input = false;
42 s_int32 input::keystatelength;
43 
44 u_int16 input::mouse_posx, input::mouse_posy;
45 bool input::mouse_button[3];
46 std::queue<std::string> input::input_queue;
47 
49 {
50  const u_int8 *state = SDL_GetKeyboardState(&keystatelength);
51  keystate=new u_int8[keystatelength];
52  memcpy(keystate, state, keystatelength);
53 
54  // set_keyboard_mode(MODE_STATE);
55  p_keystate=new u_int8[keystatelength];
56  memset(p_keystate, 0, keystatelength);
57 }
58 
60 {
61  delete[] p_keystate;
62 }
63 
65 {
66  static SDL_Event event;
67 
68  while (SDL_PollEvent(&event))
69  {
70  switch (event.type)
71  {
72  case SDL_KEYDOWN:
73  {
74  SDL_Scancode idx = event.key.keysym.scancode;
75  if (!event.key.repeat)
76  {
77  keystate[idx]=1;
78  p_keystate[idx]++;
79 
80  if (text_input)
81  {
82  if (idx == SDL_SCANCODE_BACKSPACE ||
83  idx == SDL_SCANCODE_DELETE ||
84  idx == SDL_SCANCODE_RETURN)
85  {
86  input_queue.push(std::string(1, (char)event.key.keysym.sym));
87  }
88  }
89  }
90  break;
91  }
92  case SDL_KEYUP:
93  {
94  SDL_Scancode idx = event.key.keysym.scancode;
95  keystate[idx]=0;
96  break;
97  }
98  case SDL_TEXTINPUT:
99  {
100  if (text_input)
101  {
102  input_queue.push(event.text.text);
103  }
104  break;
105  }
106  }
107  }
108 }
109 
110 bool input::is_pushed(SDL_Keycode key)
111 {
112  bool ret;
113  SDL_Scancode idx = SDL_GetScancodeFromKey(key);
114  ret=keystate[idx];
115  if((ret)&&(p_keystate[idx])) p_keystate[idx]--;
116  return ret;
117 }
118 
119 bool input::has_been_pushed(SDL_Keycode key)
120 {
121  bool ret;
122  SDL_Scancode idx = SDL_GetScancodeFromKey(key);
123  ret=p_keystate[idx];
124  if((ret)&&(!(--p_keystate[idx]))) keystate[idx]=0;
125  return ret;
126 }
127 
128 SDL_Keycode input::get_next_key()
129 {
130  static SDL_Event event;
131  static bool b;
132  b=false;
133  if(SDL_PeepEvents(&event,1,SDL_GETEVENT,SDL_KEYDOWN,SDL_KEYDOWN)==1)
134  {
135  b=true;
136  if(p_keystate[event.key.keysym.scancode]) p_keystate[event.key.keysym.scancode]--;
137  keystate[event.key.keysym.scancode]=0;
138  }
139  if (b) return(event.key.keysym.sym);
140  return(-1);
141 }
142 
144 {
145  if (!input_queue.empty())
146  {
147  std::string utf8_result(input_queue.front());
148  input_queue.pop();
149  return utf8_result;
150  }
151  return "";
152 }
153 
154 void input::start_text_input()
155 {
156  while(!input_queue.empty()) input_queue.pop();
157  SDL_StartTextInput();
158  text_input = true;
159 }
160 
161 void input::stop_text_input()
162 {
164  text_input = false;
165  SDL_StopTextInput();
166 }
167 
169 {
170  memset(p_keystate, 0, keystatelength);
171 }
static bool has_been_pushed(SDL_Keycode key)
Returns whether a key has been pushed since last function call, false otherwise.
Definition: input.cc:119
#define s_int32
32 bits long signed integer
Definition: types.h:50
Declares the input class.
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
static void update()
Update the input state.
Definition: input.cc:64
static SDL_Keycode get_next_key()
Returns the code of the next key on the input queue.
Definition: input.cc:128
Base class for events.
Definition: event.h:75
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
static void init()
Initialise the input system.
Definition: input.cc:48
static void shutdown()
Free resources occupied by the input system.
Definition: input.cc:59
static bool is_pushed(SDL_Keycode key)
Returns whether a key is currently pushed or not.
Definition: input.cc:110
static void clear_keys_queue()
Totally clears the key queue.
Definition: input.cc:168
static std::string get_next_unicode()
Returns the next text input on the input queue encoded as utf8.
Definition: input.cc:143