Changeset 153


Ignore:
Timestamp:
Aug 22, 2010, 2:55:39 AM (10 years ago)
Author:
sam
Message:

Factor Forge and Tiler using the new Dict class.

Location:
trunk/src
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r150 r153  
    55
    66libcommon_a_SOURCES = \
    7     core.h matrix.h game.cpp game.h tiler.cpp tiler.h \
     7    core.h matrix.h game.cpp game.h tiler.cpp tiler.h dict.cpp dict.h \
    88    scene.cpp scene.h font.cpp font.h layer.cpp layer.h map.cpp map.h \
    99    entity.cpp entity.h ticker.cpp ticker.h tileset.cpp tileset.h \
  • trunk/src/core.h

    r150 r153  
    2929
    3030// Other objects
     31#include "dict.h"
    3132#include "map.h"
    3233#include "layer.h"
  • trunk/src/debugfps.cpp

    r150 r153  
    2222
    2323private:
    24     Font *font;
     24    int fontid;
    2525    int frame;
    2626};
     
    3434    data = new DebugFpsData();
    3535
    36     data->font = Forge::GetFont("gfx/font/ascii.png");
     36    data->fontid = Forge::Register("gfx/font/ascii.png");
    3737    data->frame = 0;
    3838}
     
    5050
    5151    char buf[1024];
     52    Font *font = Forge::GetFont(data->fontid);
    5253
    5354    sprintf(buf, "%2.2f fps (%i)",
    5455            1e3f / Profiler::GetMean(Profiler::STAT_TICK_FRAME), data->frame);
    55     data->font->PrintBold(10, 10, buf);
     56    font->PrintBold(10, 10, buf);
    5657
    5758    sprintf(buf, "Game   % 7.2f % 7.2f",
    5859            Profiler::GetMean(Profiler::STAT_TICK_GAME),
    5960            Profiler::GetMax(Profiler::STAT_TICK_GAME));
    60     data->font->PrintBold(10, 34, buf);
     61    font->PrintBold(10, 34, buf);
    6162
    6263    sprintf(buf, "Render % 7.2f % 7.2f",
    6364            Profiler::GetMean(Profiler::STAT_TICK_RENDER),
    6465            Profiler::GetMax(Profiler::STAT_TICK_RENDER));
    65     data->font->PrintBold(10, 50, buf);
     66    font->PrintBold(10, 50, buf);
    6667
    6768    sprintf(buf, "Blit   % 7.2f % 7.2f",
    6869            Profiler::GetMean(Profiler::STAT_TICK_BLIT),
    6970            Profiler::GetMax(Profiler::STAT_TICK_BLIT));
    70     data->font->PrintBold(10, 66, buf);
     71    font->PrintBold(10, 66, buf);
    7172
    7273    sprintf(buf, "Frame  % 7.2f % 7.2f",
    7374            Profiler::GetMean(Profiler::STAT_TICK_FRAME),
    7475            Profiler::GetMax(Profiler::STAT_TICK_FRAME));
    75     data->font->PrintBold(10, 82, buf);
     76    font->PrintBold(10, 82, buf);
    7677}
    7778
    7879DebugFps::~DebugFps()
    7980{
    80     Forge::ReleaseFont(data->font);
     81    Forge::Deregister(data->fontid);
    8182    delete data;
    8283}
  • trunk/src/entity.cpp

    r150 r153  
    3636}
    3737
     38char const *Entity::GetName()
     39{
     40    return "Generic entity";
     41}
     42
    3843Entity::Group Entity::GetGroup()
    3944{
  • trunk/src/entity.h

    r149 r153  
    2121    friend class Ticker;
    2222    friend class TickerData;
     23    friend class Dict;
    2324
    2425public:
     
    4142    virtual ~Entity();
    4243
     44    virtual char const *GetName();
    4345    virtual Group GetGroup();
    4446
  • trunk/src/font.h

    r149 r153  
    2424protected:
    2525    /* Inherited from Entity */
     26    virtual char const *GetName();
    2627    virtual Group GetGroup();
    2728    virtual void TickRender(float deltams);
     
    2930public:
    3031    /* New methods */
    31     char const *GetName();
    32 
    3332    void Print(int x, int y, char const *str);
    3433    void PrintBold(int x, int y, char const *str);
  • trunk/src/forge.cpp

    r150 r153  
    77#   include "config.h"
    88#endif
    9 
    10 #include <cstring>
    11 #include <cstdio>
    12 #include <cstdlib>
    139
    1410#include "core.h"
     
    2723
    2824public:
    29     ForgeData() :
    30         fonts(0),
    31         nfonts(0)
    32     {
    33         /* Nothing to do */
    34     }
    35 
    36     ~ForgeData()
    37     {
    38         if (nfonts)
    39             fprintf(stderr, "ERROR: still %i fonts in forge\n", nfonts);
    40         free(fonts);
    41     }
    42 
    43 private:
    44     Font **fonts;
    45     int nfonts;
     25    Dict fonts;
    4626}
    4727forgedata;
     
    5333 */
    5434
    55 Font *Forge::GetFont(char const *path)
     35int Forge::Register(char const *path)
    5636{
    57     int id, empty = -1;
     37    int id = data->fonts.MakeSlot(path);
    5838
    59     /* If the font is already registered, remember its ID. Look for an
    60      * empty slot at the same time. */
    61     for (id = 0; id < data->nfonts; id++)
     39    if (!data->fonts.GetEntity(id))
    6240    {
    63         Font *t = data->fonts[id];
    64         if (!t)
    65             empty = id;
    66         else if (!strcasecmp(path, t->GetName()))
    67             break;
     41        Font *font = new Font(path);
     42        data->fonts.SetEntity(id, font);
    6843    }
    6944
    70     /* If this is a new font, create a new one. */
    71     if (id == data->nfonts)
    72     {
    73         if (empty == -1)
    74         {
    75             empty = data->nfonts++;
    76             data->fonts = (Font **)realloc(data->fonts,
    77                                            data->nfonts * sizeof(Font *));
    78         }
    79 
    80         data->fonts[empty] = new Font(path);
    81         id = empty;
    82     }
    83 
    84     data->fonts[id]->Ref();
    85     return data->fonts[id];
     45    return id;
    8646}
    8747
    88 void Forge::ReleaseFont(Font *font)
     48void Forge::Deregister(int id)
    8949{
    90     if (font->Unref() == 0)
    91         for (int id = 0; id < data->nfonts; id++)
    92             if (font == data->fonts[id])
    93             {
    94                 data->fonts[id] = NULL;
    95                 break;
    96             }
     50    data->fonts.RemoveSlot(id);
    9751}
    9852
     53Font *Forge::GetFont(int id)
     54{
     55    return (Font *)data->fonts.GetEntity(id);
     56}
     57
  • trunk/src/forge.h

    r111 r153  
    1818{
    1919public:
    20     static Font *GetFont(char const *path);
    21     static void ReleaseFont(Font *font);
     20    static int Register(char const *path);
     21    static void Deregister(int id);
     22    static Font *GetFont(int id);
    2223};
    2324
  • trunk/src/tiler.cpp

    r150 r153  
    77#   include "config.h"
    88#endif
    9 
    10 #include <cstring>
    11 #include <cstdio>
    12 #include <cstdlib>
    139
    1410#include "core.h"
     
    2723
    2824public:
    29     TilerData() :
    30         tilesets(0),
    31         ntilesets(0)
    32     {
    33         /* Nothing to do */
    34     }
    35 
    36     ~TilerData()
    37     {
    38         if (ntilesets)
    39             fprintf(stderr, "ERROR: still %i tilesets in tiler\n", ntilesets);
    40         free(tilesets);
    41     }
    42 
    43 private:
    44     TileSet **tilesets;
    45     int ntilesets;
     25    Dict tilesets;
    4626}
    4727tilerdata;
     
    5535int Tiler::Register(char const *path)
    5636{
    57     int id, empty = -1;
     37    int id = data->tilesets.MakeSlot(path);
    5838
    59     /* If the tileset is already registered, remember its ID. Look for an
    60      * empty slot at the same time. */
    61     for (id = 0; id < data->ntilesets; id++)
     39    if (!data->tilesets.GetEntity(id))
    6240    {
    63         TileSet *t = data->tilesets[id];
    64         if (!t)
    65             empty = id;
    66         else if (!strcasecmp(path, t->GetName()))
    67             break;
     41        TileSet *tileset = new TileSet(path);
     42        data->tilesets.SetEntity(id, tileset);
    6843    }
    6944
    70     /* If this is a new tileset, create a new one. */
    71     if (id == data->ntilesets)
    72     {
    73         if (empty == -1)
    74         {
    75             empty = data->ntilesets++;
    76             data->tilesets = (TileSet **)realloc(data->tilesets,
    77                                          data->ntilesets * sizeof(TileSet *));
    78         }
    79 
    80         data->tilesets[empty] = new TileSet(path);
    81         id = empty;
    82     }
    83 
    84     data->tilesets[id]->Ref();
    8545    return id + 1; /* ID 0 is for the empty tileset */
    8646}
     
    8848void Tiler::Deregister(int id)
    8949{
    90     --id; /* ID 0 is for the empty tileset */
    91 
    92     if (data->tilesets[id]->Unref() == 0)
    93         data->tilesets[id] = NULL;
     50    data->tilesets.RemoveSlot(id - 1); /* ID 0 is for the empty tileset */
    9451}
    9552
     
    9855    int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */
    9956
    100     data->tilesets[id]->BlitTile(code & 0xffff, x, y, z, o);
     57    TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id);
     58    tileset->BlitTile(code & 0xffff, x, y, z, o);
    10159}
    10260
  • trunk/src/tileset.h

    r149 r153  
    2929protected:
    3030    /* Inherited from Entity */
     31    virtual char const *GetName();
    3132    virtual Group GetGroup();
    3233    virtual void TickRender(float deltams);
     
    3435public:
    3536    /* New methods */
    36     char const *GetName();
    37 
    3837    void BlitTile(uint32_t id, int x, int y, int z, int o);
    3938
Note: See TracChangeset for help on using the changeset viewer.