25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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