108 wiersze
2.3 KiB

  1. //
  2. // Deus Hax (working title)
  3. // Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
  4. //
  5. #if defined HAVE_CONFIG_H
  6. # include "config.h"
  7. #endif
  8. #include <cstdlib>
  9. #include <cstdio>
  10. #include <stdint.h>
  11. #include "ticker.h"
  12. #include "asset.h"
  13. /*
  14. * Ticker implementation class
  15. */
  16. static class TickerData
  17. {
  18. friend class Ticker;
  19. public:
  20. TickerData() :
  21. todo(0),
  22. nassets(0)
  23. {
  24. for (int i = 0; i < Asset::GROUP_COUNT; i++)
  25. list[i] = NULL;
  26. }
  27. ~TickerData()
  28. {
  29. #if !FINAL_RELEASE
  30. if (nassets)
  31. fprintf(stderr, "ERROR: still %i assets in ticker\n", nassets);
  32. #endif
  33. }
  34. private:
  35. Asset *todo;
  36. Asset *list[Asset::GROUP_COUNT];
  37. int nassets;
  38. }
  39. tickerdata;
  40. static TickerData * const data = &tickerdata;
  41. /*
  42. * Ticker public class
  43. */
  44. void Ticker::Register(Asset *asset)
  45. {
  46. /* If we are called from its constructor, the object's vtable is not
  47. * ready yet, so we do not know which group this asset belongs to. Wait
  48. * until the first tick. */
  49. asset->next = data->todo;
  50. data->todo = asset;
  51. }
  52. void Ticker::TickGame(float delta_time)
  53. {
  54. /* Insert waiting objects in the appropriate lists */
  55. while (data->todo)
  56. {
  57. Asset *a = data->todo;
  58. data->todo = a->next;
  59. int i = a->GetGroup();
  60. a->next = data->list[i];
  61. data->list[i] = a;
  62. data->nassets++;
  63. }
  64. /* Garbage collect objects that can be destroyed */
  65. for (int i = 0; i < Asset::GROUP_COUNT; i++)
  66. for (Asset *a = data->list[i], *prev = NULL; a; prev = a, a = a->next)
  67. if (a->destroy)
  68. {
  69. if (prev)
  70. prev->next = a->next;
  71. else
  72. data->list[i] = a->next;
  73. data->nassets--;
  74. delete a;
  75. }
  76. /* Tick objects for the game loop */
  77. for (int i = 0; i < Asset::GROUP_COUNT; i++)
  78. for (Asset *a = data->list[i]; a; a = a->next)
  79. if (!a->destroy)
  80. a->TickGame(delta_time);
  81. }
  82. void Ticker::TickRender(float delta_time)
  83. {
  84. /* Tick objects for the render loop */
  85. for (int i = 0; i < Asset::GROUP_COUNT; i++)
  86. for (Asset *a = data->list[i]; a; a = a->next)
  87. if (!a->destroy)
  88. a->TickRender(delta_time);
  89. }