Changeset 2609


Ignore:
Timestamp:
Mar 28, 2013, 1:38:55 AM (7 years ago)
Author:
sam
Message:

tileset: refactor the tile generation code.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/scene.cpp

    r2558 r2609  
    273273            data->m_tiles[i].tileset->Bind();
    274274            data->m_tile_shader->SetUniform(uni_texsize,
    275                                    data->m_tiles[i].tileset->GetImageSize());
     275                           (vec2)data->m_tiles[i].tileset->GetTextureSize());
    276276
    277277            /* Bind vertex and texture coordinate buffers */
  • trunk/src/tileset.cpp

    r2603 r2609  
    5151private:
    5252    String m_name;
    53     int ntiles;
    54     ivec2 m_tile_size, m_image_size, count;
    55     vec2 m_texcoord;
    56 
    57     Image *m_img;
     53
     54    /* Pixels, then texture coordinates */
     55    Array<ibox2, box2> m_tiles;
     56    ivec2 m_tile_size, m_image_size, m_texture_size;
     57
     58    Image *m_image;
    5859    Texture *m_texture;
    5960};
     
    6364 */
    6465
     66TileSet::TileSet(char const *path)
     67  : m_data(new TileSetData())
     68{
     69    Init(path);
     70
     71    m_drawgroup = DRAWGROUP_BEFORE;
     72}
     73
    6574TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
    6675  : m_data(new TileSetData())
    6776{
    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();
    73 
     77    Init(path);
     78
     79    /* If count is valid, fix size; otherwise, fix count. */
    7480    if (count.x > 0 && count.y > 0)
    7581    {
    76         m_data->count = count;
    77         m_data->m_tile_size = m_data->m_image_size / count;
     82        size = m_data->m_image_size / count;
    7883    }
    7984    else
     
    8186        if (size.x <= 0 || size.y <= 0)
    8287            size = ivec2(32, 32);
    83         m_data->count.x = m_data->m_image_size.x / min(size.x, m_data->m_image_size.x);
    84         m_data->count.y = m_data->m_image_size.y / min(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;
     88        count = ivec2(max(1, m_data->m_image_size.x / size.x),
     89                      max(1, m_data->m_image_size.y / size.y));
     90    }
     91
     92    for (int j = 0; j < count.y; ++j)
     93    for (int i = 0; i < count.x; ++i)
     94    {
     95        AddTile(ibox2(size * ivec2(i, j),
     96                      size * ivec2(i + 1, j + 1)));
     97    }
    9298
    9399    m_drawgroup = DRAWGROUP_BEFORE;
    94100}
    95101
     102void TileSet::Init(char const *path)
     103{
     104    m_data->m_name = String("<tileset> ") + path;
     105
     106    m_data->m_texture = 0;
     107    m_data->m_image = Image::Create(path);
     108    m_data->m_image_size = m_data->m_image->GetSize();
     109    m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x),
     110                                   PotUp(m_data->m_image_size.y));
     111}
     112
     113int TileSet::AddTile(ibox2 rect)
     114{
     115    m_data->m_tiles.Push(rect,
     116                         box2((vec2)rect.A / (vec2)m_data->m_texture_size,
     117                              (vec2)rect.B / (vec2)m_data->m_texture_size));
     118    return m_data->m_tiles.Count() - 1;
     119}
     120
    96121TileSet::~TileSet()
    97122{
     
    105130    if (IsDestroying())
    106131    {
    107         if (m_data->m_img)
     132        if (m_data->m_image)
    108133        {
    109             Image::Destroy(m_data->m_img);
    110             m_data->m_img = nullptr;
     134            Image::Destroy(m_data->m_image);
     135            m_data->m_image = nullptr;
    111136        }
    112137        else
     
    116141        }
    117142    }
    118     else if (m_data->m_img)
     143    else if (m_data->m_image)
    119144    {
    120145        int planes;
    121         PixelFormat format = m_data->m_img->GetFormat();
     146        PixelFormat format = m_data->m_image->GetFormat();
    122147
    123148        switch (format)
     
    134159        }
    135160
    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();
     161        int w = m_data->m_texture_size.x;
     162        int h = m_data->m_texture_size.y;
     163
     164        uint8_t *pixels = m_data->m_image->GetData();
    140165        if (w != m_data->m_image_size.x || h != m_data->m_image_size.y)
    141166        {
     
    151176        m_data->m_texture->SetData(pixels);
    152177
    153         if (pixels != m_data->m_img->GetData())
     178        if (pixels != m_data->m_image->GetData())
    154179            delete[] pixels;
    155         Image::Destroy(m_data->m_img);
    156         m_data->m_img = nullptr;
     180        Image::Destroy(m_data->m_image);
     181        m_data->m_image = nullptr;
    157182    }
    158183}
     
    163188}
    164189
    165 ivec2 TileSet::GetTileCount() const
    166 {
    167     return m_data->count;
     190int TileSet::GetTileCount() const
     191{
     192    return m_data->m_tiles.Count();
    168193}
    169194
     
    172197    (void)tileid;
    173198
    174     return m_data->m_tile_size;
    175 }
    176 
    177 vec2 TileSet::GetImageSize() const
    178 {
    179     return vec2(PotUp(m_data->m_image_size.x),
    180                 PotUp(m_data->m_image_size.y));
     199    ibox2 const &box = m_data->m_tiles[tileid].m1;
     200    return box.B - box.A;
     201}
     202
     203ivec2 TileSet::GetTextureSize() const
     204{
     205    return m_data->m_texture_size;
    181206}
    182207
     
    188213void TileSet::Bind()
    189214{
    190     if (!m_data->m_img && m_data->m_texture)
     215    if (!m_data->m_image && m_data->m_texture)
    191216        m_data->m_texture->Bind();
    192217}
     
    200225                       float *vertex, float *texture)
    201226{
    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;
     227    ibox2 pixels = m_data->m_tiles[id].m1;
     228    box2 texels = m_data->m_tiles[id].m2;
     229    float dtx = texels.B.x - texels.A.x;
     230    float dty = texels.B.y - texels.A.y;
     231    float tx = texels.A.x;
     232    float ty = texels.A.y;
     233
     234    int dx = (pixels.B.x - pixels.A.x) * scale.x;
     235    int dy = o ? 0 : (pixels.B.y - pixels.A.y) * scale.y;
     236    int dz = o ? (pixels.B.y - pixels.A.y) * scale.y : 0;
    210237
    211238    /* If scaling is negative, switch triangle winding */
     
    227254#endif
    228255
    229     if (!m_data->m_img && m_data->m_texture)
     256    if (!m_data->m_image && m_data->m_texture)
    230257    {
    231258        float tmp[10];
  • trunk/src/tileset.h

    r2588 r2609  
    4545public:
    4646    /* New methods */
    47     ivec2 GetTileCount() const;
     47    int GetTileCount() const;
    4848    ivec2 GetTileSize(int tileid) const;
    49     vec2 GetImageSize() const;
     49    ivec2 GetTextureSize() const;
    5050    ShaderTexture GetTexture() const;
    5151    void Bind();
     
    5555
    5656private:
     57    void Init(char const *path);
     58    int AddTile(ibox2 rect);
     59
    5760    TileSetData *m_data;
    5861};
Note: See TracChangeset for help on using the changeset viewer.