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.
 
 
 
 
 
 

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