Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

446 строки
12 KiB

  1. /*
  2. * libcaca 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. geometry = getenv("CACA_GEOMETRY");
  93. if(geometry && *geometry)
  94. sscanf(geometry, "%ux%u", &width, &height);
  95. if(width && height)
  96. _cucul_set_size(kk->qq, width, height);
  97. kk->drv.p->font_width = 9;
  98. kk->drv.p->font_height = 15;
  99. kk->drv.p->width = kk->qq->width * kk->drv.p->font_width;
  100. kk->drv.p->height = kk->qq->height * kk->drv.p->font_height;
  101. kk->drv.p->bit = 0;
  102. kk->drv.p->mouse_changed = kk->drv.p->mouse_clicked = 0;
  103. kk->drv.p->mouse_button = kk->drv.p->mouse_state = 0;
  104. kk->drv.p->key = 0;
  105. kk->drv.p->special_key = 0;
  106. kk->drv.p->sw = 9.0f / 16.0f;
  107. kk->drv.p->sh = 15.0f / 16.0f;
  108. glutInit(&argc, argv);
  109. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  110. glutInitWindowSize(kk->drv.p->width, kk->drv.p->height);
  111. kk->drv.p->window = glutCreateWindow("caca for GL");
  112. gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
  113. glDisable(GL_CULL_FACE);
  114. glDisable(GL_DEPTH_TEST);
  115. glutKeyboardFunc(gl_handle_keyboard);
  116. glutSpecialFunc(gl_handle_special_key);
  117. glutReshapeFunc(gl_handle_reshape);
  118. glutMouseFunc(gl_handle_mouse);
  119. glutMotionFunc(gl_handle_mouse_motion);
  120. glutPassiveMotionFunc(gl_handle_mouse_motion);
  121. glLoadIdentity();
  122. glMatrixMode(GL_PROJECTION);
  123. glPushMatrix();
  124. glLoadIdentity();
  125. gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
  126. glMatrixMode(GL_MODELVIEW);
  127. glClear(GL_COLOR_BUFFER_BIT);
  128. empty_texture = malloc(16 * 16 * 4);
  129. if(empty_texture == NULL)
  130. return -1;
  131. memset(empty_texture, 0xff, 16 * 16 * 4);
  132. glEnable(GL_TEXTURE_2D);
  133. for(i = 32; i < 128; i++)
  134. {
  135. glGenTextures(1, (GLuint*)&kk->drv.p->id[i - 32]);
  136. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[i - 32]);
  137. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  138. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  139. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,
  140. 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture);
  141. }
  142. for(i = 32; i < 128; i++)
  143. {
  144. glDisable(GL_TEXTURE_2D);
  145. glClear(GL_COLOR_BUFFER_BIT);
  146. glColor3f(1, 1, 1);
  147. glRasterPos2f(0, 15);
  148. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i);
  149. glEnable(GL_TEXTURE_2D);
  150. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[i - 32]);
  151. glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  152. 0, kk->drv.p->height - 16, 16, 16, 0);
  153. glutMainLoopEvent();
  154. glutPostRedisplay();
  155. }
  156. return 0;
  157. }
  158. static int gl_end_graphics(caca_t *kk)
  159. {
  160. glutDestroyWindow(kk->drv.p->window);
  161. free(kk->drv.p);
  162. return 0;
  163. }
  164. static int gl_set_window_title(caca_t *kk, char const *title)
  165. {
  166. glutSetWindowTitle(title);
  167. return 0;
  168. }
  169. static unsigned int gl_get_window_width(caca_t *kk)
  170. {
  171. return kk->drv.p->width;
  172. }
  173. static unsigned int gl_get_window_height(caca_t *kk)
  174. {
  175. return kk->drv.p->height;
  176. }
  177. static void gl_display(caca_t *kk)
  178. {
  179. unsigned int x, y, line;
  180. glClear(GL_COLOR_BUFFER_BIT);
  181. line = 0;
  182. for(y = 0; y < kk->drv.p->height; y += kk->drv.p->font_height)
  183. {
  184. uint8_t *attr = kk->qq->attr + line * kk->qq->width;
  185. for(x = 0; x < kk->drv.p->width; x += kk->drv.p->font_width)
  186. {
  187. glDisable(GL_TEXTURE_2D);
  188. glColor4bv(gl_bgpal[attr[0] >> 4]);
  189. glBegin(GL_QUADS);
  190. glVertex2f(x, y);
  191. glVertex2f(x + kk->drv.p->font_width, y);
  192. glVertex2f(x + kk->drv.p->font_width,
  193. y + kk->drv.p->font_height);
  194. glVertex2f(x, y + kk->drv.p->font_height);
  195. glEnd();
  196. attr++;
  197. }
  198. line++;
  199. }
  200. /* 2nd pass, avoids changing render state too much */
  201. glEnable(GL_BLEND);
  202. glEnable(GL_TEXTURE_2D);
  203. glBlendFunc(GL_ONE, GL_ONE);
  204. line = 0;
  205. for(y = 0; y < kk->drv.p->height; y += kk->drv.p->font_height)
  206. {
  207. uint8_t *attr = kk->qq->attr + line * kk->qq->width;
  208. uint32_t *chars = kk->qq->chars + line * kk->qq->width;
  209. for(x = 0; x < kk->drv.p->width; x += kk->drv.p->font_width)
  210. {
  211. uint32_t c = *chars++;
  212. if(c > 0x00000020 && c < 0x00000080)
  213. {
  214. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[c - 32]);
  215. glColor4bv(gl_bgpal[attr[0] & 0xf]);
  216. glBegin(GL_QUADS);
  217. glTexCoord2f(0, kk->drv.p->sh);
  218. glVertex2f(x, y);
  219. glTexCoord2f(kk->drv.p->sw, kk->drv.p->sh);
  220. glVertex2f(x + kk->drv.p->font_width, y);
  221. glTexCoord2f(kk->drv.p->sw, 0);
  222. glVertex2f(x + kk->drv.p->font_width,
  223. y + kk->drv.p->font_height);
  224. glTexCoord2f(0, 0);
  225. glVertex2f(x, y + kk->drv.p->font_height);
  226. glEnd();
  227. }
  228. attr++;
  229. }
  230. line++;
  231. }
  232. glDisable(GL_BLEND);
  233. glDisable(GL_TEXTURE_2D);
  234. glutMainLoopEvent();
  235. glutSwapBuffers();
  236. glutPostRedisplay();
  237. }
  238. static void gl_handle_resize(caca_t *kk)
  239. {
  240. kk->drv.p->width = kk->drv.p->new_width;
  241. kk->drv.p->height = kk->drv.p->new_height;
  242. glMatrixMode(GL_PROJECTION);
  243. glPushMatrix();
  244. glLoadIdentity();
  245. glViewport(0, 0, kk->drv.p->width, kk->drv.p->height);
  246. gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
  247. glMatrixMode(GL_MODELVIEW);
  248. }
  249. static unsigned int gl_get_event(caca_t *kk)
  250. {
  251. unsigned int event = 0;
  252. glutMainLoopEvent();
  253. if(kk->resize.resized)
  254. return CACA_EVENT_RESIZE;
  255. if(kk->drv.p->mouse_changed)
  256. {
  257. if(kk->drv.p->mouse_clicked)
  258. {
  259. event |= CACA_EVENT_MOUSE_PRESS | kk->drv.p->mouse_button;
  260. kk->drv.p->mouse_clicked = 0;
  261. }
  262. kk->mouse.x = kk->drv.p->mouse_x;
  263. kk->mouse.y = kk->drv.p->mouse_y;
  264. event |= CACA_EVENT_MOUSE_MOTION | (kk->mouse.x << 12) | kk->mouse.y;
  265. kk->drv.p->mouse_changed = 0;
  266. }
  267. if(kk->drv.p->key != 0)
  268. {
  269. event |= CACA_EVENT_KEY_PRESS;
  270. event |= kk->drv.p->key;
  271. kk->drv.p->key = 0;
  272. return event;
  273. }
  274. if(kk->drv.p->special_key != 0)
  275. {
  276. event |= CACA_EVENT_KEY_PRESS;
  277. switch(kk->drv.p->special_key)
  278. {
  279. case GLUT_KEY_F1 : kk->drv.p->special_key = 0; return event | CACA_KEY_F1;
  280. case GLUT_KEY_F2 : kk->drv.p->special_key = 0; return event | CACA_KEY_F2;
  281. case GLUT_KEY_F3 : kk->drv.p->special_key = 0; return event | CACA_KEY_F3;
  282. case GLUT_KEY_F4 : kk->drv.p->special_key = 0; return event | CACA_KEY_F4;
  283. case GLUT_KEY_F5 : kk->drv.p->special_key = 0; return event | CACA_KEY_F5;
  284. case GLUT_KEY_F6 : kk->drv.p->special_key = 0; return event | CACA_KEY_F6;
  285. case GLUT_KEY_F7 : kk->drv.p->special_key = 0; return event | CACA_KEY_F7;
  286. case GLUT_KEY_F8 : kk->drv.p->special_key = 0; return event | CACA_KEY_F8;
  287. case GLUT_KEY_F9 : kk->drv.p->special_key = 0; return event | CACA_KEY_F9;
  288. case GLUT_KEY_F10: kk->drv.p->special_key = 0; return event | CACA_KEY_F10;
  289. case GLUT_KEY_F11: kk->drv.p->special_key = 0; return event | CACA_KEY_F11;
  290. case GLUT_KEY_F12: kk->drv.p->special_key = 0; return event | CACA_KEY_F12;
  291. case GLUT_KEY_LEFT : kk->drv.p->special_key = 0; return event | CACA_KEY_LEFT;
  292. case GLUT_KEY_RIGHT: kk->drv.p->special_key = 0; return event | CACA_KEY_RIGHT;
  293. case GLUT_KEY_UP : kk->drv.p->special_key = 0; return event | CACA_KEY_UP;
  294. case GLUT_KEY_DOWN : kk->drv.p->special_key = 0; return event | CACA_KEY_DOWN;
  295. default: return CACA_EVENT_NONE;
  296. }
  297. }
  298. return CACA_EVENT_NONE;
  299. }
  300. /*
  301. * XXX: following functions are local
  302. */
  303. static void gl_handle_keyboard(unsigned char key, int x, int y)
  304. {
  305. caca_t *kk = gl_kk;
  306. kk->drv.p->key = key;
  307. }
  308. static void gl_handle_special_key(int key, int x, int y)
  309. {
  310. caca_t *kk = gl_kk;
  311. kk->drv.p->special_key = key;
  312. }
  313. static void gl_handle_reshape(int w, int h)
  314. {
  315. caca_t *kk = gl_kk;
  316. if(kk->drv.p->bit) /* Do not handle reshaping at the first time */
  317. {
  318. kk->drv.p->new_width = w;
  319. kk->drv.p->new_height = h;
  320. kk->resize.w = w / kk->drv.p->font_width;
  321. kk->resize.h = (h / kk->drv.p->font_height) + 1;
  322. kk->resize.resized = 1;
  323. }
  324. else
  325. kk->drv.p->bit = 1;
  326. }
  327. static void gl_handle_mouse(int button, int state, int x, int y)
  328. {
  329. caca_t *kk = gl_kk;
  330. kk->drv.p->mouse_clicked = 1;
  331. kk->drv.p->mouse_button = button;
  332. kk->drv.p->mouse_state = state;
  333. kk->drv.p->mouse_x = x / kk->drv.p->font_width;
  334. kk->drv.p->mouse_y = y / kk->drv.p->font_height;
  335. kk->drv.p->mouse_changed = 1;
  336. }
  337. static void gl_handle_mouse_motion(int x, int y)
  338. {
  339. caca_t *kk = gl_kk;
  340. kk->drv.p->mouse_x = x / kk->drv.p->font_width;
  341. kk->drv.p->mouse_y = y / kk->drv.p->font_height;
  342. kk->drv.p->mouse_changed = 1;
  343. }
  344. /*
  345. * Driver initialisation
  346. */
  347. void gl_init_driver(caca_t *kk)
  348. {
  349. kk->drv.driver = CACA_DRIVER_GL;
  350. kk->drv.init_graphics = gl_init_graphics;
  351. kk->drv.end_graphics = gl_end_graphics;
  352. kk->drv.set_window_title = gl_set_window_title;
  353. kk->drv.get_window_width = gl_get_window_width;
  354. kk->drv.get_window_height = gl_get_window_height;
  355. kk->drv.display = gl_display;
  356. kk->drv.handle_resize = gl_handle_resize;
  357. kk->drv.get_event = gl_get_event;
  358. }
  359. #endif /* USE_GL */