您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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