peripheral.h
Código:
#ifndef PERIPHERAL_H
#define PERIPHERAL_H
using namespace std;
#include <string>
#include <iostream>
#define INVALID_STATE -2
#define INVALID_COMPONENT -2
class Peripheral {
public:
Peripheral();
typedef enum {
NO_EVENT,
ON_PRESS,
ON_RELEASE,
ON_PRESSING,
ON_RELEASSING,
} event_t;
typedef int state_t;
typedef int component_t;
virtual state_t get_state(component_t comp);
virtual component_t get_change();
virtual string get_component_name(component_t comp);
virtual void reset();
};
#endif
keyboard.h
Código:
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <allegro.h>
#include "peripheral.h"
class Keyboard : public Peripheral {
public:
Keyboard();
state_t get_state(component_t comp);
component_t get_change();
string get_component_name(component_t comp);
void reset();
protected:
int old_state[KEY_MAX];
};
#endif
y keyboard.cpp
Código:
using namespace std;
#include <allegro.h>
#include <string>
#include "keyboard.h"
#include "peripheral.h"
Keyboard::Keyboard() {
for (int i = 0; i < KEY_MAX; i++) old_state[i] = key[i];
}
Peripheral::state_t Keyboard::get_state(state_t comp) {
if (keyboard_needs_poll())
poll_keyboard();
if ((comp >= 0) && (comp < KEY_MAX) && (key[comp])) return TRUE;
else
return INVALID_STATE;
}
Peripheral::component_t Keyboard::get_change() {
for (int i = 0; i < KEY_MAX; i++) {
if (key[i] != old_state[i]) {
old_state[i] = key[i];
return i;
}
}
return INVALID_COMPONENT;
}
void Keyboard::reset() {
for (int i = 0; i < KEY_MAX; i++) old_state[i] = key[i];
}
string Keyboard::get_component_name(component_t comp) {
string ret;
switch (comp) {
case KEY_A:
ret = "A";
break;
case KEY_B:
ret = "B";
break;
case KEY_C:
ret = "C";
break;
case KEY_D:
ret = "D";
break;
case KEY_E:
ret = "E";
break;
case KEY_F:
ret = "F";
break;
case KEY_G:
ret = "G";
break;
case KEY_H:
ret = "H";
break;
case KEY_I:
ret = "I";
break;
case KEY_J:
ret = "J";
break;
case KEY_K:
ret = "K";
break;
case KEY_L:
ret = "L";
break;
case KEY_M:
ret = "M";
break;
case KEY_N:
ret = "N";
break;
case KEY_O:
ret = "O";
break;
case KEY_P:
ret = "P";
break;
case KEY_Q:
ret = "Q";
break;
case KEY_R:
ret = "R";
break;
case KEY_S:
ret = "S";
break;
case KEY_T:
ret = "T";
break;
case KEY_U:
ret = "U";
break;
case KEY_V:
ret = "V";
break;
case KEY_X:
ret = "X";
break;
case KEY_Y:
ret = "Y";
break;
case KEY_Z:
ret = "Z";
break;
}
return ret;
}
Espero que me puedan hechar la mano, gracias