Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Lol Engine - Graphing tutorial
  3. //
  4. // Copyright: (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/engine.h>
  14. #include "loldebug.h"
  15. #include <cstdio>
  16. using namespace lol;
  17. //-----------------------------------------------------------------------------
  18. class DemoObject : public Lolua::ObjectDef
  19. {
  20. public:
  21. DemoObject() : Lolua::ObjectDef() {}
  22. virtual ~DemoObject() {}
  23. static DemoObject* New(Lolua::State* l, int arg_nb)
  24. {
  25. UNUSED(l);
  26. UNUSED(arg_nb);
  27. return new DemoObject();
  28. }
  29. //-------------------------------------------------------------------------
  30. static int AddFive(Lolua::State* l)
  31. {
  32. Lolua::Var<int> i(l, 1);
  33. i += 5;
  34. return i.Return(l);
  35. }
  36. static int AddTenInstance(Lolua::State* l)
  37. {
  38. Lolua::VarPtr<DemoObject> obj(l, 1);
  39. Lolua::Var<float> f(l, 2);
  40. f = obj.V()->AddTenMethod(f.V());
  41. return f.Return(l);
  42. }
  43. float AddTenMethod(float f)
  44. {
  45. return (f + 10);
  46. }
  47. static int GetX(Lolua::State* l)
  48. {
  49. Lolua::VarPtr<DemoObject> obj(l, 1);
  50. Lolua::Var<int32_t> i;
  51. i = obj.V()->m_x;
  52. return i.Return(l);
  53. }
  54. static int SetX(Lolua::State* l)
  55. {
  56. Lolua::VarPtr<DemoObject> obj(l, 1);
  57. Lolua::Var<int32_t> i(l, 2);
  58. obj.V()->m_x = i.V();
  59. return 0;
  60. }
  61. //-------------------------------------------------------------------------
  62. static const Lolua::ObjectLib* GetLib()
  63. {
  64. static const Lolua::ObjectLib lib = Lolua::ObjectLib(
  65. "LoluaDemo",
  66. { { "AddFive", &DemoObject::AddFive } },
  67. { { "AddTenInstance", &DemoObject::AddTenInstance } },
  68. { { "X", &DemoObject::GetX, &DemoObject::SetX } });
  69. return &lib;
  70. }
  71. int m_x = 0;
  72. };
  73. //-----------------------------------------------------------------------------
  74. static int GlobalAddString(Lolua::State* l)
  75. {
  76. Lolua::Var<String> s(l, 1);
  77. s += "_added";
  78. return s.Return(l);
  79. }
  80. //-----------------------------------------------------------------------------
  81. class LoluaDemoLoader : public Lolua::Loader
  82. {
  83. public:
  84. LoluaDemoLoader() : Lolua::Loader()
  85. {
  86. Lolua::State* l = GetLuaState();
  87. //Registering demo object
  88. Lolua::Object::Register<DemoObject>(l);
  89. //Registering function
  90. Lolua::Function add_string(l, "GlobalAddString", &GlobalAddString);
  91. }
  92. virtual ~LoluaDemoLoader()
  93. {
  94. }
  95. void TestStuff()
  96. {
  97. Lolua::State* l = GetLuaState();
  98. /*
  99. //create property
  100. lua_pushnumber(l, 5.0);
  101. lua_setfield(l, -2, "x");
  102. lua_getglobal(l, "vector");
  103. int table = lua_istable(l, -1);
  104. lua_getfield(l, -1, "x");
  105. Lolua::Var<float> var(l, -1);
  106. lua_pop(l, 2);
  107. vec3 t;
  108. */
  109. //table = lua_isuserdata(l, -1);
  110. //Var<T> var(m_lua_state, -1);
  111. //lua_pop(m_lua_state, 1);
  112. //lua_getfield (lua_State *L, int index, const char *k);
  113. //return var.V();
  114. }
  115. };
  116. //-----------------------------------------------------------------------------
  117. class LoluaDemo : public WorldEntity
  118. {
  119. public:
  120. LoluaDemo()
  121. {
  122. }
  123. virtual void TickGame(float seconds)
  124. {
  125. WorldEntity::TickGame(seconds);
  126. LoluaDemoLoader* demo_loader = new LoluaDemoLoader();
  127. //Execute script
  128. demo_loader->ExecLua("14_lol_lua.lua");
  129. demo_loader->TestStuff();
  130. //Grab global test values
  131. float testvalue_num = demo_loader->GetVar<float>("testvalue_num");
  132. int32_t testvalue_int = demo_loader->GetVar<int32_t>("testvalue_int");
  133. uint32_t testvalue_uint = demo_loader->GetVar<uint32_t>("testvalue_uint");
  134. String testvalue_str = demo_loader->GetVar<String>("testvalue_str");
  135. //Grab string modified with function
  136. String function_return = demo_loader->GetVar<String>("function_return");
  137. //Grab global values modified with DemoObject
  138. int32_t loluademo_return = demo_loader->GetVar<int32_t>("loluademo_return");
  139. int32_t loluademo_getx = demo_loader->GetVar<int32_t>("loluademo_getx");
  140. float loluademo_inst_return = demo_loader->GetVar<float>("loluademo_inst_return");
  141. DemoObject* loluademo_inst = demo_loader->GetPtr<DemoObject>("loluademo_inst");
  142. Log::Info("Lua Vars: \
  143. testvalue_num: %.2f, testvalue_int: %i, testvalue_uint: %i, testvalue_str: %s.\n",
  144. testvalue_num, testvalue_int, testvalue_uint, testvalue_str.C());
  145. Log::Info("Lua Vars: \
  146. function_return: %s, loluademo_return: %i, loluademo_inst_return: %.f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n",
  147. function_return.C(), loluademo_return, loluademo_inst_return, loluademo_getx, loluademo_inst->m_x);
  148. delete demo_loader;
  149. Ticker::Shutdown();
  150. }
  151. virtual void TickDraw(float seconds, Scene &scene)
  152. {
  153. WorldEntity::TickDraw(seconds, scene);
  154. }
  155. };
  156. //-----------------------------------------------------------------------------
  157. int main(int argc, char **argv)
  158. {
  159. System::Init(argc, argv);
  160. Application app("Tutorial 14: Lolua Demo", ivec2(800, 600), 60.0f);
  161. new LoluaDemo();
  162. app.Run();
  163. return EXIT_SUCCESS;
  164. }