lua_environment.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "config.h"
  7. #include "types.h"
  8. #include "resource_types.h"
  9. #include "math_types.h"
  10. #include <lua.hpp>
  11. namespace crown
  12. {
  13. enum LuaArgumentType
  14. {
  15. ARGUMENT_FLOAT
  16. };
  17. /// Wraps a subset of Lua functions and provides utilities for extending Lua.
  18. ///
  19. /// @ingroup Lua
  20. struct LuaEnvironment
  21. {
  22. lua_State* L;
  23. u32 _vec3_used;
  24. Vector3 _vec3_buffer[CROWN_MAX_LUA_VECTOR3];
  25. u32 _quat_used;
  26. Quaternion _quat_buffer[CROWN_MAX_LUA_QUATERNION];
  27. u32 _mat4_used;
  28. Matrix4x4 _mat4_buffer[CROWN_MAX_LUA_MATRIX4X4];
  29. LuaEnvironment();
  30. ~LuaEnvironment();
  31. /// Loads lua libraries.
  32. void load_libs();
  33. /// Executes the lua resource @a lr.
  34. void execute(const LuaResource* lr);
  35. /// Executes the @a lua string.
  36. void execute_string(const char* lua);
  37. /// Adds the function with the given @a name and @a func to the table @a module.
  38. void add_module_function(const char* module, const char* name, const lua_CFunction func);
  39. /// Adds the function with the given @a name and @a func to the table @a module.
  40. void add_module_function(const char* module, const char* name, const char* func);
  41. /// Sets the constructor for the table @a module to the given function.
  42. void set_module_constructor(const char* module, const lua_CFunction func);
  43. /// Calls the global function @a func with @a argc argument number.
  44. /// Each argument is a pair (type, value).
  45. /// Example call:
  46. /// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
  47. /// Returns true if success, false otherwise
  48. void call_global(const char* func, u8 argc, ...);
  49. /// Returns the number of temporary objects in use.
  50. void temp_count(u32& num_vec3, u32& num_quat, u32& num_mat4);
  51. /// Sets the number of temporary objects in use.
  52. void set_temp_count(u32 num_vec3, u32 num_quat, u32 num_mat4);
  53. /// Resets temporary types.
  54. void reset_temporaries();
  55. /// Returns a new temporary Vector3.
  56. Vector3* next_vector3(const Vector3& v);
  57. /// Returns a new temporary Quaternion.
  58. Quaternion* next_quaternion(const Quaternion& q);
  59. /// Returns a new temporary Matrix4x4.
  60. Matrix4x4* next_matrix4x4(const Matrix4x4& m);
  61. /// Returns whether @a p is a temporary Vector3.
  62. bool is_vector3(const Vector3* p) const;
  63. /// Returns whether @a p is a temporary Quaternion.
  64. bool is_quaternion(const Quaternion* p) const;
  65. /// Returns whether @a p is a temporary Matrix4x4.
  66. bool is_matrix4x4(const Matrix4x4* p) const;
  67. private:
  68. // Disable copying
  69. LuaEnvironment(const LuaEnvironment&);
  70. LuaEnvironment& operator=(const LuaEnvironment&);
  71. };
  72. } // namespace crown