Browse Source

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

legacy
Sam Hocevar sam 14 years ago
parent
commit
039de818c3
1 changed files with 35 additions and 20 deletions
  1. +35
    -20
      src/font.cpp

+ 35
- 20
src/font.cpp View File

@@ -35,7 +35,8 @@ private:
char *name; char *name;


SDL_Surface *img; SDL_Surface *img;
GLuint texture[1];
int width, height;
GLuint texture;
}; };


/* /*
@@ -57,22 +58,10 @@ Font::Font(char const *path)
SDL_Quit(); SDL_Quit();
exit(1); exit(1);
} }

glGenTextures(1, data->texture);
glBindTexture(GL_TEXTURE_2D, data->texture[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
} }


Font::~Font() Font::~Font()
{ {
glDeleteTextures(1, data->texture);
SDL_FreeSurface(data->img);

delete data; delete data;
} }


@@ -84,6 +73,29 @@ Entity::Group Font::GetGroup()
void Font::TickDraw(float deltams) void Font::TickDraw(float deltams)
{ {
Entity::TickDraw(deltams); Entity::TickDraw(deltams);

if (data->img)
{
data->width = data->img->w / 16;
data->height = data->img->h / 16;

glGenTextures(1, &data->texture);
glBindTexture(GL_TEXTURE_2D, data->texture);

glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

SDL_FreeSurface(data->img);
data->img = NULL;
}
else if (ref == 0)
{
glDeleteTextures(1, &data->texture);
destroy = 1;
}
} }


char const *Font::GetName() char const *Font::GetName()
@@ -93,10 +105,10 @@ char const *Font::GetName()


void Font::Print(int x, int y, char const *str) void Font::Print(int x, int y, char const *str)
{ {
int w = data->img->w / 16;
int h = data->img->h / 16;
if (data->img)
return;


glBindTexture(GL_TEXTURE_2D, data->texture[0]);
glBindTexture(GL_TEXTURE_2D, data->texture);
glBegin(GL_QUADS); glBegin(GL_QUADS);
while (*str) while (*str)
{ {
@@ -109,14 +121,14 @@ void Font::Print(int x, int y, char const *str)
glTexCoord2f(tx, ty); glTexCoord2f(tx, ty);
glVertex2f(x, y); glVertex2f(x, y);
glTexCoord2f(tx + .0625f, ty); glTexCoord2f(tx + .0625f, ty);
glVertex2f(x + w, y);
glVertex2f(x + data->width, y);
glTexCoord2f(tx + .0625f, ty + .0625f); glTexCoord2f(tx + .0625f, ty + .0625f);
glVertex2f(x + w, y + h);
glVertex2f(x + data->width, y + data->height);
glTexCoord2f(tx, ty + .0625f); glTexCoord2f(tx, ty + .0625f);
glVertex2f(x, y + h);
glVertex2f(x, y + data->height);
} }


x += w;
x += data->width;
} }
glEnd(); glEnd();
} }
@@ -135,6 +147,9 @@ void Font::PrintBold(int x, int y, char const *str)
{ 0, 0, 1.0, 1.0, 1.0 }, { 0, 0, 1.0, 1.0, 1.0 },
}; };


if (data->img)
return;

glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT); glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++) for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++)
{ {


Loading…
Cancel
Save