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

Last change on this file since 769 was 769, checked in by Sam Hocevar, 14 years ago
  • Removed \file directives from all files except caca.h and cucul.h, to remove redundencies in the Doxygen documentation.
  • Property svn:keywords set to Id
File size: 12.9 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: driver_gl.c 769 2006-04-14 07:30:53Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains the libcaca OpenGL input and output driver
16 */
17
18#include "config.h"
19
20#if defined(USE_GL)
21
22#ifdef HAVE_OPENGL_GL_H
23#   include <OpenGL/gl.h>
24#   include <GLUT/glut.h>
25#else
26#   include <GL/gl.h>
27#   include <GL/glut.h>
28#   include <GL/freeglut_ext.h>
29#endif
30
31#include <string.h>
32#include <stdlib.h>
33#include <stdio.h>
34
35#include "caca.h"
36#include "caca_internals.h"
37#include "cucul.h"
38#include "cucul_internals.h"
39
40/*
41 * Global variables
42 */
43
44/* Ok, I just suck. */
45static GLbyte const gl_bgpal[][4] =
46{
47    { 0x00, 0x00, 0x00, 0x7f },
48    { 0x00, 0x00, 0x3f, 0x7f },
49    { 0x00, 0x3f, 0x00, 0x7f },
50    { 0x00, 0x3f, 0x3f, 0x7f },
51    { 0x3f, 0x00, 0x00, 0x7f },
52    { 0x3f, 0x00, 0x3f, 0x7f },
53    { 0x3f, 0x3f, 0x00, 0x7f },
54    { 0x3f, 0x3f, 0x3f, 0x7f },
55    // + intensity
56    // >.
57    // ()
58    // ^^
59    { 0x1f, 0x1f, 0x1f, 0x7f },
60    { 0x1f, 0x1f, 0x7f, 0x7f },
61    { 0x1f, 0x7f, 0x1f, 0x7f },
62    { 0x1f, 0x7f, 0x7f, 0x7f },
63    { 0x7f, 0x1f, 0x1f, 0x7f },
64    { 0x7f, 0x1f, 0x7f, 0x7f },
65    { 0x7f, 0x7f, 0x1f, 0x7f },
66    { 0x7f, 0x7f, 0x7f, 0x7f }
67};
68
69static caca_t *gl_kk; /* FIXME: we ought to get rid of this */
70
71/*
72 * Local functions
73 */
74static void gl_handle_keyboard(unsigned char, int, int);
75static void gl_handle_special_key(int, int, int);
76static void gl_handle_reshape(int, int);
77static void gl_handle_mouse(int, int, int, int);
78static void gl_handle_mouse_motion(int, int);
79static void _display(void);
80
81struct driver_private
82{
83    int window;
84    unsigned int width, height;
85    unsigned int new_width, new_height;
86    float font_width, font_height;
87    float incx, incy;
88    int id[128 - 32];
89    unsigned char bit;
90    unsigned char mouse_changed, mouse_clicked;
91    unsigned int mouse_x, mouse_y;
92    unsigned int mouse_button, mouse_state;
93
94    unsigned char key;
95    int special_key;
96
97    float sw, sh;
98};
99
100static int gl_init_graphics(caca_t *kk)
101{
102    char *empty_texture;
103    char const *geometry;
104    char *argv[2] = { "", NULL };
105    unsigned int width = 0, height = 0;
106    int argc = 1;
107    int i;
108
109    kk->drv.p = malloc(sizeof(struct driver_private));
110
111    gl_kk = kk;
112
113#if defined(HAVE_GETENV)
114    geometry = getenv("CACA_GEOMETRY");
115    if(geometry && *geometry)
116        sscanf(geometry, "%ux%u", &width, &height);
117#endif
118
119    if(width && height)
120        _cucul_set_size(kk->qq, width, height);
121
122    kk->drv.p->font_width = 9;
123    kk->drv.p->font_height = 15;
124
125    kk->drv.p->width = kk->qq->width * kk->drv.p->font_width;
126    kk->drv.p->height = kk->qq->height * kk->drv.p->font_height;
127
128    kk->drv.p->bit = 0;
129
130    kk->drv.p->mouse_changed = kk->drv.p->mouse_clicked = 0;
131    kk->drv.p->mouse_button = kk->drv.p->mouse_state = 0;
132
133    kk->drv.p->key = 0;
134    kk->drv.p->special_key = 0;
135
136    kk->drv.p->sw = 9.0f / 16.0f;
137    kk->drv.p->sh = 15.0f / 16.0f;
138
139    glutInit(&argc, argv);
140
141    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
142    glutInitWindowSize(kk->drv.p->width, kk->drv.p->height);
143    kk->drv.p->window = glutCreateWindow("caca for GL");
144
145    gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
146
147    glDisable(GL_CULL_FACE);
148    glDisable(GL_DEPTH_TEST);
149
150    glutKeyboardFunc(gl_handle_keyboard);
151    glutSpecialFunc(gl_handle_special_key);
152    glutReshapeFunc(gl_handle_reshape);
153    glutDisplayFunc(_display);
154
155
156    glutMouseFunc(gl_handle_mouse);
157    glutMotionFunc(gl_handle_mouse_motion);
158    glutPassiveMotionFunc(gl_handle_mouse_motion);
159
160    glLoadIdentity();
161
162    glMatrixMode(GL_PROJECTION);
163    glPushMatrix();
164    glLoadIdentity();
165    gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
166
167    glMatrixMode(GL_MODELVIEW);
168
169    glClear(GL_COLOR_BUFFER_BIT);
170
171    empty_texture = malloc(16 * 16 * 4);
172    if(empty_texture == NULL)
173        return -1;
174
175    memset(empty_texture, 0xff, 16 * 16 * 4);
176    glEnable(GL_TEXTURE_2D);
177
178    for(i = 32; i < 128; i++)
179    {
180        glGenTextures(1, (GLuint*)&kk->drv.p->id[i - 32]);
181        glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[i - 32]);
182        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
183        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
184        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,
185                     16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture);
186    }
187
188    for(i = 32; i < 128; i++)
189    {
190        glDisable(GL_TEXTURE_2D);
191        glClear(GL_COLOR_BUFFER_BIT);
192
193        glColor3f(1, 1, 1);
194        glRasterPos2f(0, 15);
195        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i);
196
197        glEnable(GL_TEXTURE_2D);
198        glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[i - 32]);
199        glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
200                         0, kk->drv.p->height - 16, 16, 16, 0);
201
202#ifdef HAVE_GLUTCHECKLOOP
203        glutCheckLoop();
204#else
205        glutMainLoopEvent();
206#endif
207        glutPostRedisplay();
208    }
209
210    return 0;
211}
212
213static int gl_end_graphics(caca_t *kk)
214{
215    glutDestroyWindow(kk->drv.p->window);
216    free(kk->drv.p);
217    return 0;
218}
219
220static int gl_set_window_title(caca_t *kk, char const *title)
221{
222    glutSetWindowTitle(title);
223    return 0;
224}
225
226static unsigned int gl_get_window_width(caca_t *kk)
227{
228    return kk->drv.p->width;
229}
230
231static unsigned int gl_get_window_height(caca_t *kk)
232{
233    return kk->drv.p->height;
234}
235
236
237static void gl_display(caca_t *kk)
238{
239    unsigned int x, y, line;
240
241    glClear(GL_COLOR_BUFFER_BIT);
242
243    line = 0;
244    for(y = 0; y < kk->drv.p->height; y += kk->drv.p->font_height)
245    {
246        uint32_t *attr = kk->qq->attr + line * kk->qq->width;
247
248        for(x = 0; x < kk->drv.p->width; x += kk->drv.p->font_width)
249        {
250            glDisable(GL_TEXTURE_2D);
251            glColor4bv(gl_bgpal[_cucul_argb32_to_ansi4bg(*attr++)]);
252            glBegin(GL_QUADS);
253                glVertex2f(x, y);
254                glVertex2f(x + kk->drv.p->font_width, y);
255                glVertex2f(x + kk->drv.p->font_width,
256                           y + kk->drv.p->font_height);
257                glVertex2f(x, y + kk->drv.p->font_height);
258            glEnd();
259        }
260
261        line++;
262    }
263
264    /* 2nd pass, avoids changing render state too much */
265    glEnable(GL_BLEND);
266    glEnable(GL_TEXTURE_2D);
267    glBlendFunc(GL_ONE, GL_ONE);
268
269    line = 0;
270    for(y = 0; y < kk->drv.p->height; y += kk->drv.p->font_height)
271    {
272        uint32_t *attr = kk->qq->attr + line * kk->qq->width;
273        uint32_t *chars = kk->qq->chars + line * kk->qq->width;
274
275        for(x = 0; x < kk->drv.p->width; x += kk->drv.p->font_width)
276        {
277            uint32_t c = *chars++;
278
279            if(c > 0x00000020 && c < 0x00000080)
280            {
281                glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[c - 32]);
282                glColor4bv(gl_bgpal[_cucul_argb32_to_ansi4fg(*attr)]);
283                glBegin(GL_QUADS);
284                    glTexCoord2f(0, kk->drv.p->sh);
285                    glVertex2f(x, y);
286                    glTexCoord2f(kk->drv.p->sw, kk->drv.p->sh);
287                    glVertex2f(x + kk->drv.p->font_width, y);
288                    glTexCoord2f(kk->drv.p->sw, 0);
289                    glVertex2f(x + kk->drv.p->font_width,
290                               y + kk->drv.p->font_height);
291                    glTexCoord2f(0, 0);
292                    glVertex2f(x, y + kk->drv.p->font_height);
293                glEnd();
294            }
295
296            attr++;
297        }
298        line++;
299    }
300    glDisable(GL_BLEND);
301    glDisable(GL_TEXTURE_2D);
302
303#ifdef HAVE_GLUTCHECKLOOP
304    glutCheckLoop();
305#else
306    glutMainLoopEvent();
307#endif
308    glutSwapBuffers();
309    glutPostRedisplay();
310}
311
312static void gl_handle_resize(caca_t *kk)
313{
314    kk->drv.p->width = kk->drv.p->new_width;
315    kk->drv.p->height = kk->drv.p->new_height;
316
317    glMatrixMode(GL_PROJECTION);
318    glPushMatrix();
319    glLoadIdentity();
320
321    glViewport(0, 0, kk->drv.p->width, kk->drv.p->height);
322    gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
323    glMatrixMode(GL_MODELVIEW);
324}
325
326static int gl_get_event(caca_t *kk, struct caca_event *ev)
327{
328#ifdef HAVE_GLUTCHECKLOOP
329    glutCheckLoop();
330#else
331    glutMainLoopEvent();
332#endif
333
334    if(kk->resize.resized)
335    {
336        ev->type = CACA_EVENT_RESIZE;
337        ev->data.resize.w = kk->qq->width;
338        ev->data.resize.h = kk->qq->height;
339        return 1;
340    }
341
342    if(kk->drv.p->mouse_changed)
343    {
344        ev->type = CACA_EVENT_MOUSE_MOTION;
345        ev->data.mouse.x = kk->mouse.x;
346        ev->data.mouse.y = kk->mouse.y;
347        kk->drv.p->mouse_changed = 0;
348
349        if(kk->drv.p->mouse_clicked)
350        {
351            _push_event(kk, ev);
352            ev->type = CACA_EVENT_MOUSE_PRESS;
353            ev->data.mouse.button = kk->drv.p->mouse_button;
354            kk->drv.p->mouse_clicked = 0;
355        }
356
357        return 1;
358    }
359
360    if(kk->drv.p->key != 0)
361    {
362        ev->type = CACA_EVENT_KEY_PRESS;
363        ev->data.key.c = kk->drv.p->key;
364        ev->data.key.ucs4 = (uint32_t)kk->drv.p->key;
365        ev->data.key.utf8[0] = kk->drv.p->key;
366        ev->data.key.utf8[1] = '\0';
367        kk->drv.p->key = 0;
368        return 1;
369    }
370
371    if(kk->drv.p->special_key != 0)
372    {
373        switch(kk->drv.p->special_key)
374        {
375            case GLUT_KEY_F1 : ev->data.key.c = CACA_KEY_F1; break;
376            case GLUT_KEY_F2 : ev->data.key.c = CACA_KEY_F2; break;
377            case GLUT_KEY_F3 : ev->data.key.c = CACA_KEY_F3; break;
378            case GLUT_KEY_F4 : ev->data.key.c = CACA_KEY_F4; break;
379            case GLUT_KEY_F5 : ev->data.key.c = CACA_KEY_F5; break;
380            case GLUT_KEY_F6 : ev->data.key.c = CACA_KEY_F6; break;
381            case GLUT_KEY_F7 : ev->data.key.c = CACA_KEY_F7; break;
382            case GLUT_KEY_F8 : ev->data.key.c = CACA_KEY_F8; break;
383            case GLUT_KEY_F9 : ev->data.key.c = CACA_KEY_F9; break;
384            case GLUT_KEY_F10: ev->data.key.c = CACA_KEY_F10; break;
385            case GLUT_KEY_F11: ev->data.key.c = CACA_KEY_F11; break;
386            case GLUT_KEY_F12: ev->data.key.c = CACA_KEY_F12; break;
387            case GLUT_KEY_LEFT : ev->data.key.c = CACA_KEY_LEFT; break;
388            case GLUT_KEY_RIGHT: ev->data.key.c = CACA_KEY_RIGHT; break;
389            case GLUT_KEY_UP   : ev->data.key.c = CACA_KEY_UP; break;
390            case GLUT_KEY_DOWN : ev->data.key.c = CACA_KEY_DOWN; break;
391            default: ev->type = CACA_EVENT_NONE; return 0;
392        }
393
394        ev->type = CACA_EVENT_KEY_PRESS;
395        ev->data.key.ucs4 = 0;
396        ev->data.key.utf8[0] = '\0';
397
398        kk->drv.p->special_key = 0;
399        return 1;
400    }
401
402    ev->type = CACA_EVENT_NONE;
403    return 0;
404}
405
406
407static void gl_set_mouse(caca_t *kk, int flag)
408{
409    if(flag)
410        glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
411    else
412        glutSetCursor(GLUT_CURSOR_NONE);
413}
414
415/*
416 * XXX: following functions are local
417 */
418
419static void gl_handle_keyboard(unsigned char key, int x, int y)
420{
421    caca_t *kk = gl_kk;
422
423    kk->drv.p->key = key;
424}
425
426static void gl_handle_special_key(int key, int x, int y)
427{
428    caca_t *kk = gl_kk;
429
430    kk->drv.p->special_key = key;
431}
432
433static void gl_handle_reshape(int w, int h)
434{
435    caca_t *kk = gl_kk;
436
437    if(kk->drv.p->bit) /* Do not handle reshaping at the first time */
438    {
439        kk->drv.p->new_width = w;
440        kk->drv.p->new_height = h;
441
442        kk->resize.w = w / kk->drv.p->font_width;
443        kk->resize.h = (h / kk->drv.p->font_height) + 1;
444
445        kk->resize.resized = 1;
446    }
447    else
448        kk->drv.p->bit = 1;
449}
450
451static void gl_handle_mouse(int button, int state, int x, int y)
452{
453    caca_t *kk = gl_kk;
454
455    kk->drv.p->mouse_clicked = 1;
456    kk->drv.p->mouse_button = button;
457    kk->drv.p->mouse_state = state;
458    kk->drv.p->mouse_x = x / kk->drv.p->font_width;
459    kk->drv.p->mouse_y = y / kk->drv.p->font_height;
460    kk->mouse.x = kk->drv.p->mouse_x;
461    kk->mouse.y = kk->drv.p->mouse_y;
462    kk->drv.p->mouse_changed = 1;
463}
464
465static void gl_handle_mouse_motion(int x, int y)
466{
467    caca_t *kk = gl_kk;
468    kk->drv.p->mouse_x = x / kk->drv.p->font_width;
469    kk->drv.p->mouse_y = y / kk->drv.p->font_height;
470    kk->mouse.x = kk->drv.p->mouse_x;
471    kk->mouse.y = kk->drv.p->mouse_y;
472    kk->drv.p->mouse_changed = 1;
473}
474
475
476
477static void _display(void)
478{
479    caca_t *kk = gl_kk;
480    gl_display(kk);
481}
482
483
484/*
485 * Driver initialisation
486 */
487
488int gl_install(caca_t *kk)
489{
490#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
491    if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
492        return -1;
493#endif
494
495    kk->drv.driver = CACA_DRIVER_GL;
496
497    kk->drv.init_graphics = gl_init_graphics;
498    kk->drv.end_graphics = gl_end_graphics;
499    kk->drv.set_window_title = gl_set_window_title;
500    kk->drv.get_window_width = gl_get_window_width;
501    kk->drv.get_window_height = gl_get_window_height;
502    kk->drv.display = gl_display;
503    kk->drv.handle_resize = gl_handle_resize;
504    kk->drv.get_event = gl_get_event;
505    kk->drv.set_mouse = gl_set_mouse;
506
507    return 0;
508}
509
510#endif /* USE_GL */
511
Note: See TracBrowser for help on using the repository browser.