lua_device.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "device.h"
  6. #include "resource_package.h"
  7. #include "world.h"
  8. #include "world_manager.h"
  9. #include "lua_environment.h"
  10. #include "lua_stack.h"
  11. #include "temp_allocator.h"
  12. #include "array.h"
  13. #include "string_stream.h"
  14. #include "console_server.h"
  15. #include "resource_manager.h"
  16. namespace crown
  17. {
  18. static int device_platform(lua_State* L)
  19. {
  20. LuaStack stack(L);
  21. stack.push_string(device()->platform());
  22. return 1;
  23. }
  24. static int device_architecture(lua_State* L)
  25. {
  26. LuaStack stack(L);
  27. stack.push_string(device()->architecture());
  28. return 1;
  29. }
  30. static int device_version(lua_State* L)
  31. {
  32. LuaStack stack(L);
  33. stack.push_string(device()->version());
  34. return 1;
  35. }
  36. static int device_last_delta_time(lua_State* L)
  37. {
  38. LuaStack stack(L);
  39. stack.push_float(device()->last_delta_time());
  40. return 1;
  41. }
  42. static int device_quit(lua_State* /*L*/)
  43. {
  44. device()->quit();
  45. return 0;
  46. }
  47. static int device_resolution(lua_State* L)
  48. {
  49. LuaStack stack(L);
  50. uint16_t w, h;
  51. device()->resolution(w, h);
  52. stack.push_int32(w);
  53. stack.push_int32(h);
  54. return 2;
  55. }
  56. static int device_create_world(lua_State* L)
  57. {
  58. LuaStack stack(L);
  59. const WorldId world_id = device()->create_world();
  60. stack.push_world(device()->world_manager()->lookup_world(world_id));
  61. return 1;
  62. }
  63. static int device_destroy_world(lua_State* L)
  64. {
  65. LuaStack stack(L);
  66. device()->destroy_world(stack.get_world(1)->id());
  67. return 0;
  68. }
  69. static int device_render_world(lua_State* L)
  70. {
  71. LuaStack stack(L);
  72. device()->render_world(stack.get_world(1), stack.get_camera(2));
  73. return 0;
  74. }
  75. static int device_create_resource_package(lua_State* L)
  76. {
  77. LuaStack stack(L);
  78. stack.push_resource_package(device()->create_resource_package(stack.get_string(1)));
  79. return 1;
  80. }
  81. static int device_destroy_resource_package(lua_State* L)
  82. {
  83. LuaStack stack(L);
  84. device()->destroy_resource_package(stack.get_resource_package(1));
  85. return 0;
  86. }
  87. static int device_console_send(lua_State* L)
  88. {
  89. using namespace string_stream;
  90. LuaStack stack(L);
  91. TempAllocator1024 alloc;
  92. StringStream json(alloc);
  93. json << "{";
  94. /* table is in the stack at index 'i' */
  95. stack.push_nil(); /* first key */
  96. while (stack.next(1) != 0)
  97. {
  98. /* uses 'key' (at index -2) and 'value' (at index -1) */
  99. json << "\"" << stack.get_string(-2) << "\":\"" << stack.get_string(-1) << "\",";
  100. /* removes 'value'; keeps 'key' for next iteration */
  101. stack.pop(1);
  102. }
  103. /* pop key */
  104. stack.pop(1);
  105. json << "}";
  106. console_server_globals::console().send_to_all(c_str(json));
  107. return 0;
  108. }
  109. static int device_can_get(lua_State* L)
  110. {
  111. LuaStack stack(L);
  112. stack.push_bool(device()->resource_manager()->can_get(stack.get_string(1), stack.get_string(2)));
  113. return 1;
  114. }
  115. static int device_enable_resource_autoload(lua_State* L)
  116. {
  117. LuaStack stack(L);
  118. device()->resource_manager()->enable_autoload(stack.get_bool(1));
  119. return 0;
  120. }
  121. void load_device(LuaEnvironment& env)
  122. {
  123. env.load_module_function("Device", "platform", device_platform);
  124. env.load_module_function("Device", "architecture", device_architecture);
  125. env.load_module_function("Device", "version", device_version);
  126. env.load_module_function("Device", "last_delta_time", device_last_delta_time);
  127. env.load_module_function("Device", "quit", device_quit);
  128. env.load_module_function("Device", "resolution", device_resolution);
  129. env.load_module_function("Device", "create_world", device_create_world);
  130. env.load_module_function("Device", "destroy_world", device_destroy_world);
  131. env.load_module_function("Device", "render_world", device_render_world);
  132. env.load_module_function("Device", "create_resource_package", device_create_resource_package);
  133. env.load_module_function("Device", "destroy_resource_package", device_destroy_resource_package);
  134. env.load_module_function("Device", "console_send", device_console_send);
  135. env.load_module_function("Device", "can_get", device_can_get);
  136. env.load_module_function("Device", "enable_resource_autoload", device_enable_resource_autoload);
  137. }
  138. } // namespace crown