Add joystickreleased()

This commit is contained in:
Rob Loach 2017-07-13 12:10:55 -04:00
parent ac9dd8d900
commit d39262a1f9
No known key found for this signature in database
GPG Key ID: 627C60834A74A21A
9 changed files with 55 additions and 10 deletions

View File

@ -77,11 +77,21 @@ namespace chaigame {
if (state == 1) {
Application::getInstance()->script->joystickpressed(i, u);
}
else if (state == 0) {
Application::getInstance()->script->joystickreleased(i, u);
}
}
}
}
}
Joystick& joystick::operator[](int i) {
if (i < 0 || i >= numJoysticks) {
i = 0;
}
return joysticks[i];
}
int joystick::getButtonKey(const std::string& name) {
if (name == "a") {
return RETRO_DEVICE_ID_JOYPAD_A;

View File

@ -21,6 +21,8 @@ namespace chaigame {
bool isDown(int index, const std::string& button);
int getButtonKey(const std::string& name);
int16_t joystick_cache[4][14];
Joystick& operator[](int i);
};
}

View File

@ -60,6 +60,13 @@ namespace chaigame {
chai.add(fun(&Config::window), "window");
chai.add(fun(&Config::modules), "modules");
// Add Joystick.
chai.add(user_type<Joystick>(), "Joystick");
chai.add(fun<bool, Joystick, const std::string&>(&Joystick::isDown), "isDown");
chai.add(fun<bool, Joystick, int>(&Joystick::isDown), "isDown");
chai.add(fun(&Joystick::getName), "getName");
chai.add(fun(&Joystick::isOpen), "isOpen");
// Register the Graphics module.
chai.add(fun(&graphics::rectangle), "rectangle");
chai.add(fun(&graphics::newImage), "newImage");
@ -143,6 +150,8 @@ namespace chaigame {
chai.add(fun(&joystick::getJoysticks), "getJoysticks");
chai.add(fun(&joystick::getJoystickCount), "getJoystickCount");
chai.add(fun<bool, joystick, int, const std::string&>(&joystick::isDown), "isDown");
chai.add(fun<bool, joystick, int, int>(&joystick::isDown), "isDown");
chai.add(fun(&joystick::operator[]), "[]");
chai.add_global(var(std::ref(app->joystick)), "joystick");
// Register the Math module.
@ -191,6 +200,12 @@ namespace chaigame {
catch (std::exception& e) {
printf("Skipping getting joystickpressed(): %s\n", e.what());
}
try {
chaijoystickreleased = chai.eval<std::function<void (int, int)> >("joystickreleased");
}
catch (std::exception& e) {
printf("Skipping getting joystickreleased(): %s\n", e.what());
}
#endif
}
@ -292,6 +307,19 @@ namespace chaigame {
#endif
}
void script::joystickreleased(int joystick, int button) {
#ifdef __HAVE_CHAISCRIPT__
try {
if (hasjoystickreleased) {
chaijoystickreleased(joystick, button);
}
}
catch (...) {
hasjoystickreleased = false;
}
#endif
}
std::string script::replaceString(std::string subject, const std::string& search, const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {

View File

@ -23,6 +23,7 @@ namespace chaigame {
void draw();
bool loadModule(const std::string& moduleName);
void joystickpressed(int joystick, int button);
void joystickreleased(int joystick, int button);
#ifdef __HAVE_CHAISCRIPT__
chaiscript::ChaiScript chai;
@ -31,9 +32,11 @@ namespace chaigame {
std::function<void (float)> chaiupdate;
std::function<void ()> chaidraw;
std::function<void (int, int)> chaijoystickpressed;
std::function<void (int, int)> chaijoystickreleased;
bool hasUpdate = true;
bool hasDraw = true;
bool hasjoystickpressed = true;
bool hasjoystickreleased = true;
#endif
std::string replaceString(std::string subject, const std::string& search, const std::string& replace);

View File

@ -52,8 +52,7 @@ namespace chaigame {
case AUDIO_U16MSB: format_str="U16MSB"; break;
case AUDIO_S16MSB: format_str="S16MSB"; break;
}
printf("\n opened=%d times\n frequency=%dHz\n format=%s\n channels=%d\n",
numtimesopened, frequency, format_str.c_str(), channels);
// printf("\n opened=%d times\n frequency=%dHz\n format=%s\n channels=%d\n", numtimesopened, frequency, format_str.c_str(), channels);
}

View File

@ -18,11 +18,13 @@ void Test::update(float delta) {
void Test::draw() {
Application* app = Application::getInstance();
app->graphics.setColor(200, 100, 100);
app->graphics.print("Test.cpp: Hello World!", 100, app->graphics.getHeight() - 100);
app->graphics.setColor(0, 255, 100);
app->graphics.print("Hello World! Press UP", 100, app->graphics.getHeight() - 100);
app->graphics.setColor(200, 200, 200);
app->graphics.ellipse("fill", 200, 200, 75, 20);
if (app->joystick[0].isDown("up")) {
app->graphics.setColor(100, 0, 255);
app->graphics.ellipse("fill", 200, 200, 75, 20);
}
}
#endif

View File

@ -34,7 +34,7 @@ def update(delta) {
}
def joystickpressed(joystick, button) {
if (button == 0) {
if (button == 0 || button == 1) {
currenttest = currenttest + 1
if (currenttest >= tests.size()) {
currenttest = 0

View File

@ -17,10 +17,10 @@ def update(delta) {
}
// Allow increasing the amount of sprites.
if (joystick.isDown(0, "up")) {
if (joystick[0].isDown("up")) {
sprites.push_back(Sprite())
}
if (joystick.isDown(0, "down") && sprites.size() > 1) {
if (joystick[0].isDown("down") && sprites.size() > 1) {
sprites.pop_back()
}
}
@ -34,7 +34,7 @@ def draw() {
// Write the text on the screen.
graphics.setColor(255, 255, 255)
graphics.print("Sprites: " + to_string(sprites.size()), 5, 5)
graphics.print("Press up/down add more", 5, 20)
graphics.print("Press up/down", 5, 20)
}
class Sprite {

View File

@ -19,6 +19,7 @@ def load() {
Player(false, 80.0f, graphics.getHeight() / 2.0f),
Player(true, graphics.getWidth() - 80.0f, graphics.getHeight() / 2.0f)
]
}
def draw() {