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

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