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