Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F127056
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/keyboard.cpp b/util/keyboard.cpp
index ddb2bfa4..97faf4a6 100644
--- a/util/keyboard.cpp
+++ b/util/keyboard.cpp
@@ -1,75 +1,79 @@
#include "keyboard.h"
#include "allegro.h"
#include <iostream>
#include <vector>
#include <map>
using namespace std;
#ifndef debug
#define debug cout<<"File: "<<__FILE__<<" Line: "<<__LINE__<<endl;
#endif
const int Keyboard::Key_A = KEY_A;
const int Keyboard::Key_S = KEY_S;
const int Keyboard::Key_D = KEY_D;
const int Keyboard::Key_LEFT = KEY_LEFT;
const int Keyboard::Key_RIGHT = KEY_RIGHT;
const int Keyboard::Key_UP = KEY_UP;
const int Keyboard::Key_DOWN = KEY_DOWN;
const int Keyboard::Key_ENTER = KEY_ENTER;
const int Keyboard::Key_SPACE = KEY_SPACE;
const int Keyboard::Key_F12 = KEY_F12;
const int Keyboard::Key_ESC = KEY_ESC;
const int Keyboard::Key_MINUS_PAD = KEY_MINUS_PAD;
const int Keyboard::Key_PLUS_PAD = KEY_PLUS_PAD;
Keyboard::Keyboard(){
}
/* KEY_MAX is defined in allegro at
* allegro/include/allegro/keyboard.h
*/
void Keyboard::poll(){
for ( int q = 0; q < KEY_MAX; q++ ){
// my_keys[ q ] = key[ q ];
if ( key[ q ] ){
+ /* my_keys[ q ] becomes negative so that the key
+ * can be pressed the first time without having
+ * to wait for a delay
+ */
if ( my_keys[ q ] <= 0 ){
my_keys[ q ] -= 1;
} else {
my_keys[ q ] += 1;
}
// printf( "Key %d = %d\n", q, my_keys[ q ] );
} else {
my_keys[ q ] = 0;
}
}
}
void Keyboard::readKeys( vector< int > & all_keys ){
for ( map<int,int>::const_iterator it = my_keys.begin(); it != my_keys.end(); it++ ){
const int & key = it->first;
const int & delay = it->second;
if ( delay < 0 || delay > key_delay[ key ] ){
all_keys.push_back( key );
}
}
}
void Keyboard::setDelay( const int key, const int delay ){
key_delay[ key ] = delay;
}
const bool Keyboard::keypressed(){
for ( map<int,int>::const_iterator it = my_keys.begin(); it != my_keys.end(); it++ ){
const int & n = (*it).second;
if ( n < 0 || n > key_delay[ it->first ] ){
return true;
}
}
return false;
}
diff --git a/util/keyboard.h b/util/keyboard.h
index 6dab45c8..af8594a0 100644
--- a/util/keyboard.h
+++ b/util/keyboard.h
@@ -1,75 +1,76 @@
#ifndef _keyboard_h
#define _keyboard_h
#include <map>
#include <vector>
using namespace std;
/* handles allegro key[] array better than keypressed()
* and readkey()
*/
class Keyboard{
public:
Keyboard();
/* poll:
* Put the keys in Allegro's key[] array into our map of int -> bool
*/
void poll();
/* []:
* Extract a boolean value given a key number
*/
inline const bool operator[] ( const int i ){
+ /* if the key has been pressed for the first time return true */
if ( my_keys[ i ] < 0 ){
my_keys[ i ] = 1;
return true;
}
bool b = my_keys[ i ] > key_delay[ i ];
if ( b ){
my_keys[ i ] = 1;
}
return b;
}
/* keypressed:
* Returns true if a key is pressed
*/
const bool keypressed();
/* readKeys:
* Store all pressed keys in a user supplied vector
*/
void readKeys( vector< int > & all_keys );
void setDelay( const int key, const int delay );
static const int Key_A;
static const int Key_S;
static const int Key_D;
static const int Key_LEFT;
static const int Key_RIGHT;
static const int Key_UP;
static const int Key_DOWN;
static const int Key_SPACE;
static const int Key_ENTER;
static const int Key_F12;
static const int Key_ESC;
static const int Key_MINUS_PAD;
static const int Key_PLUS_PAD;
protected:
map<int,int> my_keys;
map<int,int> key_delay;
};
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 2:13 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69412
Default Alt Text
(3 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline