2
0

lua_device.cpp 4.1 KB

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