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.
 
 
 
 
 
 

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