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

214 lines
5.9 KiB

  1. //
  2. // Lol Engine — Lua tutorial
  3. //
  4. // Copyright © 2014—2015 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. typedef Lolua::VarPtr<DemoObject> LuaDemoObjectPtr;
  23. public:
  24. DemoObject() : LuaObject() {}
  25. virtual ~DemoObject() {}
  26. static DemoObject* New(lua_State* l, int arg_nb)
  27. {
  28. UNUSED(l);
  29. UNUSED(arg_nb);
  30. return new DemoObject();
  31. }
  32. //-------------------------------------------------------------------------
  33. static int AddFive(lua_State* l)
  34. {
  35. LuaStack stack(l);
  36. int32_t i = stack.GetVar<int32_t>();
  37. //LuaInt32 i; i.Get(l, 1);
  38. i += 5;
  39. //return i.Return(l);
  40. return stack.SetVar(i).Return();
  41. }
  42. static int AddTenInstance(lua_State* l)
  43. {
  44. LuaStack stack(l);
  45. DemoObject* obj = nullptr; // stack.GetPtr<DemoObject>();
  46. float f = stack.GetVar<float>();
  47. //LuaDemoObjectPtr obj;
  48. //LuaFloat f;
  49. //stack >> obj >> f;
  50. f = obj->AddTenMethod(f);
  51. //return f.Return(l);
  52. return 0;// stack.SetVar(f).SetPtr().Return();
  53. }
  54. float AddTenMethod(float f)
  55. {
  56. return (f + 10);
  57. }
  58. static int GetX(lua_State* l)
  59. {
  60. LuaStack stack(l);
  61. LuaDemoObjectPtr obj;
  62. LuaInt32 i;
  63. stack >> obj;
  64. i = obj->m_x;
  65. return stack << i;
  66. }
  67. static int SetX(lua_State* l)
  68. {
  69. LuaStack stack(l);
  70. LuaDemoObjectPtr obj;
  71. LuaInt32 i;
  72. stack >> obj >> i;
  73. obj->m_x = i;
  74. return 0;
  75. }
  76. //-------------------------------------------------------------------------
  77. static const LuaObjectLib* GetLib()
  78. {
  79. static const LuaObjectLib lib = LuaObjectLib(
  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. LuaString s; s.Get(l, 1);
  92. s() += "_added";
  93. return s.Return(l);
  94. }
  95. //-----------------------------------------------------------------------------
  96. class LoluaDemoLoader : public LuaLoader
  97. {
  98. public:
  99. LoluaDemoLoader() : LuaLoader()
  100. {
  101. lua_State* l = GetLuaState();
  102. //Registering demo object
  103. LuaObjectDef::Register<DemoObject>(l);
  104. //Registering function
  105. LuaFunction add_string(l, "GlobalAddString", &GlobalAddString);
  106. }
  107. virtual ~LoluaDemoLoader()
  108. {
  109. }
  110. void TestStuff()
  111. {
  112. lua_State* l = GetLuaState();
  113. /*
  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 TickGame(float seconds)
  139. {
  140. WorldEntity::TickGame(seconds);
  141. LoluaDemoLoader* demo_loader = new LoluaDemoLoader();
  142. //Execute script
  143. demo_loader->ExecLuaFile("14_lol_lua.lua");
  144. demo_loader->TestStuff();
  145. //Grab global test values
  146. float testvalue_num = demo_loader->GetVar<float>("testvalue_num");
  147. int32_t testvalue_int = demo_loader->GetVar<int32_t>("testvalue_int");
  148. uint32_t testvalue_uint = demo_loader->GetVar<uint32_t>("testvalue_uint");
  149. String testvalue_str = demo_loader->GetVar<String>("testvalue_str");
  150. //Grab string modified with function
  151. String function_return = demo_loader->GetVar<String>("function_return");
  152. //Grab global values modified with DemoObject
  153. int32_t loluademo_return = demo_loader->GetVar<int32_t>("loluademo_return");
  154. int32_t loluademo_getx = demo_loader->GetVar<int32_t>("loluademo_getx");
  155. float loluademo_inst_return = demo_loader->GetVar<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());
  160. msg::info("Lua Vars: \
  161. function_return: %s, loluademo_return: %i, loluademo_inst_return: %.f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n",
  162. function_return.C(), loluademo_return, loluademo_inst_return, loluademo_getx, loluademo_inst->m_x);
  163. delete demo_loader;
  164. Ticker::Shutdown();
  165. }
  166. virtual void TickDraw(float seconds, Scene &scene)
  167. {
  168. WorldEntity::TickDraw(seconds, scene);
  169. }
  170. };
  171. //-----------------------------------------------------------------------------
  172. int main(int argc, char **argv)
  173. {
  174. sys::init(argc, argv);
  175. Application app("Tutorial 14: Lolua Demo", ivec2(800, 600), 60.0f);
  176. new LoluaDemo();
  177. app.Run();
  178. return EXIT_SUCCESS;
  179. }