LuaEnvironment.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "Types.h"
  26. #include "Config.h"
  27. #include "LuaStack.h"
  28. #include "LinearAllocator.h"
  29. #include "Thread.h"
  30. namespace crown
  31. {
  32. /// LuaEnvironment is a wrapper of a subset of Lua functions and
  33. /// provides utilities for extending Lua
  34. class LuaEnvironment
  35. {
  36. public:
  37. /// Constructor
  38. LuaEnvironment();
  39. /// Init Lua state and open libraries. Must be called first
  40. void init();
  41. /// Close Lua state and shutdown LuaEnvironment
  42. void shutdown();
  43. /// Return the __lua_State__ pointer required by each Lua function
  44. lua_State* state();
  45. const char* error();
  46. /// Load Lua chunk as buffer. @a buffer contains lua chunk, @len is length of buffer
  47. void load_buffer(const char* buffer, size_t len);
  48. /// Load Lua chunk as file. @a file is path to lua file
  49. void load_file(const char* file);
  50. /// Load Lua chunk as string
  51. void load_string(const char* str);
  52. /// Get global symbol from Lua stack. @symbol is the name of required symbol
  53. void get_global_symbol(const char* symbol);
  54. /// Execute Lua chunk. @a args is the number of arguments required
  55. /// @a results is the number of results returned
  56. void execute(int32_t args, int32_t results);
  57. /// Collect garbage generated by Lua
  58. void collect_garbage();
  59. /// Call init function in the Lua game file
  60. void game_init();
  61. /// Call shutdown function in the Lua game file
  62. void game_shutdown();
  63. /// Call frame function in the Lua game file
  64. void game_frame(float dt);
  65. /// Load a function which will be used in Lua. @a module is the name of table contenitor,
  66. /// @a name is the name of function in module and @func is the pointer to the function.
  67. /// _func_ must be a C/lua function (__int32_t function_name(lua_State* L)__)
  68. void load_module_function(const char* module, const char* name, const lua_CFunction func);
  69. /// Load a enum's value which will be used in Lua.
  70. /// @a module is the name of table contenitor, generally take enum's name
  71. /// @a name is module's name that refears _value_ and @value is an unsigned integer
  72. void load_module_enum(const char* module, const char* name, uint32_t value);
  73. private:
  74. /// Thread used for garbage collection. It calls __collect_garbage()__
  75. static void* background_thread(void* thiz);
  76. void lua_error();
  77. // Disable copying
  78. LuaEnvironment(const LuaEnvironment&);
  79. LuaEnvironment& operator=(const LuaEnvironment&);
  80. private:
  81. /// Required by each Lua function
  82. lua_State* m_state;
  83. /// LuaEnvironment is used right now?
  84. bool m_is_used;
  85. /// Thread used for garbage collection
  86. // os::Thread m_thread;
  87. char m_error_buffer[1024];
  88. char m_tmp_buffer[1024];
  89. static const char* class_system;
  90. static const char* commands_list;
  91. static const char* get_cmd_by_name;
  92. static const char* tmp_print_table;
  93. static const char* count_all;
  94. // static const char* type_name;
  95. // static const char* type_count;
  96. };
  97. void load_int_setting(LuaEnvironment& env);
  98. void load_float_setting(LuaEnvironment& env);
  99. void load_string_setting(LuaEnvironment& env);
  100. void load_vec2(LuaEnvironment& env);
  101. void load_vec3(LuaEnvironment& env);
  102. void load_mat4(LuaEnvironment& env);
  103. void load_quat(LuaEnvironment& env);
  104. void load_math(LuaEnvironment& env);
  105. void load_mouse(LuaEnvironment& env);
  106. void load_keyboard(LuaEnvironment& env);
  107. void load_touch(LuaEnvironment& env);
  108. void load_accelerometer(LuaEnvironment& env);
  109. void load_camera(LuaEnvironment& env);
  110. void load_device(LuaEnvironment& env);
  111. void load_window(LuaEnvironment& env);
  112. CE_EXPORT int32_t luaopen_libcrown(lua_State* L);
  113. } // namespace crown