summaryrefslogtreecommitdiff
path: root/src/sdl-opengl-test.c
blob: c483dd403dc60e5ea20faea6f52453e584a48ce5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <SDL.h>
#include <GL/gl.h>

#include <sdl-freetype.h>
#include <sdl-freetype-opengl.h>
#include <sdl-ft.h>

#include <stdio.h>

#define TEXT "sdl freetype OpenGL render"

int
main(int argc, char *argv[])
{
    SDL_Surface * screen;
    Uint8 video_bpp;
    Uint32 videoflags;
    int done;
    SDL_Event event;
    float alpha, step;
    sdl_freetype_font_t * font;
    sdl_freetype_glyph_render_t * render;
    sdl_freetype_text_extents_t extents;

    /* Initialize SDL */
    if (SDL_Init (SDL_INIT_VIDEO) < 0)
        goto errquit0;

    videoflags = SDL_HWSURFACE;
    videoflags |= SDL_OPENGL;
    video_bpp = 32;

    /* Set 640x480 video mode */
    screen = SDL_SetVideoMode (640, 480, video_bpp, videoflags);
    if (!screen)
        goto errquit0;

    font = sdl_ft_create_font ("sans", 20,
                               SDL_FT_WEIGHT_NORMAL, SDL_FT_SLANT_NORMAL, 96);
    if (!font)
        goto errquit0;

    render = sdl_freetype_opengl_render_create ();
    if (!render)
        goto errquit1;
    sdl_freetype_font_set_render (font, render);

    glMatrixMode (GL_PROJECTION) ;
    glLoadIdentity ();
    glOrtho (0, 640, 480, 0, -1.0, 1.0);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();

    glEnable (GL_TEXTURE_2D);
    glClearColor (0.8f, 0.8f, 0.8f, 0.0f);

    alpha = 1.0f;
    step = -0.01;
    /* Wait for a keystroke */
    done = 0;
    while (!done) {
        /* Check for events */
        while (SDL_PollEvent (&event)) {
            switch (event.type) {
            case SDL_MOUSEBUTTONDOWN:
                break;
            case SDL_KEYDOWN:
            case SDL_QUIT:
                done = 1;
                break;
            default:
                break;
            }
        }

        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        sdl_freetype_font_utf8_extents (font, &extents, TEXT, -1);
        glTranslatef ((640 - extents.width) / 2 - extents.x_bearing,
                      (480 - extents.height) / 2 + extents.y_bearing, 0);
        sdl_freetype_font_show_utf8 (font, NULL, 255, 0, 0, alpha * 255,
                                     0, 0, TEXT, -1);

        SDL_GL_SwapBuffers();

        alpha += step;
        if (alpha < 0.0f) {
            alpha = 0.0f;
            step = -step;
        } else if (alpha > 1.0f) {
            alpha = 1.0f;
            step = -step;
        }

        SDL_Delay(20);
    }

 errquit1:
    sdl_freetype_font_destroy (font);
    sdl_freetype_fini ();

 errquit0:
    SDL_Quit();
    return 0;
}