lua_device.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include "device.h"
  24. #include "resource_package.h"
  25. #include "world.h"
  26. #include "world_manager.h"
  27. #include "lua_environment.h"
  28. #include "lua_stack.h"
  29. #include "temp_allocator.h"
  30. #include "array.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. static int device_argv(lua_State* L)
  35. {
  36. LuaStack stack(L);
  37. const int32_t argc = device()->argc();
  38. const char** argv = device()->argv();
  39. stack.push_table();
  40. for (int32_t i = 0; i < argc; i++)
  41. {
  42. // Be friendly to Lua's one-indexed arrays
  43. stack.push_key_begin(i + 1);
  44. stack.push_string(argv[i]);
  45. stack.push_key_end();
  46. }
  47. return 1;
  48. }
  49. //-----------------------------------------------------------------------------
  50. static int device_last_delta_time(lua_State* L)
  51. {
  52. LuaStack stack(L);
  53. float delta = device()->last_delta_time();
  54. stack.push_float(delta);
  55. return 1;
  56. }
  57. //-----------------------------------------------------------------------------
  58. static int device_start(lua_State* /*L*/)
  59. {
  60. device()->start();
  61. return 0;
  62. }
  63. //-----------------------------------------------------------------------------
  64. static int device_stop(lua_State* /*L*/)
  65. {
  66. device()->stop();
  67. return 0;
  68. }
  69. //-----------------------------------------------------------------------------
  70. static int device_create_world(lua_State* L)
  71. {
  72. LuaStack stack(L);
  73. const WorldId world_id = device()->create_world();
  74. stack.push_world(device()->world_manager()->lookup_world(world_id));
  75. return 1;
  76. }
  77. //-----------------------------------------------------------------------------
  78. static int device_destroy_world(lua_State* L)
  79. {
  80. LuaStack stack(L);
  81. device()->destroy_world(stack.get_world(1)->id());
  82. return 0;
  83. }
  84. //-----------------------------------------------------------------------------
  85. static int device_update_world(lua_State* L)
  86. {
  87. LuaStack stack(L);
  88. World* world = stack.get_world(1);
  89. const float dt = stack.get_float(2);
  90. device()->update_world(world, dt);
  91. return 0;
  92. }
  93. //-----------------------------------------------------------------------------
  94. static int device_render_world(lua_State* L)
  95. {
  96. LuaStack stack(L);
  97. World* world = stack.get_world(1);
  98. Camera* camera = stack.get_camera(2);
  99. device()->render_world(world, camera);
  100. return 0;
  101. }
  102. //-----------------------------------------------------------------------------
  103. static int device_create_resource_package(lua_State* L)
  104. {
  105. LuaStack stack(L);
  106. const char* package = stack.get_string(1);
  107. stack.push_resource_package(device()->create_resource_package(package));
  108. return 1;
  109. }
  110. //-----------------------------------------------------------------------------
  111. static int device_destroy_resource_package(lua_State* L)
  112. {
  113. LuaStack stack(L);
  114. ResourcePackage* package = stack.get_resource_package(1);
  115. device()->destroy_resource_package(package);
  116. return 0;
  117. }
  118. //-----------------------------------------------------------------------------
  119. static int device_display_modes(lua_State* L)
  120. {
  121. LuaStack stack(L);
  122. TempAllocator512 alloc;
  123. Array<DisplayMode> modes(alloc);
  124. device()->display_modes(modes);
  125. stack.push_table();
  126. for (uint32_t i = 0; i < array::size(modes); i++)
  127. {
  128. stack.push_key_begin((int32_t) i + 1);
  129. stack.push_table();
  130. stack.push_key_begin("id"); stack.push_uint32(modes[i].id); stack.push_key_end();
  131. stack.push_key_begin("width"); stack.push_uint32(modes[i].width); stack.push_key_end();
  132. stack.push_key_begin("height"); stack.push_uint32(modes[i].height); stack.push_key_end();
  133. stack.push_key_end();
  134. }
  135. return 1;
  136. }
  137. //-----------------------------------------------------------------------------
  138. static int device_set_display_mode(lua_State* L)
  139. {
  140. LuaStack stack(L);
  141. device()->set_display_mode(stack.get_int(1));
  142. return 0;
  143. }
  144. //-----------------------------------------------------------------------------
  145. static int device_set_fullscreen(lua_State* L)
  146. {
  147. LuaStack stack(L);
  148. device()->set_fullscreen(stack.get_bool(1));
  149. return 0;
  150. }
  151. //-----------------------------------------------------------------------------
  152. void load_device(LuaEnvironment& env)
  153. {
  154. env.load_module_function("Device", "argv", device_argv);
  155. env.load_module_function("Device", "last_delta_time", device_last_delta_time);
  156. env.load_module_function("Device", "start", device_start);
  157. env.load_module_function("Device", "stop", device_stop);
  158. env.load_module_function("Device", "create_world", device_create_world);
  159. env.load_module_function("Device", "destroy_world", device_destroy_world);
  160. env.load_module_function("Device", "update_world", device_update_world);
  161. env.load_module_function("Device", "render_world", device_render_world);
  162. env.load_module_function("Device", "create_resource_package", device_create_resource_package);
  163. env.load_module_function("Device", "destroy_resource_package", device_destroy_resource_package);
  164. env.load_module_function("Device", "display_modes", device_display_modes);
  165. env.load_module_function("Device", "set_display_mode", device_set_display_mode);
  166. env.load_module_function("Device", "set_fullscreen", device_set_fullscreen);
  167. }
  168. } // namespace crown