source: libcaca/trunk/caca/driver_gl.c @ 539

Last change on this file since 539 was 539, checked in by Sam Hocevar, 15 years ago
  • Split caca/graphics.c into driver-specific files. Resize is currently broken, and event handling is not yet in the driver-specific files, but I will of course fix that later.
  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
1/*
2 *  libcaca       ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file graphics.c
13 *  \version \$Id: driver_gl.c 539 2006-03-06 23:01:59Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Character drawing
16 *
17 *  This file contains character and string drawing functions.
18 */
19
20#include "config.h"
21
22#if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
23#   include <inttypes.h>
24#else
25typedef unsigned int uint32_t;
26typedef unsigned char uint8_t;
27#endif
28
29#if defined(USE_GL)
30
31#include <GL/gl.h>
32#include <GL/glut.h>
33#include <GL/freeglut_ext.h>
34
35#include <stdio.h> /* BUFSIZ */
36#include <string.h>
37#include <stdlib.h>
38#if defined(HAVE_UNISTD_H)
39#   include <unistd.h>
40#endif
41#include <stdarg.h>
42
43#include "caca.h"
44#include "caca_internals.h"
45#include "cucul.h"
46#include "cucul_internals.h"
47
48/*
49 * Global variables
50 */
51
52/* Ok, I just suck. */
53static GLbyte const gl_bgpal[][4] =
54{
55    { 0x00, 0x00, 0x00, 0x7f },
56    { 0x00, 0x00, 0x3f, 0x7f },
57    { 0x00, 0x3f, 0x00, 0x7f },
58    { 0x00, 0x3f, 0x3f, 0x7f },
59    { 0x3f, 0x00, 0x00, 0x7f },
60    { 0x3f, 0x00, 0x3f, 0x7f },
61    { 0x3f, 0x3f, 0x00, 0x7f },
62    { 0x3f, 0x3f, 0x3f, 0x7f },
63    // + intensity
64    { 0x00, 0x00, 0x00, 0x7f },
65    { 0x00, 0x00, 0x7f, 0x7f },
66    { 0x00, 0x7f, 0x00, 0x7f },
67    { 0x00, 0x7f, 0x7f, 0x7f },
68    { 0x7f, 0x00, 0x00, 0x7f },
69    { 0x7f, 0x00, 0x7f, 0x7f },
70    { 0x7f, 0x7f, 0x00, 0x7f },
71    { 0x7f, 0x7f, 0x7f, 0x7f }
72};
73
74static caca_t *gl_kk; /* FIXME: we ought to get rid of this */
75
76/*
77 * Local functions
78 */
79static void gl_handle_keyboard(unsigned char, int, int);
80static void gl_handle_special_key(int, int, int);
81static void gl_handle_reshape(int, int);
82static void gl_handle_mouse(int, int, int, int);
83static void gl_handle_mouse_motion(int, int);
84
85int gl_init_graphics(caca_t *kk)
86{
87    char *empty_texture;
88    char const *geometry;
89    char *argv[2] = { "", NULL };
90    unsigned int width = 0, height = 0;
91    int argc = 1;
92    int i;
93
94    gl_kk = kk;
95
96    geometry = getenv("CACA_GEOMETRY");
97    if(geometry && *(geometry))
98        sscanf(geometry, "%ux%u", &width, &height);
99
100    if(width && height)
101        cucul_set_size(kk->qq, width, height);
102
103    kk->gl.font_width = 9;
104    kk->gl.font_height = 15;
105
106    kk->gl.width = kk->qq->width * kk->gl.font_width;
107    kk->gl.height = kk->qq->height * kk->gl.font_height;
108
109    kk->gl.resized = 0;
110    kk->gl.bit = 0;
111
112    kk->gl.mouse_changed = kk->gl.mouse_clicked = 0;
113    kk->gl.mouse_button = kk->gl.mouse_state = 0;
114
115    kk->gl.key = 0;
116    kk->gl.special_key = 0;
117
118    kk->gl.sw = 9.0f / 16.0f;
119    kk->gl.sh = 15.0f / 16.0f;
120
121    glutInit(&argc, argv);
122
123    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
124    glutInitWindowSize(kk->gl.width, kk->gl.height);
125    kk->gl.window = glutCreateWindow("caca for GL");
126
127    gluOrtho2D(0, kk->gl.width, kk->gl.height, 0);
128
129    glDisable(GL_CULL_FACE);
130    glDisable(GL_DEPTH_TEST);
131
132    glutKeyboardFunc(gl_handle_keyboard);
133    glutSpecialFunc(gl_handle_special_key);
134    glutReshapeFunc(gl_handle_reshape);
135
136    glutMouseFunc(gl_handle_mouse);
137    glutMotionFunc(gl_handle_mouse_motion);
138    glutPassiveMotionFunc(gl_handle_mouse_motion);
139
140    glLoadIdentity();
141
142    glMatrixMode(GL_PROJECTION);
143    glPushMatrix();
144    glLoadIdentity();
145    gluOrtho2D(0, kk->gl.width, kk->gl.height, 0);
146
147    glMatrixMode(GL_MODELVIEW);
148
149    glClear(GL_COLOR_BUFFER_BIT);
150
151    empty_texture = malloc(16 * 16 * 4);
152    if(empty_texture == NULL)
153        return -1;
154
155    memset(empty_texture, 0xff, 16 * 16 * 4);
156    glEnable(GL_TEXTURE_2D);
157
158    for(i = 0; i < 94; i++)
159    {
160        glGenTextures(1, (GLuint*)&kk->gl.id[i]);
161        glBindTexture(GL_TEXTURE_2D, kk->gl.id[i]);
162        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
163        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
164        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,
165                     16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture);
166    }
167
168    for(i = 0; i < 94; i++)
169    {
170        glDisable(GL_TEXTURE_2D);
171        glClear(GL_COLOR_BUFFER_BIT);
172
173        glColor3f(1, 1, 1);
174        glRasterPos2f(0, 15);
175        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i + 32);
176
177        glEnable(GL_TEXTURE_2D);
178        glBindTexture(GL_TEXTURE_2D, kk->gl.id[i]);
179        glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
180                         0, kk->gl.height - 16, 16, 16, 0);
181
182        glutMainLoopEvent();
183        glutPostRedisplay();
184    }
185
186    return 0;
187}
188
189int gl_end_graphics(caca_t *kk)
190{
191    glutDestroyWindow(kk->gl.window);
192    return 0;
193}
194
195int gl_set_window_title(caca_t *kk, char const *title)
196{
197    glutSetWindowTitle(title);
198    return 0;
199}
200
201unsigned int gl_get_window_width(caca_t *kk)
202{
203    return kk->gl.width;
204}
205
206unsigned int gl_get_window_height(caca_t *kk)
207{
208    return kk->gl.height;
209}
210
211void gl_display(caca_t *kk)
212{
213    unsigned int x, y, line;
214
215    glClear(GL_COLOR_BUFFER_BIT);
216
217    line = 0;
218    for(y = 0; y < kk->gl.height; y += kk->gl.font_height)
219    {
220        uint8_t *attr = kk->qq->attr + line * kk->qq->width;
221
222        for(x = 0; x < kk->gl.width; x += kk->gl.font_width)
223        {
224            glDisable(GL_TEXTURE_2D);
225            glColor4bv(gl_bgpal[attr[0] >> 4]);
226            glBegin(GL_QUADS);
227                glVertex2f(x, y);
228                glVertex2f(x + kk->gl.font_width, y);
229                glVertex2f(x + kk->gl.font_width, y + kk->gl.font_height);
230                glVertex2f(x, y + kk->gl.font_height);
231            glEnd();
232
233            attr++;
234        }
235
236        line++;
237    }
238
239    /* 2nd pass, avoids changing render state too much */
240    glEnable(GL_BLEND);
241    glEnable(GL_TEXTURE_2D);
242    glBlendFunc(GL_ONE, GL_ONE);
243
244    line = 0;
245    for(y = 0; y < kk->gl.height; y += kk->gl.font_height)
246    {
247        uint8_t *attr = kk->qq->attr + line * kk->qq->width;
248        uint32_t *chars = kk->qq->chars + line * kk->qq->width;
249
250        for(x = 0; x < kk->gl.width; x += kk->gl.font_width)
251        {
252            if(*chars != (uint32_t)' ')
253            {
254                char ch = *chars & 0x7f;
255
256                /* FIXME: check ch bounds */
257                glBindTexture(GL_TEXTURE_2D, kk->gl.id[ch - 32]);
258                glColor4bv(gl_bgpal[attr[0] & 0xf]);
259                glBegin(GL_QUADS);
260                    glTexCoord2f(0, kk->gl.sh);
261                    glVertex2f(x, y);
262                    glTexCoord2f(kk->gl.sw, kk->gl.sh);
263                    glVertex2f(x + kk->gl.font_width, y);
264                    glTexCoord2f(kk->gl.sw, 0);
265                    glVertex2f(x + kk->gl.font_width, y + kk->gl.font_height);
266                    glTexCoord2f(0, 0);
267                    glVertex2f(x, y + kk->gl.font_height);
268                glEnd();
269            }
270
271            attr++;
272            chars++;
273        }
274        line++;
275    }
276    glDisable(GL_BLEND);
277    glDisable(GL_TEXTURE_2D);
278
279    glutMainLoopEvent();
280    glutSwapBuffers();
281    glutPostRedisplay();
282}
283
284void gl_handle_resize(caca_t *kk)
285{
286    unsigned int new_width, new_height;
287
288    new_width = kk->qq->width;
289    new_height = kk->qq->height;
290
291    kk->gl.width = kk->gl.new_width;
292    kk->gl.height = kk->gl.new_height;
293
294    new_width = kk->gl.width / kk->gl.font_width;
295    new_height = (kk->gl.height / kk->gl.font_height) + 1;
296
297    glMatrixMode(GL_PROJECTION);
298    glPushMatrix();
299    glLoadIdentity();
300
301    glViewport(0, 0, kk->gl.width, kk->gl.height);
302    gluOrtho2D(0, kk->gl.width, kk->gl.height, 0);
303    glMatrixMode(GL_MODELVIEW);
304}
305
306/*
307 * XXX: following functions are local
308 */
309
310static void gl_handle_keyboard(unsigned char key, int x, int y)
311{
312    caca_t *kk = gl_kk;
313
314    kk->gl.key = key;
315}
316
317static void gl_handle_special_key(int key, int x, int y)
318{
319    caca_t *kk = gl_kk;
320
321    kk->gl.special_key = key;
322}
323
324static void gl_handle_reshape(int w, int h)
325{
326    caca_t *kk = gl_kk;
327
328    if(kk->gl.bit) /* Do not handle reshaping at the first time */
329    {
330        kk->gl.new_width = w;
331        kk->gl.new_height = h;
332
333        kk->gl.resized = 1;
334    }
335    else
336        kk->gl.bit = 1;
337}
338
339static void gl_handle_mouse(int button, int state, int x, int y)
340{
341    caca_t *kk = gl_kk;
342
343    kk->gl.mouse_clicked = 1;
344    kk->gl.mouse_button = button;
345    kk->gl.mouse_state = state;
346    kk->gl.mouse_x = x / kk->gl.font_width;
347    kk->gl.mouse_y = y / kk->gl.font_height;
348    kk->gl.mouse_changed = 1;
349}
350
351static void gl_handle_mouse_motion(int x, int y)
352{
353    caca_t *kk = gl_kk;
354
355    kk->gl.mouse_x = x / kk->gl.font_width;
356    kk->gl.mouse_y = y / kk->gl.font_height;
357    kk->gl.mouse_changed = 1;
358}
359
360/*
361 * Driver initialisation
362 */
363
364void gl_init_driver(caca_t *kk)
365{
366    kk->driver.driver = CACA_DRIVER_GL;
367
368    kk->driver.init_graphics = gl_init_graphics;
369    kk->driver.end_graphics = gl_end_graphics;
370    kk->driver.set_window_title = gl_set_window_title;
371    kk->driver.get_window_width = gl_get_window_width;
372    kk->driver.get_window_height = gl_get_window_height;
373    kk->driver.display = gl_display;
374    kk->driver.handle_resize = gl_handle_resize;
375}
376
377#endif /* USE_GL */
378
Note: See TracBrowser for help on using the repository browser.