Ver código fonte

Implement ticking groups.

legacy
Sam Hocevar sam 14 anos atrás
pai
commit
48e3f7c6b3
3 arquivos alterados com 44 adições e 24 exclusões
  1. +5
    -0
      src/asset.cpp
  2. +15
    -3
      src/asset.h
  3. +24
    -21
      src/ticker.cpp

+ 5
- 0
src/asset.cpp Ver arquivo

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

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

void Asset::TickGame(float delta_time)
{



+ 15
- 3
src/asset.h Ver arquivo

@@ -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;
};


+ 24
- 21
src/ticker.cpp Ver arquivo

@@ -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);
}


Carregando…
Cancelar
Salvar