App.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "App.h"
  2. #include "Path.h"
  3. #include "FileSystem.h"
  4. #include "Config.h"
  5. #include "Logging.h"
  6. #include "Windowing.h"
  7. #include "Window.h"
  8. #include "Renderer.h"
  9. #include "UI.h"
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #include <cstring>
  13. #include <imgui.h>
  14. namespace gameplay
  15. {
  16. struct App::Impl
  17. {
  18. bool running = false;
  19. FileSystem* fs = nullptr;
  20. Config* config = nullptr;
  21. Logging* logging = nullptr;
  22. Windowing* windowing = nullptr;
  23. Window* window = nullptr;
  24. Renderer* renderer = nullptr;
  25. UI* ui = nullptr;
  26. std::unordered_map<std::string, std::string> resourceAliases;
  27. };
  28. App::App()
  29. {
  30. _impl = new App::Impl();
  31. }
  32. App::~App()
  33. {
  34. GP_SAFE_DELETE(_impl);
  35. }
  36. App* App::get_app()
  37. {
  38. static App app;
  39. return &app;
  40. }
  41. int App::exec(int argc, char** argv)
  42. {
  43. if (_impl->running)
  44. return 1;
  45. _impl->running = true;
  46. // create the file system
  47. _impl->fs = new FileSystem();
  48. // create and load settings
  49. _impl->config = new Config();
  50. _impl->config->load(argc, argv);
  51. // register application executable directory as a file system alias
  52. _impl->resourceAliases["app.dir"] = _impl->fs->get_app_directory_path();
  53. // register file system aliases from config
  54. _impl->config->for_each_table("resource.alias",
  55. [](Config* config, void* userPtr) -> bool
  56. {
  57. App::Impl* impl = (App::Impl*)userPtr;
  58. std::string name = config->get_string("name", "");
  59. std::string pathStr = config->get_string("path", "");
  60. if (App::get_app()->resolve_resource_path(pathStr))
  61. {
  62. impl->resourceAliases[name] = pathStr;
  63. }
  64. return true;
  65. }, (void*)_impl);
  66. // startup the logging system
  67. _impl->logging = new Logging();
  68. _impl->logging->startup();
  69. // startup the windowing system
  70. _impl->windowing = new Windowing();
  71. _impl->windowing->startup();
  72. // load the main window from config
  73. std::string windowTitle = _impl->config->get_string("window.title", "");
  74. int windowWidth = _impl->config->get_int("window.width", 1280);
  75. int windowHeight = _impl->config->get_int("window.height", 720);
  76. bool windowFullscreen = _impl->config->get_bool("window.fullscreen", false);
  77. std::string windowHintsStr = _impl->config->get_string("window.hints", "NONE");
  78. WindowHints windowHints = Windowing::parse_window_hints(windowHintsStr.c_str());
  79. // create the main window
  80. WindowDesc windowDesc = { windowTitle.c_str(), windowWidth, windowHeight, windowFullscreen, windowHints };
  81. _impl->window = _impl->windowing->create_window(windowDesc);
  82. // adjust the window position where the frame is at 0, 0
  83. int top;
  84. _impl->window->get_frame_size(nullptr, &top, nullptr, nullptr);
  85. _impl->window->set_pos({0, top});
  86. // startup the ui system
  87. _impl->ui = new UI();
  88. _impl->ui->startup();
  89. // startup the renderer
  90. _impl->renderer = new Renderer();
  91. _impl->renderer->startup();
  92. // run the main window event loop until we should close
  93. while (!_impl->window->should_close())
  94. {
  95. _impl->windowing->poll_events();
  96. _impl->renderer->next_frame();
  97. _impl->renderer->update();
  98. _impl->ui->update();
  99. _impl->renderer->render_frame();
  100. _impl->renderer->present_frame();
  101. }
  102. // destroy main window and shutdown
  103. _impl->windowing->destroy_window(_impl->window);
  104. _impl->windowing->shutdown();
  105. return 0;
  106. }
  107. void App::exit()
  108. {
  109. _impl->window->close();
  110. }
  111. void App::set_time(double timeSecs)
  112. {
  113. return _impl->windowing->set_time(timeSecs);
  114. }
  115. double App::get_time() const
  116. {
  117. return _impl->windowing->get_time();
  118. }
  119. FileSystem* App::get_file_system() const
  120. {
  121. return _impl->fs;
  122. }
  123. Config* App::get_config() const
  124. {
  125. return _impl->config;
  126. }
  127. Logging* App::get_logging() const
  128. {
  129. return _impl->logging;
  130. }
  131. Windowing* App::get_windowing() const
  132. {
  133. return _impl->windowing;
  134. }
  135. Window* App::get_main_window() const
  136. {
  137. return _impl->window;
  138. }
  139. Renderer* App::get_renderer() const
  140. {
  141. return _impl->renderer;
  142. }
  143. UI* App::get_ui() const
  144. {
  145. return _impl->ui;
  146. }
  147. void App::set_resource_path(const char* alias, const char* path)
  148. {
  149. _impl->resourceAliases[alias] = path;
  150. }
  151. void App::clear_resource_path(const char* alias)
  152. {
  153. _impl->resourceAliases.erase(alias);
  154. }
  155. bool App::resolve_resource_path(std::string& resourcePath)
  156. {
  157. std::string path = resourcePath;
  158. size_t startPos = 0;
  159. const std::string startDelim = "@";
  160. while((startPos = path.find(startDelim, startPos)) != std::string::npos)
  161. {
  162. const std::string stopDelim = "/";
  163. size_t endPos = path.find(stopDelim, startPos);
  164. if (endPos != std::string::npos)
  165. {
  166. size_t aliasLen = endPos - startPos;
  167. std::string alias = path.substr(startPos + 1, aliasLen - 1);
  168. auto pair = _impl->resourceAliases.find(alias);
  169. if ( pair == _impl->resourceAliases.end())
  170. return false;
  171. std::string aliasPath = _impl->resourceAliases[alias];
  172. path.replace(startPos, aliasLen, aliasPath);
  173. }
  174. }
  175. resourcePath = path;
  176. return true;
  177. }
  178. }