Changeset 156


Ignore:
Timestamp:
Aug 22, 2010, 6:28:24 AM (10 years ago)
Author:
sam
Message:

Fix the Font class so that it does lazy initialisation of the texture.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/font.cpp

    r154 r156  
    3636
    3737    SDL_Surface *img;
    38     GLuint texture[1];
     38    int width, height;
     39    GLuint texture;
    3940};
    4041
     
    5859        exit(1);
    5960    }
    60 
    61     glGenTextures(1, data->texture);
    62     glBindTexture(GL_TEXTURE_2D, data->texture[0]);
    63 
    64     glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
    65                  GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
    66 
    67     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    68     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    6961}
    7062
    7163Font::~Font()
    7264{
    73     glDeleteTextures(1, data->texture);
    74     SDL_FreeSurface(data->img);
    75 
    7665    delete data;
    7766}
     
    8574{
    8675    Entity::TickDraw(deltams);
     76
     77    if (data->img)
     78    {
     79        data->width = data->img->w / 16;
     80        data->height = data->img->h / 16;
     81
     82        glGenTextures(1, &data->texture);
     83        glBindTexture(GL_TEXTURE_2D, data->texture);
     84
     85        glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
     86                     GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
     87
     88        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     89        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     90
     91        SDL_FreeSurface(data->img);
     92        data->img = NULL;
     93    }
     94    else if (ref == 0)
     95    {
     96        glDeleteTextures(1, &data->texture);
     97        destroy = 1;
     98    }
    8799}
    88100
     
    94106void Font::Print(int x, int y, char const *str)
    95107{
    96     int w = data->img->w / 16;
    97     int h = data->img->h / 16;
     108    if (data->img)
     109        return;
    98110
    99     glBindTexture(GL_TEXTURE_2D, data->texture[0]);
     111    glBindTexture(GL_TEXTURE_2D, data->texture);
    100112    glBegin(GL_QUADS);
    101113    while (*str)
     
    110122            glVertex2f(x, y);
    111123            glTexCoord2f(tx + .0625f, ty);
    112             glVertex2f(x + w, y);
     124            glVertex2f(x + data->width, y);
    113125            glTexCoord2f(tx + .0625f, ty + .0625f);
    114             glVertex2f(x + w, y + h);
     126            glVertex2f(x + data->width, y + data->height);
    115127            glTexCoord2f(tx, ty + .0625f);
    116             glVertex2f(x, y + h);
     128            glVertex2f(x, y + data->height);
    117129        }
    118130
    119         x += w;
     131        x += data->width;
    120132    }
    121133    glEnd();
     
    136148    };
    137149
     150    if (data->img)
     151        return;
     152
    138153    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
    139154    for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++)
Note: See TracChangeset for help on using the changeset viewer.