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.
 
 
 
 
 
 

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