No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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