25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

237 lines
6.7 KiB

  1. //
  2. // Lol Engine — Lua tutorial
  3. //
  4. // Copyright © 2014—2017 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  5. // © 2017—2018 Sam Hocevar <sam@hocevar.net>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #if HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include <lol/engine.h>
  17. #include <lol/lua.h>
  18. #include "loldebug.h"
  19. #include <cstdio>
  20. using namespace lol;
  21. //-----------------------------------------------------------------------------
  22. class DemoObject : public LuaObject
  23. {
  24. public:
  25. DemoObject() : LuaObject() {}
  26. virtual ~DemoObject() {}
  27. static DemoObject* New(lua_State* l, int arg_nb)
  28. {
  29. UNUSED(l);
  30. UNUSED(arg_nb);
  31. return new DemoObject();
  32. }
  33. //-------------------------------------------------------------------------
  34. static int AddFive(lua_State* l)
  35. {
  36. auto stack = LuaStack::Begin(l);
  37. int32_t i = stack.Get<int32_t>();
  38. i += 5;
  39. return (stack << i).End();
  40. }
  41. LOLUA_DECLARE_RETURN_METHOD_ARGS(AddTenInstance, GetPtr<DemoObject>(), AddTenMethod, Get<float>(), Get<int32_t>(), Get<int32_t>());
  42. static int _AddTenInstance(lua_State* l)
  43. {
  44. auto stack = LuaStack::Begin(l);
  45. DemoObject* obj = stack.GetPtr<DemoObject>();
  46. float f = stack.Get<float>();
  47. int32_t i = stack.Get<int32_t>();
  48. int32_t i2 = stack.Get<int32_t>();
  49. f = obj->AddTenMethod(f, i, i2);
  50. return (stack << f).End();
  51. }
  52. float AddTenMethod(float f, int32_t i, int32_t i2)
  53. {
  54. UNUSED(i, i2);
  55. return (f + 10);
  56. }
  57. static int GetX(lua_State* l)
  58. {
  59. auto stack = LuaStack::Begin(l);
  60. DemoObject* obj = stack.GetPtr<DemoObject>();
  61. auto i = stack.Get<int32_t>();
  62. i = obj->m_x;
  63. return (stack << i).End();
  64. }
  65. LOLUA_DECLARE_VOID_METHOD_ARGS(SetX, GetPtr<DemoObject>(), SetXMethod, Get<int32_t>());
  66. static int _SetX(lua_State* l)
  67. {
  68. auto stack = LuaStack::Begin(l);
  69. DemoObject* obj = stack.GetPtr<DemoObject>();
  70. auto i = stack.Get<int32_t>();
  71. obj->m_x = i;
  72. return stack.End();
  73. }
  74. void SetXMethod(int32_t i)
  75. {
  76. m_x = i;
  77. }
  78. //-------------------------------------------------------------------------
  79. static const LuaObjectLibrary* GetLib()
  80. {
  81. static const LuaObjectLibrary lib = LuaObjectLibrary(
  82. "LoluaDemo",
  83. { { "AddFive", &DemoObject::AddFive } },
  84. { { "AddTenInstance", &DemoObject::AddTenInstance } },
  85. { { "X", &DemoObject::GetX, &DemoObject::SetX } });
  86. return &lib;
  87. }
  88. int m_x = 0;
  89. };
  90. //-----------------------------------------------------------------------------
  91. static int GlobalAddString(lua_State* l)
  92. {
  93. auto stack = LuaStack::Begin(l);
  94. auto s = stack.Get<std::string>();
  95. s += "_added";
  96. return (stack << s).End();
  97. }
  98. //-----------------------------------------------------------------------------
  99. class LoluaDemoLoader : public LuaLoader
  100. {
  101. public:
  102. LoluaDemoLoader() : LuaLoader()
  103. {
  104. lua_State* l = GetLuaState();
  105. //Registering demo object
  106. LuaObjectHelper::Register<DemoObject>(l);
  107. //Registering function
  108. LuaFunction add_string(l, "GlobalAddString", &GlobalAddString);
  109. }
  110. virtual ~LoluaDemoLoader()
  111. {
  112. }
  113. void TestStuff()
  114. {
  115. /*
  116. lua_State* l = GetLuaState();
  117. //create property
  118. lua_pushnumber(l, 5.0);
  119. lua_setfield(l, -2, "x");
  120. lua_getglobal(l, "vector");
  121. int table = lua_istable(l, -1);
  122. lua_getfield(l, -1, "x");
  123. Lolua::Var<float> var(l, -1);
  124. lua_pop(l, 2);
  125. vec3 t;
  126. */
  127. //table = lua_isuserdata(l, -1);
  128. //Var<T> var(m_lua_state, -1);
  129. //lua_pop(m_lua_state, 1);
  130. //lua_getfield (lua_State *L, int index, const char *k);
  131. //return var.V();
  132. }
  133. };
  134. //-----------------------------------------------------------------------------
  135. class LoluaDemo : public WorldEntity
  136. {
  137. public:
  138. LoluaDemo()
  139. {
  140. }
  141. virtual void tick_game(float seconds)
  142. {
  143. WorldEntity::tick_game(seconds);
  144. LoluaDemoLoader* demo_loader = new LoluaDemoLoader();
  145. //Execute script
  146. demo_loader->ExecLuaFile("14_lua.lua");
  147. demo_loader->TestStuff();
  148. //Grab global test values
  149. float testvalue_num = demo_loader->Get<float>("testvalue_num");
  150. int32_t testvalue_int = demo_loader->Get<int32_t>("testvalue_int");
  151. uint32_t testvalue_uint = demo_loader->Get<uint32_t>("testvalue_uint");
  152. std::string testvalue_str = demo_loader->Get<std::string>("testvalue_str");
  153. //Grab string modified with function
  154. std::string function_return = demo_loader->Get<std::string>("function_return");
  155. //Grab global values modified with DemoObject
  156. int32_t loluademo_return = demo_loader->Get<int32_t>("loluademo_return");
  157. int32_t loluademo_getx = demo_loader->Get<int32_t>("loluademo_getx");
  158. float loluademo_inst_return = demo_loader->Get<float>("loluademo_inst_return");
  159. DemoObject* loluademo_inst = demo_loader->GetPtr<DemoObject>("loluademo_inst");
  160. msg::info("Lua Vars: \
  161. testvalue_num: %.2f, testvalue_int: %i, testvalue_uint: %i, testvalue_str: %s.\n",
  162. testvalue_num, testvalue_int, testvalue_uint, testvalue_str.c_str());
  163. msg::info("Lua Vars: \
  164. function_return: %s, loluademo_return: %i, loluademo_inst_return: %.2f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n",
  165. function_return.c_str(), loluademo_return, loluademo_inst_return, loluademo_getx, loluademo_inst->m_x);
  166. #define /***/ _LOLUA_ARG_1(a00) (float)a00
  167. #define /***/ _LOLUA_ARG_2(a00, a01) _LOLUA_ARG_1(a00), _LOLUA_ARG_1(a01)
  168. #define /***/ _LOLUA_ARG_3(a00, a01, a02) _LOLUA_ARG_1(a00), _LOLUA_ARG_2(a01, a02)
  169. #define /***/ _LOLUA_ARG_4(a00, a01, a02, a03) _LOLUA_ARG_1(a00), _LOLUA_ARG_3(a01, a02, a03)
  170. msg::info("_LOLUA_ARG_1: %f, %f, %f, %f\n", _LOLUA_ARG_4(0, 1, 2, 3));
  171. delete demo_loader;
  172. Ticker::Shutdown();
  173. }
  174. virtual void tick_draw(float seconds, Scene &scene)
  175. {
  176. WorldEntity::tick_draw(seconds, scene);
  177. }
  178. };
  179. //-----------------------------------------------------------------------------
  180. int main(int argc, char **argv)
  181. {
  182. sys::init(argc, argv);
  183. Application app("Tutorial 14: Lolua Demo", ivec2(800, 600), 60.0f);
  184. new LoluaDemo();
  185. app.Run();
  186. return EXIT_SUCCESS;
  187. }