lua_environment.h 2.0 KB

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