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

Last change on this file since 1063 was 1063, checked in by Jean-Yves Lamoureux, 14 years ago
  • Fixed compilation warning, and .... AHAHAHAHAHAHAHA GL DRIVER IS B0RKEN YOU SMELLY GOATS ! (no time to fix it, thought)



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