You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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