Skip to content

Instantly share code, notes, and snippets.

@dejaime
Last active January 3, 2016 23:09
Show Gist options
  • Save dejaime/8533081 to your computer and use it in GitHub Desktop.
Save dejaime/8533081 to your computer and use it in GitHub Desktop.
Code Snippet to exemplify the usage of HLGE's first Input interpretation System prototype. It shows the entire process of creating and configuring Patterns using Basic Inputs and the Key Translator.
#include <iostream>
#include <string>
#include "Controller.h"
#include "KeyboardController.h"
#include "GestureInterpreter.h"
#include "DisplayManager.h"
#include "allegro5.h"
#define MAX_DELAY 0.5
#define W 1
#define A 2
#define S 3
#define D 4
#define Space 8
#define Escape 9
bool willExit;
void callback_up_w (){
willExit = true;
std::cout << "W up hit" << std::endl;
}
void callback_asd (){
std::cout << "ASD" << std::endl;
}
int main () {
std::cout<<"\nDo Something\n";
if(!al_init()) return -1;
//Create and configure the KeyTranslator.
KeyTranslator *translator = new KeyTranslator();
translator->registerTranslation(ALLEGRO_KEY_W, W);
translator->registerTranslation(ALLEGRO_KEY_A, A);
translator->registerTranslation(ALLEGRO_KEY_S, S);
translator->registerTranslation(ALLEGRO_KEY_D, D);
translator->registerTranslation(ALLEGRO_KEY_ESCAPE, Escape);
//Create and configure a controller for the Keyboard.
KeyboardController* controller;
controller = new KeyboardController (Controller::Keyboard);
controller->activate();
//Create a GestureInterpreter
GestureInterpreter interpreter;
//Configure the interpreter
interpreter.setController(controller);
interpreter.setKeyTranslator(translator);
//Add some patterns
Pattern* tempPattern;
//W key Up, 'cuz I can
tempPattern = new Pattern();
tempPattern->pushInput( new BasicInput (InputEvent::KeyUp, W, 0, MAX_DELAY) );
interpreter.registerPattern(tempPattern, callback_up_w);
//Exit with ESC, using the same callback as the other one above.
tempPattern = new Pattern();
tempPattern->pushInput( new BasicInput (InputEvent::KeyUp, Escape, 0, MAX_DELAY) );
interpreter.registerPattern(tempPattern, callback_up_w);
//ASD, possibility one
tempPattern = new Pattern();
tempPattern->pushInput( new BasicInput (InputEvent::KeyDown, A, 0, MAX_DELAY) );
tempPattern->pushInput( new BasicInput (InputEvent::KeyDown, S, 0, MAX_DELAY) );
tempPattern->pushInput( new BasicInput (InputEvent::KeyDown, D, 0, MAX_DELAY) );
interpreter.registerPattern(tempPattern, callback_asd);
//ASD, possibility two
tempPattern = new Pattern();
tempPattern->pushInput( new BasicInput (InputEvent::KeyDown, A, 0, MAX_DELAY) );
tempPattern->pushInput( new BasicInput (InputEvent::KeyDown, S, 0, MAX_DELAY) );
tempPattern->pushInput( new BasicInput (InputEvent::KeyUp, A, 0, MAX_DELAY) );
tempPattern->pushInput( new BasicInput (InputEvent::KeyDown, D, 0, MAX_DELAY) );
interpreter.registerPattern(tempPattern, callback_asd);
//Create a test display.
DisplayManager* displayManager;
displayManager = new DisplayManager(800, 600);
do {
interpreter.update();
displayManager->flip();
al_rest(0.1);
} while (!willExit);
std::cout<<"\nDid Something\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment