source: trunk/src/ticker.cpp @ 616

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

The client application no longer needs to call Video::Clea().

  • Property svn:keywords set to Id
File size: 9.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16#include <cstdio>
17#include <stdint.h>
18
19#include "core.h"
20
21/*
22 * Ticker implementation class
23 */
24
25static class TickerData
26{
27    friend class Ticker;
28
29public:
30    TickerData() :
31        todolist(0), autolist(0),
32        nentities(0),
33        frame(0), deltams(0), bias(0), fps(0),
34        quit(0), quitframe(0), quitdelay(20), panic(0)
35    {
36        for (int i = 0; i < Entity::ALLGROUP_END; i++)
37            list[i] = NULL;
38    }
39
40    ~TickerData()
41    {
42#if !FINAL_RELEASE
43        if (nentities)
44            fprintf(stderr, "ERROR: still %i entities in ticker\n", nentities);
45        if (autolist)
46        {
47            int count = 0;
48            for (Entity *e = autolist; e; e = e->autonext, count++)
49                ;
50            fprintf(stderr, "ERROR: still %i autoreleased entities\n", count);
51        }
52        fprintf(stderr, "INFO: %i frames required to quit\n",
53                frame - quitframe);
54#endif
55    }
56
57private:
58    /* Entity management */
59    Entity *todolist, *autolist;
60    Entity *list[Entity::ALLGROUP_END];
61    int nentities;
62
63    /* Fixed framerate management */
64    int frame;
65    Timer timer;
66    float deltams, bias, fps;
67
68    /* Shutdown management */
69    int quit, quitframe, quitdelay, panic;
70}
71tickerdata;
72
73static TickerData * const data = &tickerdata;
74
75/*
76 * Ticker public class
77 */
78
79void Ticker::Register(Entity *entity)
80{
81    /* If we are called from its constructor, the object's vtable is not
82     * ready yet, so we do not know which group this entity belongs to. Wait
83     * until the first tick. */
84    entity->gamenext = data->todolist;
85    data->todolist = entity;
86    /* Objects are autoreleased by default. Put them in a circular list. */
87    entity->autorelease = 1;
88    entity->autonext = data->autolist;
89    data->autolist = entity;
90    entity->ref = 1;
91
92    data->nentities++;
93}
94
95void Ticker::Ref(Entity *entity)
96{
97#if !FINAL_RELEASE
98    if (!entity)
99    {
100        fprintf(stderr, "ERROR: refing NULL entity\n");
101        return;
102    }
103    if (entity->destroy)
104        fprintf(stderr, "ERROR: refing entity scheduled for destruction\n");
105#endif
106    if (entity->autorelease)
107    {
108        /* Get the entity out of the autorelease list. This is usually
109         * very fast since the first entry in autolist is the last
110         * registered entity. */
111        for (Entity *e = data->autolist, *prev = NULL; e;
112             prev = e, e = e->autonext)
113        {
114            if (e == entity)
115            {
116                (prev ? prev->autonext : data->autolist) = e->autonext;
117                break;
118            }
119        }
120        entity->autorelease = 0;
121    }
122    else
123        entity->ref++;
124}
125
126int Ticker::Unref(Entity *entity)
127{
128#if !FINAL_RELEASE
129    if (!entity)
130    {
131        fprintf(stderr, "ERROR: dereferencing NULL entity\n");
132        return 0;
133    }
134    if (entity->ref <= 0)
135        fprintf(stderr, "ERROR: dereferencing unreferenced entity\n");
136    if (entity->autorelease)
137        fprintf(stderr, "ERROR: dereferencing autoreleased entity\n");
138#endif
139    return --entity->ref;
140}
141
142void Ticker::TickGame()
143{
144    Profiler::Stop(Profiler::STAT_TICK_FRAME);
145    Profiler::Start(Profiler::STAT_TICK_FRAME);
146
147    Profiler::Start(Profiler::STAT_TICK_GAME);
148
149#if 0
150    fprintf(stderr, "-------------------------------------\n");
151    for (int i = 0; i < Entity::ALLGROUP_END; i++)
152    {
153        fprintf(stderr, "%s Group %i\n",
154                (i < Entity::GAMEGROUP_END) ? "Game" : "Draw", i);
155
156        for (Entity *e = data->list[i]; e; )
157        {
158            fprintf(stderr, "  \\-- %s (ref %i, destroy %i)\n", e->GetName(), e->ref, e->destroy);
159            e = (i < Entity::GAMEGROUP_END) ? e->gamenext : e->drawnext;
160        }
161    }
162#endif
163
164    data->frame++;
165
166    data->deltams = data->timer.GetMs();
167    data->bias += data->deltams;
168
169    /* If shutdown is stuck, kick the first entity we meet and see
170     * whether it makes things better. Note that it is always a bug to
171     * have referenced entities after 20 frames, but at least this
172     * safeguard makes it possible to exit the program cleanly. */
173    if (data->quit && !((data->frame - data->quitframe) % data->quitdelay))
174    {
175        int n = 0;
176        data->panic = 2 * (data->panic + 1);
177
178        for (int i = 0; i < Entity::ALLGROUP_END && n < data->panic; i++)
179        for (Entity *e = data->list[i]; e && n < data->panic; e = e->gamenext)
180            if (e->ref)
181            {
182#if !FINAL_RELEASE
183                fprintf(stderr, "ERROR: poking %s\n", e->GetName());
184#endif
185                e->ref--;
186                n++;
187            }
188
189#if !FINAL_RELEASE
190        if (n)
191            fprintf(stderr, "ERROR: %i entities stuck after %i frames, "
192                    "poked %i\n", data->nentities, data->quitdelay, n);
193#endif
194
195        data->quitdelay = data->quitdelay > 1 ? data->quitdelay / 2 : 1;
196    }
197
198    /* Garbage collect objects that can be destroyed. We can do this
199     * before inserting awaiting objects, because only objects already in
200     * the tick lists can be marked for destruction. */
201    for (int i = 0; i < Entity::ALLGROUP_END; i++)
202        for (Entity *e = data->list[i], *prev = NULL; e; )
203        {
204            if (e->destroy && i < Entity::GAMEGROUP_END)
205            {
206                /* If entity is to be destroyed, remove it from the
207                 * game tick list. */
208                (prev ? prev->gamenext : data->list[i]) = e->gamenext;
209
210                e = e->gamenext;
211            }
212            else if (e->destroy)
213            {
214                /* If entity is to be destroyed, remove it from the
215                 * draw tick list and destroy it. */
216                (prev ? prev->drawnext : data->list[i]) = e->drawnext;
217
218                Entity *tmp = e;
219                e = e->drawnext; /* Can only be in a draw group list */
220                delete tmp;
221
222                data->nentities--;
223            }
224            else
225            {
226                if (e->ref <= 0 && i >= Entity::DRAWGROUP_BEGIN)
227                    e->destroy = 1;
228                prev = e;
229                e = (i < Entity::GAMEGROUP_END) ? e->gamenext : e->drawnext;
230            }
231        }
232
233    /* Insert waiting objects into the appropriate lists */
234    while (data->todolist)
235    {
236        Entity *e = data->todolist;
237        data->todolist = e->gamenext;
238
239        e->gamenext = data->list[e->gamegroup];
240        data->list[e->gamegroup] = e;
241        e->drawnext = data->list[e->drawgroup];
242        data->list[e->drawgroup] = e;
243    }
244
245    /* Tick objects for the game loop */
246    for (int i = Entity::GAMEGROUP_BEGIN; i < Entity::GAMEGROUP_END; i++)
247        for (Entity *e = data->list[i]; e; e = e->gamenext)
248            if (!e->destroy)
249            {
250#if !FINAL_RELEASE
251                if (e->state != Entity::STATE_IDLE)
252                    fprintf(stderr, "ERROR: entity not idle for game tick\n");
253                e->state = Entity::STATE_PRETICK_GAME;
254#endif
255                e->TickGame(data->deltams);
256#if !FINAL_RELEASE
257                if (e->state != Entity::STATE_POSTTICK_GAME)
258                    fprintf(stderr, "ERROR: entity missed super game tick\n");
259                e->state = Entity::STATE_IDLE;
260#endif
261            }
262
263    Profiler::Stop(Profiler::STAT_TICK_GAME);
264}
265
266void Ticker::TickDraw()
267{
268    Profiler::Start(Profiler::STAT_TICK_DRAW);
269
270    Video::Clear();
271
272    Scene::GetDefault();
273
274    /* Tick objects for the draw loop */
275    for (int i = Entity::DRAWGROUP_BEGIN; i < Entity::DRAWGROUP_END; i++)
276    {
277        switch (i)
278        {
279        case Entity::DRAWGROUP_HUD:
280            Scene::GetDefault()->Render();
281            Video::SetDepth(false);
282            break;
283        default:
284            Video::SetDepth(true);
285            break;
286        }
287
288        for (Entity *e = data->list[i]; e; e = e->drawnext)
289            if (!e->destroy)
290            {
291#if !FINAL_RELEASE
292                if (e->state != Entity::STATE_IDLE)
293                    fprintf(stderr, "ERROR: entity not idle for draw tick\n");
294                e->state = Entity::STATE_PRETICK_DRAW;
295#endif
296                e->TickDraw(data->deltams);
297#if !FINAL_RELEASE
298                if (e->state != Entity::STATE_POSTTICK_DRAW)
299                    fprintf(stderr, "ERROR: entity missed super draw tick\n");
300                e->state = Entity::STATE_IDLE;
301#endif
302            }
303    }
304
305    Scene::Reset();
306
307    Profiler::Stop(Profiler::STAT_TICK_DRAW);
308    Profiler::Start(Profiler::STAT_TICK_BLIT);
309}
310
311void Ticker::ClampFps(float deltams)
312{
313    Profiler::Stop(Profiler::STAT_TICK_BLIT);
314
315    if (deltams > data->bias + 200.0f)
316        deltams = data->bias + 200.0f; // Don't go below 5 fps
317    if (deltams > data->bias)
318        data->timer.WaitMs(deltams - data->bias);
319    data->bias -= deltams;
320}
321
322int Ticker::GetFrameNum()
323{
324    return data->frame;
325}
326
327void Ticker::Shutdown()
328{
329    /* We're bailing out. Release all autorelease objects. */
330    while (data->autolist)
331    {
332        data->autolist->ref--;
333        data->autolist = data->autolist->autonext;
334    }
335
336    data->quit = 1;
337    data->quitframe = data->frame;
338}
339
340int Ticker::Finished()
341{
342    return !data->nentities;
343}
344
Note: See TracBrowser for help on using the repository browser.