選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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