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ů.
 
 
 
 
 
 

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