Changeset 2588


Ignore:
Timestamp:
Mar 13, 2013, 5:33:22 PM (7 years ago)
Author:
sam
Message:

base: some refactoring in the tileset class.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/font.cpp

    r2546 r2588  
    4747
    4848    data->tileset = Tiler::Register(path, ivec2(0), ivec2(16));
    49     data->size = data->tileset->GetSize(0);
     49    data->size = data->tileset->GetTileSize(0);
    5050
    5151    m_drawgroup = DRAWGROUP_BEFORE;
  • trunk/src/tileset.cpp

    r2567 r2588  
    5151private:
    5252    String m_name;
    53     int *tiles, ntiles;
    54     ivec2 size, isize, count;
    55     vec2 scale;
    56     float tx, ty;
    57 
    58     Image *img;
     53    int ntiles;
     54    ivec2 m_tile_size, m_image_size, count;
     55    vec2 m_texcoord;
     56
     57    Image *m_img;
    5958    Texture *m_texture;
    6059};
     
    6564
    6665TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
    67   : data(new TileSetData())
    68 {
    69     data->m_name = String("<tileset> ") + path;
    70 
    71     data->tiles = nullptr;
    72     data->m_texture = 0;
    73     data->img = Image::Create(path);
    74     data->isize = data->img->GetSize();
     66  : m_data(new TileSetData())
     67{
     68    m_data->m_name = String("<tileset> ") + path;
     69
     70    m_data->m_texture = 0;
     71    m_data->m_img = Image::Create(path);
     72    m_data->m_image_size = m_data->m_img->GetSize();
    7573
    7674    if (count.x > 0 && count.y > 0)
    7775    {
    78         data->count = count;
    79         data->size = data->isize / count;
     76        m_data->count = count;
     77        m_data->m_tile_size = m_data->m_image_size / count;
    8078    }
    8179    else
     
    8381        if (size.x <= 0 || size.y <= 0)
    8482            size = ivec2(32, 32);
    85         data->count.x = data->isize.x > size.x ? data->isize.x / size.x : 1;
    86         data->count.y = data->isize.y > size.y ? data->isize.y / size.y : 1;
    87         data->size = size;
    88     }
    89 
    90     data->tx = (float)data->size.x / PotUp(data->isize.x);
    91     data->ty = (float)data->size.y / PotUp(data->isize.y);
    92 
    93     data->ntiles = data->count.x * data->count.y;
     83        m_data->count.x = m_data->m_image_size.x / max(size.x, m_data->m_image_size.x);
     84        m_data->count.y = m_data->m_image_size.y / max(size.y, m_data->m_image_size.y);
     85        m_data->m_tile_size = size;
     86    }
     87
     88    m_data->m_texcoord.x = (float)m_data->m_tile_size.x / PotUp(m_data->m_image_size.x);
     89    m_data->m_texcoord.y = (float)m_data->m_tile_size.y / PotUp(m_data->m_image_size.y);
     90
     91    m_data->ntiles = m_data->count.x * m_data->count.y;
    9492
    9593    m_drawgroup = DRAWGROUP_BEFORE;
     
    9896TileSet::~TileSet()
    9997{
    100     free(data->tiles);
    101     delete data;
     98    delete m_data;
    10299}
    103100
     
    108105    if (IsDestroying())
    109106    {
    110         if (data->img)
    111         {
    112             Image::Destroy(data->img);
    113             data->img = nullptr;
     107        if (m_data->m_img)
     108        {
     109            Image::Destroy(m_data->m_img);
     110            m_data->m_img = nullptr;
    114111        }
    115112        else
    116113        {
    117             delete data->m_texture;
    118             data->m_texture = nullptr;
    119         }
    120     }
    121     else if (data->img)
     114            delete m_data->m_texture;
     115            m_data->m_texture = nullptr;
     116        }
     117    }
     118    else if (m_data->m_img)
    122119    {
    123120        int planes;
    124         PixelFormat format = data->img->GetFormat();
     121        PixelFormat format = m_data->m_img->GetFormat();
    125122
    126123        switch (format)
     
    137134        }
    138135
    139         int w = PotUp(data->isize.x);
    140         int h = PotUp(data->isize.y);
    141 
    142         uint8_t *pixels = data->img->GetData();
    143         if (w != data->isize.x || h != data->isize.y)
     136        int w = PotUp(m_data->m_image_size.x);
     137        int h = PotUp(m_data->m_image_size.y);
     138
     139        uint8_t *pixels = m_data->m_img->GetData();
     140        if (w != m_data->m_image_size.x || h != m_data->m_image_size.y)
    144141        {
    145142            uint8_t *tmp = new uint8_t[planes * w * h];
    146             for (int line = 0; line < data->isize.y; line++)
     143            for (int line = 0; line < m_data->m_image_size.y; line++)
    147144                memcpy(tmp + planes * w * line,
    148                        pixels + planes * data->isize.x * line,
    149                        planes * data->isize.x);
     145                       pixels + planes * m_data->m_image_size.x * line,
     146                       planes * m_data->m_image_size.x);
    150147            pixels = tmp;
    151148        }
    152149
    153         data->m_texture = new Texture(ivec2(w, h), format);
    154         data->m_texture->SetData(pixels);
    155 
    156         if (pixels != data->img->GetData())
     150        m_data->m_texture = new Texture(ivec2(w, h), format);
     151        m_data->m_texture->SetData(pixels);
     152
     153        if (pixels != m_data->m_img->GetData())
    157154            delete[] pixels;
    158         Image::Destroy(data->img);
    159         data->img = nullptr;
     155        Image::Destroy(m_data->m_img);
     156        m_data->m_img = nullptr;
    160157    }
    161158}
     
    163160char const *TileSet::GetName()
    164161{
    165     return data->m_name.C();
    166 }
    167 
    168 ivec2 TileSet::GetCount() const
    169 {
    170     return data->count;
    171 }
    172 
    173 ivec2 TileSet::GetSize(int tileid) const
     162    return m_data->m_name.C();
     163}
     164
     165ivec2 TileSet::GetTileCount() const
     166{
     167    return m_data->count;
     168}
     169
     170ivec2 TileSet::GetTileSize(int tileid) const
    174171{
    175172    (void)tileid;
    176173
    177     return data->size;
     174    return m_data->m_tile_size;
    178175}
    179176
    180177vec2 TileSet::GetImageSize() const
    181178{
    182     return vec2(PotUp(data->isize.x), PotUp(data->isize.y));
    183 }
    184 
    185 vec2 TileSet::GetTileSize() const
    186 {
    187     return vec2(data->tx, data->ty);
     179    return vec2(PotUp(m_data->m_image_size.x),
     180                PotUp(m_data->m_image_size.y));
    188181}
    189182
    190183ShaderTexture TileSet::GetTexture() const
    191184{
    192     return data->m_texture->GetTexture();
     185    return m_data->m_texture->GetTexture();
    193186}
    194187
    195188void TileSet::Bind()
    196189{
    197     if (!data->img && data->m_texture)
    198         data->m_texture->Bind();
     190    if (!m_data->m_img && m_data->m_texture)
     191        m_data->m_texture->Bind();
    199192}
    200193
     
    207200                       float *vertex, float *texture)
    208201{
    209     float dtx = data->tx;
    210     float dty = data->ty;
    211     float tx = dtx * ((id & 0xffff) % data->count.x);
    212     float ty = dty * ((id & 0xffff) / data->count.x);
    213 
    214     int dx = data->size.x * scale.x;
    215     int dy = o ? 0 : data->size.y * scale.y;
    216     int dz = o ? data->size.y * scale.y : 0;
     202    float dtx = m_data->m_texcoord.x;
     203    float dty = m_data->m_texcoord.y;
     204    float tx = dtx * ((id & 0xffff) % m_data->count.x);
     205    float ty = dty * ((id & 0xffff) / m_data->count.x);
     206
     207    int dx = m_data->m_tile_size.x * scale.x;
     208    int dy = o ? 0 : m_data->m_tile_size.y * scale.y;
     209    int dz = o ? m_data->m_tile_size.y * scale.y : 0;
    217210
    218211    /* If scaling is negative, switch triangle winding */
     
    234227#endif
    235228
    236     if (!data->img && data->m_texture)
     229    if (!m_data->m_img && m_data->m_texture)
    237230    {
    238231        float tmp[10];
  • trunk/src/tileset.h

    r2558 r2588  
    3232{
    3333public:
     34    TileSet(char const *path);
     35    virtual ~TileSet();
     36
     37    /* Old style: path to PNG file */
    3438    TileSet(char const *path, ivec2 size, ivec2 count);
    35     virtual ~TileSet();
    3639
    3740protected:
     
    4245public:
    4346    /* New methods */
    44     ivec2 GetCount() const;
    45     ivec2 GetSize(int tileid) const;
     47    ivec2 GetTileCount() const;
     48    ivec2 GetTileSize(int tileid) const;
    4649    vec2 GetImageSize() const;
    47     vec2 GetTileSize() const;
    4850    ShaderTexture GetTexture() const;
    4951    void Bind();
     
    5355
    5456private:
    55     TileSetData *data;
     57    TileSetData *m_data;
    5658};
    5759
Note: See TracChangeset for help on using the changeset viewer.