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

Last change on this file since 548 was 548, checked in by Sam Hocevar, 14 years ago
  • Split event.c into the appropriate driver_*.c files.
  • Property svn:keywords set to Id
File size: 11.3 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 548 2006-03-08 09:28:41Z 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
295static unsigned int gl_get_event(caca_t *kk)
296{
297    unsigned int event = 0;
298
299    glutMainLoopEvent();
300
301    if(kk->gl.resized && !kk->resize)
302    {
303        kk->resize = 1;
304        kk->gl.resized = 0;
305        return CACA_EVENT_RESIZE;
306    }
307
308    if(kk->gl.mouse_changed)
309    {
310        if(kk->gl.mouse_clicked)
311        {
312            event |= CACA_EVENT_MOUSE_PRESS | kk->gl.mouse_button;
313            kk->gl.mouse_clicked = 0;
314        }
315        kk->mouse_x = kk->gl.mouse_x;
316        kk->mouse_y = kk->gl.mouse_y;
317        event |= CACA_EVENT_MOUSE_MOTION | (kk->mouse_x << 12) | kk->mouse_y;
318        kk->gl.mouse_changed = 0;
319    }
320
321    if(kk->gl.key != 0)
322    {
323        event |= CACA_EVENT_KEY_PRESS;
324        event |= kk->gl.key;
325        kk->gl.key = 0;
326        return event;
327    }
328
329    if(kk->gl.special_key != 0)
330    {
331        event |= CACA_EVENT_KEY_PRESS;
332
333        switch(kk->gl.special_key)
334        {
335            case GLUT_KEY_F1 : kk->gl.special_key = 0; return event | CACA_KEY_F1;
336            case GLUT_KEY_F2 : kk->gl.special_key = 0; return event | CACA_KEY_F2;
337            case GLUT_KEY_F3 : kk->gl.special_key = 0; return event | CACA_KEY_F3;
338            case GLUT_KEY_F4 : kk->gl.special_key = 0; return event | CACA_KEY_F4;
339            case GLUT_KEY_F5 : kk->gl.special_key = 0; return event | CACA_KEY_F5;
340            case GLUT_KEY_F6 : kk->gl.special_key = 0; return event | CACA_KEY_F6;
341            case GLUT_KEY_F7 : kk->gl.special_key = 0; return event | CACA_KEY_F7;
342            case GLUT_KEY_F8 : kk->gl.special_key = 0; return event | CACA_KEY_F8;
343            case GLUT_KEY_F9 : kk->gl.special_key = 0; return event | CACA_KEY_F9;
344            case GLUT_KEY_F10: kk->gl.special_key = 0; return event | CACA_KEY_F10;
345            case GLUT_KEY_F11: kk->gl.special_key = 0; return event | CACA_KEY_F11;
346            case GLUT_KEY_F12: kk->gl.special_key = 0; return event | CACA_KEY_F12;
347            case GLUT_KEY_LEFT : kk->gl.special_key = 0; return event | CACA_KEY_LEFT;
348            case GLUT_KEY_RIGHT: kk->gl.special_key = 0; return event | CACA_KEY_RIGHT;
349            case GLUT_KEY_UP   : kk->gl.special_key = 0; return event | CACA_KEY_UP;
350            case GLUT_KEY_DOWN : kk->gl.special_key = 0; return event | CACA_KEY_DOWN;
351            default: return CACA_EVENT_NONE;
352        }
353    }
354    return CACA_EVENT_NONE;
355}
356
357/*
358 * XXX: following functions are local
359 */
360
361static void gl_handle_keyboard(unsigned char key, int x, int y)
362{
363    caca_t *kk = gl_kk;
364
365    kk->gl.key = key;
366}
367
368static void gl_handle_special_key(int key, int x, int y)
369{
370    caca_t *kk = gl_kk;
371
372    kk->gl.special_key = key;
373}
374
375static void gl_handle_reshape(int w, int h)
376{
377    caca_t *kk = gl_kk;
378
379    if(kk->gl.bit) /* Do not handle reshaping at the first time */
380    {
381        kk->gl.new_width = w;
382        kk->gl.new_height = h;
383
384        kk->gl.resized = 1;
385    }
386    else
387        kk->gl.bit = 1;
388}
389
390static void gl_handle_mouse(int button, int state, int x, int y)
391{
392    caca_t *kk = gl_kk;
393
394    kk->gl.mouse_clicked = 1;
395    kk->gl.mouse_button = button;
396    kk->gl.mouse_state = state;
397    kk->gl.mouse_x = x / kk->gl.font_width;
398    kk->gl.mouse_y = y / kk->gl.font_height;
399    kk->gl.mouse_changed = 1;
400}
401
402static void gl_handle_mouse_motion(int x, int y)
403{
404    caca_t *kk = gl_kk;
405
406    kk->gl.mouse_x = x / kk->gl.font_width;
407    kk->gl.mouse_y = y / kk->gl.font_height;
408    kk->gl.mouse_changed = 1;
409}
410
411/*
412 * Driver initialisation
413 */
414
415void gl_init_driver(caca_t *kk)
416{
417    kk->driver.driver = CACA_DRIVER_GL;
418
419    kk->driver.init_graphics = gl_init_graphics;
420    kk->driver.end_graphics = gl_end_graphics;
421    kk->driver.set_window_title = gl_set_window_title;
422    kk->driver.get_window_width = gl_get_window_width;
423    kk->driver.get_window_height = gl_get_window_height;
424    kk->driver.display = gl_display;
425    kk->driver.handle_resize = gl_handle_resize;
426    kk->driver.get_event = gl_get_event;
427}
428
429#endif /* USE_GL */
430
Note: See TracBrowser for help on using the repository browser.