You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

599 lines
16 KiB

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