Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

625 řádky
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 "cucul.h"
  33. #include "caca.h"
  34. #include "caca_internals.h"
  35. /*
  36. * Global variables
  37. */
  38. static int glut_init;
  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. int width, height;
  57. int new_width, new_height;
  58. cucul_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 = cucul_get_canvas_width(dp->cv);
  78. 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. 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. cucul_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 = (uint32_t const *)cucul_get_canvas_chars(dp->cv);
  177. uint32_t const *cvattrs = (uint32_t const *)cucul_get_canvas_attrs(dp->cv);
  178. int width = cucul_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 = cucul_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 = cucul_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 = cucul_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. #ifdef HAVE_GLUTCHECKLOOP
  277. glutCheckLoop();
  278. #else
  279. glutMainLoopEvent();
  280. #endif
  281. #ifdef HAVE_GLUTCLOSEFUNC
  282. if(dp->drv.p->close)
  283. {
  284. dp->drv.p->close = 0;
  285. ev->type = CACA_EVENT_QUIT;
  286. return 1;
  287. }
  288. #endif
  289. if(dp->resize.resized)
  290. {
  291. ev->type = CACA_EVENT_RESIZE;
  292. ev->data.resize.w = cucul_get_canvas_width(dp->cv);
  293. ev->data.resize.h = cucul_get_canvas_height(dp->cv);
  294. return 1;
  295. }
  296. if(dp->drv.p->mouse_changed)
  297. {
  298. ev->type = CACA_EVENT_MOUSE_MOTION;
  299. ev->data.mouse.x = dp->mouse.x;
  300. ev->data.mouse.y = dp->mouse.y;
  301. dp->drv.p->mouse_changed = 0;
  302. if(dp->drv.p->mouse_clicked)
  303. {
  304. _push_event(dp, ev);
  305. ev->type = CACA_EVENT_MOUSE_PRESS;
  306. ev->data.mouse.button = dp->drv.p->mouse_button;
  307. dp->drv.p->mouse_clicked = 0;
  308. }
  309. return 1;
  310. }
  311. if(dp->drv.p->key != 0)
  312. {
  313. ev->type = CACA_EVENT_KEY_PRESS;
  314. ev->data.key.ch = dp->drv.p->key;
  315. ev->data.key.utf32 = (uint32_t)dp->drv.p->key;
  316. ev->data.key.utf8[0] = dp->drv.p->key;
  317. ev->data.key.utf8[1] = '\0';
  318. dp->drv.p->key = 0;
  319. return 1;
  320. }
  321. if(dp->drv.p->special_key != 0)
  322. {
  323. switch(dp->drv.p->special_key)
  324. {
  325. case GLUT_KEY_F1 : ev->data.key.ch = CACA_KEY_F1; break;
  326. case GLUT_KEY_F2 : ev->data.key.ch = CACA_KEY_F2; break;
  327. case GLUT_KEY_F3 : ev->data.key.ch = CACA_KEY_F3; break;
  328. case GLUT_KEY_F4 : ev->data.key.ch = CACA_KEY_F4; break;
  329. case GLUT_KEY_F5 : ev->data.key.ch = CACA_KEY_F5; break;
  330. case GLUT_KEY_F6 : ev->data.key.ch = CACA_KEY_F6; break;
  331. case GLUT_KEY_F7 : ev->data.key.ch = CACA_KEY_F7; break;
  332. case GLUT_KEY_F8 : ev->data.key.ch = CACA_KEY_F8; break;
  333. case GLUT_KEY_F9 : ev->data.key.ch = CACA_KEY_F9; break;
  334. case GLUT_KEY_F10: ev->data.key.ch = CACA_KEY_F10; break;
  335. case GLUT_KEY_F11: ev->data.key.ch = CACA_KEY_F11; break;
  336. case GLUT_KEY_F12: ev->data.key.ch = CACA_KEY_F12; break;
  337. case GLUT_KEY_LEFT : ev->data.key.ch = CACA_KEY_LEFT; break;
  338. case GLUT_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
  339. case GLUT_KEY_UP : ev->data.key.ch = CACA_KEY_UP; break;
  340. case GLUT_KEY_DOWN : ev->data.key.ch = CACA_KEY_DOWN; break;
  341. case GLUT_KEY_PAGE_UP : ev->data.key.ch = CACA_KEY_PAGEUP; break;
  342. case GLUT_KEY_PAGE_DOWN : ev->data.key.ch = CACA_KEY_PAGEDOWN;
  343. break;
  344. case GLUT_KEY_HOME : ev->data.key.ch = CACA_KEY_HOME; break;
  345. case GLUT_KEY_END : ev->data.key.ch = CACA_KEY_END; break;
  346. case GLUT_KEY_INSERT : ev->data.key.ch = CACA_KEY_INSERT; break;
  347. default: ev->type = CACA_EVENT_NONE; return 0;
  348. }
  349. ev->type = CACA_EVENT_KEY_PRESS;
  350. ev->data.key.utf32 = 0;
  351. ev->data.key.utf8[0] = '\0';
  352. dp->drv.p->special_key = 0;
  353. return 1;
  354. }
  355. ev->type = CACA_EVENT_NONE;
  356. return 0;
  357. }
  358. static void gl_set_mouse(caca_display_t *dp, int flag)
  359. {
  360. if(flag)
  361. glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
  362. else
  363. glutSetCursor(GLUT_CURSOR_NONE);
  364. }
  365. /*
  366. * XXX: following functions are local
  367. */
  368. static void gl_handle_keyboard(unsigned char key, int x, int y)
  369. {
  370. caca_display_t *dp = gl_d;
  371. dp->drv.p->key = key;
  372. }
  373. static void gl_handle_special_key(int key, int x, int y)
  374. {
  375. caca_display_t *dp = gl_d;
  376. dp->drv.p->special_key = key;
  377. }
  378. static void gl_handle_reshape(int w, int h)
  379. {
  380. caca_display_t *dp = gl_d;
  381. if(dp->drv.p->bit) /* Do not handle reshaping at the first time */
  382. {
  383. dp->drv.p->new_width = w;
  384. dp->drv.p->new_height = h;
  385. dp->resize.w = w / dp->drv.p->font_width;
  386. dp->resize.h = (h / dp->drv.p->font_height) + 1;
  387. dp->resize.resized = 1;
  388. }
  389. else
  390. dp->drv.p->bit = 1;
  391. }
  392. static void gl_handle_mouse(int button, int state, int x, int y)
  393. {
  394. caca_display_t *dp = gl_d;
  395. dp->drv.p->mouse_clicked = 1;
  396. dp->drv.p->mouse_button = button;
  397. dp->drv.p->mouse_state = state;
  398. dp->drv.p->mouse_x = x / dp->drv.p->font_width;
  399. dp->drv.p->mouse_y = y / dp->drv.p->font_height;
  400. dp->mouse.x = dp->drv.p->mouse_x;
  401. dp->mouse.y = dp->drv.p->mouse_y;
  402. dp->drv.p->mouse_changed = 1;
  403. }
  404. static void gl_handle_mouse_motion(int x, int y)
  405. {
  406. caca_display_t *dp = gl_d;
  407. dp->drv.p->mouse_x = x / dp->drv.p->font_width;
  408. dp->drv.p->mouse_y = y / dp->drv.p->font_height;
  409. dp->mouse.x = dp->drv.p->mouse_x;
  410. dp->mouse.y = dp->drv.p->mouse_y;
  411. dp->drv.p->mouse_changed = 1;
  412. }
  413. #ifdef HAVE_GLUTCLOSEFUNC
  414. static void gl_handle_close(void)
  415. {
  416. caca_display_t *dp = gl_d;
  417. dp->drv.p->close = 1;
  418. }
  419. #endif
  420. static void _display(void)
  421. {
  422. caca_display_t *dp = gl_d;
  423. gl_display(dp);
  424. }
  425. static void gl_compute_font(caca_display_t *dp)
  426. {
  427. cucul_canvas_t *cv;
  428. uint32_t *image;
  429. int i, b, w, h, x, y;
  430. /* Count how many glyphs this font has */
  431. dp->drv.p->blocks = cucul_get_font_blocks(dp->drv.p->f);
  432. for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
  433. b += (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
  434. /* Allocate a libcucul canvas and print all the glyphs on it */
  435. cv = cucul_create_canvas(2, b);
  436. cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
  437. for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
  438. {
  439. int j, n = (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
  440. for(j = 0; j < n; j++)
  441. cucul_put_char(cv, 0, b + j, dp->drv.p->blocks[i] + j);
  442. b += n;
  443. }
  444. /* Draw the cucul canvas onto an image buffer */
  445. image = malloc(b * dp->drv.p->font_height *
  446. 2 * dp->drv.p->font_width * sizeof(uint32_t));
  447. cucul_render_canvas(cv, dp->drv.p->f, image, 2 * dp->drv.p->font_width,
  448. b * dp->drv.p->font_height, 8 * dp->drv.p->font_width);
  449. cucul_free_canvas(cv);
  450. /* Convert all glyphs in the image buffer to GL textures */
  451. dp->drv.p->txid = malloc(b * sizeof(int));
  452. w = dp->drv.p->font_width <= 16 ? dp->drv.p->font_width : 16;
  453. h = dp->drv.p->font_height <= 16 ? dp->drv.p->font_height : 16;
  454. for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2)
  455. {
  456. int j, n = (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]);
  457. for(j = 0; j < n; j++)
  458. {
  459. uint8_t tmp[16 * 8 * 16];
  460. uint32_t *glyph = image + (int)((b + j) * dp->drv.p->font_width * 2
  461. * dp->drv.p->font_height);
  462. int fullwidth =
  463. cucul_utf32_is_fullwidth(dp->drv.p->blocks[i] + j);
  464. memset(tmp, 0, 16 * 8 * 16);
  465. for(y = 0; y < h; y++)
  466. {
  467. for(x = 0; x < w * (fullwidth ? 2 : 1); x++)
  468. {
  469. int offset = x + (15 - y) * (fullwidth ? 32 : 16);
  470. uint8_t c = glyph[x + y * 2 * (int)dp->drv.p->font_width]
  471. >> 8;
  472. tmp[offset * 4] = c;
  473. tmp[offset * 4 + 1] = c;
  474. tmp[offset * 4 + 2] = c;
  475. tmp[offset * 4 + 3] = c;
  476. }
  477. }
  478. glGenTextures(1, (GLuint*)&dp->drv.p->txid[b + j]);
  479. glBindTexture(GL_TEXTURE_2D, dp->drv.p->txid[b + j]);
  480. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  481. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  482. glTexImage2D(GL_TEXTURE_2D, 0, 4, (fullwidth ? 32 : 16), 16, 0,
  483. GL_RGBA, GL_UNSIGNED_BYTE, tmp);
  484. }
  485. b += n;
  486. }
  487. free(image);
  488. }
  489. /*
  490. * Driver initialisation
  491. */
  492. int gl_install(caca_display_t *dp)
  493. {
  494. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  495. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  496. return -1;
  497. #endif
  498. dp->drv.id = CACA_DRIVER_GL;
  499. dp->drv.driver = "gl";
  500. dp->drv.init_graphics = gl_init_graphics;
  501. dp->drv.end_graphics = gl_end_graphics;
  502. dp->drv.set_display_title = gl_set_display_title;
  503. dp->drv.get_display_width = gl_get_display_width;
  504. dp->drv.get_display_height = gl_get_display_height;
  505. dp->drv.display = gl_display;
  506. dp->drv.handle_resize = gl_handle_resize;
  507. dp->drv.get_event = gl_get_event;
  508. dp->drv.set_mouse = gl_set_mouse;
  509. dp->drv.set_cursor = NULL;
  510. return 0;
  511. }
  512. #endif /* USE_GL */