source: trunk/src/font.cpp @ 156

Last change on this file since 156 was 156, checked in by sam, 11 years ago

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

  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#ifdef WIN32
11#   define WIN32_LEAN_AND_MEAN
12#   include <windows.h>
13#endif
14#if defined __APPLE__ && defined __MACH__
15#   include <OpenGL/gl.h>
16#else
17#   define GL_GLEXT_PROTOTYPES
18#   include <GL/gl.h>
19#endif
20
21#include <SDL.h>
22#include <SDL_image.h>
23
24#include "core.h"
25
26/*
27 * Font implementation class
28 */
29
30class FontData
31{
32    friend class Font;
33
34private:
35    char *name;
36
37    SDL_Surface *img;
38    int width, height;
39    GLuint texture;
40};
41
42/*
43 * Public Font class
44 */
45
46Font::Font(char const *path)
47{
48    data = new FontData();
49    data->name = strdup(path);
50    data->img = NULL;
51
52    for (char const *name = path; *name; name++)
53        if ((data->img = IMG_Load(name)))
54            break;
55
56    if (!data->img)
57    {
58        SDL_Quit();
59        exit(1);
60    }
61}
62
63Font::~Font()
64{
65    delete data;
66}
67
68Entity::Group Font::GetGroup()
69{
70    return GROUP_BEFORE;
71}
72
73void Font::TickDraw(float deltams)
74{
75    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    }
99}
100
101char const *Font::GetName()
102{
103    return data->name;
104}
105
106void Font::Print(int x, int y, char const *str)
107{
108    if (data->img)
109        return;
110
111    glBindTexture(GL_TEXTURE_2D, data->texture);
112    glBegin(GL_QUADS);
113    while (*str)
114    {
115        uint32_t ch = (uint8_t)*str++;
116        float tx = .0625f * (ch & 0xf);
117        float ty = .0625f * ((ch >> 4) & 0xf);
118
119        if (ch != ' ')
120        {
121            glTexCoord2f(tx, ty);
122            glVertex2f(x, y);
123            glTexCoord2f(tx + .0625f, ty);
124            glVertex2f(x + data->width, y);
125            glTexCoord2f(tx + .0625f, ty + .0625f);
126            glVertex2f(x + data->width, y + data->height);
127            glTexCoord2f(tx, ty + .0625f);
128            glVertex2f(x, y + data->height);
129        }
130
131        x += data->width;
132    }
133    glEnd();
134}
135
136void Font::PrintBold(int x, int y, char const *str)
137{
138    static struct { int dx, dy; float r, g, b; } tab[] =
139    {
140        { -1,  0, 0.0, 0.0, 0.0 },
141        {  0, -1, 0.0, 0.0, 0.0 },
142        {  0,  1, 0.0, 0.0, 0.0 },
143        {  1, -1, 0.0, 0.0, 0.0 },
144        {  1,  1, 0.0, 0.0, 0.0 },
145        {  2,  0, 0.0, 0.0, 0.0 },
146        {  1,  0, 0.5, 0.5, 0.5 },
147        {  0,  0, 1.0, 1.0, 1.0 },
148    };
149
150    if (data->img)
151        return;
152
153    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
154    for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++)
155    {
156        glColor3f(tab[i].r, tab[i].g, tab[i].b);
157        Print(x + tab[i].dx, y + tab[i].dy, str);
158    }
159    glPopAttrib();
160}
161
Note: See TracBrowser for help on using the repository browser.