Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

635 linhas
18 KiB

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