Adonthell  0.4
input.cc
Go to the documentation of this file.
1 /*
2  $Id: input.cc,v 1.7 2003/05/05 18:52:48 ksterker Exp $
3 
4  Copyright (C) 1999/2000/2001 Alexandre Courbot.
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file input.cc
18  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
19  *
20  * @brief Defines the input class.
21  *
22  *
23  */
24 
25 
26 #include <iostream>
27 #include <cstdio>
28 #include <cstdlib>
29 #include <string.h>
30 #include "input.h"
31 
32 u_int8 * input::keystate=NULL;
33 u_int8 * input::p_keystate=NULL;
34 u_int16 input::last_key;
35 s_int32 input::keystatelength;
36 
37 u_int16 input::mouse_posx, input::mouse_posy;
38 bool input::mouse_button[3];
39 
40 int input::filterevents(const SDL_Event *event)
41 {
42  if(event->type==SDL_KEYDOWN) p_keystate[event->key.keysym.sym]++;
43  return 1;
44 }
45 
47 {
48  // keyboard_mode=0;
49  keystate=SDL_GetKeyState(&keystatelength);
50  // set_keyboard_mode(MODE_STATE);
51  p_keystate=new u_int8[keystatelength];
52  memset(p_keystate, 0, keystatelength);
53  set_key_repeat(0,0);
54  SDL_SetEventFilter(filterevents);
55  SDL_EnableUNICODE(1);
56 }
57 
59 {
60  delete[] p_keystate;
61 }
62 
64 {
65  SDL_PumpEvents();
66 }
67 
68 bool input::is_pushed(SDLKey key)
69 {
70  bool ret;
71  ret=keystate[key];
72  if((ret)&&(p_keystate[key])) p_keystate[key]--;
73  return ret;
74 }
75 
76 bool input::has_been_pushed(SDLKey key)
77 {
78  bool ret;
79  ret=p_keystate[key];
80  if((ret)&&(!(--p_keystate[key]))) keystate[key]=0;
81  return ret;
82 }
83 
84 void input::set_key_repeat(int delay, int interval)
85 {
86  SDL_EnableKeyRepeat(delay, interval);
87 }
88 
90 {
91  static SDL_Event event;
92  static bool b;
93  b=false;
94  if(SDL_PeepEvents(&event,1,SDL_GETEVENT,SDL_KEYDOWNMASK)==1)
95  {
96  b=true;
97  if(p_keystate[event.key.keysym.sym]) p_keystate[event.key.keysym.sym]--;
98  keystate[event.key.keysym.sym]=0;
99  }
100  // FIXME: this should be placed elsewhere.
101  while(SDL_PeepEvents
102  (&event,1,SDL_GETEVENT,SDL_ALLEVENTS-SDL_KEYDOWNMASK)==1);
103  if (b) return(event.key.keysym.sym);
104  return(-1);
105 }
106 
108 {
109  static SDL_Event event;
110  static bool b;
111  b=false;
112  if(SDL_PeepEvents(&event,1,SDL_GETEVENT,SDL_KEYDOWNMASK)==1)
113  {
114  b=true;
115  if(event.key.keysym.unicode)
116  {
117  if(p_keystate[event.key.keysym.sym]) p_keystate[event.key.keysym.sym]--;
118  keystate[event.key.keysym.sym]=0;
119  }
120  }
121  // FIXME: this should be placed elsewhere.
122  while(SDL_PeepEvents
123  (&event,1,SDL_GETEVENT,SDL_ALLEVENTS-SDL_KEYDOWNMASK)==1);
124  if (b) return(event.key.keysym.unicode);
125  return(-1);
126 }
127 
129 {
130  while(get_next_key()!=-1);
131  memset(p_keystate, 0, keystatelength);
132 }
#define s_int32
32 bits long signed integer
Definition: types.h:44
Declares the input class.
#define u_int16
16 bits long unsigned integer
Definition: types.h:32
static void update()
Update the input state.
Definition: input.cc:63
Base class for events.
Definition: event.h:71
#define u_int8
8 bits long unsigned integer
Definition: types.h:29
static bool has_been_pushed(SDLKey key)
Returns whether a key has been pushed since last function call, false otherwise.
Definition: input.cc:76
static s_int32 get_next_key()
Returns the code of the next key on the input queue.
Definition: input.cc:89
static void init()
Initialise the input system.
Definition: input.cc:46
static void shutdown()
Free resources occupied by the input system.
Definition: input.cc:58
static bool is_pushed(SDLKey key)
Returns whether a key is currently pushed or not.
Definition: input.cc:68
static void clear_keys_queue()
Totally clears the key queue.
Definition: input.cc:128
static s_int32 get_next_unicode()
Returns the next unicode on the input queue.
Definition: input.cc:107
static void set_key_repeat(int delay=SDL_DEFAULT_REPEAT_DELAY, int interval=SDL_DEFAULT_REPEAT_INTERVAL)
Sets whether the key repeat is active or not.
Definition: input.cc:84