您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

234 行
6.6 KiB

  1. //
  2. // Lol Engine — Lua tutorial
  3. //
  4. // Copyright © 2014—2017 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  5. // © 2017—2020 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 *, int)
  28. {
  29. return new DemoObject();
  30. }
  31. //-------------------------------------------------------------------------
  32. static int AddFive(lua_State* l)
  33. {
  34. auto stack = LuaStack::Begin(l);
  35. int32_t i = stack.Get<int32_t>();
  36. i += 5;
  37. return (stack << i).End();
  38. }
  39. LOLUA_DECLARE_RETURN_METHOD_ARGS(AddTenInstance, GetPtr<DemoObject>(), AddTenMethod, Get<float>(), Get<int32_t>(), Get<int32_t>());
  40. static int _AddTenInstance(lua_State* l)
  41. {
  42. auto stack = LuaStack::Begin(l);
  43. DemoObject* obj = stack.GetPtr<DemoObject>();
  44. float f = stack.Get<float>();
  45. int32_t i = stack.Get<int32_t>();
  46. int32_t i2 = stack.Get<int32_t>();
  47. f = obj->AddTenMethod(f, i, i2);
  48. return (stack << f).End();
  49. }
  50. float AddTenMethod(float f, int32_t, int32_t)
  51. {
  52. return (f + 10);
  53. }
  54. static int GetX(lua_State* l)
  55. {
  56. auto stack = LuaStack::Begin(l);
  57. DemoObject* obj = stack.GetPtr<DemoObject>();
  58. auto i = stack.Get<int32_t>();
  59. i = obj->m_x;
  60. return (stack << i).End();
  61. }
  62. LOLUA_DECLARE_VOID_METHOD_ARGS(SetX, GetPtr<DemoObject>(), SetXMethod, Get<int32_t>());
  63. static int _SetX(lua_State* l)
  64. {
  65. auto stack = LuaStack::Begin(l);
  66. DemoObject* obj = stack.GetPtr<DemoObject>();
  67. auto i = stack.Get<int32_t>();
  68. obj->m_x = i;
  69. return stack.End();
  70. }
  71. void SetXMethod(int32_t i)
  72. {
  73. m_x = i;
  74. }
  75. //-------------------------------------------------------------------------
  76. static const LuaObjectLibrary* GetLib()
  77. {
  78. static const LuaObjectLibrary lib = LuaObjectLibrary(
  79. "LoluaDemo",
  80. { { "AddFive", &DemoObject::AddFive } },
  81. { { "AddTenInstance", &DemoObject::AddTenInstance } },
  82. { { "X", &DemoObject::GetX, &DemoObject::SetX } });
  83. return &lib;
  84. }
  85. int m_x = 0;
  86. };
  87. //-----------------------------------------------------------------------------
  88. static int GlobalAddString(lua_State* l)
  89. {
  90. auto stack = LuaStack::Begin(l);
  91. auto s = stack.Get<std::string>();
  92. s += "_added";
  93. return (stack << s).End();
  94. }
  95. //-----------------------------------------------------------------------------
  96. class LoluaDemoLoader : public LuaLoader
  97. {
  98. public:
  99. LoluaDemoLoader() : LuaLoader()
  100. {
  101. lua_State* l = GetLuaState();
  102. //Registering demo object
  103. LuaObjectHelper::Register<DemoObject>(l);
  104. //Registering function
  105. LuaFunction add_string(l, "GlobalAddString", &GlobalAddString);
  106. }
  107. virtual ~LoluaDemoLoader()
  108. {
  109. }
  110. void TestStuff()
  111. {
  112. /*
  113. lua_State* l = GetLuaState();
  114. //create property
  115. lua_pushnumber(l, 5.0);
  116. lua_setfield(l, -2, "x");
  117. lua_getglobal(l, "vector");
  118. int table = lua_istable(l, -1);
  119. lua_getfield(l, -1, "x");
  120. Lolua::Var<float> var(l, -1);
  121. lua_pop(l, 2);
  122. vec3 t;
  123. */
  124. //table = lua_isuserdata(l, -1);
  125. //Var<T> var(m_lua_state, -1);
  126. //lua_pop(m_lua_state, 1);
  127. //lua_getfield (lua_State *L, int index, const char *k);
  128. //return var.V();
  129. }
  130. };
  131. //-----------------------------------------------------------------------------
  132. class LoluaDemo : public WorldEntity
  133. {
  134. public:
  135. LoluaDemo()
  136. {
  137. }
  138. virtual void tick_game(float seconds)
  139. {
  140. WorldEntity::tick_game(seconds);
  141. LoluaDemoLoader* demo_loader = new LoluaDemoLoader();
  142. //Execute script
  143. demo_loader->ExecLuaFile("14_lua.lua");
  144. demo_loader->TestStuff();
  145. //Grab global test values
  146. float testvalue_num = demo_loader->Get<float>("testvalue_num");
  147. int32_t testvalue_int = demo_loader->Get<int32_t>("testvalue_int");
  148. uint32_t testvalue_uint = demo_loader->Get<uint32_t>("testvalue_uint");
  149. std::string testvalue_str = demo_loader->Get<std::string>("testvalue_str");
  150. //Grab string modified with function
  151. std::string function_return = demo_loader->Get<std::string>("function_return");
  152. //Grab global values modified with DemoObject
  153. int32_t loluademo_return = demo_loader->Get<int32_t>("loluademo_return");
  154. int32_t loluademo_getx = demo_loader->Get<int32_t>("loluademo_getx");
  155. float loluademo_inst_return = demo_loader->Get<float>("loluademo_inst_return");
  156. DemoObject* loluademo_inst = demo_loader->GetPtr<DemoObject>("loluademo_inst");
  157. msg::info("Lua Vars: \
  158. testvalue_num: %.2f, testvalue_int: %i, testvalue_uint: %i, testvalue_str: %s.\n",
  159. testvalue_num, testvalue_int, testvalue_uint, testvalue_str.c_str());
  160. msg::info("Lua Vars: \
  161. function_return: %s, loluademo_return: %i, loluademo_inst_return: %.2f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n",
  162. function_return.c_str(), loluademo_return, loluademo_inst_return, loluademo_getx, loluademo_inst->m_x);
  163. #define /***/ _LOLUA_ARG_1(a00) (float)a00
  164. #define /***/ _LOLUA_ARG_2(a00, a01) _LOLUA_ARG_1(a00), _LOLUA_ARG_1(a01)
  165. #define /***/ _LOLUA_ARG_3(a00, a01, a02) _LOLUA_ARG_1(a00), _LOLUA_ARG_2(a01, a02)
  166. #define /***/ _LOLUA_ARG_4(a00, a01, a02, a03) _LOLUA_ARG_1(a00), _LOLUA_ARG_3(a01, a02, a03)
  167. msg::info("_LOLUA_ARG_1: %f, %f, %f, %f\n", _LOLUA_ARG_4(0, 1, 2, 3));
  168. delete demo_loader;
  169. Ticker::Shutdown();
  170. }
  171. virtual void tick_draw(float seconds, Scene &scene)
  172. {
  173. WorldEntity::tick_draw(seconds, scene);
  174. }
  175. };
  176. //-----------------------------------------------------------------------------
  177. int main(int argc, char **argv)
  178. {
  179. sys::init(argc, argv);
  180. Application app("Tutorial 14: Lolua Demo", ivec2(800, 600), 60.0f);
  181. new LoluaDemo();
  182. app.Run();
  183. return EXIT_SUCCESS;
  184. }