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

Last change on this file since 2303 was 2303, checked in by Sam Hocevar, 15 years ago
  • Changed most of the long ints in the API into C99 types. WARNING: this completely breaks compatibility with previous versions of libcaca on 64-bit systems.
  • Property svn:keywords set to Id
File size: 17.5 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                2007 Ben Wiley Sittler <bsittler@gmail.com>
6 *                All Rights Reserved
7 *
8 *  $Id: driver_gl.c 2303 2008-04-19 19:25:41Z sam $
9 *
10 *  This library is free software. It comes without any warranty, to
11 *  the extent permitted by applicable law. You can redistribute it
12 *  and/or modify it under the terms of the Do What The Fuck You Want
13 *  To Public License, Version 2, as published by Sam Hocevar. See
14 *  http://sam.zoy.org/wtfpl/COPYING for more details.
15 */
16
17/*
18 *  This file contains the libcaca OpenGL input and output driver
19 */
20
21#include "config.h"
22
23#if defined(USE_GL)
24
25#ifdef HAVE_OPENGL_GL_H
26#   include <OpenGL/gl.h>
27#   include <GLUT/glut.h>
28#else
29#   include <GL/gl.h>
30#   include <GL/glut.h>
31#   include <GL/freeglut_ext.h>
32#endif
33
34#include <string.h>
35#include <stdlib.h>
36#include <stdio.h>
37
38#include "cucul.h"
39#include "caca.h"
40#include "caca_internals.h"
41
42/*
43 * Global variables
44 */
45
46static int glut_init;
47static caca_display_t *gl_d; /* FIXME: we ought to get rid of this */
48
49/*
50 * Local functions
51 */
52static void gl_handle_keyboard(unsigned char, int, int);
53static void gl_handle_special_key(int, int, int);
54static void gl_handle_reshape(int, int);
55static void gl_handle_mouse(int, int, int, int);
56static void gl_handle_mouse_motion(int, int);
57#ifdef HAVE_GLUTCLOSEFUNC
58static void gl_handle_close(void);
59#endif
60static void _display(void);
61static void gl_compute_font(caca_display_t *);
62
63struct driver_private
64{
65    int window;
66    unsigned int width, height;
67    unsigned int new_width, new_height;
68    cucul_font_t *f;
69    float font_width, font_height;
70    float incx, incy;
71    uint32_t const *blocks;
72    int *txid;
73    uint8_t close;
74    uint8_t bit;
75    uint8_t mouse_changed, mouse_clicked;
76    unsigned int mouse_x, mouse_y;
77    unsigned int mouse_button, mouse_state;
78
79    uint8_t key;
80    int special_key;
81
82    float sw, sh;
83};
84
85static int gl_init_graphics(caca_display_t *dp)
86{
87    char const *geometry;
88    char *argv[2] = { "", NULL };
89    char const * const * fonts;
90    unsigned int width = cucul_get_canvas_width(dp->cv);
91    unsigned int height = cucul_get_canvas_height(dp->cv);
92    int argc = 1;
93
94    dp->drv.p = malloc(sizeof(struct driver_private));
95
96    gl_d = dp;
97
98#if defined(HAVE_GETENV)
99    geometry = getenv("CACA_GEOMETRY");
100    if(geometry && *geometry)
101        sscanf(geometry, "%ux%u", &width, &height);
102#endif
103
104    dp->resize.allow = 1;
105    cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
106    dp->resize.allow = 0;
107
108    /* Load a libcucul internal font */
109    fonts = cucul_get_font_list();
110    if(fonts[0] == NULL)
111    {
112        fprintf(stderr, "error: libcucul was compiled without any fonts\n");
113        return -1;
114    }
115    dp->drv.p->f = cucul_load_font(fonts[0], 0);
116    if(dp->drv.p->f == NULL)
117    {
118        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
119        return -1;
120    }
121
122    dp->drv.p->font_width = cucul_get_font_width(dp->drv.p->f);
123    dp->drv.p->font_height = cucul_get_font_height(dp->drv.p->f);
124
125    dp->drv.p->width = cucul_get_canvas_width(dp->cv) * dp->drv.p->font_width;
126    dp->drv.p->height = cucul_get_canvas_height(dp->cv) * dp->drv.p->font_height;
127
128#ifdef HAVE_GLUTCLOSEFUNC
129    dp->drv.p->close = 0;
130#endif
131    dp->drv.p->bit = 0;
132
133    dp->drv.p->mouse_changed = dp->drv.p->mouse_clicked = 0;
134    dp->drv.p->mouse_button = dp->drv.p->mouse_state = 0;
135
136    dp->drv.p->key = 0;
137    dp->drv.p->special_key = 0;
138
139    dp->drv.p->sw = ((float)dp->drv.p->font_width) / 16.0f;
140    dp->drv.p->sh = ((float)dp->drv.p->font_height) / 16.0f;
141
142    if(!glut_init)
143    {
144        glut_init = 1;
145        glutInit(&argc, argv);
146    }
147
148    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
149    glutInitWindowSize(dp->drv.p->width, dp->drv.p->height);
150    dp->drv.p->window = glutCreateWindow("caca for GL");
151
152    gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0);
153
154    glDisable(GL_CULL_FACE);
155    glDisable(GL_DEPTH_TEST);
156
157    glutKeyboardFunc(gl_handle_keyboard);
158    glutSpecialFunc(gl_handle_special_key);
159    glutReshapeFunc(gl_handle_reshape);
160    glutDisplayFunc(_display);
161
162#ifdef HAVE_GLUTCLOSEFUNC
163    glutCloseFunc(gl_handle_close);
164#endif
165
166    glutMouseFunc(gl_handle_mouse);
167    glutMotionFunc(gl_handle_mouse_motion);
168    glutPassiveMotionFunc(gl_handle_mouse_motion);
169
170    glLoadIdentity();
171
172    glMatrixMode(GL_PROJECTION);
173    glPushMatrix();
174    glLoadIdentity();
175    gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0);
176
177    glMatrixMode(GL_MODELVIEW);
178
179    glClear(GL_COLOR_BUFFER_BIT);
180    glEnable(GL_TEXTURE_2D);
181    glEnable(GL_BLEND);
182    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
183
184    glEnable(GL_TEXTURE_2D);
185
186    gl_compute_font(dp);
187
188    return 0;
189}
190
191static int gl_end_graphics(caca_display_t *dp)
192{
193    glutHideWindow();
194    glutDestroyWindow(dp->drv.p->window);
195    cucul_free_font(dp->drv.p->f);
196    free(dp->drv.p->txid);
197    free(dp->drv.p);
198    return 0;
199}
200
201static int gl_set_display_title(caca_display_t *dp, char const *title)
202{
203    glutSetWindowTitle(title);
204    return 0;
205}
206
207static unsigned int gl_get_display_width(caca_display_t const *dp)
208{
209    return dp->drv.p->width;
210}
211
212static unsigned int gl_get_display_height(caca_display_t const *dp)
213{
214    return dp->drv.p->height;
215}
216
217static void gl_display(caca_display_t *dp)
218{
219    uint32_t const *cvchars = (uint32_t const *)cucul_get_canvas_chars(dp->cv);
220    uint32_t const *cvattrs = (uint32_t const *)cucul_get_canvas_attrs(dp->cv);
221    unsigned int width = cucul_get_canvas_width(dp->cv);
222    unsigned int x, y, line;
223
224    glClear(GL_COLOR_BUFFER_BIT);
225    glDisable(GL_TEXTURE_2D);
226    glDisable(GL_BLEND);
227    line = 0;
228    for(y = 0; y < dp->drv.p->height; y += dp->drv.p->font_height)
229    {
230        uint32_t const *attrs = cvattrs + line * width;
231
232        /* FIXME: optimise using stride */
233        for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width)
234        {
235            uint16_t bg = cucul_attr_to_rgb12_bg(*attrs++);
236
237            glColor4b(((bg & 0xf00) >> 8) * 8,
238                      ((bg & 0x0f0) >> 4) * 8,
239                      (bg & 0x00f) * 8,
240                      0xff);
241            glBegin(GL_QUADS);
242            glVertex2f(x, y);
243            glVertex2f(x + dp->drv.p->font_width, y);
244            glVertex2f(x + dp->drv.p->font_width,
245                       y + dp->drv.p->font_height);
246            glVertex2f(x, y + dp->drv.p->font_height);
247            glEnd();
248        }
249
250        line++;
251    }
252
253    /* 2nd pass, avoids changing render state too much */
254    glEnable(GL_TEXTURE_2D);
255    glEnable(GL_BLEND);
256
257    line = 0;
258    for(y = 0; y < dp->drv.p->height; y += dp->drv.p->font_height, line++)
259    {
260        uint32_t const *attrs = cvattrs + line * width;
261        uint32_t const *chars = cvchars + line * width;
262
263        for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width, attrs++)
264        {
265            uint32_t ch = *chars++;
266            uint16_t fg;
267            int i, b, fullwidth;
268
269            fullwidth = cucul_utf32_is_fullwidth(ch);
270
271            for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
272            {
273                if(ch < (uint32_t)dp->drv.p->blocks[i])
274                     break;
275
276                if(ch >= (uint32_t)dp->drv.p->blocks[i + 1])
277                {
278                    b += (uint32_t)(dp->drv.p->blocks[i + 1]
279                                     - dp->drv.p->blocks[i]);
280                    continue;
281                }
282
283                glBindTexture(GL_TEXTURE_2D,
284                              dp->drv.p->txid[b + ch
285                                        - (uint32_t)dp->drv.p->blocks[i]]);
286
287                fg = cucul_attr_to_rgb12_fg(*attrs);
288                glColor3b(((fg & 0xf00) >> 8) * 8,
289                          ((fg & 0x0f0) >> 4) * 8,
290                          (fg & 0x00f) * 8);
291                /* FIXME: handle fullwidth glyphs here */
292                glBegin(GL_QUADS);
293                glTexCoord2f(0, dp->drv.p->sh);
294                glVertex2f(x, y);
295                glTexCoord2f(dp->drv.p->sw, dp->drv.p->sh);
296                glVertex2f(x + dp->drv.p->font_width * (fullwidth ? 2 : 1), y);
297                glTexCoord2f(dp->drv.p->sw, 0);
298                glVertex2f(x + dp->drv.p->font_width * (fullwidth ? 2 : 1),
299                           y + dp->drv.p->font_height);
300                glTexCoord2f(0, 0);
301                glVertex2f(x, y + dp->drv.p->font_height);
302                glEnd();
303            }
304
305            if(fullwidth)
306            {
307                chars++; attrs++; x += dp->drv.p->font_width;
308            }
309        }
310    }
311
312#ifdef HAVE_GLUTCHECKLOOP
313    glutCheckLoop();
314#else
315    glutMainLoopEvent();
316#endif
317    glutSwapBuffers();
318    glutPostRedisplay();
319}
320
321static void gl_handle_resize(caca_display_t *dp)
322{
323    dp->drv.p->width = dp->drv.p->new_width;
324    dp->drv.p->height = dp->drv.p->new_height;
325
326    glMatrixMode(GL_PROJECTION);
327    glPushMatrix();
328    glLoadIdentity();
329
330    glViewport(0, 0, dp->drv.p->width, dp->drv.p->height);
331    gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0);
332    glMatrixMode(GL_MODELVIEW);
333}
334
335static int gl_get_event(caca_display_t *dp, caca_privevent_t *ev)
336{
337#ifdef HAVE_GLUTCHECKLOOP
338    glutCheckLoop();
339#else
340    glutMainLoopEvent();
341#endif
342
343#ifdef HAVE_GLUTCLOSEFUNC
344    if(dp->drv.p->close)
345    {
346        dp->drv.p->close = 0;
347        ev->type = CACA_EVENT_QUIT;
348        return 1;
349    }
350#endif
351
352    if(dp->resize.resized)
353    {
354        ev->type = CACA_EVENT_RESIZE;
355        ev->data.resize.w = cucul_get_canvas_width(dp->cv);
356        ev->data.resize.h = cucul_get_canvas_height(dp->cv);
357        return 1;
358    }
359
360    if(dp->drv.p->mouse_changed)
361    {
362        ev->type = CACA_EVENT_MOUSE_MOTION;
363        ev->data.mouse.x = dp->mouse.x;
364        ev->data.mouse.y = dp->mouse.y;
365        dp->drv.p->mouse_changed = 0;
366
367        if(dp->drv.p->mouse_clicked)
368        {
369            _push_event(dp, ev);
370            ev->type = CACA_EVENT_MOUSE_PRESS;
371            ev->data.mouse.button = dp->drv.p->mouse_button;
372            dp->drv.p->mouse_clicked = 0;
373        }
374
375        return 1;
376    }
377
378    if(dp->drv.p->key != 0)
379    {
380        ev->type = CACA_EVENT_KEY_PRESS;
381        ev->data.key.ch = dp->drv.p->key;
382        ev->data.key.utf32 = (uint32_t)dp->drv.p->key;
383        ev->data.key.utf8[0] = dp->drv.p->key;
384        ev->data.key.utf8[1] = '\0';
385        dp->drv.p->key = 0;
386        return 1;
387    }
388
389    if(dp->drv.p->special_key != 0)
390    {
391        switch(dp->drv.p->special_key)
392        {
393            case GLUT_KEY_F1 : ev->data.key.ch = CACA_KEY_F1; break;
394            case GLUT_KEY_F2 : ev->data.key.ch = CACA_KEY_F2; break;
395            case GLUT_KEY_F3 : ev->data.key.ch = CACA_KEY_F3; break;
396            case GLUT_KEY_F4 : ev->data.key.ch = CACA_KEY_F4; break;
397            case GLUT_KEY_F5 : ev->data.key.ch = CACA_KEY_F5; break;
398            case GLUT_KEY_F6 : ev->data.key.ch = CACA_KEY_F6; break;
399            case GLUT_KEY_F7 : ev->data.key.ch = CACA_KEY_F7; break;
400            case GLUT_KEY_F8 : ev->data.key.ch = CACA_KEY_F8; break;
401            case GLUT_KEY_F9 : ev->data.key.ch = CACA_KEY_F9; break;
402            case GLUT_KEY_F10: ev->data.key.ch = CACA_KEY_F10; break;
403            case GLUT_KEY_F11: ev->data.key.ch = CACA_KEY_F11; break;
404            case GLUT_KEY_F12: ev->data.key.ch = CACA_KEY_F12; break;
405            case GLUT_KEY_LEFT : ev->data.key.ch = CACA_KEY_LEFT; break;
406            case GLUT_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
407            case GLUT_KEY_UP   : ev->data.key.ch = CACA_KEY_UP; break;
408            case GLUT_KEY_DOWN : ev->data.key.ch = CACA_KEY_DOWN; break;
409            case GLUT_KEY_PAGE_UP : ev->data.key.ch = CACA_KEY_PAGEUP; break;
410            case GLUT_KEY_PAGE_DOWN  : ev->data.key.ch = CACA_KEY_PAGEDOWN;
411                break;
412            case GLUT_KEY_HOME : ev->data.key.ch = CACA_KEY_HOME; break;
413            case GLUT_KEY_END : ev->data.key.ch = CACA_KEY_END; break;
414            case GLUT_KEY_INSERT : ev->data.key.ch = CACA_KEY_INSERT; break;
415
416            default: ev->type = CACA_EVENT_NONE; return 0;
417        }
418
419        ev->type = CACA_EVENT_KEY_PRESS;
420        ev->data.key.utf32 = 0;
421        ev->data.key.utf8[0] = '\0';
422
423        dp->drv.p->special_key = 0;
424        return 1;
425    }
426
427    ev->type = CACA_EVENT_NONE;
428    return 0;
429}
430
431
432static void gl_set_mouse(caca_display_t *dp, int flag)
433{
434    if(flag)
435        glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
436    else
437        glutSetCursor(GLUT_CURSOR_NONE);
438}
439
440/*
441 * XXX: following functions are local
442 */
443
444static void gl_handle_keyboard(unsigned char key, int x, int y)
445{
446    caca_display_t *dp = gl_d;
447
448    dp->drv.p->key = key;
449}
450
451static void gl_handle_special_key(int key, int x, int y)
452{
453    caca_display_t *dp = gl_d;
454
455    dp->drv.p->special_key = key;
456}
457
458static void gl_handle_reshape(int w, int h)
459{
460    caca_display_t *dp = gl_d;
461
462    if(dp->drv.p->bit) /* Do not handle reshaping at the first time */
463    {
464        dp->drv.p->new_width = w;
465        dp->drv.p->new_height = h;
466
467        dp->resize.w = w / dp->drv.p->font_width;
468        dp->resize.h = (h / dp->drv.p->font_height) + 1;
469
470        dp->resize.resized = 1;
471    }
472    else
473        dp->drv.p->bit = 1;
474}
475
476static void gl_handle_mouse(int button, int state, int x, int y)
477{
478    caca_display_t *dp = gl_d;
479
480    dp->drv.p->mouse_clicked = 1;
481    dp->drv.p->mouse_button = button;
482    dp->drv.p->mouse_state = state;
483    dp->drv.p->mouse_x = x / dp->drv.p->font_width;
484    dp->drv.p->mouse_y = y / dp->drv.p->font_height;
485    dp->mouse.x = dp->drv.p->mouse_x;
486    dp->mouse.y = dp->drv.p->mouse_y;
487    dp->drv.p->mouse_changed = 1;
488}
489
490static void gl_handle_mouse_motion(int x, int y)
491{
492    caca_display_t *dp = gl_d;
493    dp->drv.p->mouse_x = x / dp->drv.p->font_width;
494    dp->drv.p->mouse_y = y / dp->drv.p->font_height;
495    dp->mouse.x = dp->drv.p->mouse_x;
496    dp->mouse.y = dp->drv.p->mouse_y;
497    dp->drv.p->mouse_changed = 1;
498}
499
500#ifdef HAVE_GLUTCLOSEFUNC
501static void gl_handle_close(void)
502{
503    caca_display_t *dp = gl_d;
504    dp->drv.p->close = 1;
505}
506#endif
507
508static void _display(void)
509{
510    caca_display_t *dp = gl_d;
511    gl_display(dp);
512}
513
514static void gl_compute_font(caca_display_t *dp)
515{
516    cucul_canvas_t *cv;
517    uint32_t *image;
518    int i, b, w, h, x, y;
519
520    /* Count how many glyphs this font has */
521    dp->drv.p->blocks = cucul_get_font_blocks(dp->drv.p->f);
522
523    for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
524        b += (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
525
526    /* Allocate a libcucul canvas and print all the glyphs on it */
527    cv = cucul_create_canvas(2, b);
528    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
529
530    for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
531    {
532        int j, n = (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
533
534        for(j = 0; j < n; j++)
535            cucul_put_char(cv, 0, b + j, dp->drv.p->blocks[i] + j);
536
537        b += n;
538    }
539
540    /* Draw the cucul canvas onto an image buffer */
541    image = malloc(b * dp->drv.p->font_height *
542                   2 * dp->drv.p->font_width * sizeof(uint32_t));
543    cucul_render_canvas(cv, dp->drv.p->f, image, 2 * dp->drv.p->font_width,
544                        b * dp->drv.p->font_height, 8 * dp->drv.p->font_width);
545    cucul_free_canvas(cv);
546
547    /* Convert all glyphs in the image buffer to GL textures */
548    dp->drv.p->txid = malloc(b * sizeof(int));
549
550    w = dp->drv.p->font_width <= 16 ? dp->drv.p->font_width : 16;
551    h = dp->drv.p->font_height <= 16 ? dp->drv.p->font_height : 16;
552
553    for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
554    {
555        int j, n = (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
556
557        for(j = 0; j < n; j++)
558        {
559            uint8_t tmp[16 * 8 * 16];
560            uint32_t *glyph = image + (int)((b + j) * dp->drv.p->font_width * 2
561                                            * dp->drv.p->font_height);
562            int fullwidth =
563                    cucul_utf32_is_fullwidth(dp->drv.p->blocks[i] + j);
564
565            memset(tmp, 0, 16 * 8 * 16);
566           
567            for(y = 0; y < h; y++)
568            {
569                for(x = 0; x < w * (fullwidth ? 2 : 1); x++)
570                {
571                    int offset = x + (15 - y) * (fullwidth ? 32 : 16);
572                    uint8_t c = glyph[x + y * 2 * (int)dp->drv.p->font_width]
573                                 >> 8;
574                    tmp[offset * 4] = c;
575                    tmp[offset * 4 + 1] = c;
576                    tmp[offset * 4 + 2] = c;
577                    tmp[offset * 4 + 3] = c;
578                }
579            }
580           
581            glGenTextures(1, (GLuint*)&dp->drv.p->txid[b + j]);
582            glBindTexture(GL_TEXTURE_2D, dp->drv.p->txid[b + j]);
583            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
584            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
585            glTexImage2D(GL_TEXTURE_2D, 0, 4, (fullwidth ? 32 : 16), 16, 0,
586                         GL_RGBA, GL_UNSIGNED_BYTE, tmp);
587        }
588
589        b += n;
590    }
591
592    free(image);
593}
594
595/*
596 * Driver initialisation
597 */
598
599int gl_install(caca_display_t *dp)
600{
601#if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
602    if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
603        return -1;
604#endif
605
606    dp->drv.id = CACA_DRIVER_GL;
607    dp->drv.driver = "gl";
608
609    dp->drv.init_graphics = gl_init_graphics;
610    dp->drv.end_graphics = gl_end_graphics;
611    dp->drv.set_display_title = gl_set_display_title;
612    dp->drv.get_display_width = gl_get_display_width;
613    dp->drv.get_display_height = gl_get_display_height;
614    dp->drv.display = gl_display;
615    dp->drv.handle_resize = gl_handle_resize;
616    dp->drv.get_event = gl_get_event;
617    dp->drv.set_mouse = gl_set_mouse;
618    dp->drv.set_cursor = NULL;
619
620    return 0;
621}
622
623#endif /* USE_GL */
624
Note: See TracBrowser for help on using the repository browser.