source: trunk/src/font.cpp @ 210

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

Allow for different priorities in game and draw tick levels.

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