Ignore:
Timestamp:
Jul 7, 2010, 2:11:16 AM (10 years ago)
Author:
sam
Message:

Much better resource allocation and release. Scene manager stub.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tileset.cpp

    r74 r75  
    2020
    2121/*
    22  * Tileset implementation class
     22 * TileSet implementation class
    2323 */
    2424
    25 class TilesetData
    26 {
    27     friend class Tileset;
     25class TileSetData
     26{
     27    friend class TileSet;
    2828
    2929private:
     
    3636    }
    3737
     38    char *name;
     39    int ref;
    3840    int *tiles;
    3941    int ntiles;
     
    4446
    4547/*
    46  * Public Tileset class
     48 * Public TileSet class
    4749 */
    4850
    49 Tileset::Tileset()
    50 {
    51     data = new TilesetData();
     51TileSet::TileSet(char const *path)
     52{
     53    SDL_Surface *img = NULL;
     54
     55    data = new TileSetData();
     56    data->name = strdup(path);
     57    data->ref = 0;
    5258    data->tiles = NULL;
    5359    data->ntiles = 0;
    5460
    5561    /* One tile texture */
    56     SDL_Surface *img = IMG_Load("art/test/grasstest.png");
     62    for (char const *name = path; *name; name++)
     63        if ((img = IMG_Load(name)))
     64            break;
    5765
    5866    if (!img)
     
    6270    }
    6371
    64     glGenTextures(1, &data->texture[0]);
     72    glGenTextures(1, data->texture);
    6573    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
    6674
     
    7583}
    7684
    77 Tileset::~Tileset()
    78 {
     85TileSet::~TileSet()
     86{
     87    glDeleteTextures(1, data->texture);
     88    glDeleteBuffers(3, data->buflist);
     89
    7990    free(data->tiles);
     91    free(data->name);
    8092    delete data;
    8193}
    8294
    83 void Tileset::AddTile(int n, int x, int y, int z)
     95void TileSet::Ref()
     96{
     97    data->ref++;
     98}
     99
     100int TileSet::Unref()
     101{
     102    return --data->ref;
     103}
     104
     105char const *TileSet::GetName()
     106{
     107    return data->name;
     108}
     109
     110void TileSet::AddTile(int n, int x, int y, int z)
    84111{
    85112    if ((data->ntiles % 1024) == 0)
     
    97124}
    98125
    99 void Tileset::Render()
     126void TileSet::Render()
    100127{
    101128    /* Sort tiles */
    102     qsort(data->tiles, data->ntiles, 4 * sizeof(int), TilesetData::Compare);
     129    qsort(data->tiles, data->ntiles, 4 * sizeof(int), TileSetData::Compare);
    103130
    104131    /* Texture coord buffer */
Note: See TracChangeset for help on using the changeset viewer.