LuaEnvironment.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. struct 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. LuaEnvironment(lua_State* L);
  40. /// Loads and execute the given @a res_name Lua resource, returns
  41. /// true if success, false otherwise.
  42. bool load_and_execute(const char* res_name);
  43. bool execute_string(const char* s);
  44. /// Load a function which will be used in Lua. @a module is the name of table contenitor,
  45. /// @a name is the name of function in module and @func is the pointer to the function.
  46. /// _func_ must be a C/lua function (__int32_t function_name(lua_State* L)__)
  47. void load_module_function(const char* module, const char* name, const lua_CFunction func);
  48. /// Load a enum's value which will be used in Lua.
  49. /// @a module is the name of table contenitor, generally take enum's name
  50. /// @a name is module's name that refears _value_ and @value is an unsigned integer
  51. void load_module_enum(const char* module, const char* name, uint32_t value);
  52. /// Calls the global function @a func with @a argc argument number.
  53. /// Each argument is a pair (type, value).
  54. /// Example call:
  55. /// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
  56. /// Returns true if success, false otherwise
  57. bool call_global(const char* func, uint8_t argc, ...);
  58. void error();
  59. private:
  60. // Disable copying
  61. LuaEnvironment(const LuaEnvironment&);
  62. LuaEnvironment& operator=(const LuaEnvironment&);
  63. private:
  64. lua_State* m_state;
  65. };
  66. void load_accelerometer(LuaEnvironment& env);
  67. void load_actor(LuaEnvironment& env);
  68. void load_camera(LuaEnvironment& env);
  69. void load_controller(LuaEnvironment& env);
  70. void load_device(LuaEnvironment& env);
  71. void load_float_setting(LuaEnvironment& env);
  72. void load_gui(LuaEnvironment& env);
  73. void load_int_setting(LuaEnvironment& env);
  74. void load_keyboard(LuaEnvironment& env);
  75. void load_math(LuaEnvironment& env);
  76. void load_matrix4x4(LuaEnvironment& env);
  77. void load_mesh(LuaEnvironment& env);
  78. void load_mouse(LuaEnvironment& env);
  79. void load_physics_world(LuaEnvironment& env);
  80. void load_quaternion(LuaEnvironment& env);
  81. void load_resource_package(LuaEnvironment& env);
  82. void load_sound_world(LuaEnvironment& env);
  83. void load_sprite(LuaEnvironment& env);
  84. void load_string_setting(LuaEnvironment& env);
  85. void load_touch(LuaEnvironment& env);
  86. void load_unit(LuaEnvironment& env);
  87. void load_vector2(LuaEnvironment& env);
  88. void load_vector3(LuaEnvironment& env);
  89. void load_window(LuaEnvironment& env);
  90. void load_world(LuaEnvironment& env);
  91. void load_debug_line(LuaEnvironment& env);
  92. CE_EXPORT int32_t luaopen_libcrown(lua_State* L);
  93. } // namespace crown