Changeset 2817


Ignore:
Timestamp:
Jul 24, 2013, 9:43:56 PM (4 years ago)
Author:
sam
Message:

ticker: replace linked lists wih dynamic arrays for entity groups.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/entity.cpp

    r2816 r2817  
    2525
    2626Entity::Entity() :
    27     m_gamenext(0),
    28     m_drawnext(0),
     27    m_initstate(InitState::Ready),
    2928    m_ref(0),
    3029    m_destroy(0)
     
    4948{
    5049    return "<entity>";
     50}
     51
     52void Entity::InitGame()
     53{
     54}
     55
     56void Entity::InitDraw()
     57{
    5158}
    5259
  • trunk/src/entity.h

    r2816 r2817  
    2525{
    2626
     27struct InitState
     28{
     29    enum Value
     30    {
     31        Unknown,
     32        Error,
     33        NeedInitDraw,
     34        NeedInitGame,
     35        Ready,
     36    }
     37    m_value;
     38
     39    inline InitState(Value v) : m_value(v) {}
     40    inline operator Value() { return m_value; }
     41};
     42
    2743class Entity
    2844{
     
    4056
    4157    inline int IsDestroying() { return m_destroy; }
     58
     59    virtual void InitGame();
     60    virtual void InitDraw();
    4261
    4362    virtual void TickGame(float seconds);
     
    7796    static int const ALLGROUP_END = DRAWGROUP_END;
    7897
     98    /* The initialisation state */
     99    InitState m_initstate;
     100
    79101#if !LOL_BUILD_RELEASE
    80102    enum
     
    103125
    104126private:
    105     Entity *m_gamenext, *m_drawnext, *m_autonext;
    106127    int m_ref, m_autorelease, m_destroy;
    107128};
  • trunk/src/ticker.cpp

    r2816 r2817  
    3131public:
    3232    TickerData() :
    33         todolist(0), autolist(0),
    3433        nentities(0),
    3534        frame(0), recording(0), deltatime(0), bias(0), fps(0),
     
    3938        quit(0), quitframe(0), quitdelay(20), panic(0)
    4039    {
    41         for (int i = 0; i < Entity::ALLGROUP_END; i++)
    42             list[i] = nullptr;
    4340    }
    4441
     
    4744        ASSERT(nentities == 0,
    4845               "still %i entities in ticker\n", nentities);
    49 #if !LOL_BUILD_RELEASE
    50         if (autolist)
    51         {
    52             int count = 0;
    53             for (Entity *e = autolist; e; e = e->m_autonext, count++)
    54                 ;
    55             ASSERT(count == 0, "still %i autoreleased entities\n", count);
    56         }
    57 #endif
    58         Log::Debug("%i frames required to quit\n",
    59                 frame - quitframe);
     46        ASSERT(m_autolist.Count() == 0,
     47               "still %i autoreleased entities\n", m_autolist.Count());
     48        Log::Debug("%i frames required to quit\n", frame - quitframe);
    6049
    6150#if LOL_FEATURE_THREADS
     
    6958private:
    7059    /* Entity management */
    71     Entity *todolist, *autolist;
    72     Entity *list[Entity::ALLGROUP_END];
     60    Array<Entity *> m_todolist, m_autolist;
     61    Array<Entity *> m_list[Entity::ALLGROUP_END];
    7362    int nentities;
    7463
     
    111100     * ready yet, so we do not know which group this entity belongs to. Wait
    112101     * until the first tick. */
    113     entity->m_gamenext = data->todolist;
    114     data->todolist = entity;
    115     /* Objects are autoreleased by default. Put them in a circular list. */
     102    data->m_todolist.Push(entity);
     103
     104    /* Objects are autoreleased by default. Put them in a list. */
     105    data->m_autolist.Push(entity);
    116106    entity->m_autorelease = 1;
    117     entity->m_autonext = data->autolist;
    118     data->autolist = entity;
    119107    entity->m_ref = 1;
    120108
     
    132120    {
    133121        /* Get the entity out of the m_autorelease list. This is usually
    134          * very fast since the first entry in autolist is the last
     122         * very fast since the last entry in autolist is the last
    135123         * registered entity. */
    136         for (Entity *e = data->autolist, *prev = nullptr; e;
    137              prev = e, e = e->m_autonext)
    138         {
    139             if (e == entity)
    140             {
    141                 (prev ? prev->m_autonext : data->autolist) = e->m_autonext;
     124        for (int i = data->m_autolist.Count(); --i; )
     125        {
     126            if (data->m_autolist[i] == entity)
     127            {
     128                data->m_autolist.RemoveSwap(i);
    142129                break;
    143130            }
     
    233220#if 0
    234221    Log::Debug("-------------------------------------\n");
    235     for (int i = 0; i < Entity::ALLGROUP_END; i++)
     222    for (int g = 0; g < Entity::ALLGROUP_END; ++g)
    236223    {
    237224        Log::Debug("%s Group %i\n",
    238                    (i < Entity::GAMEGROUP_END) ? "Game" : "Draw", i);
    239 
    240         for (Entity *e = data->list[i]; e; )
    241         {
     225                   (g < Entity::GAMEGROUP_END) ? "Game" : "Draw", g);
     226
     227        for (int i = 0; i < data->m_list[g].Count(); ++i)
     228        {
     229            Entity *e = data->m_list[g][i];
    242230            Log::Debug("  \\-- %s (m_ref %i, destroy %i)\n", e->GetName(), e->m_ref, e->m_destroy);
    243             e = (i < Entity::GAMEGROUP_END) ? e->m_gamenext : e->m_drawnext;
    244231        }
    245232    }
     
    287274        data->panic = 2 * (data->panic + 1);
    288275
    289         for (int i = 0; i < Entity::ALLGROUP_END && n < data->panic; i++)
    290         for (Entity *e = data->list[i]; e && n < data->panic; e = e->m_gamenext)
     276        for (int g = 0; g < Entity::ALLGROUP_END && n < data->panic; ++g)
     277        for (int i = 0; i < data->m_list[g].Count() && n < data->panic; ++i)
     278        {
     279            Entity * e = data->m_list[g][i];
    291280            if (e->m_ref)
    292281            {
     
    297286                n++;
    298287            }
     288        }
    299289
    300290#if !LOL_BUILD_RELEASE
     
    310300     * before inserting awaiting objects, because only objects already
    311301     * inthe tick lists can be marked for destruction. */
    312     for (int i = 0; i < Entity::ALLGROUP_END; i++)
    313         for (Entity *e = data->list[i], *prev = nullptr; e; )
    314         {
    315             if (e->m_destroy && i < Entity::GAMEGROUP_END)
     302    for (int g = 0; g < Entity::ALLGROUP_END; ++g)
     303    {
     304        for (int i = 0; i < data->m_list[g].Count(); ++i)
     305        {
     306            Entity *e = data->m_list[g][i];
     307
     308            if (e->m_destroy && g < Entity::GAMEGROUP_END)
    316309            {
    317310                /* If entity is to be destroyed, remove it from the
    318311                 * game tick list. */
    319                 (prev ? prev->m_gamenext : data->list[i]) = e->m_gamenext;
    320 
    321                 e = e->m_gamenext;
     312                data->m_list[g].RemoveSwap(i);
    322313            }
    323314            else if (e->m_destroy)
     
    325316                /* If entity is to be destroyed, remove it from the
    326317                 * draw tick list and destroy it. */
    327                 (prev ? prev->m_drawnext : data->list[i]) = e->m_drawnext;
    328 
    329                 Entity *tmp = e;
    330                 e = e->m_drawnext; /* Can only be in a draw group list */
    331                 delete tmp;
     318                data->m_list[g].RemoveSwap(i);
     319                delete e;
    332320
    333321                data->nentities--;
     
    335323            else
    336324            {
    337                 if (e->m_ref <= 0 && i >= Entity::DRAWGROUP_BEGIN)
     325                if (e->m_ref <= 0 && g >= Entity::DRAWGROUP_BEGIN)
    338326                    e->m_destroy = 1;
    339                 prev = e;
    340                 e = (i < Entity::GAMEGROUP_END) ? e->m_gamenext : e->m_drawnext;
    341             }
    342         }
     327            }
     328        }
     329    }
    343330
    344331    /* Insert waiting objects into the appropriate lists */
    345     while (data->todolist)
    346     {
    347         Entity *e = data->todolist;
    348         data->todolist = e->m_gamenext;
    349 
    350         e->m_gamenext = data->list[e->m_gamegroup];
    351         data->list[e->m_gamegroup] = e;
    352         e->m_drawnext = data->list[e->m_drawgroup];
    353         data->list[e->m_drawgroup] = e;
     332    while (data->m_todolist.Count())
     333    {
     334        Entity *e = data->m_todolist.Last();
     335
     336        data->m_todolist.Remove(-1);
     337        data->m_list[e->m_gamegroup].Push(e);
     338        data->m_list[e->m_drawgroup].Push(e);
    354339    }
    355340
    356341    /* Tick objects for the game loop */
    357     for (int i = Entity::GAMEGROUP_BEGIN; i < Entity::GAMEGROUP_END; i++)
    358         for (Entity *e = data->list[i]; e; e = e->m_gamenext)
     342    for (int g = Entity::GAMEGROUP_BEGIN; g < Entity::GAMEGROUP_END; ++g)
     343        for (int i = 0; i < data->m_list[g].Count(); ++i)
     344        {
     345            Entity *e = data->m_list[g][i];
     346
    359347            if (!e->m_destroy)
    360348            {
     
    373361#endif
    374362            }
     363        }
    375364
    376365    Profiler::Stop(Profiler::STAT_TICK_GAME);
     
    382371
    383372    /* Tick objects for the draw loop */
    384     for (int i = Entity::DRAWGROUP_BEGIN; i < Entity::DRAWGROUP_END; i++)
    385     {
    386         switch (i)
     373    for (int g = Entity::DRAWGROUP_BEGIN; g < Entity::DRAWGROUP_END; ++g)
     374    {
     375        switch (g)
    387376        {
    388377        case Entity::DRAWGROUP_BEGIN:
     
    394383        }
    395384
    396         for (Entity *e = data->list[i]; e; e = e->m_drawnext)
     385        for (int i = 0; i < data->m_list[g].Count(); ++i)
     386        {
     387            Entity *e = data->m_list[g][i];
     388
    397389            if (!e->m_destroy)
    398390            {
     
    411403#endif
    412404            }
     405        }
    413406
    414407        /* Do this render step */
     
    503496{
    504497    /* We're bailing out. Release all m_autorelease objects. */
    505     while (data->autolist)
    506     {
    507         data->autolist->m_ref--;
    508         data->autolist = data->autolist->m_autonext;
     498    while (data->m_autolist.Count())
     499    {
     500        data->m_autolist.Last()->m_ref--;
     501        data->m_autolist.Remove(-1);
    509502    }
    510503
  • trunk/tools/neercs/video/render.cpp

    r2816 r2817  
    860860}
    861861
    862 int Render::InitDraw(void)
     862int Render::InitDrawResources(void)
    863863{
    864864    glDepthMask(GL_TRUE);     // do not write z-buffer
     
    956956    InitShaderVar();
    957957    UpdateSize();
    958     InitDraw();
     958    InitDrawResources();
    959959    return true;
    960960}
  • trunk/tools/neercs/video/render.h

    r2816 r2817  
    2323private:
    2424    int CreateGLWindow();
    25     int InitDraw();
     25    int InitDrawResources();
    2626    void ShaderSimple(Framebuffer *fbo_output, int n);
    2727    void TraceQuad();
Note: See TracChangeset for help on using the changeset viewer.