lua.cpp 675 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "Crown.h"
  2. #include "Game.h"
  3. namespace crown
  4. {
  5. StringSetting g_boot("boot_file", "lua main file", "lua/game.raw");
  6. lua_State* L;
  7. void init()
  8. {
  9. L = luaL_newstate();
  10. luaL_openlibs(L);
  11. lua_cpcall(L, luaopen_libcrownlua, NULL);
  12. const char* path = device()->filesystem()->os_path(g_boot.value());
  13. if (luaL_loadfile(L, path) || lua_pcall(L, 0, 0, 0))
  14. {
  15. os::printf("error: %s", lua_tostring(L, -1));
  16. }
  17. lua_getglobal(L, "init");
  18. lua_pcall(L, 0, 0, 0);
  19. }
  20. void shutdown()
  21. {
  22. lua_getglobal(L, "shutdown");
  23. lua_pcall(L, 0, 0, 0);
  24. lua_close(L);
  25. }
  26. void frame(float dt)
  27. {
  28. lua_getglobal(L, "frame");
  29. lua_pushnumber(L, dt);
  30. lua_pcall(L, 1, 0, 0);
  31. }
  32. }