source: trunk/src/ticker.cpp @ 620

Last change on this file since 620 was 620, checked in by sam, 11 years ago

Get rid of benchmarking mode, it's useless: Ticker::Setup(0) does the same.

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