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

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