Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

516 рядки
13 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file driver_gl.c
  12. * \version \$Id$
  13. * \author Jean-Yves Lamoureux <jylam@lnxscene.org>
  14. * \brief OpenGL driver
  15. *
  16. * This file contains the libcaca OpenGL input and output driver
  17. */
  18. #include "config.h"
  19. #if defined(USE_GL)
  20. #ifdef HAVE_OPENGL_GL_H
  21. # include <OpenGL/gl.h>
  22. # include <GLUT/glut.h>
  23. #else
  24. # include <GL/gl.h>
  25. # include <GL/glut.h>
  26. # include <GL/freeglut_ext.h>
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include "caca.h"
  32. #include "caca_internals.h"
  33. #include "cucul.h"
  34. #include "cucul_internals.h"
  35. /*
  36. * Global variables
  37. */
  38. /* Ok, I just suck. */
  39. static GLbyte const gl_bgpal[][4] =
  40. {
  41. { 0x00, 0x00, 0x00, 0x7f },
  42. { 0x00, 0x00, 0x3f, 0x7f },
  43. { 0x00, 0x3f, 0x00, 0x7f },
  44. { 0x00, 0x3f, 0x3f, 0x7f },
  45. { 0x3f, 0x00, 0x00, 0x7f },
  46. { 0x3f, 0x00, 0x3f, 0x7f },
  47. { 0x3f, 0x3f, 0x00, 0x7f },
  48. { 0x3f, 0x3f, 0x3f, 0x7f },
  49. // + intensity
  50. // >.
  51. // ()
  52. // ^^
  53. { 0x1f, 0x1f, 0x1f, 0x7f },
  54. { 0x1f, 0x1f, 0x7f, 0x7f },
  55. { 0x1f, 0x7f, 0x1f, 0x7f },
  56. { 0x1f, 0x7f, 0x7f, 0x7f },
  57. { 0x7f, 0x1f, 0x1f, 0x7f },
  58. { 0x7f, 0x1f, 0x7f, 0x7f },
  59. { 0x7f, 0x7f, 0x1f, 0x7f },
  60. { 0x7f, 0x7f, 0x7f, 0x7f }
  61. };
  62. static caca_t *gl_kk; /* FIXME: we ought to get rid of this */
  63. /*
  64. * Local functions
  65. */
  66. static void gl_handle_keyboard(unsigned char, int, int);
  67. static void gl_handle_special_key(int, int, int);
  68. static void gl_handle_reshape(int, int);
  69. static void gl_handle_mouse(int, int, int, int);
  70. static void gl_handle_mouse_motion(int, int);
  71. static void _display(void);
  72. struct driver_private
  73. {
  74. int window;
  75. unsigned int width, height;
  76. unsigned int new_width, new_height;
  77. float font_width, font_height;
  78. float incx, incy;
  79. int id[128 - 32];
  80. unsigned char bit;
  81. unsigned char mouse_changed, mouse_clicked;
  82. unsigned int mouse_x, mouse_y;
  83. unsigned int mouse_button, mouse_state;
  84. unsigned char key;
  85. int special_key;
  86. float sw, sh;
  87. };
  88. static int gl_init_graphics(caca_t *kk)
  89. {
  90. char *empty_texture;
  91. char const *geometry;
  92. char *argv[2] = { "", NULL };
  93. unsigned int width = 0, height = 0;
  94. int argc = 1;
  95. int i;
  96. kk->drv.p = malloc(sizeof(struct driver_private));
  97. gl_kk = kk;
  98. #if defined(HAVE_GETENV)
  99. geometry = getenv("CACA_GEOMETRY");
  100. if(geometry && *geometry)
  101. sscanf(geometry, "%ux%u", &width, &height);
  102. #endif
  103. if(width && height)
  104. _cucul_set_size(kk->qq, width, height);
  105. kk->drv.p->font_width = 9;
  106. kk->drv.p->font_height = 15;
  107. kk->drv.p->width = kk->qq->width * kk->drv.p->font_width;
  108. kk->drv.p->height = kk->qq->height * kk->drv.p->font_height;
  109. kk->drv.p->bit = 0;
  110. kk->drv.p->mouse_changed = kk->drv.p->mouse_clicked = 0;
  111. kk->drv.p->mouse_button = kk->drv.p->mouse_state = 0;
  112. kk->drv.p->key = 0;
  113. kk->drv.p->special_key = 0;
  114. kk->drv.p->sw = 9.0f / 16.0f;
  115. kk->drv.p->sh = 15.0f / 16.0f;
  116. glutInit(&argc, argv);
  117. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  118. glutInitWindowSize(kk->drv.p->width, kk->drv.p->height);
  119. kk->drv.p->window = glutCreateWindow("caca for GL");
  120. gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
  121. glDisable(GL_CULL_FACE);
  122. glDisable(GL_DEPTH_TEST);
  123. glutKeyboardFunc(gl_handle_keyboard);
  124. glutSpecialFunc(gl_handle_special_key);
  125. glutReshapeFunc(gl_handle_reshape);
  126. glutDisplayFunc(_display);
  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, kk->drv.p->width, kk->drv.p->height, 0);
  135. glMatrixMode(GL_MODELVIEW);
  136. glClear(GL_COLOR_BUFFER_BIT);
  137. empty_texture = malloc(16 * 16 * 4);
  138. if(empty_texture == NULL)
  139. return -1;
  140. memset(empty_texture, 0xff, 16 * 16 * 4);
  141. glEnable(GL_TEXTURE_2D);
  142. for(i = 32; i < 128; i++)
  143. {
  144. glGenTextures(1, (GLuint*)&kk->drv.p->id[i - 32]);
  145. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[i - 32]);
  146. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  147. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  148. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,
  149. 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture);
  150. }
  151. for(i = 32; i < 128; i++)
  152. {
  153. glDisable(GL_TEXTURE_2D);
  154. glClear(GL_COLOR_BUFFER_BIT);
  155. glColor3f(1, 1, 1);
  156. glRasterPos2f(0, 15);
  157. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i);
  158. glEnable(GL_TEXTURE_2D);
  159. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[i - 32]);
  160. glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  161. 0, kk->drv.p->height - 16, 16, 16, 0);
  162. #ifdef HAVE_GLUTCHECKLOOP
  163. glutCheckLoop();
  164. #else
  165. glutMainLoopEvent();
  166. #endif
  167. glutPostRedisplay();
  168. }
  169. return 0;
  170. }
  171. static int gl_end_graphics(caca_t *kk)
  172. {
  173. glutDestroyWindow(kk->drv.p->window);
  174. free(kk->drv.p);
  175. return 0;
  176. }
  177. static int gl_set_window_title(caca_t *kk, char const *title)
  178. {
  179. glutSetWindowTitle(title);
  180. return 0;
  181. }
  182. static unsigned int gl_get_window_width(caca_t *kk)
  183. {
  184. return kk->drv.p->width;
  185. }
  186. static unsigned int gl_get_window_height(caca_t *kk)
  187. {
  188. return kk->drv.p->height;
  189. }
  190. static void gl_display(caca_t *kk)
  191. {
  192. unsigned int x, y, line;
  193. glClear(GL_COLOR_BUFFER_BIT);
  194. line = 0;
  195. for(y = 0; y < kk->drv.p->height; y += kk->drv.p->font_height)
  196. {
  197. uint8_t *attr = kk->qq->attr + line * kk->qq->width;
  198. for(x = 0; x < kk->drv.p->width; x += kk->drv.p->font_width)
  199. {
  200. glDisable(GL_TEXTURE_2D);
  201. glColor4bv(gl_bgpal[attr[0] >> 4]);
  202. glBegin(GL_QUADS);
  203. glVertex2f(x, y);
  204. glVertex2f(x + kk->drv.p->font_width, y);
  205. glVertex2f(x + kk->drv.p->font_width,
  206. y + kk->drv.p->font_height);
  207. glVertex2f(x, y + kk->drv.p->font_height);
  208. glEnd();
  209. attr++;
  210. }
  211. line++;
  212. }
  213. /* 2nd pass, avoids changing render state too much */
  214. glEnable(GL_BLEND);
  215. glEnable(GL_TEXTURE_2D);
  216. glBlendFunc(GL_ONE, GL_ONE);
  217. line = 0;
  218. for(y = 0; y < kk->drv.p->height; y += kk->drv.p->font_height)
  219. {
  220. uint8_t *attr = kk->qq->attr + line * kk->qq->width;
  221. uint32_t *chars = kk->qq->chars + line * kk->qq->width;
  222. for(x = 0; x < kk->drv.p->width; x += kk->drv.p->font_width)
  223. {
  224. uint32_t c = *chars++;
  225. if(c > 0x00000020 && c < 0x00000080)
  226. {
  227. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[c - 32]);
  228. glColor4bv(gl_bgpal[attr[0] & 0xf]);
  229. glBegin(GL_QUADS);
  230. glTexCoord2f(0, kk->drv.p->sh);
  231. glVertex2f(x, y);
  232. glTexCoord2f(kk->drv.p->sw, kk->drv.p->sh);
  233. glVertex2f(x + kk->drv.p->font_width, y);
  234. glTexCoord2f(kk->drv.p->sw, 0);
  235. glVertex2f(x + kk->drv.p->font_width,
  236. y + kk->drv.p->font_height);
  237. glTexCoord2f(0, 0);
  238. glVertex2f(x, y + kk->drv.p->font_height);
  239. glEnd();
  240. }
  241. attr++;
  242. }
  243. line++;
  244. }
  245. glDisable(GL_BLEND);
  246. glDisable(GL_TEXTURE_2D);
  247. #ifdef HAVE_GLUTCHECKLOOP
  248. glutCheckLoop();
  249. #else
  250. glutMainLoopEvent();
  251. #endif
  252. glutSwapBuffers();
  253. glutPostRedisplay();
  254. }
  255. static void gl_handle_resize(caca_t *kk)
  256. {
  257. kk->drv.p->width = kk->drv.p->new_width;
  258. kk->drv.p->height = kk->drv.p->new_height;
  259. glMatrixMode(GL_PROJECTION);
  260. glPushMatrix();
  261. glLoadIdentity();
  262. glViewport(0, 0, kk->drv.p->width, kk->drv.p->height);
  263. gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
  264. glMatrixMode(GL_MODELVIEW);
  265. }
  266. static int gl_get_event(caca_t *kk, struct caca_event *ev)
  267. {
  268. #ifdef HAVE_GLUTCHECKLOOP
  269. glutCheckLoop();
  270. #else
  271. glutMainLoopEvent();
  272. #endif
  273. if(kk->resize.resized)
  274. {
  275. ev->type = CACA_EVENT_RESIZE;
  276. ev->data.resize.w = kk->qq->width;
  277. ev->data.resize.h = kk->qq->height;
  278. return 1;
  279. }
  280. if(kk->drv.p->mouse_changed)
  281. {
  282. ev->type = CACA_EVENT_MOUSE_MOTION;
  283. ev->data.mouse.x = kk->mouse.x;
  284. ev->data.mouse.y = kk->mouse.y;
  285. kk->drv.p->mouse_changed = 0;
  286. if(kk->drv.p->mouse_clicked)
  287. {
  288. _push_event(kk, ev);
  289. ev->type = CACA_EVENT_MOUSE_PRESS;
  290. ev->data.mouse.button = kk->drv.p->mouse_button;
  291. kk->drv.p->mouse_clicked = 0;
  292. }
  293. return 1;
  294. }
  295. if(kk->drv.p->key != 0)
  296. {
  297. ev->type = CACA_EVENT_KEY_PRESS;
  298. ev->data.key.c = kk->drv.p->key;
  299. ev->data.key.ucs4 = (uint32_t)kk->drv.p->key;
  300. ev->data.key.utf8[0] = kk->drv.p->key;
  301. ev->data.key.utf8[1] = '\0';
  302. kk->drv.p->key = 0;
  303. return 1;
  304. }
  305. if(kk->drv.p->special_key != 0)
  306. {
  307. switch(kk->drv.p->special_key)
  308. {
  309. case GLUT_KEY_F1 : ev->data.key.c = CACA_KEY_F1; break;
  310. case GLUT_KEY_F2 : ev->data.key.c = CACA_KEY_F2; break;
  311. case GLUT_KEY_F3 : ev->data.key.c = CACA_KEY_F3; break;
  312. case GLUT_KEY_F4 : ev->data.key.c = CACA_KEY_F4; break;
  313. case GLUT_KEY_F5 : ev->data.key.c = CACA_KEY_F5; break;
  314. case GLUT_KEY_F6 : ev->data.key.c = CACA_KEY_F6; break;
  315. case GLUT_KEY_F7 : ev->data.key.c = CACA_KEY_F7; break;
  316. case GLUT_KEY_F8 : ev->data.key.c = CACA_KEY_F8; break;
  317. case GLUT_KEY_F9 : ev->data.key.c = CACA_KEY_F9; break;
  318. case GLUT_KEY_F10: ev->data.key.c = CACA_KEY_F10; break;
  319. case GLUT_KEY_F11: ev->data.key.c = CACA_KEY_F11; break;
  320. case GLUT_KEY_F12: ev->data.key.c = CACA_KEY_F12; break;
  321. case GLUT_KEY_LEFT : ev->data.key.c = CACA_KEY_LEFT; break;
  322. case GLUT_KEY_RIGHT: ev->data.key.c = CACA_KEY_RIGHT; break;
  323. case GLUT_KEY_UP : ev->data.key.c = CACA_KEY_UP; break;
  324. case GLUT_KEY_DOWN : ev->data.key.c = CACA_KEY_DOWN; break;
  325. default: ev->type = CACA_EVENT_NONE; return 0;
  326. }
  327. ev->type = CACA_EVENT_KEY_PRESS;
  328. ev->data.key.ucs4 = 0;
  329. ev->data.key.utf8[0] = '\0';
  330. kk->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_t *kk, 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_t *kk = gl_kk;
  349. kk->drv.p->key = key;
  350. }
  351. static void gl_handle_special_key(int key, int x, int y)
  352. {
  353. caca_t *kk = gl_kk;
  354. kk->drv.p->special_key = key;
  355. }
  356. static void gl_handle_reshape(int w, int h)
  357. {
  358. caca_t *kk = gl_kk;
  359. if(kk->drv.p->bit) /* Do not handle reshaping at the first time */
  360. {
  361. kk->drv.p->new_width = w;
  362. kk->drv.p->new_height = h;
  363. kk->resize.w = w / kk->drv.p->font_width;
  364. kk->resize.h = (h / kk->drv.p->font_height) + 1;
  365. kk->resize.resized = 1;
  366. }
  367. else
  368. kk->drv.p->bit = 1;
  369. }
  370. static void gl_handle_mouse(int button, int state, int x, int y)
  371. {
  372. caca_t *kk = gl_kk;
  373. kk->drv.p->mouse_clicked = 1;
  374. kk->drv.p->mouse_button = button;
  375. kk->drv.p->mouse_state = state;
  376. kk->drv.p->mouse_x = x / kk->drv.p->font_width;
  377. kk->drv.p->mouse_y = y / kk->drv.p->font_height;
  378. kk->mouse.x = kk->drv.p->mouse_x;
  379. kk->mouse.y = kk->drv.p->mouse_y;
  380. kk->drv.p->mouse_changed = 1;
  381. }
  382. static void gl_handle_mouse_motion(int x, int y)
  383. {
  384. caca_t *kk = gl_kk;
  385. kk->drv.p->mouse_x = x / kk->drv.p->font_width;
  386. kk->drv.p->mouse_y = y / kk->drv.p->font_height;
  387. kk->mouse.x = kk->drv.p->mouse_x;
  388. kk->mouse.y = kk->drv.p->mouse_y;
  389. kk->drv.p->mouse_changed = 1;
  390. }
  391. static void _display(void)
  392. {
  393. caca_t *kk = gl_kk;
  394. gl_display(kk);
  395. }
  396. /*
  397. * Driver initialisation
  398. */
  399. int gl_install(caca_t *kk)
  400. {
  401. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  402. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  403. return -1;
  404. #endif
  405. kk->drv.driver = CACA_DRIVER_GL;
  406. kk->drv.init_graphics = gl_init_graphics;
  407. kk->drv.end_graphics = gl_end_graphics;
  408. kk->drv.set_window_title = gl_set_window_title;
  409. kk->drv.get_window_width = gl_get_window_width;
  410. kk->drv.get_window_height = gl_get_window_height;
  411. kk->drv.display = gl_display;
  412. kk->drv.handle_resize = gl_handle_resize;
  413. kk->drv.get_event = gl_get_event;
  414. kk->drv.set_mouse = gl_set_mouse;
  415. return 0;
  416. }
  417. #endif /* USE_GL */