Explorar el Código

Implement ticking groups.

legacy
Sam Hocevar sam hace 14 años
padre
commit
48e3f7c6b3
Se han modificado 3 ficheros con 44 adiciones y 24 borrados
  1. +5
    -0
      src/asset.cpp
  2. +15
    -3
      src/asset.h
  3. +24
    -21
      src/ticker.cpp

+ 5
- 0
src/asset.cpp Ver fichero

@@ -28,6 +28,11 @@ Asset::~Asset()
{ {
} }


Asset::Group Asset::GetGroup()
{
return GROUP_DEFAULT;
}

void Asset::TickGame(float delta_time) void Asset::TickGame(float delta_time)
{ {




+ 15
- 3
src/asset.h Ver fichero

@@ -19,18 +19,30 @@
class Asset class Asset
{ {
friend class Ticker; friend class Ticker;
friend class TickerData;


public: public:
Asset(); Asset();
virtual ~Asset(); virtual ~Asset();


virtual void TickGame(float delta_time);
virtual void TickRender(float delta_time);

virtual void Ref(); virtual void Ref();
virtual int Unref(); virtual int Unref();


protected: 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; Asset *next;
int ref, destroy; int ref, destroy;
}; };


+ 24
- 21
src/ticker.cpp Ver fichero

@@ -24,9 +24,10 @@ static class TickerData


public: public:
TickerData() : TickerData() :
list(0),
nassets(0) nassets(0)
{ {
for (int i = 0; i < Asset::GROUP_COUNT; i++)
list[i] = NULL;
} }


~TickerData() ~TickerData()
@@ -36,7 +37,7 @@ public:
} }


private: private:
Asset *list; Asset *list[Asset::GROUP_COUNT];
int nassets; int nassets;
} }
tickerdata; tickerdata;
@@ -49,37 +50,39 @@ static TickerData * const data = &tickerdata;


void Ticker::Register(Asset *asset) void Ticker::Register(Asset *asset)
{ {
asset->next = data->list; int i = asset->GetGroup();
data->list = asset; asset->next = data->list[i];
data->list[i] = asset;
data->nassets++; data->nassets++;
} }


void Ticker::TickGame(float delta_time) void Ticker::TickGame(float delta_time)
{ {
/* Garbage collect objects that can be destroyed */ /* Garbage collect objects that can be destroyed */
for (Asset *asset = data->list, *prev = NULL; for (int i = 0; i < Asset::GROUP_COUNT; i++)
asset; for (Asset *a = data->list[i], *prev = NULL; a; prev = a, a = a->next)
prev = asset, asset = asset->next) if (a->destroy)
if (asset->destroy) {
{ if (prev)
if (prev) prev->next = a->next;
prev->next = asset->next; else
else data->list[i] = a->next;
data->list = asset->next; data->nassets--;

delete a;
data->nassets--; }
delete asset;
}


/* Tick objects for the game loop */ /* Tick objects for the game loop */
for (Asset *asset = data->list; asset; asset = asset->next) for (int i = 0; i < Asset::GROUP_COUNT; i++)
asset->TickGame(delta_time); for (Asset *a = data->list[i]; a; a = a->next)
a->TickGame(delta_time);
} }


void Ticker::TickRender(float delta_time) void Ticker::TickRender(float delta_time)
{ {
/* Tick objects for the render loop */ /* Tick objects for the render loop */
for (Asset *asset = data->list; asset; asset = asset->next) for (int i = 0; i < Asset::GROUP_COUNT; i++)
asset->TickRender(delta_time); for (Asset *a = data->list[i]; a; a = a->next)
a->TickRender(delta_time);
} }



||||||
x
 
000:0
Cargando…
Cancelar
Guardar