No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

234 líneas
6.6 KiB

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