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

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