source: trunk/src/ticker.cpp @ 351

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

More aggressive ticker panic mode.

  • Property svn:keywords set to Id
File size: 9.2 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),
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;
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                e->ref--;
183                n++;
184            }
185
186#if !FINAL_RELEASE
187        if (n)
188            fprintf(stderr, "ERROR: %i entities stuck after %i frames, "
189                    "poked %i\n", data->nentities, data->quitdelay, n);
190#endif
191
192        data->quitdelay = data->quitdelay > 1 ? data->quitdelay / 2 : 1;
193    }
194
195    /* Garbage collect objects that can be destroyed. We can do this
196     * before inserting awaiting objects, because only objects already in
197     * the tick lists can be marked for destruction. */
198    for (int i = 0; i < Entity::ALLGROUP_END; i++)
199        for (Entity *e = data->list[i], *prev = NULL; e; )
200        {
201            if (e->destroy && i < Entity::GAMEGROUP_END)
202            {
203                /* If entity is to be destroyed, remove it from the
204                 * game tick list. */
205                (prev ? prev->gamenext : data->list[i]) = e->gamenext;
206
207                e = e->gamenext;
208            }
209            else if (e->destroy)
210            {
211                /* If entity is to be destroyed, remove it from the
212                 * draw tick list and destroy it. */
213                (prev ? prev->drawnext : data->list[i]) = e->drawnext;
214
215                Entity *tmp = e;
216                e = e->drawnext; /* Can only be in a draw group list */
217                delete tmp;
218
219                data->nentities--;
220            }
221            else
222            {
223                if (e->ref <= 0 && i >= Entity::DRAWGROUP_BEGIN)
224                    e->destroy = 1;
225                prev = e;
226                e = (i < Entity::GAMEGROUP_END) ? e->gamenext : e->drawnext;
227            }
228        }
229
230    /* Insert waiting objects into the appropriate lists */
231    while (data->todolist)
232    {
233        Entity *e = data->todolist;
234        data->todolist = e->gamenext;
235
236        e->gamenext = data->list[e->gamegroup];
237        data->list[e->gamegroup] = e;
238        e->drawnext = data->list[e->drawgroup];
239        data->list[e->drawgroup] = e;
240    }
241
242    /* Tick objects for the game loop */
243    for (int i = Entity::GAMEGROUP_BEGIN; i < Entity::GAMEGROUP_END; i++)
244        for (Entity *e = data->list[i]; e; e = e->gamenext)
245            if (!e->destroy)
246            {
247#if !FINAL_RELEASE
248                if (e->state != Entity::STATE_IDLE)
249                    fprintf(stderr, "ERROR: entity not idle for game tick\n");
250                e->state = Entity::STATE_PRETICK_GAME;
251#endif
252                e->TickGame(data->deltams);
253#if !FINAL_RELEASE
254                if (e->state != Entity::STATE_POSTTICK_GAME)
255                    fprintf(stderr, "ERROR: entity missed super game tick\n");
256                e->state = Entity::STATE_IDLE;
257#endif
258            }
259
260    Profiler::Stop(Profiler::STAT_TICK_GAME);
261}
262
263void Ticker::TickDraw()
264{
265    Profiler::Start(Profiler::STAT_TICK_DRAW);
266
267    Scene::GetDefault();
268
269    /* Tick objects for the draw loop */
270    for (int i = Entity::DRAWGROUP_BEGIN; i < Entity::DRAWGROUP_END; i++)
271    {
272        switch (i)
273        {
274        case Entity::DRAWGROUP_HUD:
275            Scene::GetDefault()->Render();
276            Video::SetDepth(false);
277            break;
278        default:
279            Video::SetDepth(true);
280            break;
281        }
282
283        for (Entity *e = data->list[i]; e; e = e->drawnext)
284            if (!e->destroy)
285            {
286#if !FINAL_RELEASE
287                if (e->state != Entity::STATE_IDLE)
288                    fprintf(stderr, "ERROR: entity not idle for draw tick\n");
289                e->state = Entity::STATE_PRETICK_DRAW;
290#endif
291                e->TickDraw(data->deltams);
292#if !FINAL_RELEASE
293                if (e->state != Entity::STATE_POSTTICK_DRAW)
294                    fprintf(stderr, "ERROR: entity missed super draw tick\n");
295                e->state = Entity::STATE_IDLE;
296#endif
297            }
298    }
299
300    Scene::Reset();
301
302    Profiler::Stop(Profiler::STAT_TICK_DRAW);
303    Profiler::Start(Profiler::STAT_TICK_BLIT);
304}
305
306void Ticker::ClampFps(float deltams)
307{
308    Profiler::Stop(Profiler::STAT_TICK_BLIT);
309
310    if (deltams > data->bias + 200.0f)
311        deltams = data->bias + 200.0f; // Don't go below 5 fps
312    if (deltams > data->bias)
313        data->timer.WaitMs(deltams - data->bias);
314    data->bias -= deltams;
315}
316
317int Ticker::GetFrameNum()
318{
319    return data->frame;
320}
321
322void Ticker::Shutdown()
323{
324    /* We're bailing out. Release all autorelease objects. */
325    while (data->autolist)
326    {
327        data->autolist->ref--;
328        data->autolist = data->autolist->autonext;
329    }
330
331    data->quit = 1;
332    data->quitframe = data->frame;
333}
334
335int Ticker::Finished()
336{
337    return !data->nentities;
338}
339
Note: See TracBrowser for help on using the repository browser.