LuaEnvironment.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "lua.hpp"
  25. #include "Config.h"
  26. #include "Types.h"
  27. namespace crown
  28. {
  29. enum LuaArgumentType
  30. {
  31. ARGUMENT_FLOAT
  32. };
  33. class LuaResource;
  34. /// LuaEnvironment is a wrapper of a subset of Lua functions and
  35. /// provides utilities for extending Lua
  36. class LuaEnvironment
  37. {
  38. public:
  39. /// Constructor
  40. LuaEnvironment();
  41. /// Init Lua state and open libraries. Must be called first
  42. void init();
  43. /// Close Lua state and shutdown LuaEnvironment
  44. void shutdown();
  45. /// Loads and execute the given @a res_name Lua resource, returns
  46. /// true if success, false otherwise.
  47. bool load_and_execute(const char* res_name);
  48. /// Load a function which will be used in Lua. @a module is the name of table contenitor,
  49. /// @a name is the name of function in module and @func is the pointer to the function.
  50. /// _func_ must be a C/lua function (__int32_t function_name(lua_State* L)__)
  51. void load_module_function(const char* module, const char* name, const lua_CFunction func);
  52. /// Load a enum's value which will be used in Lua.
  53. /// @a module is the name of table contenitor, generally take enum's name
  54. /// @a name is module's name that refears _value_ and @value is an unsigned integer
  55. void load_module_enum(const char* module, const char* name, uint32_t value);
  56. /// Calls the global function @a func with @a argc argument number.
  57. /// Each argument is a pair (type, value).
  58. /// Example call:
  59. /// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
  60. /// Returns true if success, false otherwise
  61. bool call_global(const char* func, uint8_t argc, ...);
  62. void error();
  63. private:
  64. // Disable copying
  65. LuaEnvironment(const LuaEnvironment&);
  66. LuaEnvironment& operator=(const LuaEnvironment&);
  67. private:
  68. /// Required by each Lua function
  69. lua_State* m_state;
  70. /// LuaEnvironment is used right now?
  71. bool m_is_used;
  72. };
  73. void load_int_setting(LuaEnvironment& env);
  74. void load_float_setting(LuaEnvironment& env);
  75. void load_string_setting(LuaEnvironment& env);
  76. void load_vec2(LuaEnvironment& env);
  77. void load_vec3(LuaEnvironment& env);
  78. void load_mat4(LuaEnvironment& env);
  79. void load_quat(LuaEnvironment& env);
  80. void load_math(LuaEnvironment& env);
  81. void load_mouse(LuaEnvironment& env);
  82. void load_keyboard(LuaEnvironment& env);
  83. void load_touch(LuaEnvironment& env);
  84. void load_accelerometer(LuaEnvironment& env);
  85. void load_camera(LuaEnvironment& env);
  86. void load_device(LuaEnvironment& env);
  87. void load_window(LuaEnvironment& env);
  88. void load_resource_package(LuaEnvironment& env);
  89. } // namespace crown