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.

driver_gl.c 12 KiB

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