Skip to content

Instantly share code, notes, and snippets.

@voidproc
Created October 4, 2016 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voidproc/ba05e32d6d426bcede2853ceb736b76c to your computer and use it in GitHub Desktop.
Save voidproc/ba05e32d6d426bcede2853ceb736b76c to your computer and use it in GitHub Desktop.
#include <Siv3D.hpp>
#include <memory>
// Lua 5.2.x (need 'lua52.dll' at runtime)
#include <lua.hpp>
#pragma comment(lib, "lua52.lib")
// Sol 2.14
#include <sol.hpp>
struct Colorful
{
int frame = 0;
Color color = Palette::White;
sol::thread thread;
sol::coroutine coro;
Colorful(sol::state& lua)
{
thread = sol::thread::create(lua.lua_state());
coro = thread.state()["colorful_update"];
}
void update()
{
if (coro.runnable())
{
coro(this);
PutText(L"coroutine running");
}
else
{
PutText(L"coroutine finished");
}
RectF(200, 200).setCenter(Window::Center()).draw(color);
++frame;
}
};
void Main()
{
// Init Sol
sol::state lua;
lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::math, sol::lib::coroutine);
// Register classes
lua.new_usertype<Color>("Color",
"set", &Color::set,
"r", sol::property([](Color& c) { return c.r; }, [](Color& c, int r) { c.r = r; }),
"g", sol::property([](Color& c) { return c.g; }, [](Color& c, int g) { c.g = g; }),
"b", sol::property([](Color& c) { return c.b; }, [](Color& c, int b) { c.b = b; }),
"a", sol::property([](Color& c) { return c.a; }, [](Color& c, int a) { c.a = a; })
);
lua.new_usertype<Colorful>("Colorful",
"frame", &Colorful::frame,
"color", &Colorful::color
);
// Define 'Colorful' coroutine
lua.script(
"function colorful_update(c)\n"
" -- Blink white\n"
" for i=0, 120 do\n"
" local v = 255 * i / 120\n"
" c.color:set(v, v, v, 128 + 127 * (math.floor(i / 2) % 2))\n"
" coroutine.yield()\n"
" end\n"
""
" -- White to orange\n"
" for i=0, 100 do\n"
" c.color:set(255, 255 - 127 * i / 100, 255 - 255 * i / 100, 255)\n"
" coroutine.yield()\n"
" end\n"
""
" -- Orange to green\n"
" for i=0, 60 do\n"
" c.color:set(255 - 255 * i / 60, 128 + 112 * i / 60, 0, 255)\n"
" coroutine.yield()\n"
" end\n"
""
" return 0\n"
"end"
);
Colorful colorful(lua);
while (System::Update())
{
colorful.update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment