Ignore:
Timestamp:
Aug 11, 2010, 6:18:36 PM (10 years ago)
Author:
sam
Message:

Implement lazy initialisation of TileSet objects.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tileset.cpp

    r95 r96  
    3232
    3333    SDL_Surface *img;
    34     GLuint texture[1];
     34    GLuint texture;
    3535};
    3636
     
    4646    data->ntiles = 0;
    4747    data->img = NULL;
     48    data->texture = 0;
    4849
    4950    for (char const *name = path; *name; name++)
     
    5657        exit(1);
    5758    }
    58 
    59     glGenTextures(1, data->texture);
    60     glBindTexture(GL_TEXTURE_2D, data->texture[0]);
    61 
    62     glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
    63                  GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
    64 
    65     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    66     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    6759}
    6860
    6961TileSet::~TileSet()
    7062{
    71     glDeleteTextures(1, data->texture);
    72 
    7363    free(data->tiles);
    7464    free(data->name);
    7565    delete data;
     66}
     67
     68void TileSet::TickRender(float delta_time)
     69{
     70    Asset::TickRender(delta_time);
     71
     72    if (data->img)
     73    {
     74        glGenTextures(1, &data->texture);
     75        glBindTexture(GL_TEXTURE_2D, data->texture);
     76
     77        glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
     78                     GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
     79
     80        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     81        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     82
     83        SDL_FreeSurface(data->img);
     84        data->img = NULL;
     85    }
     86    else if (ref == 0)
     87    {
     88        glDeleteTextures(1, &data->texture);
     89        destroy = 1;
     90    }
    7691}
    7792
     
    86101    float ty = .0625f * ((id >> 4) & 0xf);
    87102
    88     glBindTexture(GL_TEXTURE_2D, data->texture[0]);
    89     glBegin(GL_QUADS);
    90         glTexCoord2f(tx, ty);
    91         glVertex2f(x, y);
    92         glTexCoord2f(tx + .0625f, ty);
    93         glVertex2f(x + 32, y);
    94         glTexCoord2f(tx + .0625f, ty + .0625f);
    95         glVertex2f(x + 32, y + 32);
    96         glTexCoord2f(tx, ty + .0625f);
    97         glVertex2f(x, y + 32);
    98     glEnd();
     103    if (!data->img)
     104    {
     105        glBindTexture(GL_TEXTURE_2D, data->texture);
     106        glBegin(GL_QUADS);
     107            glTexCoord2f(tx, ty);
     108            glVertex2f(x, y);
     109            glTexCoord2f(tx + .0625f, ty);
     110            glVertex2f(x + 32, y);
     111            glTexCoord2f(tx + .0625f, ty + .0625f);
     112            glVertex2f(x + 32, y + 32);
     113            glTexCoord2f(tx, ty + .0625f);
     114            glVertex2f(x, y + 32);
     115        glEnd();
     116    }
    99117}
    100118
Note: See TracChangeset for help on using the changeset viewer.