source: trunk/src/font.cpp @ 221

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

Complete Lol Engine / Deus Hax / Monsterz split.

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