source: trunk/src/ticker.cpp @ 104

Last change on this file since 104 was 104, checked in by sam, 11 years ago

Do not tick destroyed objects.

  • Property svn:keywords set to Id
File size: 1.8 KB
RevLine 
[100]1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
[94]5
[100]6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
[94]9
[98]10#include <cstdlib>
11#include <cstdio>
[94]12#include <stdint.h>
13
14#include "ticker.h"
15#include "asset.h"
16
17/*
18 * Ticker implementation class
19 */
20
21static class TickerData
22{
23    friend class Ticker;
24
25public:
26    TickerData() :
27        nassets(0)
28    {
[101]29        for (int i = 0; i < Asset::GROUP_COUNT; i++)
30            list[i] = NULL;
[94]31    }
32
33    ~TickerData()
34    {
[98]35        if (nassets)
36            fprintf(stderr, "ERROR: still %i assets in ticker\n", nassets);
[94]37    }
38
39private:
[101]40    Asset *list[Asset::GROUP_COUNT];
[94]41    int nassets;
42}
43tickerdata;
44
45static TickerData * const data = &tickerdata;
46
47/*
48 * Ticker public class
49 */
50
51void Ticker::Register(Asset *asset)
52{
[101]53    int i = asset->GetGroup();
54    asset->next = data->list[i];
55    data->list[i] = asset;
[98]56    data->nassets++;
[94]57}
58
59void Ticker::TickGame(float delta_time)
60{
[98]61    /* Garbage collect objects that can be destroyed */
[101]62    for (int i = 0; i < Asset::GROUP_COUNT; i++)
63        for (Asset *a = data->list[i], *prev = NULL; a; prev = a, a = a->next)
64            if (a->destroy)
65            {
66                if (prev)
67                    prev->next = a->next;
68                else
69                    data->list[i] = a->next;
[98]70
[101]71                data->nassets--;
72                delete a;
73            }
[98]74
75    /* Tick objects for the game loop */
[101]76    for (int i = 0; i < Asset::GROUP_COUNT; i++)
77        for (Asset *a = data->list[i]; a; a = a->next)
[104]78            if (!a->destroy)
79                a->TickGame(delta_time);
[94]80}
81
82void Ticker::TickRender(float delta_time)
83{
[98]84    /* Tick objects for the render loop */
[101]85    for (int i = 0; i < Asset::GROUP_COUNT; i++)
86        for (Asset *a = data->list[i]; a; a = a->next)
[104]87            if (!a->destroy)
88                a->TickRender(delta_time);
[94]89}
90
Note: See TracBrowser for help on using the repository browser.