130 rader
3.8 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 FooTestLua : public Lolua::ObjectDef
  19. {
  20. public:
  21. FooTestLua() : Lolua::ObjectDef() {}
  22. virtual ~FooTestLua() {}
  23. static FooTestLua* New(Lolua::State* l, int arg_nb)
  24. {
  25. return new FooTestLua();
  26. }
  27. static const char* GetClassName();
  28. static const char* GetClassLibName();
  29. static const char* GetClassInstName();
  30. static const Lolua::ClassMethod* GetStaticMethods();
  31. static const Lolua::ClassMethod* GetInstanceMethods();
  32. static int Test1(Lolua::State* L)
  33. {
  34. Lolua::Var<int> i1(L, 1);
  35. i1 += 5;
  36. return i1.Return(L);
  37. }
  38. static int Test2(Lolua::State* L)
  39. {
  40. Lolua::VarPtr<FooTestLua> foo(L, 1);
  41. Lolua::Var<float> i1(L, 2);
  42. i1 = foo.V()->Test2Inst(i1.V());
  43. return i1.Return(L);
  44. }
  45. float Test2Inst(float f)
  46. {
  47. return (f + 10);
  48. }
  49. };
  50. static const Lolua::ClassMethod FooTest_statics[] = { { "Test1", &FooTestLua::Test1 }, { NULL, NULL } };
  51. const Lolua::ClassMethod FooTest_methods[] = { { "Test2", &FooTestLua::Test2 }, { NULL, NULL } };
  52. const char* FooTestLua::GetClassName() { static const char name[] = "FooTest"; return name; }
  53. const char* FooTestLua::GetClassLibName() { static const char name[] = "FooTestLib"; return name; }
  54. const char* FooTestLua::GetClassInstName() { static const char name[] = "FooTestInst"; return name; }
  55. const Lolua::ClassMethod* FooTestLua::GetStaticMethods() { return FooTest_statics; }
  56. const Lolua::ClassMethod* FooTestLua::GetInstanceMethods() { return FooTest_methods; }
  57. //-----------------------------------------------------------------------------
  58. class LoluaDemoLoader : public Lolua::Loader
  59. {
  60. public:
  61. LoluaDemoLoader() : Lolua::Loader()
  62. {
  63. Lolua::State* l = GetLuaState();
  64. Lolua::Object::Register<FooTestLua>(l);
  65. ExecLua("14_lol_lua.lua");
  66. }
  67. virtual ~LoluaDemoLoader()
  68. {
  69. }
  70. };
  71. //-----------------------------------------------------------------------------
  72. class LoluaDemo : public WorldEntity
  73. {
  74. public:
  75. LoluaDemo()
  76. {
  77. }
  78. virtual void TickGame(float seconds)
  79. {
  80. WorldEntity::TickGame(seconds);
  81. LoluaDemoLoader* demo_loader = new LoluaDemoLoader();
  82. float TestValueNum = demo_loader->GetVar<float>("TestValueNum");
  83. int32_t TestValueInt = demo_loader->GetVar<int32_t>("TestValueInt");
  84. uint32_t TestValueUint = demo_loader->GetVar<uint32_t>("TestValueUint");
  85. String TestValueStr = demo_loader->GetVar<String>("TestValueStr");
  86. int32_t testtruc_return = demo_loader->GetVar<int32_t>("footest_return");
  87. float testtruc_return2 = demo_loader->GetVar<float>("footest_return2");
  88. String::Printf("Lua Vars: TestValueNum: %.2f, TestValueInt: %i, TestValueUint: %i, TestValueStr: %s.",
  89. TestValueNum, TestValueInt, TestValueUint, TestValueStr);
  90. delete demo_loader;
  91. Ticker::Shutdown();
  92. }
  93. virtual void TickDraw(float seconds, Scene &scene)
  94. {
  95. WorldEntity::TickDraw(seconds, scene);
  96. }
  97. };
  98. //-----------------------------------------------------------------------------
  99. int main(int argc, char **argv)
  100. {
  101. System::Init(argc, argv);
  102. Application app("Tutorial 14: Lolua Demo", ivec2(800, 600), 60.0f);
  103. new LoluaDemo();
  104. app.Run();
  105. return EXIT_SUCCESS;
  106. }