You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

473 lines
13 KiB

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