Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

Core.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "Core.h"
  19. #ifndef HEADLESS
  20. #include "graphics/Renderer.h"
  21. #endif
  22. #include "net/SessionManager.h"
  23. #include "resource/Region.h"
  24. #include "Camera.h"
  25. #include "Config.h"
  26. #include "DatFile.h"
  27. #include "Land.h"
  28. #include "LandcellManager.h"
  29. #include "Log.h"
  30. #include "ObjectManager.h"
  31. #include "ResourceCache.h"
  32. #include "util.h"
  33. static const fp_t kStepRate = 60.0;
  34. static unique_ptr<Core> g_singleton;
  35. void Core::execute()
  36. {
  37. assert(!g_singleton);
  38. g_singleton.reset(new Core{});
  39. g_singleton->init();
  40. g_singleton->run();
  41. g_singleton->cleanup();
  42. }
  43. Core& Core::get()
  44. {
  45. return *g_singleton;
  46. }
  47. void Core::stop()
  48. {
  49. done_ = true;
  50. }
  51. Config& Core::config()
  52. {
  53. return *config_;
  54. }
  55. Log& Core::log()
  56. {
  57. return *log_;
  58. }
  59. DatFile& Core::portalDat()
  60. {
  61. return *portalDat_;
  62. }
  63. DatFile& Core::cellDat()
  64. {
  65. return *cellDat_;
  66. }
  67. DatFile& Core::highresDat()
  68. {
  69. return *highresDat_;
  70. }
  71. ResourceCache& Core::resourceCache()
  72. {
  73. return *resourceCache_;
  74. }
  75. LandcellManager& Core::landcellManager()
  76. {
  77. return *landcellManager_;
  78. }
  79. ObjectManager& Core::objectManager()
  80. {
  81. return *objectManager_;
  82. }
  83. SessionManager& Core::sessionManager()
  84. {
  85. return *sessionManager_;
  86. }
  87. const Region& Core::region() const
  88. {
  89. return region_->cast<Region>();
  90. }
  91. Camera& Core::camera()
  92. {
  93. return *camera_;
  94. }
  95. #ifndef HEADLESS
  96. Renderer& Core::renderer()
  97. {
  98. return *renderer_;
  99. }
  100. #endif
  101. Core::Core() : done_(false)
  102. {}
  103. void Core::init()
  104. {
  105. if(SDL_Init(SDL_INIT_TIMER) < 0)
  106. {
  107. throwSDLError();
  108. }
  109. config_.reset(new Config{});
  110. log_.reset(new Log{});
  111. portalDat_.reset(new DatFile{"data/client_portal.dat"});
  112. cellDat_.reset(new DatFile{"data/client_cell_1.dat"});
  113. highresDat_.reset(new DatFile{"data/client_highres.dat"});
  114. resourceCache_.reset(new ResourceCache{});
  115. landcellManager_.reset(new LandcellManager{});
  116. objectManager_.reset(new ObjectManager{});
  117. sessionManager_.reset(new SessionManager{});
  118. region_ = resourceCache_->get(0x13000000);
  119. camera_.reset(new Camera{});
  120. #ifndef HEADLESS
  121. renderer_.reset(new Renderer{});
  122. renderer_->init();
  123. #endif
  124. landcellManager_->setCenter(LandcellId(0x31, 0xD6));
  125. #if 0
  126. for(uint32_t resourceId : portalDat_->list())
  127. {
  128. if((resourceId & 0xFF000000) > 0x0D000000)
  129. {
  130. continue;
  131. }
  132. try
  133. {
  134. resourceCache_->get(resourceId);
  135. printf("%08x OK\n", resourceId);
  136. }
  137. catch(runtime_error& e)
  138. {
  139. printf("%08x FAIL %s\n", resourceId, e.what());
  140. }
  141. }
  142. #endif
  143. }
  144. void Core::cleanup()
  145. {
  146. #ifndef HEADLESS
  147. renderer_.reset();
  148. #endif
  149. camera_.reset();
  150. sessionManager_.reset();
  151. objectManager_.reset();
  152. landcellManager_.reset();
  153. resourceCache_.reset();
  154. portalDat_.reset();
  155. cellDat_.reset();
  156. highresDat_.reset();
  157. log_.reset();
  158. config_.reset();
  159. SDL_Quit();
  160. }
  161. void Core::run()
  162. {
  163. uint64_t frequency = SDL_GetPerformanceFrequency();
  164. uint64_t fixedStep = frequency / static_cast<uint64_t>(kStepRate);
  165. uint64_t maxTotalDelta = fixedStep * 6;
  166. uint64_t stepTime = SDL_GetPerformanceCounter();
  167. while(!done_)
  168. {
  169. uint64_t loopTime = SDL_GetPerformanceCounter();
  170. if(loopTime > stepTime + maxTotalDelta)
  171. {
  172. stepTime = loopTime - maxTotalDelta;
  173. }
  174. while(loopTime >= stepTime + fixedStep)
  175. {
  176. handleEvents();
  177. sessionManager_->handleBlobs();
  178. step(fp_t(1.0) / kStepRate);
  179. stepTime += fixedStep;
  180. }
  181. #ifndef HEADLESS
  182. fp_t interp = static_cast<fp_t>(loopTime - stepTime) / static_cast<fp_t>(frequency);
  183. renderer_->render(interp);
  184. #else
  185. // simulate ~83 without game logic
  186. SDL_Delay(12);
  187. #endif
  188. }
  189. }
  190. void Core::handleEvents()
  191. {
  192. SDL_Event event;
  193. while(SDL_PollEvent(&event) != 0)
  194. {
  195. switch(event.type)
  196. {
  197. case SDL_QUIT:
  198. done_ = true;
  199. break;
  200. case SDL_KEYDOWN:
  201. #ifdef _WIN32
  202. // SDL does not respond normally to alt-f4 on Windows, so handle it ourselves
  203. if(event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) != 0)
  204. {
  205. done_ = true;
  206. }
  207. #endif
  208. break;
  209. }
  210. }
  211. }
  212. void Core::step(fp_t dt)
  213. {
  214. const Uint8* state = SDL_GetKeyboardState(nullptr);
  215. fp_t speed = 0.0;
  216. if(state[SDL_SCANCODE_LSHIFT])
  217. {
  218. speed = 100.0;
  219. }
  220. else
  221. {
  222. speed = 10.0;
  223. }
  224. camera_->setSpeed(speed);
  225. fp_t lx = 0.0;
  226. fp_t ly = 0.0;
  227. if(state[SDL_SCANCODE_LEFT])
  228. {
  229. lx -= dt;
  230. }
  231. if(state[SDL_SCANCODE_RIGHT])
  232. {
  233. lx += dt;
  234. }
  235. if(state[SDL_SCANCODE_DOWN])
  236. {
  237. ly -= dt;
  238. }
  239. if(state[SDL_SCANCODE_UP])
  240. {
  241. ly += dt;
  242. }
  243. if(lx != 0.0 || ly != 0.0)
  244. {
  245. camera_->look(lx, ly);
  246. }
  247. fp_t mx = 0.0;
  248. fp_t my = 0.0;
  249. if(state[SDL_SCANCODE_A])
  250. {
  251. mx -= dt;
  252. }
  253. if(state[SDL_SCANCODE_D])
  254. {
  255. mx += dt;
  256. }
  257. if(state[SDL_SCANCODE_S])
  258. {
  259. my -= dt;
  260. }
  261. if(state[SDL_SCANCODE_W])
  262. {
  263. my += dt;
  264. }
  265. if(mx != 0 || my != 0)
  266. {
  267. camera_->move(mx, my);
  268. }
  269. const glm::vec3& position = camera_->position();
  270. LandcellId id = landcellManager_->center();
  271. if(position.x < 0.0)
  272. {
  273. camera_->setPosition(glm::vec3(position.x + Land::kBlockSize, position.y, position.z));
  274. landcellManager_->setCenter(LandcellId(id.x() - 1, id.y()));
  275. }
  276. if(position.x >= Land::kBlockSize)
  277. {
  278. camera_->setPosition(glm::vec3(position.x - Land::kBlockSize, position.y, position.z));
  279. landcellManager_->setCenter(LandcellId(id.x() + 1, id.y()));
  280. }
  281. if(position.y < 0.0)
  282. {
  283. camera_->setPosition(glm::vec3(position.x, position.y + Land::kBlockSize, position.z));
  284. landcellManager_->setCenter(LandcellId(id.x(), id.y() - 1));
  285. }
  286. if(position.y >= Land::kBlockSize)
  287. {
  288. camera_->setPosition(glm::vec3(position.x, position.y - Land::kBlockSize, position.z));
  289. landcellManager_->setCenter(LandcellId(id.x(), id.y() + 1));
  290. }
  291. camera_->step(dt);
  292. }