Adonthell 0.4
|
00001 /* 00002 $Id: input.cc,v 1.7 2003/05/05 18:52:48 ksterker Exp $ 00003 00004 Copyright (C) 1999/2000/2001 Alexandre Courbot. 00005 Part of the Adonthell Project http://adonthell.linuxgames.com 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License. 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY. 00011 00012 See the COPYING file for more details. 00013 */ 00014 00015 00016 /** 00017 * @file input.cc 00018 * @author Alexandre Courbot <alexandrecourbot@linuxgames.com> 00019 * 00020 * @brief Defines the input class. 00021 * 00022 * 00023 */ 00024 00025 00026 #include <iostream> 00027 #include <cstdio> 00028 #include <cstdlib> 00029 #include <string.h> 00030 #include "input.h" 00031 00032 u_int8 * input::keystate=NULL; 00033 u_int8 * input::p_keystate=NULL; 00034 u_int16 input::last_key; 00035 s_int32 input::keystatelength; 00036 00037 u_int16 input::mouse_posx, input::mouse_posy; 00038 bool input::mouse_button[3]; 00039 00040 int input::filterevents(const SDL_Event *event) 00041 { 00042 if(event->type==SDL_KEYDOWN) p_keystate[event->key.keysym.sym]++; 00043 return 1; 00044 } 00045 00046 void input::init() 00047 { 00048 // keyboard_mode=0; 00049 keystate=SDL_GetKeyState(&keystatelength); 00050 // set_keyboard_mode(MODE_STATE); 00051 p_keystate=new u_int8[keystatelength]; 00052 memset(p_keystate, 0, keystatelength); 00053 set_key_repeat(0,0); 00054 SDL_SetEventFilter(filterevents); 00055 SDL_EnableUNICODE(1); 00056 } 00057 00058 void input::shutdown() 00059 { 00060 delete[] p_keystate; 00061 } 00062 00063 void input::update() 00064 { 00065 SDL_PumpEvents(); 00066 } 00067 00068 bool input::is_pushed(SDLKey key) 00069 { 00070 bool ret; 00071 ret=keystate[key]; 00072 if((ret)&&(p_keystate[key])) p_keystate[key]--; 00073 return ret; 00074 } 00075 00076 bool input::has_been_pushed(SDLKey key) 00077 { 00078 bool ret; 00079 ret=p_keystate[key]; 00080 if((ret)&&(!(--p_keystate[key]))) keystate[key]=0; 00081 return ret; 00082 } 00083 00084 void input::set_key_repeat(int delay, int interval) 00085 { 00086 SDL_EnableKeyRepeat(delay, interval); 00087 } 00088 00089 s_int32 input::get_next_key() 00090 { 00091 static SDL_Event event; 00092 static bool b; 00093 b=false; 00094 if(SDL_PeepEvents(&event,1,SDL_GETEVENT,SDL_KEYDOWNMASK)==1) 00095 { 00096 b=true; 00097 if(p_keystate[event.key.keysym.sym]) p_keystate[event.key.keysym.sym]--; 00098 keystate[event.key.keysym.sym]=0; 00099 } 00100 // FIXME: this should be placed elsewhere. 00101 while(SDL_PeepEvents 00102 (&event,1,SDL_GETEVENT,SDL_ALLEVENTS-SDL_KEYDOWNMASK)==1); 00103 if (b) return(event.key.keysym.sym); 00104 return(-1); 00105 } 00106 00107 s_int32 input::get_next_unicode() 00108 { 00109 static SDL_Event event; 00110 static bool b; 00111 b=false; 00112 if(SDL_PeepEvents(&event,1,SDL_GETEVENT,SDL_KEYDOWNMASK)==1) 00113 { 00114 b=true; 00115 if(event.key.keysym.unicode) 00116 { 00117 if(p_keystate[event.key.keysym.sym]) p_keystate[event.key.keysym.sym]--; 00118 keystate[event.key.keysym.sym]=0; 00119 } 00120 } 00121 // FIXME: this should be placed elsewhere. 00122 while(SDL_PeepEvents 00123 (&event,1,SDL_GETEVENT,SDL_ALLEVENTS-SDL_KEYDOWNMASK)==1); 00124 if (b) return(event.key.keysym.unicode); 00125 return(-1); 00126 } 00127 00128 void input::clear_keys_queue() 00129 { 00130 while(get_next_key()!=-1); 00131 memset(p_keystate, 0, keystatelength); 00132 }