source: trunk/src/font.cpp @ 100

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

Slightly improve the documentation in a few files, add the missing
joystick class, and put a header in each source file.

  • Property svn:keywords set to Id
File size: 1.9 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 "font.h"
25
26/*
27 * Font implementation class
28 */
29
30class FontData
31{
32    friend class Font;
33
34private:
35    SDL_Surface *img;
36    GLuint texture[1];
37};
38
39/*
40 * Public Font class
41 */
42
43Font::Font(char const *path)
44{
45    data = new FontData();
46    data->img = NULL;
47
48    for (char const *name = path; *name; name++)
49        if ((data->img = IMG_Load(name)))
50            break;
51
52    if (!data->img)
53    {
54        SDL_Quit();
55        exit(1);
56    }
57
58    glGenTextures(1, data->texture);
59    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
60
61    glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
62                 GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
63
64    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
65    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
66}
67
68Font::~Font()
69{
70    glDeleteTextures(1, data->texture);
71    SDL_FreeSurface(data->img);
72
73    delete data;
74}
75
76void Font::Print(int x, int y, char const *str)
77{
78    int w = data->img->w / 16;
79    int h = data->img->h / 16;
80
81    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
82    glBegin(GL_QUADS);
83    while (*str)
84    {
85        uint32_t ch = (uint8_t)*str++;
86        float tx = .0625f * (ch & 0xf);
87        float ty = .0625f * ((ch >> 4) & 0xf);
88
89        glTexCoord2f(tx, ty);
90        glVertex2f(x, y);
91        glTexCoord2f(tx + .0625f, ty);
92        glVertex2f(x + w, y);
93        glTexCoord2f(tx + .0625f, ty + .0625f);
94        glVertex2f(x + w, y + h);
95        glTexCoord2f(tx, ty + .0625f);
96        glVertex2f(x, y + h);
97
98        x += w;
99    }
100    glEnd();
101}
102
Note: See TracBrowser for help on using the repository browser.