Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

14_lol_lua.cpp 5.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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(LuaState* l, int arg_nb)
  27. {
  28. UNUSED(l);
  29. UNUSED(arg_nb);
  30. return new DemoObject();
  31. }
  32. //-------------------------------------------------------------------------
  33. static int AddFive(LuaState* l)
  34. {
  35. LuaInt32 i; i.Get(l, 1);
  36. i += 5;
  37. return i.Return(l);
  38. }
  39. static int AddTenInstance(LuaState* l)
  40. {
  41. LuaStack stack(l);
  42. LuaDemoObjectPtr obj;
  43. LuaFloat f;
  44. stack >> obj >> f;
  45. f = obj->AddTenMethod(f);
  46. return f.Return(l);
  47. }
  48. float AddTenMethod(float f)
  49. {
  50. return (f + 10);
  51. }
  52. static int GetX(LuaState* l)
  53. {
  54. LuaStack stack(l);
  55. LuaDemoObjectPtr obj;
  56. LuaInt32 i;
  57. stack >> obj;
  58. i = obj->m_x;
  59. return stack << i;
  60. }
  61. static int SetX(LuaState* l)
  62. {
  63. LuaStack stack(l);
  64. LuaDemoObjectPtr obj;
  65. LuaInt32 i;
  66. stack >> obj >> i;
  67. obj->m_x = i;
  68. return 0;
  69. }
  70. //-------------------------------------------------------------------------
  71. static const LuaObjectLib* GetLib()
  72. {
  73. static const LuaObjectLib lib = LuaObjectLib(
  74. "LoluaDemo",
  75. { { "AddFive", &DemoObject::AddFive } },
  76. { { "AddTenInstance", &DemoObject::AddTenInstance } },
  77. { { "X", &DemoObject::GetX, &DemoObject::SetX } });
  78. return &lib;
  79. }
  80. int m_x = 0;
  81. };
  82. //-----------------------------------------------------------------------------
  83. static int GlobalAddString(LuaState* l)
  84. {
  85. LuaString s; s.Get(l, 1);
  86. s() += "_added";
  87. return s.Return(l);
  88. }
  89. //-----------------------------------------------------------------------------
  90. class LoluaDemoLoader : public LuaLoader
  91. {
  92. public:
  93. LoluaDemoLoader() : LuaLoader()
  94. {
  95. LuaState* l = GetLuaState();
  96. //Registering demo object
  97. LuaObjectDef::Register<DemoObject>(l);
  98. //Registering function
  99. LuaFunction add_string(l, "GlobalAddString", &GlobalAddString);
  100. }
  101. virtual ~LoluaDemoLoader()
  102. {
  103. }
  104. void TestStuff()
  105. {
  106. LuaState* l = GetLuaState();
  107. /*
  108. //create property
  109. lua_pushnumber(l, 5.0);
  110. lua_setfield(l, -2, "x");
  111. lua_getglobal(l, "vector");
  112. int table = lua_istable(l, -1);
  113. lua_getfield(l, -1, "x");
  114. Lolua::Var<float> var(l, -1);
  115. lua_pop(l, 2);
  116. vec3 t;
  117. */
  118. //table = lua_isuserdata(l, -1);
  119. //Var<T> var(m_lua_state, -1);
  120. //lua_pop(m_lua_state, 1);
  121. //lua_getfield (lua_State *L, int index, const char *k);
  122. //return var.V();
  123. }
  124. };
  125. //-----------------------------------------------------------------------------
  126. class LoluaDemo : public WorldEntity
  127. {
  128. public:
  129. LoluaDemo()
  130. {
  131. }
  132. virtual void TickGame(float seconds)
  133. {
  134. WorldEntity::TickGame(seconds);
  135. LoluaDemoLoader* demo_loader = new LoluaDemoLoader();
  136. //Execute script
  137. demo_loader->ExecLuaFile("14_lol_lua.lua");
  138. demo_loader->TestStuff();
  139. //Grab global test values
  140. float testvalue_num = demo_loader->GetVar<float>("testvalue_num");
  141. int32_t testvalue_int = demo_loader->GetVar<int32_t>("testvalue_int");
  142. uint32_t testvalue_uint = demo_loader->GetVar<uint32_t>("testvalue_uint");
  143. String testvalue_str = demo_loader->GetVar<String>("testvalue_str");
  144. //Grab string modified with function
  145. String function_return = demo_loader->GetVar<String>("function_return");
  146. //Grab global values modified with DemoObject
  147. int32_t loluademo_return = demo_loader->GetVar<int32_t>("loluademo_return");
  148. int32_t loluademo_getx = demo_loader->GetVar<int32_t>("loluademo_getx");
  149. float loluademo_inst_return = demo_loader->GetVar<float>("loluademo_inst_return");
  150. DemoObject* loluademo_inst = demo_loader->GetPtr<DemoObject>("loluademo_inst");
  151. msg::info("Lua Vars: \
  152. testvalue_num: %.2f, testvalue_int: %i, testvalue_uint: %i, testvalue_str: %s.\n",
  153. testvalue_num, testvalue_int, testvalue_uint, testvalue_str.C());
  154. msg::info("Lua Vars: \
  155. function_return: %s, loluademo_return: %i, loluademo_inst_return: %.f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n",
  156. function_return.C(), loluademo_return, loluademo_inst_return, loluademo_getx, loluademo_inst->m_x);
  157. delete demo_loader;
  158. Ticker::Shutdown();
  159. }
  160. virtual void TickDraw(float seconds, Scene &scene)
  161. {
  162. WorldEntity::TickDraw(seconds, scene);
  163. }
  164. };
  165. //-----------------------------------------------------------------------------
  166. int main(int argc, char **argv)
  167. {
  168. System::Init(argc, argv);
  169. Application app("Tutorial 14: Lolua Demo", ivec2(800, 600), 60.0f);
  170. new LoluaDemo();
  171. app.Run();
  172. return EXIT_SUCCESS;
  173. }