You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

204 lines
5.5 KiB

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