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.
 
 
 

453 lines
12 KiB

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