You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

618 lines
17 KiB

  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$
  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. * This file contains the libcaca OpenGL input and output driver
  18. */
  19. #include "config.h"
  20. #include "common.h"
  21. #if defined(USE_GL)
  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. #include <string.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include "cucul.h"
  34. #include "caca.h"
  35. #include "caca_internals.h"
  36. /*
  37. * Global variables
  38. */
  39. static caca_display_t *gl_d; /* FIXME: we ought to get rid of this */
  40. /*
  41. * Local functions
  42. */
  43. static void gl_handle_keyboard(unsigned char, int, int);
  44. static void gl_handle_special_key(int, int, int);
  45. static void gl_handle_reshape(int, int);
  46. static void gl_handle_mouse(int, int, int, int);
  47. static void gl_handle_mouse_motion(int, int);
  48. #ifdef HAVE_GLUTCLOSEFUNC
  49. static void gl_handle_close(void);
  50. #endif
  51. static void _display(void);
  52. static void gl_compute_font(caca_display_t *);
  53. struct driver_private
  54. {
  55. int window;
  56. unsigned int width, height;
  57. unsigned int new_width, new_height;
  58. cucul_font_t *f;
  59. float font_width, font_height;
  60. float incx, incy;
  61. unsigned long int const *blocks;
  62. int *txid;
  63. unsigned char close;
  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. unsigned char key;
  69. int special_key;
  70. float sw, sh;
  71. };
  72. static int gl_init_graphics(caca_display_t *dp)
  73. {
  74. char const *geometry;
  75. char *argv[2] = { "", NULL };
  76. char const * const * fonts;
  77. unsigned int width = cucul_get_canvas_width(dp->cv);
  78. unsigned int height = cucul_get_canvas_height(dp->cv);
  79. int argc = 1;
  80. dp->drv.p = malloc(sizeof(struct driver_private));
  81. gl_d = dp;
  82. #if defined(HAVE_GETENV)
  83. geometry = getenv("CACA_GEOMETRY");
  84. if(geometry && *geometry)
  85. sscanf(geometry, "%ux%u", &width, &height);
  86. #endif
  87. dp->resize.allow = 1;
  88. cucul_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32);
  89. dp->resize.allow = 0;
  90. /* Load a libcucul internal font */
  91. fonts = cucul_get_font_list();
  92. if(fonts[0] == NULL)
  93. {
  94. fprintf(stderr, "error: libcucul was compiled without any fonts\n");
  95. return -1;
  96. }
  97. dp->drv.p->f = cucul_load_font(fonts[0], 0);
  98. if(dp->drv.p->f == NULL)
  99. {
  100. fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
  101. return -1;
  102. }
  103. dp->drv.p->font_width = cucul_get_font_width(dp->drv.p->f);
  104. dp->drv.p->font_height = cucul_get_font_height(dp->drv.p->f);
  105. dp->drv.p->width = cucul_get_canvas_width(dp->cv) * dp->drv.p->font_width;
  106. dp->drv.p->height = cucul_get_canvas_height(dp->cv) * dp->drv.p->font_height;
  107. #ifdef HAVE_GLUTCLOSEFUNC
  108. dp->drv.p->close = 0;
  109. #endif
  110. dp->drv.p->bit = 0;
  111. dp->drv.p->mouse_changed = dp->drv.p->mouse_clicked = 0;
  112. dp->drv.p->mouse_button = dp->drv.p->mouse_state = 0;
  113. dp->drv.p->key = 0;
  114. dp->drv.p->special_key = 0;
  115. dp->drv.p->sw = ((float)dp->drv.p->font_width) / 16.0f;
  116. dp->drv.p->sh = ((float)dp->drv.p->font_height) / 16.0f;
  117. glutInit(&argc, argv);
  118. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  119. glutInitWindowSize(dp->drv.p->width, dp->drv.p->height);
  120. dp->drv.p->window = glutCreateWindow("caca for GL");
  121. gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0);
  122. glDisable(GL_CULL_FACE);
  123. glDisable(GL_DEPTH_TEST);
  124. glutKeyboardFunc(gl_handle_keyboard);
  125. glutSpecialFunc(gl_handle_special_key);
  126. glutReshapeFunc(gl_handle_reshape);
  127. glutDisplayFunc(_display);
  128. #ifdef HAVE_GLUTCLOSEFUNC
  129. glutCloseFunc(gl_handle_close);
  130. #endif
  131. glutMouseFunc(gl_handle_mouse);
  132. glutMotionFunc(gl_handle_mouse_motion);
  133. glutPassiveMotionFunc(gl_handle_mouse_motion);
  134. glLoadIdentity();
  135. glMatrixMode(GL_PROJECTION);
  136. glPushMatrix();
  137. glLoadIdentity();
  138. gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0);
  139. glMatrixMode(GL_MODELVIEW);
  140. glClear(GL_COLOR_BUFFER_BIT);
  141. glEnable(GL_TEXTURE_2D);
  142. glEnable(GL_BLEND);
  143. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  144. glEnable(GL_TEXTURE_2D);
  145. gl_compute_font(dp);
  146. return 0;
  147. }
  148. static int gl_end_graphics(caca_display_t *dp)
  149. {
  150. glutDestroyWindow(dp->drv.p->window);
  151. free(dp->drv.p->txid);
  152. free(dp->drv.p);
  153. return 0;
  154. }
  155. static int gl_set_display_title(caca_display_t *dp, char const *title)
  156. {
  157. glutSetWindowTitle(title);
  158. return 0;
  159. }
  160. static unsigned int gl_get_display_width(caca_display_t const *dp)
  161. {
  162. return dp->drv.p->width;
  163. }
  164. static unsigned int gl_get_display_height(caca_display_t const *dp)
  165. {
  166. return dp->drv.p->height;
  167. }
  168. static void gl_display(caca_display_t *dp)
  169. {
  170. uint32_t const *cvchars = (uint32_t const *)cucul_get_canvas_chars(dp->cv);
  171. uint32_t const *cvattrs = (uint32_t const *)cucul_get_canvas_attrs(dp->cv);
  172. unsigned int width = cucul_get_canvas_width(dp->cv);
  173. unsigned int x, y, line;
  174. glClear(GL_COLOR_BUFFER_BIT);
  175. glDisable(GL_TEXTURE_2D);
  176. glDisable(GL_BLEND);
  177. line = 0;
  178. for(y = 0; y < dp->drv.p->height; y += dp->drv.p->font_height)
  179. {
  180. uint32_t const *attrs = cvattrs + line * width;
  181. /* FIXME: optimise using stride */
  182. for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width)
  183. {
  184. uint16_t bg = cucul_attr_to_rgb12_bg(*attrs++);
  185. glColor4b(((bg & 0xf00) >> 8) * 8,
  186. ((bg & 0x0f0) >> 4) * 8,
  187. (bg & 0x00f) * 8,
  188. 0xff);
  189. glBegin(GL_QUADS);
  190. glVertex2f(x, y);
  191. glVertex2f(x + dp->drv.p->font_width, y);
  192. glVertex2f(x + dp->drv.p->font_width,
  193. y + dp->drv.p->font_height);
  194. glVertex2f(x, y + dp->drv.p->font_height);
  195. glEnd();
  196. }
  197. line++;
  198. }
  199. /* 2nd pass, avoids changing render state too much */
  200. glEnable(GL_TEXTURE_2D);
  201. glEnable(GL_BLEND);
  202. line = 0;
  203. for(y = 0; y < dp->drv.p->height; y += dp->drv.p->font_height, line++)
  204. {
  205. uint32_t const *attrs = cvattrs + line * width;
  206. uint32_t const *chars = cvchars + line * width;
  207. for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width, attrs++)
  208. {
  209. uint32_t ch = *chars++;
  210. uint16_t fg;
  211. int i, b, fullwidth;
  212. fullwidth = cucul_utf32_is_fullwidth(ch);
  213. for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
  214. {
  215. if(ch < (uint32_t)dp->drv.p->blocks[i])
  216. break;
  217. if(ch >= (uint32_t)dp->drv.p->blocks[i + 1])
  218. {
  219. b += (uint32_t)(dp->drv.p->blocks[i + 1]
  220. - dp->drv.p->blocks[i]);
  221. continue;
  222. }
  223. glBindTexture(GL_TEXTURE_2D,
  224. dp->drv.p->txid[b + ch
  225. - (uint32_t)dp->drv.p->blocks[i]]);
  226. fg = cucul_attr_to_rgb12_fg(*attrs);
  227. glColor3b(((fg & 0xf00) >> 8) * 8,
  228. ((fg & 0x0f0) >> 4) * 8,
  229. (fg & 0x00f) * 8);
  230. /* FIXME: handle fullwidth glyphs here */
  231. glBegin(GL_QUADS);
  232. glTexCoord2f(0, dp->drv.p->sh);
  233. glVertex2f(x, y);
  234. glTexCoord2f(dp->drv.p->sw, dp->drv.p->sh);
  235. glVertex2f(x + dp->drv.p->font_width * (fullwidth ? 2 : 1), y);
  236. glTexCoord2f(dp->drv.p->sw, 0);
  237. glVertex2f(x + dp->drv.p->font_width * (fullwidth ? 2 : 1),
  238. y + dp->drv.p->font_height);
  239. glTexCoord2f(0, 0);
  240. glVertex2f(x, y + dp->drv.p->font_height);
  241. glEnd();
  242. }
  243. if(fullwidth)
  244. {
  245. chars++; attrs++; x += dp->drv.p->font_width;
  246. }
  247. }
  248. }
  249. #ifdef HAVE_GLUTCHECKLOOP
  250. glutCheckLoop();
  251. #else
  252. glutMainLoopEvent();
  253. #endif
  254. glutSwapBuffers();
  255. glutPostRedisplay();
  256. }
  257. static void gl_handle_resize(caca_display_t *dp)
  258. {
  259. dp->drv.p->width = dp->drv.p->new_width;
  260. dp->drv.p->height = dp->drv.p->new_height;
  261. glMatrixMode(GL_PROJECTION);
  262. glPushMatrix();
  263. glLoadIdentity();
  264. glViewport(0, 0, dp->drv.p->width, dp->drv.p->height);
  265. gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0);
  266. glMatrixMode(GL_MODELVIEW);
  267. }
  268. static int gl_get_event(caca_display_t *dp, caca_privevent_t *ev)
  269. {
  270. #ifdef HAVE_GLUTCHECKLOOP
  271. glutCheckLoop();
  272. #else
  273. glutMainLoopEvent();
  274. #endif
  275. #ifdef HAVE_GLUTCLOSEFUNC
  276. if(dp->drv.p->close)
  277. {
  278. dp->drv.p->close = 0;
  279. ev->type = CACA_EVENT_QUIT;
  280. return 1;
  281. }
  282. #endif
  283. if(dp->resize.resized)
  284. {
  285. ev->type = CACA_EVENT_RESIZE;
  286. ev->data.resize.w = cucul_get_canvas_width(dp->cv);
  287. ev->data.resize.h = cucul_get_canvas_height(dp->cv);
  288. return 1;
  289. }
  290. if(dp->drv.p->mouse_changed)
  291. {
  292. ev->type = CACA_EVENT_MOUSE_MOTION;
  293. ev->data.mouse.x = dp->mouse.x;
  294. ev->data.mouse.y = dp->mouse.y;
  295. dp->drv.p->mouse_changed = 0;
  296. if(dp->drv.p->mouse_clicked)
  297. {
  298. _push_event(dp, ev);
  299. ev->type = CACA_EVENT_MOUSE_PRESS;
  300. ev->data.mouse.button = dp->drv.p->mouse_button;
  301. dp->drv.p->mouse_clicked = 0;
  302. }
  303. return 1;
  304. }
  305. if(dp->drv.p->key != 0)
  306. {
  307. ev->type = CACA_EVENT_KEY_PRESS;
  308. ev->data.key.ch = dp->drv.p->key;
  309. ev->data.key.utf32 = (uint32_t)dp->drv.p->key;
  310. ev->data.key.utf8[0] = dp->drv.p->key;
  311. ev->data.key.utf8[1] = '\0';
  312. dp->drv.p->key = 0;
  313. return 1;
  314. }
  315. if(dp->drv.p->special_key != 0)
  316. {
  317. switch(dp->drv.p->special_key)
  318. {
  319. case GLUT_KEY_F1 : ev->data.key.ch = CACA_KEY_F1; break;
  320. case GLUT_KEY_F2 : ev->data.key.ch = CACA_KEY_F2; break;
  321. case GLUT_KEY_F3 : ev->data.key.ch = CACA_KEY_F3; break;
  322. case GLUT_KEY_F4 : ev->data.key.ch = CACA_KEY_F4; break;
  323. case GLUT_KEY_F5 : ev->data.key.ch = CACA_KEY_F5; break;
  324. case GLUT_KEY_F6 : ev->data.key.ch = CACA_KEY_F6; break;
  325. case GLUT_KEY_F7 : ev->data.key.ch = CACA_KEY_F7; break;
  326. case GLUT_KEY_F8 : ev->data.key.ch = CACA_KEY_F8; break;
  327. case GLUT_KEY_F9 : ev->data.key.ch = CACA_KEY_F9; break;
  328. case GLUT_KEY_F10: ev->data.key.ch = CACA_KEY_F10; break;
  329. case GLUT_KEY_F11: ev->data.key.ch = CACA_KEY_F11; break;
  330. case GLUT_KEY_F12: ev->data.key.ch = CACA_KEY_F12; break;
  331. case GLUT_KEY_LEFT : ev->data.key.ch = CACA_KEY_LEFT; break;
  332. case GLUT_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
  333. case GLUT_KEY_UP : ev->data.key.ch = CACA_KEY_UP; break;
  334. case GLUT_KEY_DOWN : ev->data.key.ch = CACA_KEY_DOWN; break;
  335. case GLUT_KEY_PAGE_UP : ev->data.key.ch = CACA_KEY_PAGEUP; break;
  336. case GLUT_KEY_PAGE_DOWN : ev->data.key.ch = CACA_KEY_PAGEDOWN;
  337. break;
  338. case GLUT_KEY_HOME : ev->data.key.ch = CACA_KEY_HOME; break;
  339. case GLUT_KEY_END : ev->data.key.ch = CACA_KEY_END; break;
  340. case GLUT_KEY_INSERT : ev->data.key.ch = CACA_KEY_INSERT; break;
  341. default: ev->type = CACA_EVENT_NONE; return 0;
  342. }
  343. ev->type = CACA_EVENT_KEY_PRESS;
  344. ev->data.key.utf32 = 0;
  345. ev->data.key.utf8[0] = '\0';
  346. dp->drv.p->special_key = 0;
  347. return 1;
  348. }
  349. ev->type = CACA_EVENT_NONE;
  350. return 0;
  351. }
  352. static void gl_set_mouse(caca_display_t *dp, int flag)
  353. {
  354. if(flag)
  355. glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
  356. else
  357. glutSetCursor(GLUT_CURSOR_NONE);
  358. }
  359. /*
  360. * XXX: following functions are local
  361. */
  362. static void gl_handle_keyboard(unsigned char key, int x, int y)
  363. {
  364. caca_display_t *dp = gl_d;
  365. dp->drv.p->key = key;
  366. }
  367. static void gl_handle_special_key(int key, int x, int y)
  368. {
  369. caca_display_t *dp = gl_d;
  370. dp->drv.p->special_key = key;
  371. }
  372. static void gl_handle_reshape(int w, int h)
  373. {
  374. caca_display_t *dp = gl_d;
  375. if(dp->drv.p->bit) /* Do not handle reshaping at the first time */
  376. {
  377. dp->drv.p->new_width = w;
  378. dp->drv.p->new_height = h;
  379. dp->resize.w = w / dp->drv.p->font_width;
  380. dp->resize.h = (h / dp->drv.p->font_height) + 1;
  381. dp->resize.resized = 1;
  382. }
  383. else
  384. dp->drv.p->bit = 1;
  385. }
  386. static void gl_handle_mouse(int button, int state, int x, int y)
  387. {
  388. caca_display_t *dp = gl_d;
  389. dp->drv.p->mouse_clicked = 1;
  390. dp->drv.p->mouse_button = button;
  391. dp->drv.p->mouse_state = state;
  392. dp->drv.p->mouse_x = x / dp->drv.p->font_width;
  393. dp->drv.p->mouse_y = y / dp->drv.p->font_height;
  394. dp->mouse.x = dp->drv.p->mouse_x;
  395. dp->mouse.y = dp->drv.p->mouse_y;
  396. dp->drv.p->mouse_changed = 1;
  397. }
  398. static void gl_handle_mouse_motion(int x, int y)
  399. {
  400. caca_display_t *dp = gl_d;
  401. dp->drv.p->mouse_x = x / dp->drv.p->font_width;
  402. dp->drv.p->mouse_y = y / dp->drv.p->font_height;
  403. dp->mouse.x = dp->drv.p->mouse_x;
  404. dp->mouse.y = dp->drv.p->mouse_y;
  405. dp->drv.p->mouse_changed = 1;
  406. }
  407. #ifdef HAVE_GLUTCLOSEFUNC
  408. static void gl_handle_close(void)
  409. {
  410. caca_display_t *dp = gl_d;
  411. dp->drv.p->close = 1;
  412. }
  413. #endif
  414. static void _display(void)
  415. {
  416. caca_display_t *dp = gl_d;
  417. gl_display(dp);
  418. }
  419. static void gl_compute_font(caca_display_t *dp)
  420. {
  421. cucul_canvas_t *cv;
  422. uint32_t *image;
  423. int i, b, w, h, x, y;
  424. /* Count how many glyphs this font has */
  425. dp->drv.p->blocks = cucul_get_font_blocks(dp->drv.p->f);
  426. for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
  427. b += (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
  428. /* Allocate a libcucul canvas and print all the glyphs on it */
  429. cv = cucul_create_canvas(2, b);
  430. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  431. for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
  432. {
  433. int j, n = (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
  434. for(j = 0; j < n; j++)
  435. cucul_put_char(cv, 0, b + j, dp->drv.p->blocks[i] + j);
  436. b += n;
  437. }
  438. /* Draw the cucul canvas onto an image buffer */
  439. image = malloc(b * dp->drv.p->font_height *
  440. 2 * dp->drv.p->font_width * sizeof(uint32_t));
  441. cucul_render_canvas(cv, dp->drv.p->f, image, 2 * dp->drv.p->font_width,
  442. b * dp->drv.p->font_height, 8 * dp->drv.p->font_width);
  443. cucul_free_canvas(cv);
  444. /* Convert all glyphs in the image buffer to GL textures */
  445. dp->drv.p->txid = malloc(b * sizeof(int));
  446. w = dp->drv.p->font_width <= 16 ? dp->drv.p->font_width : 16;
  447. h = dp->drv.p->font_height <= 16 ? dp->drv.p->font_height : 16;
  448. for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
  449. {
  450. int j, n = (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
  451. for(j = 0; j < n; j++)
  452. {
  453. uint8_t tmp[16 * 8 * 16];
  454. uint32_t *glyph = image + (int)((b + j) * dp->drv.p->font_width * 2
  455. * dp->drv.p->font_height);
  456. int fullwidth =
  457. cucul_utf32_is_fullwidth(dp->drv.p->blocks[i] + j);
  458. memset(tmp, 0, 16 * 8 * 16);
  459. for(y = 0; y < h; y++)
  460. {
  461. for(x = 0; x < w * (fullwidth ? 2 : 1); x++)
  462. {
  463. int offset = x + (15 - y) * (fullwidth ? 32 : 16);
  464. uint8_t c = glyph[x + y * 2 * (int)dp->drv.p->font_width]
  465. >> 8;
  466. tmp[offset * 4] = c;
  467. tmp[offset * 4 + 1] = c;
  468. tmp[offset * 4 + 2] = c;
  469. tmp[offset * 4 + 3] = c;
  470. }
  471. }
  472. glGenTextures(1, (GLuint*)&dp->drv.p->txid[b + j]);
  473. glBindTexture(GL_TEXTURE_2D, dp->drv.p->txid[b + j]);
  474. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  475. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  476. glTexImage2D(GL_TEXTURE_2D, 0, 4, (fullwidth ? 32 : 16), 16, 0,
  477. GL_RGBA, GL_UNSIGNED_BYTE, tmp);
  478. }
  479. b += n;
  480. }
  481. free(image);
  482. }
  483. /*
  484. * Driver initialisation
  485. */
  486. int gl_install(caca_display_t *dp)
  487. {
  488. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  489. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  490. return -1;
  491. #endif
  492. dp->drv.driver = CACA_DRIVER_GL;
  493. dp->drv.init_graphics = gl_init_graphics;
  494. dp->drv.end_graphics = gl_end_graphics;
  495. dp->drv.set_display_title = gl_set_display_title;
  496. dp->drv.get_display_width = gl_get_display_width;
  497. dp->drv.get_display_height = gl_get_display_height;
  498. dp->drv.display = gl_display;
  499. dp->drv.handle_resize = gl_handle_resize;
  500. dp->drv.get_event = gl_get_event;
  501. dp->drv.set_mouse = gl_set_mouse;
  502. dp->drv.set_cursor = NULL;
  503. return 0;
  504. }
  505. #endif /* USE_GL */