Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    570
  • comments
    2427
  • views
    216937

Entry #60

Sign in to follow this  
Mushu

66 views

  • Random Observation of the Day!
    Inperfection is good. It defines everything in the world. Without it, everything would be exactly the same - perfect, just like me. And we all know there's already more than enough Mushu to go around. [wink]

Input Class (function-pointer kung-foo):
// header
#ifndef GUARD_INPUT
#define GUARD_INPUT

#include "sdl.h"
#include
#include
#include "global.h"

int EVNT_QUIT(int);

class input {

typedef int (*keyEvent)(int);

public:

input();
~input();

void pollEvents();

int fireFunction(int, int);
void setFunction(int, keyEvent);

private:

SDL_Event event;

keyEvent eventFunctions[SDLK_LAST];
bool keyStates[SDLK_LAST];

};

#endif

// implementation
#include "input.h"

input::input() {
for (int i = 0; i < SDLK_LAST; ++i) {
keyStates = false;
eventFunctions = NULL;
}
}

input::~input() {
for (int i = 0; i < SDLK_LAST; ++i) {
delete[] &keyStates;
delete[] &eventFunctions;
}
}

int EVNT_QUIT(int val) {
b_running = false;
return 0;
}

void input::setFunction(int i, keyEvent eventFunction) {
if (eventFunction == NULL || i >= SDLK_LAST)
return;

if (eventFunctions != NULL)
delete &eventFunctions;

eventFunctions = eventFunction;
}

int input::fireFunction(int i, int val = 0) {
if (eventFunctions == NULL)
return 0;

return (*eventFunctions)(val);
}

void input::pollEvents() {

while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_KEYDOWN) {
keyStates[event.key.keysym.sym] = true;
fireFunction(event.key.keysym.sym, 0);

} else if (event.type == SDL_KEYUP) {
keyStates[event.key.keysym.sym] = false;
}
}
}
Sign in to follow this  


1 Comment


Recommended Comments

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!