Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

517 рядки
13 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This library is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <cstdlib>
  14. #include <stdint.h>
  15. #include <functional>
  16. namespace lol
  17. {
  18. /*
  19. * Ticker implementation class
  20. */
  21. static class TickerData
  22. {
  23. friend class Ticker;
  24. public:
  25. TickerData() :
  26. nentities(0),
  27. frame(0), recording(0), deltatime(0), bias(0), fps(0),
  28. #if LOL_BUILD_DEBUG
  29. keepalive(0),
  30. #endif
  31. quit(0), quitframe(0), quitdelay(20), panic(0)
  32. {
  33. }
  34. ~TickerData()
  35. {
  36. ASSERT(nentities == 0,
  37. "still %td entities in ticker\n", nentities);
  38. ASSERT(m_autolist.Count() == 0,
  39. "still %ti autoreleased entities\n", m_autolist.Count());
  40. Log::Debug("%d frames required to quit\n", frame - quitframe);
  41. #if LOL_FEATURE_THREADS
  42. gametick.push(0);
  43. disktick.push(0);
  44. delete gamethread;
  45. delete diskthread;
  46. #endif
  47. }
  48. private:
  49. /* Entity management */
  50. array<Entity *> m_todolist, m_autolist;
  51. array<Entity *> m_list[Entity::ALLGROUP_END];
  52. ptrdiff_t nentities;
  53. /* Fixed framerate management */
  54. int frame, recording;
  55. Timer timer;
  56. float deltatime, bias, fps;
  57. #if LOL_BUILD_DEBUG
  58. float keepalive;
  59. #endif
  60. /* The three main functions (for now) */
  61. static void GameThreadTick();
  62. static void DrawThreadTick();
  63. static void DiskThreadTick();
  64. #if LOL_FEATURE_THREADS
  65. /* The associated background threads */
  66. void GameThreadMain();
  67. void DrawThreadMain(); /* unused */
  68. void DiskThreadMain();
  69. thread *gamethread, *drawthread, *diskthread;
  70. queue<int> gametick, drawtick, disktick;
  71. #endif
  72. /* Shutdown management */
  73. int quit, quitframe, quitdelay, panic;
  74. }
  75. tickerdata;
  76. static TickerData * const data = &tickerdata;
  77. /*
  78. * Ticker public class
  79. */
  80. void Ticker::Register(Entity *entity)
  81. {
  82. /* If we are called from its constructor, the object's vtable is not
  83. * ready yet, so we do not know which group this entity belongs to. Wait
  84. * until the first tick. */
  85. data->m_todolist.push(entity);
  86. /* Objects are autoreleased by default. Put them in a list. */
  87. data->m_autolist.push(entity);
  88. entity->m_autorelease = 1;
  89. entity->m_ref = 1;
  90. data->nentities++;
  91. }
  92. void Ticker::Ref(Entity *entity)
  93. {
  94. ASSERT(entity, "dereferencing nullptr entity\n");
  95. ASSERT(!entity->m_destroy,
  96. "referencing entity scheduled for destruction %s\n",
  97. entity->GetName());
  98. if (entity->m_autorelease)
  99. {
  100. /* Get the entity out of the m_autorelease list. This is usually
  101. * very fast since the last entry in autolist is the last
  102. * registered entity. */
  103. for (int i = data->m_autolist.Count(); i--; )
  104. {
  105. if (data->m_autolist[i] == entity)
  106. {
  107. data->m_autolist.RemoveSwap(i);
  108. break;
  109. }
  110. }
  111. entity->m_autorelease = 0;
  112. }
  113. else
  114. entity->m_ref++;
  115. }
  116. int Ticker::Unref(Entity *entity)
  117. {
  118. ASSERT(entity, "dereferencing null entity\n");
  119. ASSERT(entity->m_ref > 0, "dereferencing unreferenced entity %s\n",
  120. entity->GetName());
  121. ASSERT(!entity->m_autorelease, "dereferencing autoreleased entity %s\n",
  122. entity->GetName());
  123. return --entity->m_ref;
  124. }
  125. #if LOL_FEATURE_THREADS
  126. void TickerData::GameThreadMain()
  127. {
  128. #if LOL_BUILD_DEBUG
  129. Log::Info("ticker game thread initialised\n");
  130. #endif
  131. for (;;)
  132. {
  133. int tick = gametick.pop();
  134. if (!tick)
  135. break;
  136. GameThreadTick();
  137. drawtick.push(1);
  138. }
  139. drawtick.push(0);
  140. #if LOL_BUILD_DEBUG
  141. Log::Info("ticker game thread terminated\n");
  142. #endif
  143. }
  144. #endif /* LOL_FEATURE_THREADS */
  145. #if LOL_FEATURE_THREADS
  146. void TickerData::DrawThreadMain()
  147. {
  148. #if LOL_BUILD_DEBUG
  149. Log::Info("ticker draw thread initialised\n");
  150. #endif
  151. for (;;)
  152. {
  153. int tick = drawtick.pop();
  154. if (!tick)
  155. break;
  156. DrawThreadTick();
  157. gametick.push(1);
  158. }
  159. #if LOL_BUILD_DEBUG
  160. Log::Info("ticker draw thread terminated\n");
  161. #endif
  162. }
  163. #endif /* LOL_FEATURE_THREADS */
  164. #if LOL_FEATURE_THREADS
  165. void TickerData::DiskThreadMain()
  166. {
  167. /* FIXME: temporary hack to avoid crashes on the PS3 */
  168. disktick.pop();
  169. }
  170. #endif /* LOL_FEATURE_THREADS */
  171. void TickerData::GameThreadTick()
  172. {
  173. Profiler::Stop(Profiler::STAT_TICK_FRAME);
  174. Profiler::Start(Profiler::STAT_TICK_FRAME);
  175. Profiler::Start(Profiler::STAT_TICK_GAME);
  176. #if 0
  177. Log::Debug("-------------------------------------\n");
  178. for (int g = 0; g < Entity::ALLGROUP_END; ++g)
  179. {
  180. Log::Debug("%s Group %d\n",
  181. (g < Entity::GAMEGROUP_END) ? "Game" : "Draw", g);
  182. for (int i = 0; i < data->m_list[g].Count(); ++i)
  183. {
  184. Entity *e = data->m_list[g][i];
  185. Log::Debug(" \\-- [%p] %s (m_ref %d, destroy %d)\n",
  186. e, e->GetName(), e->m_ref, e->m_destroy);
  187. }
  188. }
  189. #endif
  190. data->frame++;
  191. /* Ensure some randomness */
  192. rand<int>();
  193. /* If recording with fixed framerate, set deltatime to a fixed value */
  194. if (data->recording && data->fps)
  195. {
  196. data->deltatime = 1.f / data->fps;
  197. }
  198. else
  199. {
  200. data->deltatime = data->timer.Get();
  201. data->bias += data->deltatime;
  202. }
  203. /* Do not go below 15 fps */
  204. if (data->deltatime > 1.f / 15.f)
  205. {
  206. data->deltatime = 1.f / 15.f;
  207. data->bias = 0.f;
  208. }
  209. #if LOL_BUILD_DEBUG
  210. data->keepalive += data->deltatime;
  211. if (data->keepalive > 10.f)
  212. {
  213. Log::Info("ticker keepalive: tick!\n");
  214. data->keepalive = 0.f;
  215. }
  216. #endif
  217. /* If shutdown is stuck, kick the first entity we meet and see
  218. * whether it makes things better. Note that it is always a bug to
  219. * have referenced entities after 20 frames, but at least this
  220. * safeguard makes it possible to exit the program cleanly. */
  221. if (data->quit && !((data->frame - data->quitframe) % data->quitdelay))
  222. {
  223. int n = 0;
  224. data->panic = 2 * (data->panic + 1);
  225. for (int g = 0; g < Entity::ALLGROUP_END && n < data->panic; ++g)
  226. for (int i = 0; i < data->m_list[g].Count() && n < data->panic; ++i)
  227. {
  228. Entity * e = data->m_list[g][i];
  229. if (e->m_ref)
  230. {
  231. #if !LOL_BUILD_RELEASE
  232. Log::Error("poking %s\n", e->GetName());
  233. #endif
  234. e->m_ref--;
  235. n++;
  236. }
  237. }
  238. #if !LOL_BUILD_RELEASE
  239. if (n)
  240. Log::Error("%td entities stuck after %d frames, poked %d\n",
  241. data->nentities, data->quitdelay, n);
  242. #endif
  243. data->quitdelay = data->quitdelay > 1 ? data->quitdelay / 2 : 1;
  244. }
  245. /* Garbage collect objects that can be destroyed. We can do this
  246. * before inserting awaiting objects, because only objects already
  247. * in the tick lists can be marked for destruction. */
  248. for (int g = 0; g < Entity::ALLGROUP_END; ++g)
  249. {
  250. for (int i = data->m_list[g].Count(); i--; )
  251. {
  252. Entity *e = data->m_list[g][i];
  253. if (e->m_destroy && g < Entity::GAMEGROUP_END)
  254. {
  255. /* If entity is to be destroyed, remove it from the
  256. * game tick list. */
  257. data->m_list[g].RemoveSwap(i);
  258. }
  259. else if (e->m_destroy)
  260. {
  261. /* If entity is to be destroyed, remove it from the
  262. * draw tick list and destroy it. */
  263. data->m_list[g].RemoveSwap(i);
  264. delete e;
  265. data->nentities--;
  266. }
  267. else
  268. {
  269. if (e->m_ref <= 0 && g >= Entity::DRAWGROUP_BEGIN)
  270. e->m_destroy = 1;
  271. }
  272. }
  273. }
  274. /* Insert waiting objects into the appropriate lists */
  275. while (data->m_todolist.Count())
  276. {
  277. Entity *e = data->m_todolist.Last();
  278. data->m_todolist.Remove(-1);
  279. data->m_list[e->m_gamegroup].Push(e);
  280. data->m_list[e->m_drawgroup].Push(e);
  281. // Initialize the entity
  282. e->InitGame();
  283. }
  284. /* Tick objects for the game loop */
  285. for (int g = Entity::GAMEGROUP_BEGIN; g < Entity::GAMEGROUP_END && !data->quit /* Stop as soon as required */; ++g)
  286. {
  287. for (int i = 0; i < data->m_list[g].Count() && !data->quit /* Stop as soon as required */; ++i)
  288. {
  289. Entity *e = data->m_list[g][i];
  290. if (!e->m_destroy)
  291. {
  292. #if !LOL_BUILD_RELEASE
  293. if (e->m_tickstate != Entity::STATE_IDLE)
  294. Log::Error("entity %s [%p] not idle for game tick\n",
  295. e->GetName(), e);
  296. e->m_tickstate = Entity::STATE_PRETICK_GAME;
  297. #endif
  298. e->TickGame(data->deltatime);
  299. #if !LOL_BUILD_RELEASE
  300. if (e->m_tickstate != Entity::STATE_POSTTICK_GAME)
  301. Log::Error("entity %s [%p] missed super game tick\n",
  302. e->GetName(), e);
  303. e->m_tickstate = Entity::STATE_IDLE;
  304. #endif
  305. }
  306. }
  307. }
  308. Profiler::Stop(Profiler::STAT_TICK_GAME);
  309. }
  310. void TickerData::DrawThreadTick()
  311. {
  312. Profiler::Start(Profiler::STAT_TICK_DRAW);
  313. /* Tick objects for the draw loop */
  314. for (int g = Entity::DRAWGROUP_BEGIN; g < Entity::DRAWGROUP_END && !data->quit /* Stop as soon as required */; ++g)
  315. {
  316. switch (g)
  317. {
  318. case Entity::DRAWGROUP_BEGIN:
  319. g_scene->Reset();
  320. g_renderer->Clear(ClearMask::All);
  321. break;
  322. default:
  323. break;
  324. }
  325. //Stop as soon as required
  326. for (int i = 0; i < data->m_list[g].Count() && !data->quit /* Stop as soon as required */; ++i)
  327. {
  328. Entity *e = data->m_list[g][i];
  329. if (!e->m_destroy)
  330. {
  331. #if !LOL_BUILD_RELEASE
  332. if (e->m_tickstate != Entity::STATE_IDLE)
  333. Log::Error("entity %s [%p] not idle for draw tick\n",
  334. e->GetName(), e);
  335. e->m_tickstate = Entity::STATE_PRETICK_DRAW;
  336. #endif
  337. e->TickDraw(data->deltatime, *g_scene);
  338. #if !LOL_BUILD_RELEASE
  339. if (e->m_tickstate != Entity::STATE_POSTTICK_DRAW)
  340. Log::Error("entity %s [%p] missed super draw tick\n",
  341. e->GetName(), e);
  342. e->m_tickstate = Entity::STATE_IDLE;
  343. #endif
  344. }
  345. }
  346. /* Do this render step */
  347. g_scene->RenderPrimitives();
  348. g_scene->RenderTiles();
  349. g_scene->RenderLines(data->deltatime);
  350. }
  351. Profiler::Stop(Profiler::STAT_TICK_DRAW);
  352. }
  353. void TickerData::DiskThreadTick()
  354. {
  355. ;
  356. }
  357. void Ticker::SetState(Entity * /* entity */, uint32_t /* state */)
  358. {
  359. }
  360. void Ticker::SetStateWhenMatch(Entity * /* entity */, uint32_t /* state */,
  361. Entity * /* other_entity */, uint32_t /* other_state */)
  362. {
  363. }
  364. void Ticker::Setup(float fps)
  365. {
  366. data->fps = fps;
  367. #if LOL_FEATURE_THREADS
  368. data->gamethread = new thread(std::bind(&TickerData::GameThreadMain, data));
  369. data->gametick.push(1);
  370. data->diskthread = new thread(std::bind(&TickerData::DiskThreadMain, data));
  371. #endif
  372. }
  373. void Ticker::TickDraw()
  374. {
  375. #if LOL_FEATURE_THREADS
  376. data->drawtick.pop();
  377. #else
  378. TickerData::GameThreadTick();
  379. #endif
  380. TickerData::DrawThreadTick();
  381. Profiler::Start(Profiler::STAT_TICK_BLIT);
  382. /* Signal game thread that it can carry on */
  383. #if LOL_FEATURE_THREADS
  384. data->gametick.push(1);
  385. #else
  386. TickerData::DiskThreadTick();
  387. #endif
  388. /* Clamp FPS */
  389. Profiler::Stop(Profiler::STAT_TICK_BLIT);
  390. #if !EMSCRIPTEN
  391. /* If framerate is fixed, force wait time to 1/FPS. Otherwise, set wait
  392. * time to 0. */
  393. float frametime = data->fps ? 1.f / data->fps : 0.f;
  394. if (frametime > data->bias + .2f)
  395. frametime = data->bias + .2f; /* Don't go below 5 fps */
  396. if (frametime > data->bias)
  397. data->timer.Wait(frametime - data->bias);
  398. /* If recording, do not try to compensate for lag. */
  399. if (!data->recording)
  400. data->bias -= frametime;
  401. #endif
  402. }
  403. void Ticker::StartRecording()
  404. {
  405. data->recording++;
  406. }
  407. void Ticker::StopRecording()
  408. {
  409. data->recording--;
  410. }
  411. int Ticker::GetFrameNum()
  412. {
  413. return data->frame;
  414. }
  415. void Ticker::Shutdown()
  416. {
  417. /* We're bailing out. Release all m_autorelease objects. */
  418. while (data->m_autolist.Count())
  419. {
  420. data->m_autolist.Last()->m_ref--;
  421. data->m_autolist.Remove(-1);
  422. }
  423. data->quit = 1;
  424. data->quitframe = data->frame;
  425. }
  426. int Ticker::Finished()
  427. {
  428. return !data->nentities;
  429. }
  430. } /* namespace lol */