source: trunk/src/font.cpp @ 170

Last change on this file since 170 was 170, checked in by sam, 10 years ago

Properly implement program termination, including in the GTK program.

  • 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 (destroy)
78    {
79        if (data->img)
80            SDL_FreeSurface(data->img);
81        else
82            glDeleteTextures(1, &data->texture);
83    }
84    else if (data->img)
85    {
86        data->width = data->img->w / 16;
87        data->height = data->img->h / 16;
88
89        glGenTextures(1, &data->texture);
90        glBindTexture(GL_TEXTURE_2D, data->texture);
91
92        glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
93                     GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
94
95        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
96        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
97
98        SDL_FreeSurface(data->img);
99        data->img = NULL;
100    }
101}
102
103char const *Font::GetName()
104{
105    return data->name;
106}
107
108void Font::Print(int x, int y, char const *str)
109{
110    if (data->img)
111        return;
112
113    glBindTexture(GL_TEXTURE_2D, data->texture);
114    glBegin(GL_QUADS);
115    while (*str)
116    {
117        uint32_t ch = (uint8_t)*str++;
118        float tx = .0625f * (ch & 0xf);
119        float ty = .0625f * ((ch >> 4) & 0xf);
120
121        if (ch != ' ')
122        {
123            glTexCoord2f(tx, ty);
124            glVertex2f(x, y);
125            glTexCoord2f(tx + .0625f, ty);
126            glVertex2f(x + data->width, y);
127            glTexCoord2f(tx + .0625f, ty + .0625f);
128            glVertex2f(x + data->width, y + data->height);
129            glTexCoord2f(tx, ty + .0625f);
130            glVertex2f(x, y + data->height);
131        }
132
133        x += data->width;
134    }
135    glEnd();
136}
137
138void Font::PrintBold(int x, int y, char const *str)
139{
140    static struct { int dx, dy; float r, g, b; } tab[] =
141    {
142        { -1,  0, 0.0, 0.0, 0.0 },
143        {  0, -1, 0.0, 0.0, 0.0 },
144        {  0,  1, 0.0, 0.0, 0.0 },
145        {  1, -1, 0.0, 0.0, 0.0 },
146        {  1,  1, 0.0, 0.0, 0.0 },
147        {  2,  0, 0.0, 0.0, 0.0 },
148        {  1,  0, 0.5, 0.5, 0.5 },
149        {  0,  0, 1.0, 1.0, 1.0 },
150    };
151
152    if (data->img)
153        return;
154
155    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
156    for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++)
157    {
158        glColor3f(tab[i].r, tab[i].g, tab[i].b);
159        Print(x + tab[i].dx, y + tab[i].dy, str);
160    }
161    glPopAttrib();
162}
163
Note: See TracBrowser for help on using the repository browser.