25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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