Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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