source: trunk/src/ticker.cpp @ 617

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

Store the requested FPS in the Ticker class.

  • Property svn:keywords set to Id
File size: 9.4 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::Setup(float fps)
143{
144    data->fps = fps;
145}
146
147void Ticker::TickGame()
148{
149    Profiler::Stop(Profiler::STAT_TICK_FRAME);
150    Profiler::Start(Profiler::STAT_TICK_FRAME);
151
152    Profiler::Start(Profiler::STAT_TICK_GAME);
153
154#if 0
155    fprintf(stderr, "-------------------------------------\n");
156    for (int i = 0; i < Entity::ALLGROUP_END; i++)
157    {
158        fprintf(stderr, "%s Group %i\n",
159                (i < Entity::GAMEGROUP_END) ? "Game" : "Draw", i);
160
161        for (Entity *e = data->list[i]; e; )
162        {
163            fprintf(stderr, "  \\-- %s (ref %i, destroy %i)\n", e->GetName(), e->ref, e->destroy);
164            e = (i < Entity::GAMEGROUP_END) ? e->gamenext : e->drawnext;
165        }
166    }
167#endif
168
169    data->frame++;
170
171    data->deltams = data->timer.GetMs();
172    data->bias += data->deltams;
173
174    /* If shutdown is stuck, kick the first entity we meet and see
175     * whether it makes things better. Note that it is always a bug to
176     * have referenced entities after 20 frames, but at least this
177     * safeguard makes it possible to exit the program cleanly. */
178    if (data->quit && !((data->frame - data->quitframe) % data->quitdelay))
179    {
180        int n = 0;
181        data->panic = 2 * (data->panic + 1);
182
183        for (int i = 0; i < Entity::ALLGROUP_END && n < data->panic; i++)
184        for (Entity *e = data->list[i]; e && n < data->panic; e = e->gamenext)
185            if (e->ref)
186            {
187#if !FINAL_RELEASE
188                fprintf(stderr, "ERROR: poking %s\n", e->GetName());
189#endif
190                e->ref--;
191                n++;
192            }
193
194#if !FINAL_RELEASE
195        if (n)
196            fprintf(stderr, "ERROR: %i entities stuck after %i frames, "
197                    "poked %i\n", data->nentities, data->quitdelay, n);
198#endif
199
200        data->quitdelay = data->quitdelay > 1 ? data->quitdelay / 2 : 1;
201    }
202
203    /* Garbage collect objects that can be destroyed. We can do this
204     * before inserting awaiting objects, because only objects already in
205     * the tick lists can be marked for destruction. */
206    for (int i = 0; i < Entity::ALLGROUP_END; i++)
207        for (Entity *e = data->list[i], *prev = NULL; e; )
208        {
209            if (e->destroy && i < Entity::GAMEGROUP_END)
210            {
211                /* If entity is to be destroyed, remove it from the
212                 * game tick list. */
213                (prev ? prev->gamenext : data->list[i]) = e->gamenext;
214
215                e = e->gamenext;
216            }
217            else if (e->destroy)
218            {
219                /* If entity is to be destroyed, remove it from the
220                 * draw tick list and destroy it. */
221                (prev ? prev->drawnext : data->list[i]) = e->drawnext;
222
223                Entity *tmp = e;
224                e = e->drawnext; /* Can only be in a draw group list */
225                delete tmp;
226
227                data->nentities--;
228            }
229            else
230            {
231                if (e->ref <= 0 && i >= Entity::DRAWGROUP_BEGIN)
232                    e->destroy = 1;
233                prev = e;
234                e = (i < Entity::GAMEGROUP_END) ? e->gamenext : e->drawnext;
235            }
236        }
237
238    /* Insert waiting objects into the appropriate lists */
239    while (data->todolist)
240    {
241        Entity *e = data->todolist;
242        data->todolist = e->gamenext;
243
244        e->gamenext = data->list[e->gamegroup];
245        data->list[e->gamegroup] = e;
246        e->drawnext = data->list[e->drawgroup];
247        data->list[e->drawgroup] = e;
248    }
249
250    /* Tick objects for the game loop */
251    for (int i = Entity::GAMEGROUP_BEGIN; i < Entity::GAMEGROUP_END; i++)
252        for (Entity *e = data->list[i]; e; e = e->gamenext)
253            if (!e->destroy)
254            {
255#if !FINAL_RELEASE
256                if (e->state != Entity::STATE_IDLE)
257                    fprintf(stderr, "ERROR: entity not idle for game tick\n");
258                e->state = Entity::STATE_PRETICK_GAME;
259#endif
260                e->TickGame(data->deltams);
261#if !FINAL_RELEASE
262                if (e->state != Entity::STATE_POSTTICK_GAME)
263                    fprintf(stderr, "ERROR: entity missed super game tick\n");
264                e->state = Entity::STATE_IDLE;
265#endif
266            }
267
268    Profiler::Stop(Profiler::STAT_TICK_GAME);
269}
270
271void Ticker::TickDraw()
272{
273    Profiler::Start(Profiler::STAT_TICK_DRAW);
274
275    Video::Clear();
276
277    Scene::GetDefault();
278
279    /* Tick objects for the draw loop */
280    for (int i = Entity::DRAWGROUP_BEGIN; i < Entity::DRAWGROUP_END; i++)
281    {
282        switch (i)
283        {
284        case Entity::DRAWGROUP_HUD:
285            Scene::GetDefault()->Render();
286            Video::SetDepth(false);
287            break;
288        default:
289            Video::SetDepth(true);
290            break;
291        }
292
293        for (Entity *e = data->list[i]; e; e = e->drawnext)
294            if (!e->destroy)
295            {
296#if !FINAL_RELEASE
297                if (e->state != Entity::STATE_IDLE)
298                    fprintf(stderr, "ERROR: entity not idle for draw tick\n");
299                e->state = Entity::STATE_PRETICK_DRAW;
300#endif
301                e->TickDraw(data->deltams);
302#if !FINAL_RELEASE
303                if (e->state != Entity::STATE_POSTTICK_DRAW)
304                    fprintf(stderr, "ERROR: entity missed super draw tick\n");
305                e->state = Entity::STATE_IDLE;
306#endif
307            }
308    }
309
310    Scene::Reset();
311
312    Profiler::Stop(Profiler::STAT_TICK_DRAW);
313    Profiler::Start(Profiler::STAT_TICK_BLIT);
314}
315
316void Ticker::ClampFps()
317{
318    Profiler::Stop(Profiler::STAT_TICK_BLIT);
319
320    float deltams = data->fps ? 1000.0f / data->fps : 0.0f;
321
322    if (deltams > data->bias + 200.0f)
323        deltams = data->bias + 200.0f; // Don't go below 5 fps
324    if (deltams > data->bias)
325        data->timer.WaitMs(deltams - data->bias);
326    data->bias -= deltams;
327}
328
329int Ticker::GetFrameNum()
330{
331    return data->frame;
332}
333
334void Ticker::Shutdown()
335{
336    /* We're bailing out. Release all autorelease objects. */
337    while (data->autolist)
338    {
339        data->autolist->ref--;
340        data->autolist = data->autolist->autonext;
341    }
342
343    data->quit = 1;
344    data->quitframe = data->frame;
345}
346
347int Ticker::Finished()
348{
349    return !data->nentities;
350}
351
Note: See TracBrowser for help on using the repository browser.