Add chaigame.keyboard.isDown()

This commit is contained in:
Rob Loach 2017-06-26 14:39:54 -04:00
parent c8fdf2f763
commit f47ea82927
No known key found for this signature in database
GPG Key ID: 627C60834A74A21A
5 changed files with 82 additions and 2 deletions

View File

@ -52,6 +52,9 @@ bool Application::load() {
// Disable the mouse cursor from showing up.
SDL_ShowCursor(SDL_DISABLE);
// Initalize the chaigame subsystems.
chaigame::keyboard::load();
// ChaiScript.
#ifndef __DISABLE_CHAISCRIPT__
// Load main.chai.
@ -65,6 +68,12 @@ bool Application::load() {
// Add all the modules.
chai.add(chaiscript::fun(chaigame::graphics::rectangle), "rectangle");
//chai.add(chaiscript::fun<bool (std::string)>(chaigame::keyboard::isDown), "isdown");
//chai.add(chaiscript::fun(std::static_cast<bool (*)(std::string)>(&chaigame::keyboard::isDown)), "isDown");
// Initialize the game.
chaiload();
#endif
@ -100,6 +109,8 @@ bool Application::update() {
}
}
chaigame::keyboard::update();
// Retrieve the new game time.
Uint32 current = SDL_GetTicks();
@ -124,7 +135,11 @@ void Application::draw(){
// Test drawing a rectangle.
static int x = 10;
chaigame::graphics::rectangle(x++, 10, 100, 100, 0, 255, 255, 255);
chaigame::graphics::rectangle(x, 10, 100, 100, 0, 255, 255, 255);
if (chaigame::keyboard::isDown("down")) {
x++;
}
#ifndef __DISABLE_CHAISCRIPT__
chaidraw();

View File

@ -57,7 +57,10 @@ else
SDL_PREFIX := win
endif
OBJECTS := libretro.o Application.o chaigame/chaigame.o chaigame/graphics.o
OBJECTS := libretro.o Application.o \
chaigame/chaigame.o \
chaigame/graphics.o \
chaigame/keyboard.o
all: vendor/libretro-common/include/libretro.h $(TARGET)

View File

@ -2,5 +2,6 @@
#define CHAIGAME_H
#include "graphics.h"
#include "keyboard.h"
#endif

42
chaigame/keyboard.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "keyboard.h"
#include <string>
#include <SDL/SDL.h>
namespace chaigame {
namespace keyboard {
Uint8* keys;
bool isDown(int key) {
return (bool)keys[key];
}
bool isDown(std::string key) {
if (key == "down") {
return isDown(SDLK_DOWN);
}
if (key == "up") {
return isDown(SDLK_UP);
}
if (key == "left") {
return isDown(SDLK_LEFT);
}
if (key == "right") {
return isDown(SDLK_RIGHT);
}
}
void setKeyRepeat(int delay, int interval) {
SDL_EnableKeyRepeat(delay, interval);
}
bool load() {
SDL_EnableUNICODE(1);
}
void update() {
keys = SDL_GetKeyState(NULL);
}
}
}

19
chaigame/keyboard.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef _KEYBOARD_H_INCLUDED_
#define _KEYBOARD_H_INCLUDED_
#include <string>
#include <SDL/SDL.h>
namespace chaigame {
namespace keyboard {
bool load();
bool isDown(std::string key);
bool isDown(int key);
void setKeyRepeat(int delay = 400, int interval = 30);
void update();
}
}
#endif