Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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