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.
 
 
 
 
 
 

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