Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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