選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

205 行
5.5 KiB

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