lua_environment.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. LuaEnvironment();
  23. ~LuaEnvironment();
  24. void load_libs();
  25. void execute(const LuaResource* lr);
  26. /// Loads and executes the given @a s lua string.
  27. void execute_string(const char* s);
  28. /// Loads the function with the given @a name and @a func into the table @a module.
  29. void load_module_function(const char* module, const char* name, const lua_CFunction func);
  30. void load_module_function(const char* module, const char* name, const char* value);
  31. void load_module_constructor(const char* module, const lua_CFunction func);
  32. /// Calls the global function @a func with @a argc argument number.
  33. /// Each argument is a pair (type, value).
  34. /// Example call:
  35. /// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
  36. /// Returns true if success, false otherwise
  37. void call_global(const char* func, uint8_t argc, ...);
  38. void clear_temporaries();
  39. /// Returns a new temporary Vector3.
  40. Vector3* next_vector3(const Vector3& v);
  41. /// Returns a new temporary Quaternion.
  42. Quaternion* next_quaternion(const Quaternion& q);
  43. /// Returns a new temporary Matrix4x4.
  44. Matrix4x4* next_matrix4x4(const Matrix4x4& m);
  45. /// Returns whether @a p is a temporary Vector3.
  46. bool is_vector3(const Vector3* p) const;
  47. /// Returns whether @a p is a temporary Quaternion.
  48. bool is_quaternion(const Quaternion* p) const;
  49. /// Returns whether @a p is a temporary Matrix4x4.
  50. bool is_matrix4x4(const Matrix4x4* p) const;
  51. private:
  52. lua_State* L;
  53. uint32_t _vec3_used;
  54. Vector3 _vec3_buffer[CROWN_MAX_LUA_VECTOR3];
  55. uint32_t _quat_used;
  56. Quaternion _quat_buffer[CROWN_MAX_LUA_QUATERNION];
  57. uint32_t _mat4_used;
  58. Matrix4x4 s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4];
  59. private:
  60. // Disable copying
  61. LuaEnvironment(const LuaEnvironment&);
  62. LuaEnvironment& operator=(const LuaEnvironment&);
  63. };
  64. } // namespace crown