source: trunk/src/font.cpp @ 328

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

Make Entity variable members private.

  • Property svn:keywords set to Id
File size: 4.4 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    int2 size;
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->size = int2(data->img->w, data->img->h) / 16;
72    data->tx = (float)data->size.x / PotUp(data->img->w);
73    data->ty = (float)data->size.y / PotUp(data->img->h);
74
75    drawgroup = DRAWGROUP_BEFORE;
76}
77
78Font::~Font()
79{
80    delete data;
81}
82
83void Font::TickDraw(float deltams)
84{
85    Entity::TickDraw(deltams);
86
87    if (IsDestroying())
88    {
89        if (data->img)
90            SDL_FreeSurface(data->img);
91        else
92            glDeleteTextures(1, &data->texture);
93    }
94    else if (data->img)
95    {
96        GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
97        int planes = data->img->format->Amask ? 4 : 3;
98
99        int w = PotUp(data->img->w);
100        int h = PotUp(data->img->h);
101
102        uint8_t *pixels = (uint8_t *)data->img->pixels;
103        if (w != data->img->w || h != data->img->h)
104        {
105            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
106            for (int line = 0; line < data->img->h; line++)
107                memcpy(tmp + planes * w * line,
108                       pixels + planes * data->img->w * line,
109                       planes * data->img->w);
110            pixels = tmp;
111        }
112
113        glGenTextures(1, &data->texture);
114        glBindTexture(GL_TEXTURE_2D, data->texture);
115
116        glTexImage2D(GL_TEXTURE_2D, 0, planes, w, h, 0,
117                     format, GL_UNSIGNED_BYTE, pixels);
118
119        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
120        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
121
122        if (pixels != data->img->pixels)
123            free(pixels);
124        SDL_FreeSurface(data->img);
125        data->img = NULL;
126    }
127}
128
129char const *Font::GetName()
130{
131    return data->name;
132}
133
134void Font::Print(int3 pos, char const *str)
135{
136    if (data->img)
137        return;
138
139    glBindTexture(GL_TEXTURE_2D, data->texture);
140    glBegin(GL_QUADS);
141    while (*str)
142    {
143        uint32_t ch = (uint8_t)*str++;
144        float tx = data->tx * (ch & 0xf);
145        float ty = data->ty * ((ch >> 4) & 0xf);
146
147        if (ch != ' ')
148        {
149            glTexCoord2f(tx, ty + data->ty);
150            glVertex2f(pos.x, pos.y);
151            glTexCoord2f(tx + data->tx, ty + data->ty);
152            glVertex2f(pos.x + data->size.x, pos.y);
153            glTexCoord2f(tx + data->tx, ty);
154            glVertex2f(pos.x + data->size.x, pos.y + data->size.y);
155            glTexCoord2f(tx, ty);
156            glVertex2f(pos.x, pos.y + data->size.y);
157        }
158
159        pos.x += data->size.x;
160    }
161    glEnd();
162}
163
164void Font::PrintBold(int3 pos, char const *str)
165{
166    static struct { int dx, dy; float r, g, b; } tab[] =
167    {
168        { -1,  0, 0.0, 0.0, 0.0 },
169        {  0, -1, 0.0, 0.0, 0.0 },
170        {  0,  1, 0.0, 0.0, 0.0 },
171        {  1, -1, 0.0, 0.0, 0.0 },
172        {  1,  1, 0.0, 0.0, 0.0 },
173        {  2,  0, 0.0, 0.0, 0.0 },
174        {  1,  0, 0.5, 0.5, 0.5 },
175        {  0,  0, 1.0, 1.0, 1.0 },
176    };
177
178    if (data->img)
179        return;
180
181    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
182    for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++)
183    {
184        glColor3f(tab[i].r, tab[i].g, tab[i].b);
185        Print(pos + int3(tab[i].dx, tab[i].dy, 0), str);
186    }
187    glPopAttrib();
188}
189
190int2 Font::GetSize() const
191{
192    return data->size;
193}
194
Note: See TracBrowser for help on using the repository browser.