Created
October 23, 2018 11:11
-
-
Save Pharap/6212d5b6888026e539a39bd7dbb6b6fa to your computer and use it in GitHub Desktop.
Pokitto + Lua example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Pokitto.h> | |
#include <cstdlib> | |
#define LUA_32BITS | |
#include <lua.hpp> | |
#include "PokittoLua.h" | |
const char * Program = | |
"Pokitto_Display_setColor(1);" | |
"Pokitto_Display_fillRect(10, 10, 40, 50);"; | |
int main () | |
{ | |
using Pokitto::Core; | |
Core::begin(); | |
lua_State * state = luaL_newstate(); | |
loadPokittoLua(state); | |
while (Core::isRunning()) | |
{ | |
if (Core::update()) | |
{ | |
auto status = luaL_loadstring(state, Program); | |
if (status != 0) | |
{ | |
Pokitto::Display::println("Error loading!"); | |
Pokitto::Display::println(lua_tostring(state, -1)); | |
continue; | |
} | |
auto result = lua_pcall(state, 0, 0, 0); | |
if (result != 0) | |
{ | |
Pokitto::Display::println("Error running!"); | |
Pokitto::Display::println(lua_tostring(state, -1)); | |
continue; | |
} | |
} | |
} | |
lua_close(state); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "PokittoLua.h" | |
#include <Pokitto.h> | |
#include <cstdint> | |
int Pokitto_Display_getWidth(lua_State * state) | |
{ | |
lua_pushinteger(state, Pokitto::Display::getWidth()); | |
return 1; | |
} | |
int Pokitto_Display_getHeight(lua_State * state) | |
{ | |
lua_pushinteger(state, Pokitto::Display::getHeight()); | |
return 1; | |
} | |
int Pokitto_Display_setColor(lua_State * state) | |
{ | |
std::uint8_t index = static_cast<std::uint8_t>(luaL_checkinteger(state, 1)); | |
Pokitto::Display::setColor(index); | |
return 0; | |
} | |
int Pokitto_Display_fillRect(lua_State * state) | |
{ | |
std::int16_t x = static_cast<std::int16_t>(luaL_checkinteger(state, 1)); | |
std::int16_t y = static_cast<std::int16_t>(luaL_checkinteger(state, 2)); | |
std::int16_t w = static_cast<std::int16_t>(luaL_checkinteger(state, 3)); | |
std::int16_t h = static_cast<std::int16_t>(luaL_checkinteger(state, 4)); | |
Pokitto::Display::fillRect(x, y, w, h); | |
return 0; | |
} | |
#define ENTRY(f) { #f, f } | |
const luaL_Reg PokittoLuaFunctions[] | |
{ | |
ENTRY(Pokitto_Display_getWidth), | |
ENTRY(Pokitto_Display_getHeight), | |
ENTRY(Pokitto_Display_setColor), | |
ENTRY(Pokitto_Display_fillRect), | |
}; | |
#undef ENTRY | |
void loadPokittoLua(lua_State * state) | |
{ | |
for (const auto & func : PokittoLuaFunctions) | |
{ | |
lua_pushcfunction(state, func.func); | |
lua_setglobal(state, func.name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#define LUA_32BITS | |
#include <lua.hpp> | |
void loadPokittoLua(lua_State * state); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment