source: trunk/src/font.cpp @ 318

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

Fix a texture size bug in the font initialisation.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#ifdef WIN32
16#   define WIN32_LEAN_AND_MEAN
17#   include <windows.h>
18#endif
19#if defined __APPLE__ && defined __MACH__
20#   include <OpenGL/gl.h>
21#else
22#   define GL_GLEXT_PROTOTYPES
23#   include <GL/gl.h>
24#endif
25
26#include <SDL.h>
27#include <SDL_image.h>
28
29#include "core.h"
30
31/*
32 * Font implementation class
33 */
34
35class FontData
36{
37    friend class Font;
38
39private:
40    char *name;
41
42    SDL_Surface *img;
43    int width, height;
44    float tx, ty;
45    GLuint texture;
46};
47
48/*
49 * Public Font class
50 */
51
52Font::Font(char const *path)
53  : data(new FontData())
54{
55    data->name = strdup(path);
56    data->img = NULL;
57
58    for (char const *name = path; *name; name++)
59        if ((data->img = IMG_Load(name)))
60            break;
61
62    if (!data->img)
63    {
64#if !FINAL_RELEASE
65        fprintf(stderr, "ERROR: could not load %s\n", path);
66#endif
67        SDL_Quit();
68        exit(1);
69    }
70
71    data->width = data->img->w / 16;
72    data->height = data->img->h / 16;
73    data->tx = (float)data->width / PotUp(data->img->w);
74    data->ty = (float)data->height / PotUp(data->img->h);
75
76    drawgroup = DRAWGROUP_BEFORE;
77}
78
79Font::~Font()
80{
81    delete data;
82}
83
84void Font::TickDraw(float deltams)
85{
86    Entity::TickDraw(deltams);
87
88    if (destroy)
89    {
90        if (data->img)
91            SDL_FreeSurface(data->img);
92        else
93            glDeleteTextures(1, &data->texture);
94    }
95    else if (data->img)
96    {
97        GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
98        int planes = data->img->format->Amask ? 4 : 3;
99
100        int w = PotUp(data->img->w);
101        int h = PotUp(data->img->h);
102
103        uint8_t *pixels = (uint8_t *)data->img->pixels;
104        if (w != data->img->w || h != data->img->h)
105        {
106            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
107            for (int line = 0; line < data->img->h; line++)
108                memcpy(tmp + planes * w * line,
109                       pixels + planes * data->img->w * line,
110                       planes * data->img->w);
111            pixels = tmp;
112        }
113
114        glGenTextures(1, &data->texture);
115        glBindTexture(GL_TEXTURE_2D, data->texture);
116
117        glTexImage2D(GL_TEXTURE_2D, 0, planes, w, h, 0,
118                     format, GL_UNSIGNED_BYTE, pixels);
119
120        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
121        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
122
123        if (pixels != data->img->pixels)
124            free(pixels);
125        SDL_FreeSurface(data->img);
126        data->img = NULL;
127    }
128}
129
130char const *Font::GetName()
131{
132    return data->name;
133}
134
135void Font::Print(int x, int y, char const *str)
136{
137    if (data->img)
138        return;
139
140    glBindTexture(GL_TEXTURE_2D, data->texture);
141    glBegin(GL_QUADS);
142    while (*str)
143    {
144        uint32_t ch = (uint8_t)*str++;
145        float tx = data->tx * (ch & 0xf);
146        float ty = data->ty * ((ch >> 4) & 0xf);
147
148        if (ch != ' ')
149        {
150            glTexCoord2f(tx, ty + data->ty);
151            glVertex2f(x, y);
152            glTexCoord2f(tx + data->tx, ty + data->ty);
153            glVertex2f(x + data->width, y);
154            glTexCoord2f(tx + data->tx, ty);
155            glVertex2f(x + data->width, y + data->height);
156            glTexCoord2f(tx, ty);
157            glVertex2f(x, y + data->height);
158        }
159
160        x += data->width;
161    }
162    glEnd();
163}
164
165void Font::PrintBold(int x, int y, char const *str)
166{
167    static struct { int dx, dy; float r, g, b; } tab[] =
168    {
169        { -1,  0, 0.0, 0.0, 0.0 },
170        {  0, -1, 0.0, 0.0, 0.0 },
171        {  0,  1, 0.0, 0.0, 0.0 },
172        {  1, -1, 0.0, 0.0, 0.0 },
173        {  1,  1, 0.0, 0.0, 0.0 },
174        {  2,  0, 0.0, 0.0, 0.0 },
175        {  1,  0, 0.5, 0.5, 0.5 },
176        {  0,  0, 1.0, 1.0, 1.0 },
177    };
178
179    if (data->img)
180        return;
181
182    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
183    for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++)
184    {
185        glColor3f(tab[i].r, tab[i].g, tab[i].b);
186        Print(x + tab[i].dx, y + tab[i].dy, str);
187    }
188    glPopAttrib();
189}
190
Note: See TracBrowser for help on using the repository browser.