source: trunk/src/ticker.cpp @ 111

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

The frame counter is now an asset object.

  • Property svn:keywords set to Id
File size: 2.3 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        todo(0),
28        nassets(0)
29    {
30        for (int i = 0; i < Asset::GROUP_COUNT; i++)
31            list[i] = NULL;
32    }
33
34    ~TickerData()
35    {
36#if !FINAL_RELEASE
37        if (nassets)
38            fprintf(stderr, "ERROR: still %i assets in ticker\n", nassets);
39#endif
40    }
41
42private:
43    Asset *todo;
44    Asset *list[Asset::GROUP_COUNT];
45    int nassets;
46}
47tickerdata;
48
49static TickerData * const data = &tickerdata;
50
51/*
52 * Ticker public class
53 */
54
55void Ticker::Register(Asset *asset)
56{
57    /* If we are called from its constructor, the object's vtable is not
58     * ready yet, so we do not know which group this asset belongs to. Wait
59     * until the first tick. */
60    asset->next = data->todo;
61    data->todo = asset;
62}
63
64void Ticker::TickGame(float delta_time)
65{
66    /* Insert waiting objects in the appropriate lists */
67    while (data->todo)
68    {
69        Asset *a = data->todo;
70        data->todo = a->next;
71
72        int i = a->GetGroup();
73        a->next = data->list[i];
74        data->list[i] = a;
75        data->nassets++;
76    }
77
78    /* Garbage collect objects that can be destroyed */
79    for (int i = 0; i < Asset::GROUP_COUNT; i++)
80        for (Asset *a = data->list[i], *prev = NULL; a; prev = a, a = a->next)
81            if (a->destroy)
82            {
83                if (prev)
84                    prev->next = a->next;
85                else
86                    data->list[i] = a->next;
87
88                data->nassets--;
89                delete a;
90            }
91
92    /* Tick objects for the game loop */
93    for (int i = 0; i < Asset::GROUP_COUNT; i++)
94        for (Asset *a = data->list[i]; a; a = a->next)
95            if (!a->destroy)
96                a->TickGame(delta_time);
97}
98
99void Ticker::TickRender(float delta_time)
100{
101    /* Tick objects for the render loop */
102    for (int i = 0; i < Asset::GROUP_COUNT; i++)
103        for (Asset *a = data->list[i]; a; a = a->next)
104            if (!a->destroy)
105                a->TickRender(delta_time);
106}
107
Note: See TracBrowser for help on using the repository browser.