Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 < dp->drv.p->blocks[i])
  207. break;
  208. if(cv >= dp->drv.p->blocks[i + 1])
  209. {
  210. b += dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i];
  211. continue;
  212. }
  213. glBindTexture(GL_TEXTURE_2D,
  214. dp->drv.p->txid[b + cv - dp->drv.p->blocks[i]]);
  215. fg = _cucul_argb32_to_rgb12fg(*attr);
  216. glColor3b(((fg & 0xf00) >> 8) * 8,
  217. ((fg & 0x0f0) >> 4) * 8,
  218. (fg & 0x00f) * 8);
  219. glBegin(GL_QUADS);
  220. glTexCoord2f(0, dp->drv.p->sh);
  221. glVertex2f(x, y);
  222. glTexCoord2f(dp->drv.p->sw, dp->drv.p->sh);
  223. glVertex2f(x + dp->drv.p->font_width, y);
  224. glTexCoord2f(dp->drv.p->sw, 0);
  225. glVertex2f(x + dp->drv.p->font_width,
  226. y + dp->drv.p->font_height);
  227. glTexCoord2f(0, 0);
  228. glVertex2f(x, y + dp->drv.p->font_height);
  229. glEnd();
  230. }
  231. }
  232. }
  233. #ifdef HAVE_GLUTCHECKLOOP
  234. glutCheckLoop();
  235. #else
  236. glutMainLoopEvent();
  237. #endif
  238. glutSwapBuffers();
  239. glutPostRedisplay();
  240. }
  241. static void gl_handle_resize(caca_display_t *dp)
  242. {
  243. dp->drv.p->width = dp->drv.p->new_width;
  244. dp->drv.p->height = dp->drv.p->new_height;
  245. glMatrixMode(GL_PROJECTION);
  246. glPushMatrix();
  247. glLoadIdentity();
  248. glViewport(0, 0, dp->drv.p->width, dp->drv.p->height);
  249. gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0);
  250. glMatrixMode(GL_MODELVIEW);
  251. }
  252. static int gl_get_event(caca_display_t *dp, caca_event_t *ev)
  253. {
  254. #ifdef HAVE_GLUTCHECKLOOP
  255. glutCheckLoop();
  256. #else
  257. glutMainLoopEvent();
  258. #endif
  259. #ifdef HAVE_GLUTCLOSEFUNC
  260. if(dp->drv.p->close)
  261. {
  262. dp->drv.p->close = 0;
  263. ev->type = CACA_EVENT_QUIT;
  264. return 1;
  265. }
  266. #endif
  267. if(dp->resize.resized)
  268. {
  269. ev->type = CACA_EVENT_RESIZE;
  270. ev->data.resize.w = dp->cv->width;
  271. ev->data.resize.h = dp->cv->height;
  272. return 1;
  273. }
  274. if(dp->drv.p->mouse_changed)
  275. {
  276. ev->type = CACA_EVENT_MOUSE_MOTION;
  277. ev->data.mouse.x = dp->mouse.x;
  278. ev->data.mouse.y = dp->mouse.y;
  279. dp->drv.p->mouse_changed = 0;
  280. if(dp->drv.p->mouse_clicked)
  281. {
  282. _push_event(dp, ev);
  283. ev->type = CACA_EVENT_MOUSE_PRESS;
  284. ev->data.mouse.button = dp->drv.p->mouse_button;
  285. dp->drv.p->mouse_clicked = 0;
  286. }
  287. return 1;
  288. }
  289. if(dp->drv.p->key != 0)
  290. {
  291. ev->type = CACA_EVENT_KEY_PRESS;
  292. ev->data.key.ch = dp->drv.p->key;
  293. ev->data.key.utf32 = (uint32_t)dp->drv.p->key;
  294. ev->data.key.utf8[0] = dp->drv.p->key;
  295. ev->data.key.utf8[1] = '\0';
  296. dp->drv.p->key = 0;
  297. return 1;
  298. }
  299. if(dp->drv.p->special_key != 0)
  300. {
  301. switch(dp->drv.p->special_key)
  302. {
  303. case GLUT_KEY_F1 : ev->data.key.ch = CACA_KEY_F1; break;
  304. case GLUT_KEY_F2 : ev->data.key.ch = CACA_KEY_F2; break;
  305. case GLUT_KEY_F3 : ev->data.key.ch = CACA_KEY_F3; break;
  306. case GLUT_KEY_F4 : ev->data.key.ch = CACA_KEY_F4; break;
  307. case GLUT_KEY_F5 : ev->data.key.ch = CACA_KEY_F5; break;
  308. case GLUT_KEY_F6 : ev->data.key.ch = CACA_KEY_F6; break;
  309. case GLUT_KEY_F7 : ev->data.key.ch = CACA_KEY_F7; break;
  310. case GLUT_KEY_F8 : ev->data.key.ch = CACA_KEY_F8; break;
  311. case GLUT_KEY_F9 : ev->data.key.ch = CACA_KEY_F9; break;
  312. case GLUT_KEY_F10: ev->data.key.ch = CACA_KEY_F10; break;
  313. case GLUT_KEY_F11: ev->data.key.ch = CACA_KEY_F11; break;
  314. case GLUT_KEY_F12: ev->data.key.ch = CACA_KEY_F12; break;
  315. case GLUT_KEY_LEFT : ev->data.key.ch = CACA_KEY_LEFT; break;
  316. case GLUT_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
  317. case GLUT_KEY_UP : ev->data.key.ch = CACA_KEY_UP; break;
  318. case GLUT_KEY_DOWN : ev->data.key.ch = CACA_KEY_DOWN; break;
  319. case GLUT_KEY_PAGE_UP : ev->data.key.ch = CACA_KEY_PAGEUP; break;
  320. case GLUT_KEY_PAGE_DOWN : ev->data.key.ch = CACA_KEY_PAGEDOWN;
  321. break;
  322. case GLUT_KEY_HOME : ev->data.key.ch = CACA_KEY_HOME; break;
  323. case GLUT_KEY_END : ev->data.key.ch = CACA_KEY_END; break;
  324. case GLUT_KEY_INSERT : ev->data.key.ch = CACA_KEY_INSERT; break;
  325. default: ev->type = CACA_EVENT_NONE; return 0;
  326. }
  327. ev->type = CACA_EVENT_KEY_PRESS;
  328. ev->data.key.utf32 = 0;
  329. ev->data.key.utf8[0] = '\0';
  330. dp->drv.p->special_key = 0;
  331. return 1;
  332. }
  333. ev->type = CACA_EVENT_NONE;
  334. return 0;
  335. }
  336. static void gl_set_mouse(caca_display_t *dp, int flag)
  337. {
  338. if(flag)
  339. glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
  340. else
  341. glutSetCursor(GLUT_CURSOR_NONE);
  342. }
  343. /*
  344. * XXX: following functions are local
  345. */
  346. static void gl_handle_keyboard(unsigned char key, int x, int y)
  347. {
  348. caca_display_t *dp = gl_d;
  349. dp->drv.p->key = key;
  350. }
  351. static void gl_handle_special_key(int key, int x, int y)
  352. {
  353. caca_display_t *dp = gl_d;
  354. dp->drv.p->special_key = key;
  355. }
  356. static void gl_handle_reshape(int w, int h)
  357. {
  358. caca_display_t *dp = gl_d;
  359. if(dp->drv.p->bit) /* Do not handle reshaping at the first time */
  360. {
  361. dp->drv.p->new_width = w;
  362. dp->drv.p->new_height = h;
  363. dp->resize.w = w / dp->drv.p->font_width;
  364. dp->resize.h = (h / dp->drv.p->font_height) + 1;
  365. dp->resize.resized = 1;
  366. }
  367. else
  368. dp->drv.p->bit = 1;
  369. }
  370. static void gl_handle_mouse(int button, int state, int x, int y)
  371. {
  372. caca_display_t *dp = gl_d;
  373. dp->drv.p->mouse_clicked = 1;
  374. dp->drv.p->mouse_button = button;
  375. dp->drv.p->mouse_state = state;
  376. dp->drv.p->mouse_x = x / dp->drv.p->font_width;
  377. dp->drv.p->mouse_y = y / dp->drv.p->font_height;
  378. dp->mouse.x = dp->drv.p->mouse_x;
  379. dp->mouse.y = dp->drv.p->mouse_y;
  380. dp->drv.p->mouse_changed = 1;
  381. }
  382. static void gl_handle_mouse_motion(int x, int y)
  383. {
  384. caca_display_t *dp = gl_d;
  385. dp->drv.p->mouse_x = x / dp->drv.p->font_width;
  386. dp->drv.p->mouse_y = y / dp->drv.p->font_height;
  387. dp->mouse.x = dp->drv.p->mouse_x;
  388. dp->mouse.y = dp->drv.p->mouse_y;
  389. dp->drv.p->mouse_changed = 1;
  390. }
  391. #ifdef HAVE_GLUTCLOSEFUNC
  392. static void gl_handle_close(void)
  393. {
  394. caca_display_t *dp = gl_d;
  395. dp->drv.p->close = 1;
  396. }
  397. #endif
  398. static void _display(void)
  399. {
  400. caca_display_t *dp = gl_d;
  401. gl_display(dp);
  402. }
  403. static void gl_compute_font(caca_display_t *dp)
  404. {
  405. cucul_canvas_t *cv;
  406. uint32_t *image;
  407. int i, b, w, h, x, y;
  408. /* Count how many glyphs this font has */
  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. /* Allocate a libcucul canvas and print all the glyphs on it */
  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. /* Draw the cucul canvas onto an image buffer */
  423. image = malloc(b * dp->drv.p->font_height
  424. * dp->drv.p->font_width * sizeof(uint32_t));
  425. cucul_render_canvas(cv, dp->drv.p->f, image, dp->drv.p->font_width,
  426. b * dp->drv.p->font_height, 4 * dp->drv.p->font_width);
  427. cucul_free_canvas(cv);
  428. /* Convert all glyphs in the image buffer to GL textures */
  429. dp->drv.p->txid = malloc(b * sizeof(int));
  430. w = dp->drv.p->font_width <= 16 ? dp->drv.p->font_width : 16;
  431. h = dp->drv.p->font_height <= 16 ? dp->drv.p->font_height : 16;
  432. for(i = 0; i < b; i++)
  433. {
  434. uint8_t tmp[16 * 4 * 16];
  435. uint32_t *glyph = image + (int)(i * dp->drv.p->font_width
  436. * dp->drv.p->font_height);
  437. memset(tmp, 0, 16 * 4 * 16);
  438. for(y = 0; y < h; y++)
  439. {
  440. for(x = 0; x < w; x++)
  441. {
  442. uint32_t offset = x + (15 - y) * 16;
  443. uint8_t c = glyph[x + y * (int)dp->drv.p->font_width] >> 8;
  444. tmp[offset * 4] = c;
  445. tmp[offset * 4 + 1] = c;
  446. tmp[offset * 4 + 2] = c;
  447. tmp[offset * 4 + 3] = c;
  448. }
  449. }
  450. glGenTextures(1, (GLuint*)&dp->drv.p->txid[i]);
  451. glBindTexture(GL_TEXTURE_2D, dp->drv.p->txid[i]);
  452. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  453. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  454. glTexImage2D(GL_TEXTURE_2D, 0, 4, 16, 16, 0,
  455. GL_RGBA, GL_UNSIGNED_BYTE, tmp);
  456. }
  457. free(image);
  458. }
  459. /*
  460. * Driver initialisation
  461. */
  462. int gl_install(caca_display_t *dp)
  463. {
  464. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  465. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  466. return -1;
  467. #endif
  468. dp->drv.driver = CACA_DRIVER_GL;
  469. dp->drv.init_graphics = gl_init_graphics;
  470. dp->drv.end_graphics = gl_end_graphics;
  471. dp->drv.set_display_title = gl_set_display_title;
  472. dp->drv.get_display_width = gl_get_display_width;
  473. dp->drv.get_display_height = gl_get_display_height;
  474. dp->drv.display = gl_display;
  475. dp->drv.handle_resize = gl_handle_resize;
  476. dp->drv.get_event = gl_get_event;
  477. dp->drv.set_mouse = gl_set_mouse;
  478. return 0;
  479. }
  480. #endif /* USE_GL */