| Unuk 1.0

- Main Page
- Classes
- Files

- File List
- File Members

src/libUnuk/Input.cpp
Go to the documentation of this file.

    00001 #include <string.h>
    00002 #include "Input.h"
    00003
    00004 static mouse_t mouse;
    00005 static keyboard_t keyboard;
    00006
    00007 bool _curr_key(int index) {
    00008   return(keyboard.keys[index] != 0);
    00009 }
    00010
    00011 bool _old_key(int index) {
    00012   return(keyboard.oldKeys[index] != 0);
    00013 }
    00014
    00015 bool _curr_mouse(int button) {
    00016   return((mouse.buttons * SDL_BUTTON(button)) != 0);
    00017 }
    00018
    00019 bool _old_mouse(int button) {
    00020   return((mouse.oldButtons & SDL_BUTTON(button)) != 0);
    00021 }
    00022
    00023 bool CreateInput(void) {
    00024   memset(&keyboard, 0, sizeof(keyboard_t));
    00025   memset(&mouse, 0, sizeof(mouse_t));
    00026   SDL_PumpEvents();
    00027   SDL_PumpEvents();
    00028   unsigned char* tempKeys      = SDL_GetKeyState(&keyboard.keycount);
    00029   keyboard.keys                = (unsigned char*)malloc(sizeof(char) * keyboard.keycount);
    00030   keyboard.oldKeys             = (unsigned char*)malloc(sizeof(char) * keyboard.keycount);
    00031
    00032   memcpy(keyboard.keys, tempKeys, sizeof(char) * keyboard.keycount);
    00033   mouse.buttons = SDL_GetMouseState(&mouse.dx, &mouse.dy);
    00034   return true;
    00035 }
    00036
    00037 void UpdateInput(void) {
    00038   SDL_PumpEvents();
    00039   keyboard.lastChar     = -1;
    00040   mouse.oldx            = mouse.dx;
    00041   mouse.oldy            = mouse.dy;
    00042   mouse.oldButtons      = SDL_GetMouseState(&mouse.dx, &mouse.dy);
    00043
    00044   memcpy(keyboard.oldKeys, keyboard.keys, sizeof(char) * keyboard.keycount);
    00045
    00046   unsigned char *tmp = SDL_GetKeyState(&keyboard.keycount);
    00047   memcpy(keyboard.keys, tmp, sizeof(char) * keyboard.keycount);
    00048
    00049   keyboard.mods = SDL_GetModState();
    00050
    00051   SDL_Event event;
    00052   while(SDL_PollEvent(&event)) {
    00053     if(event.type == SDL_KEYDOWN) {
    00054       keyboard.lastChar = event.key.keysym.sym;
    00055     }
    00056   }
    00057 }
    00058
    00059 char GetKey(void) {
    00060   if(keyboard.lastChar != -1)
    00061     return keyboard.lastChar;
    00062   return 0;
    00063 }
    00064
    00065 unsigned int GetX(void)           { return mouse.dx; }
    00066 unsigned int GetY(void)           { return mouse.dy; }
    00067 unsigned int GetOldX(void)        { return mouse.oldx; }
    00068 unsigned int GetOldY(void)        { return mouse.oldy; }
    00069 unsigned int GetMods(void)        { return  keyboard.mods; }
    00070 bool KeyDown(int index)           { return(_curr_key(index)   && !_old_key(index)); }
    00071 bool KeyStillDown(int index)      { return(_curr_key(index)   && _old_key(index)); }
    00072 bool KeyUp(int index)             { return(!_curr_key(index) && _old_key(index)); }
    00073 bool KeyStillUp(int index)        { return(!_curr_key(index) && !_old_key(index)); }
    00074 bool MouseDown(int button)        { return(_curr_mouse(button) && !_old_mouse(button)); }
    00075 bool MouseStillDown(int button)   { return(_curr_mouse(button) && _old_mouse(button)); }
    00076 bool MouseUp(int button)          { return(!_curr_mouse(button) && _old_mouse(button)); }
    00077 bool MouseStillUp(int button)     { return(!_curr_mouse(button) && !_old_mouse(button)); }
    00078
    00079 void DestroyInput(void) {
    00080   free(keyboard.keys);
    00081   free(keyboard.oldKeys);
    00082 }

All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines