source: trunk/src/ticker.cpp @ 101

Last change on this file since 101 was 101, checked in by sam, 10 years ago

Implement ticking groups.

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstdlib>
11#include <cstdio>
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    {
29        for (int i = 0; i < Asset::GROUP_COUNT; i++)
30            list[i] = NULL;
31    }
32
33    ~TickerData()
34    {
35        if (nassets)
36            fprintf(stderr, "ERROR: still %i assets in ticker\n", nassets);
37    }
38
39private:
40    Asset *list[Asset::GROUP_COUNT];
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{
53    int i = asset->GetGroup();
54    asset->next = data->list[i];
55    data->list[i] = asset;
56    data->nassets++;
57}
58
59void Ticker::TickGame(float delta_time)
60{
61    /* Garbage collect objects that can be destroyed */
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;
70
71                data->nassets--;
72                delete a;
73            }
74
75    /* Tick objects for the game loop */
76    for (int i = 0; i < Asset::GROUP_COUNT; i++)
77        for (Asset *a = data->list[i]; a; a = a->next)
78            a->TickGame(delta_time);
79}
80
81void Ticker::TickRender(float delta_time)
82{
83    /* Tick objects for the render loop */
84    for (int i = 0; i < Asset::GROUP_COUNT; i++)
85        for (Asset *a = data->list[i]; a; a = a->next)
86            a->TickRender(delta_time);
87}
88
Note: See TracBrowser for help on using the repository browser.