From 48e3f7c6b376df34a646fc30096924da42cd6d47 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 11 Aug 2010 16:18:41 +0000 Subject: [PATCH] Implement ticking groups. --- src/asset.cpp | 5 +++++ src/asset.h | 18 +++++++++++++++--- src/ticker.cpp | 45 ++++++++++++++++++++++++--------------------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/asset.cpp b/src/asset.cpp index 4d8e416f..1f6c111f 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -28,6 +28,11 @@ Asset::~Asset() { } +Asset::Group Asset::GetGroup() +{ + return GROUP_DEFAULT; +} + void Asset::TickGame(float delta_time) { diff --git a/src/asset.h b/src/asset.h index c3487b52..8adc70b2 100644 --- a/src/asset.h +++ b/src/asset.h @@ -19,18 +19,30 @@ class Asset { friend class Ticker; + friend class TickerData; public: Asset(); virtual ~Asset(); - virtual void TickGame(float delta_time); - virtual void TickRender(float delta_time); - virtual void Ref(); virtual int Unref(); protected: + typedef enum + { + GROUP_BEFORE = 0, + GROUP_DEFAULT, + GROUP_AFTER, + GROUP_COUNT + } + Group; + + virtual Group GetGroup(); + + virtual void TickGame(float delta_time); + virtual void TickRender(float delta_time); + Asset *next; int ref, destroy; }; diff --git a/src/ticker.cpp b/src/ticker.cpp index d1c43fae..8d73d56e 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -24,9 +24,10 @@ static class TickerData public: TickerData() : - list(0), nassets(0) { + for (int i = 0; i < Asset::GROUP_COUNT; i++) + list[i] = NULL; } ~TickerData() @@ -36,7 +37,7 @@ public: } private: - Asset *list; + Asset *list[Asset::GROUP_COUNT]; int nassets; } tickerdata; @@ -49,37 +50,39 @@ static TickerData * const data = &tickerdata; void Ticker::Register(Asset *asset) { - asset->next = data->list; - data->list = asset; + int i = asset->GetGroup(); + asset->next = data->list[i]; + data->list[i] = asset; data->nassets++; } void Ticker::TickGame(float delta_time) { /* Garbage collect objects that can be destroyed */ - for (Asset *asset = data->list, *prev = NULL; - asset; - prev = asset, asset = asset->next) - if (asset->destroy) - { - if (prev) - prev->next = asset->next; - else - data->list = asset->next; - - data->nassets--; - delete asset; - } + for (int i = 0; i < Asset::GROUP_COUNT; i++) + for (Asset *a = data->list[i], *prev = NULL; a; prev = a, a = a->next) + if (a->destroy) + { + if (prev) + prev->next = a->next; + else + data->list[i] = a->next; + + data->nassets--; + delete a; + } /* Tick objects for the game loop */ - for (Asset *asset = data->list; asset; asset = asset->next) - asset->TickGame(delta_time); + for (int i = 0; i < Asset::GROUP_COUNT; i++) + for (Asset *a = data->list[i]; a; a = a->next) + a->TickGame(delta_time); } void Ticker::TickRender(float delta_time) { /* Tick objects for the render loop */ - for (Asset *asset = data->list; asset; asset = asset->next) - asset->TickRender(delta_time); + for (int i = 0; i < Asset::GROUP_COUNT; i++) + for (Asset *a = data->list[i]; a; a = a->next) + a->TickRender(delta_time); }