Skip to content

Instantly share code, notes, and snippets.

@Pharap
Created October 23, 2018 11:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pharap/6212d5b6888026e539a39bd7dbb6b6fa to your computer and use it in GitHub Desktop.
Save Pharap/6212d5b6888026e539a39bd7dbb6b6fa to your computer and use it in GitHub Desktop.
Pokitto + Lua example
#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);
}
#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);
}
}
#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