ScriptSystem.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "lua.hpp"
  3. #include "Types.h"
  4. #include "String.h"
  5. #include "ScriptResource.h"
  6. #include "Vec2.h"
  7. #include "Vec3.h"
  8. #include "Mat4.h"
  9. #include "Quat.h"
  10. namespace crown
  11. {
  12. /// Abstraction of lua state.
  13. /// It provides 2 ways for loading lua code, as a buffer or as a string.
  14. /// It must be included only by ScriptSystem.
  15. class LuaState
  16. {
  17. public:
  18. /// Constructor, private for singleton
  19. LuaState();
  20. /// Destructor
  21. ~LuaState();
  22. /// Load lua chunk as buffer
  23. int32_t load_buffer(const char* buf, size_t len);
  24. /// Load lua chunk as string
  25. int32_t load_string(const char* str);
  26. /// Load lua chunk as file
  27. int32_t load_file(const char* file);
  28. /// Executes lua chunk loaded in stack
  29. int32_t execute();
  30. private:
  31. /// Lua state incapsulated by this class
  32. lua_State* m_state;
  33. };
  34. /// ScriptSystem allows to execute lua code or bytecode chunks
  35. /// It also provides some utilities for crown-lua environment
  36. class ScriptSystem
  37. {
  38. public:
  39. /// Constructor
  40. ScriptSystem();
  41. /// Loads script resource
  42. void load(const char* script);
  43. /// Executes
  44. void execute();
  45. /// Unloads script resource
  46. void unload(const char* script);
  47. /// Returns the first free Vec2
  48. Vec2& next_vec2(float nx, float ny);
  49. /// Returns the first free Vec3
  50. Vec3& next_vec3(float nx, float ny, float nz);
  51. /// Returns the first free Mat4
  52. Mat4& next_mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3);
  53. /// Returns the first free Quat
  54. Quat& next_quat(float angle, const Vec3& v);
  55. /// Returns the number of vec2 used in lua environment
  56. uint32_t vec2_used();
  57. /// Returns the number of vec3 used in lua environment
  58. uint32_t vec3_used();
  59. /// Returns the number of mat4 used in lua environment
  60. uint32_t mat4_used();
  61. /// Returns the number of quat used in lua environment
  62. uint32_t quat_used();
  63. /// First file loaded by ScriptSystem
  64. static const char* BOOT_SCRIPT;
  65. /// Max number of temporary objects allowed
  66. static const uint32_t MAX_TEMP_OBJECTS = 1024;
  67. private:
  68. LuaState m_state;
  69. /// Vec2 used by lua environment
  70. Vec2 m_vec2_list[MAX_TEMP_OBJECTS];
  71. /// Counter which points to the next free Vec2
  72. uint32_t m_vec2_count;
  73. /// Vec3 used by lua environment
  74. Vec3 m_vec3_list[MAX_TEMP_OBJECTS];
  75. /// Counter which points to the next free Vec3
  76. uint32_t m_vec3_count;
  77. /// Mat4 used by lua environment
  78. Mat4 m_mat4_list[MAX_TEMP_OBJECTS];
  79. /// Counter which points to the next free Mat4
  80. uint32_t m_mat4_count;
  81. /// Quaternions used by lua environment
  82. Quat m_quat_list[MAX_TEMP_OBJECTS];
  83. /// Counter which points to the nect free Quat
  84. uint32_t m_quat_count;
  85. };
  86. // This block provides fews utilities for lua environment
  87. extern "C"
  88. {
  89. uint32_t script_system_vec2_used();
  90. uint32_t script_system_vec3_used();
  91. uint32_t script_system_mat4_used();
  92. uint32_t script_system_quat_used();
  93. }
  94. } // namespace crown