1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: driver_gl.c 1072 2006-09-19 19:11:33Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * This file contains the libcaca OpenGL input and output driver |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | #if defined(USE_GL) |
---|
22 | |
---|
23 | #ifdef HAVE_OPENGL_GL_H |
---|
24 | # include <OpenGL/gl.h> |
---|
25 | # include <GLUT/glut.h> |
---|
26 | #else |
---|
27 | # include <GL/gl.h> |
---|
28 | # include <GL/glut.h> |
---|
29 | # include <GL/freeglut_ext.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | #include <string.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <stdio.h> |
---|
35 | |
---|
36 | #include "caca.h" |
---|
37 | #include "caca_internals.h" |
---|
38 | #include "cucul.h" |
---|
39 | #include "cucul_internals.h" |
---|
40 | |
---|
41 | |
---|
42 | /* |
---|
43 | * Global variables |
---|
44 | */ |
---|
45 | |
---|
46 | static caca_display_t *gl_d; /* FIXME: we ought to get rid of this */ |
---|
47 | |
---|
48 | /* |
---|
49 | * Local functions |
---|
50 | */ |
---|
51 | static void gl_handle_keyboard(unsigned char, int, int); |
---|
52 | static void gl_handle_special_key(int, int, int); |
---|
53 | static void gl_handle_reshape(int, int); |
---|
54 | static void gl_handle_mouse(int, int, int, int); |
---|
55 | static void gl_handle_mouse_motion(int, int); |
---|
56 | #ifdef HAVE_GLUTCLOSEFUNC |
---|
57 | static void gl_handle_close(void); |
---|
58 | #endif |
---|
59 | static void _display(void); |
---|
60 | static void gl_compute_font(caca_display_t *); |
---|
61 | |
---|
62 | struct driver_private |
---|
63 | { |
---|
64 | int window; |
---|
65 | unsigned int width, height; |
---|
66 | unsigned int new_width, new_height; |
---|
67 | cucul_font_t *f; |
---|
68 | float font_width, font_height; |
---|
69 | float incx, incy; |
---|
70 | unsigned long int const *blocks; |
---|
71 | int *txid; |
---|
72 | unsigned char close; |
---|
73 | unsigned char bit; |
---|
74 | unsigned char mouse_changed, mouse_clicked; |
---|
75 | unsigned int mouse_x, mouse_y; |
---|
76 | unsigned int mouse_button, mouse_state; |
---|
77 | |
---|
78 | unsigned char key; |
---|
79 | int special_key; |
---|
80 | |
---|
81 | float sw, sh; |
---|
82 | }; |
---|
83 | |
---|
84 | static int gl_init_graphics(caca_display_t *dp) |
---|
85 | { |
---|
86 | char const *geometry; |
---|
87 | char *argv[2] = { "", NULL }; |
---|
88 | char const * const * fonts; |
---|
89 | unsigned int width = 0, height = 0; |
---|
90 | int argc = 1; |
---|
91 | |
---|
92 | dp->drv.p = malloc(sizeof(struct driver_private)); |
---|
93 | |
---|
94 | gl_d = dp; |
---|
95 | |
---|
96 | #if defined(HAVE_GETENV) |
---|
97 | geometry = getenv("CACA_GEOMETRY"); |
---|
98 | if(geometry && *geometry) |
---|
99 | sscanf(geometry, "%ux%u", &width, &height); |
---|
100 | #endif |
---|
101 | |
---|
102 | if(width && height) |
---|
103 | _cucul_set_canvas_size(dp->cv, width, height); |
---|
104 | |
---|
105 | |
---|
106 | /* Load a libcucul internal font */ |
---|
107 | fonts = cucul_get_font_list(); |
---|
108 | if(fonts[0] == NULL) |
---|
109 | { |
---|
110 | fprintf(stderr, "error: libcucul was compiled without any fonts\n"); |
---|
111 | return -1; |
---|
112 | } |
---|
113 | dp->drv.p->f = cucul_load_font(fonts[0], 0); |
---|
114 | if(dp->drv.p->f == NULL) |
---|
115 | { |
---|
116 | fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]); |
---|
117 | return -1; |
---|
118 | } |
---|
119 | |
---|
120 | dp->drv.p->font_width = cucul_get_font_width(dp->drv.p->f); |
---|
121 | dp->drv.p->font_height = cucul_get_font_height(dp->drv.p->f); |
---|
122 | |
---|
123 | dp->drv.p->width = dp->cv->width * dp->drv.p->font_width; |
---|
124 | dp->drv.p->height = dp->cv->height * dp->drv.p->font_height; |
---|
125 | |
---|
126 | #ifdef HAVE_GLUTCLOSEFUNC |
---|
127 | dp->drv.p->close = 0; |
---|
128 | #endif |
---|
129 | dp->drv.p->bit = 0; |
---|
130 | |
---|
131 | dp->drv.p->mouse_changed = dp->drv.p->mouse_clicked = 0; |
---|
132 | dp->drv.p->mouse_button = dp->drv.p->mouse_state = 0; |
---|
133 | |
---|
134 | dp->drv.p->key = 0; |
---|
135 | dp->drv.p->special_key = 0; |
---|
136 | |
---|
137 | dp->drv.p->sw = ((float)dp->drv.p->font_width) / 16.0f; |
---|
138 | dp->drv.p->sh = ((float)dp->drv.p->font_height) / 16.0f; |
---|
139 | |
---|
140 | glutInit(&argc, argv); |
---|
141 | |
---|
142 | glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); |
---|
143 | glutInitWindowSize(dp->drv.p->width, dp->drv.p->height); |
---|
144 | dp->drv.p->window = glutCreateWindow("caca for GL"); |
---|
145 | |
---|
146 | gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0); |
---|
147 | |
---|
148 | glDisable(GL_CULL_FACE); |
---|
149 | glDisable(GL_DEPTH_TEST); |
---|
150 | |
---|
151 | glutKeyboardFunc(gl_handle_keyboard); |
---|
152 | glutSpecialFunc(gl_handle_special_key); |
---|
153 | glutReshapeFunc(gl_handle_reshape); |
---|
154 | glutDisplayFunc(_display); |
---|
155 | |
---|
156 | #ifdef HAVE_GLUTCLOSEFUNC |
---|
157 | glutCloseFunc(gl_handle_close); |
---|
158 | #endif |
---|
159 | |
---|
160 | glutMouseFunc(gl_handle_mouse); |
---|
161 | glutMotionFunc(gl_handle_mouse_motion); |
---|
162 | glutPassiveMotionFunc(gl_handle_mouse_motion); |
---|
163 | |
---|
164 | glLoadIdentity(); |
---|
165 | |
---|
166 | glMatrixMode(GL_PROJECTION); |
---|
167 | glPushMatrix(); |
---|
168 | glLoadIdentity(); |
---|
169 | gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0); |
---|
170 | |
---|
171 | glMatrixMode(GL_MODELVIEW); |
---|
172 | |
---|
173 | glClear(GL_COLOR_BUFFER_BIT); |
---|
174 | glEnable(GL_TEXTURE_2D); |
---|
175 | glEnable(GL_BLEND); |
---|
176 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
177 | |
---|
178 | glEnable(GL_TEXTURE_2D); |
---|
179 | |
---|
180 | gl_compute_font(dp); |
---|
181 | |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | static int gl_end_graphics(caca_display_t *dp) |
---|
186 | { |
---|
187 | glutDestroyWindow(dp->drv.p->window); |
---|
188 | free(dp->drv.p->txid); |
---|
189 | free(dp->drv.p); |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | static int gl_set_display_title(caca_display_t *dp, char const *title) |
---|
194 | { |
---|
195 | glutSetWindowTitle(title); |
---|
196 | return 0; |
---|
197 | } |
---|
198 | |
---|
199 | static unsigned int gl_get_display_width(caca_display_t *dp) |
---|
200 | { |
---|
201 | return dp->drv.p->width; |
---|
202 | } |
---|
203 | |
---|
204 | static unsigned int gl_get_display_height(caca_display_t *dp) |
---|
205 | { |
---|
206 | return dp->drv.p->height; |
---|
207 | } |
---|
208 | |
---|
209 | static void gl_display(caca_display_t *dp) |
---|
210 | { |
---|
211 | unsigned int x, y, line; |
---|
212 | |
---|
213 | glClear(GL_COLOR_BUFFER_BIT); |
---|
214 | glDisable(GL_TEXTURE_2D); |
---|
215 | glDisable(GL_BLEND); |
---|
216 | line = 0; |
---|
217 | for(y = 0; y < dp->drv.p->height; y += dp->drv.p->font_height) |
---|
218 | { |
---|
219 | uint32_t *attr = dp->cv->attr + line * dp->cv->width; |
---|
220 | |
---|
221 | for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width) |
---|
222 | { |
---|
223 | uint16_t bg = _cucul_argb32_to_rgb12bg(*attr++); |
---|
224 | |
---|
225 | glColor4b(((bg & 0xf00) >> 8) * 8, |
---|
226 | ((bg & 0x0f0) >> 4) * 8, |
---|
227 | (bg & 0x00f) * 8, |
---|
228 | 0xff); |
---|
229 | glBegin(GL_QUADS); |
---|
230 | glVertex2f(x, y); |
---|
231 | glVertex2f(x + dp->drv.p->font_width, y); |
---|
232 | glVertex2f(x + dp->drv.p->font_width, |
---|
233 | y + dp->drv.p->font_height); |
---|
234 | glVertex2f(x, y + dp->drv.p->font_height); |
---|
235 | glEnd(); |
---|
236 | } |
---|
237 | |
---|
238 | line++; |
---|
239 | } |
---|
240 | |
---|
241 | /* 2nd pass, avoids changing render state too much */ |
---|
242 | glEnable(GL_TEXTURE_2D); |
---|
243 | glEnable(GL_BLEND); |
---|
244 | |
---|
245 | line = 0; |
---|
246 | for(y = 0; y < dp->drv.p->height; y += dp->drv.p->font_height, line++) |
---|
247 | { |
---|
248 | uint32_t *attr = dp->cv->attr + line * dp->cv->width; |
---|
249 | uint32_t *chars = dp->cv->chars + line * dp->cv->width; |
---|
250 | |
---|
251 | for(x = 0; x < dp->drv.p->width; x += dp->drv.p->font_width, attr++) |
---|
252 | { |
---|
253 | uint32_t cv = *chars++; |
---|
254 | uint16_t fg; |
---|
255 | int i, b; |
---|
256 | |
---|
257 | for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2) |
---|
258 | { |
---|
259 | if(cv < (uint32_t)dp->drv.p->blocks[i]) |
---|
260 | break; |
---|
261 | |
---|
262 | if(cv >= (uint32_t)dp->drv.p->blocks[i + 1]) |
---|
263 | { |
---|
264 | b += (uint32_t)(dp->drv.p->blocks[i + 1] |
---|
265 | - dp->drv.p->blocks[i]); |
---|
266 | continue; |
---|
267 | } |
---|
268 | |
---|
269 | glBindTexture(GL_TEXTURE_2D, |
---|
270 | dp->drv.p->txid[b + cv |
---|
271 | - (uint32_t)dp->drv.p->blocks[i]]); |
---|
272 | |
---|
273 | fg = _cucul_argb32_to_rgb12fg(*attr); |
---|
274 | glColor3b(((fg & 0xf00) >> 8) * 8, |
---|
275 | ((fg & 0x0f0) >> 4) * 8, |
---|
276 | (fg & 0x00f) * 8); |
---|
277 | glBegin(GL_QUADS); |
---|
278 | glTexCoord2f(0, dp->drv.p->sh); |
---|
279 | glVertex2f(x, y); |
---|
280 | glTexCoord2f(dp->drv.p->sw, dp->drv.p->sh); |
---|
281 | glVertex2f(x + dp->drv.p->font_width, y); |
---|
282 | glTexCoord2f(dp->drv.p->sw, 0); |
---|
283 | glVertex2f(x + dp->drv.p->font_width, |
---|
284 | y + dp->drv.p->font_height); |
---|
285 | glTexCoord2f(0, 0); |
---|
286 | glVertex2f(x, y + dp->drv.p->font_height); |
---|
287 | glEnd(); |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | #ifdef HAVE_GLUTCHECKLOOP |
---|
293 | glutCheckLoop(); |
---|
294 | #else |
---|
295 | glutMainLoopEvent(); |
---|
296 | #endif |
---|
297 | glutSwapBuffers(); |
---|
298 | glutPostRedisplay(); |
---|
299 | } |
---|
300 | |
---|
301 | static void gl_handle_resize(caca_display_t *dp) |
---|
302 | { |
---|
303 | dp->drv.p->width = dp->drv.p->new_width; |
---|
304 | dp->drv.p->height = dp->drv.p->new_height; |
---|
305 | |
---|
306 | glMatrixMode(GL_PROJECTION); |
---|
307 | glPushMatrix(); |
---|
308 | glLoadIdentity(); |
---|
309 | |
---|
310 | glViewport(0, 0, dp->drv.p->width, dp->drv.p->height); |
---|
311 | gluOrtho2D(0, dp->drv.p->width, dp->drv.p->height, 0); |
---|
312 | glMatrixMode(GL_MODELVIEW); |
---|
313 | } |
---|
314 | |
---|
315 | static int gl_get_event(caca_display_t *dp, caca_event_t *ev) |
---|
316 | { |
---|
317 | #ifdef HAVE_GLUTCHECKLOOP |
---|
318 | glutCheckLoop(); |
---|
319 | #else |
---|
320 | glutMainLoopEvent(); |
---|
321 | #endif |
---|
322 | |
---|
323 | #ifdef HAVE_GLUTCLOSEFUNC |
---|
324 | if(dp->drv.p->close) |
---|
325 | { |
---|
326 | dp->drv.p->close = 0; |
---|
327 | ev->type = CACA_EVENT_QUIT; |
---|
328 | return 1; |
---|
329 | } |
---|
330 | #endif |
---|
331 | |
---|
332 | if(dp->resize.resized) |
---|
333 | { |
---|
334 | ev->type = CACA_EVENT_RESIZE; |
---|
335 | ev->data.resize.w = dp->cv->width; |
---|
336 | ev->data.resize.h = dp->cv->height; |
---|
337 | return 1; |
---|
338 | } |
---|
339 | |
---|
340 | if(dp->drv.p->mouse_changed) |
---|
341 | { |
---|
342 | ev->type = CACA_EVENT_MOUSE_MOTION; |
---|
343 | ev->data.mouse.x = dp->mouse.x; |
---|
344 | ev->data.mouse.y = dp->mouse.y; |
---|
345 | dp->drv.p->mouse_changed = 0; |
---|
346 | |
---|
347 | if(dp->drv.p->mouse_clicked) |
---|
348 | { |
---|
349 | _push_event(dp, ev); |
---|
350 | ev->type = CACA_EVENT_MOUSE_PRESS; |
---|
351 | ev->data.mouse.button = dp->drv.p->mouse_button; |
---|
352 | dp->drv.p->mouse_clicked = 0; |
---|
353 | } |
---|
354 | |
---|
355 | return 1; |
---|
356 | } |
---|
357 | |
---|
358 | if(dp->drv.p->key != 0) |
---|
359 | { |
---|
360 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
361 | ev->data.key.ch = dp->drv.p->key; |
---|
362 | ev->data.key.utf32 = (uint32_t)dp->drv.p->key; |
---|
363 | ev->data.key.utf8[0] = dp->drv.p->key; |
---|
364 | ev->data.key.utf8[1] = '\0'; |
---|
365 | dp->drv.p->key = 0; |
---|
366 | return 1; |
---|
367 | } |
---|
368 | |
---|
369 | if(dp->drv.p->special_key != 0) |
---|
370 | { |
---|
371 | switch(dp->drv.p->special_key) |
---|
372 | { |
---|
373 | case GLUT_KEY_F1 : ev->data.key.ch = CACA_KEY_F1; break; |
---|
374 | case GLUT_KEY_F2 : ev->data.key.ch = CACA_KEY_F2; break; |
---|
375 | case GLUT_KEY_F3 : ev->data.key.ch = CACA_KEY_F3; break; |
---|
376 | case GLUT_KEY_F4 : ev->data.key.ch = CACA_KEY_F4; break; |
---|
377 | case GLUT_KEY_F5 : ev->data.key.ch = CACA_KEY_F5; break; |
---|
378 | case GLUT_KEY_F6 : ev->data.key.ch = CACA_KEY_F6; break; |
---|
379 | case GLUT_KEY_F7 : ev->data.key.ch = CACA_KEY_F7; break; |
---|
380 | case GLUT_KEY_F8 : ev->data.key.ch = CACA_KEY_F8; break; |
---|
381 | case GLUT_KEY_F9 : ev->data.key.ch = CACA_KEY_F9; break; |
---|
382 | case GLUT_KEY_F10: ev->data.key.ch = CACA_KEY_F10; break; |
---|
383 | case GLUT_KEY_F11: ev->data.key.ch = CACA_KEY_F11; break; |
---|
384 | case GLUT_KEY_F12: ev->data.key.ch = CACA_KEY_F12; break; |
---|
385 | case GLUT_KEY_LEFT : ev->data.key.ch = CACA_KEY_LEFT; break; |
---|
386 | case GLUT_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break; |
---|
387 | case GLUT_KEY_UP : ev->data.key.ch = CACA_KEY_UP; break; |
---|
388 | case GLUT_KEY_DOWN : ev->data.key.ch = CACA_KEY_DOWN; break; |
---|
389 | case GLUT_KEY_PAGE_UP : ev->data.key.ch = CACA_KEY_PAGEUP; break; |
---|
390 | case GLUT_KEY_PAGE_DOWN : ev->data.key.ch = CACA_KEY_PAGEDOWN; |
---|
391 | break; |
---|
392 | case GLUT_KEY_HOME : ev->data.key.ch = CACA_KEY_HOME; break; |
---|
393 | case GLUT_KEY_END : ev->data.key.ch = CACA_KEY_END; break; |
---|
394 | case GLUT_KEY_INSERT : ev->data.key.ch = CACA_KEY_INSERT; break; |
---|
395 | |
---|
396 | default: ev->type = CACA_EVENT_NONE; return 0; |
---|
397 | } |
---|
398 | |
---|
399 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
400 | ev->data.key.utf32 = 0; |
---|
401 | ev->data.key.utf8[0] = '\0'; |
---|
402 | |
---|
403 | dp->drv.p->special_key = 0; |
---|
404 | return 1; |
---|
405 | } |
---|
406 | |
---|
407 | ev->type = CACA_EVENT_NONE; |
---|
408 | return 0; |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | static void gl_set_mouse(caca_display_t *dp, int flag) |
---|
413 | { |
---|
414 | if(flag) |
---|
415 | glutSetCursor(GLUT_CURSOR_RIGHT_ARROW); |
---|
416 | else |
---|
417 | glutSetCursor(GLUT_CURSOR_NONE); |
---|
418 | } |
---|
419 | |
---|
420 | /* |
---|
421 | * XXX: following functions are local |
---|
422 | */ |
---|
423 | |
---|
424 | static void gl_handle_keyboard(unsigned char key, int x, int y) |
---|
425 | { |
---|
426 | caca_display_t *dp = gl_d; |
---|
427 | |
---|
428 | dp->drv.p->key = key; |
---|
429 | } |
---|
430 | |
---|
431 | static void gl_handle_special_key(int key, int x, int y) |
---|
432 | { |
---|
433 | caca_display_t *dp = gl_d; |
---|
434 | |
---|
435 | dp->drv.p->special_key = key; |
---|
436 | } |
---|
437 | |
---|
438 | static void gl_handle_reshape(int w, int h) |
---|
439 | { |
---|
440 | caca_display_t *dp = gl_d; |
---|
441 | |
---|
442 | if(dp->drv.p->bit) /* Do not handle reshaping at the first time */ |
---|
443 | { |
---|
444 | dp->drv.p->new_width = w; |
---|
445 | dp->drv.p->new_height = h; |
---|
446 | |
---|
447 | dp->resize.w = w / dp->drv.p->font_width; |
---|
448 | dp->resize.h = (h / dp->drv.p->font_height) + 1; |
---|
449 | |
---|
450 | dp->resize.resized = 1; |
---|
451 | } |
---|
452 | else |
---|
453 | dp->drv.p->bit = 1; |
---|
454 | } |
---|
455 | |
---|
456 | static void gl_handle_mouse(int button, int state, int x, int y) |
---|
457 | { |
---|
458 | caca_display_t *dp = gl_d; |
---|
459 | |
---|
460 | dp->drv.p->mouse_clicked = 1; |
---|
461 | dp->drv.p->mouse_button = button; |
---|
462 | dp->drv.p->mouse_state = state; |
---|
463 | dp->drv.p->mouse_x = x / dp->drv.p->font_width; |
---|
464 | dp->drv.p->mouse_y = y / dp->drv.p->font_height; |
---|
465 | dp->mouse.x = dp->drv.p->mouse_x; |
---|
466 | dp->mouse.y = dp->drv.p->mouse_y; |
---|
467 | dp->drv.p->mouse_changed = 1; |
---|
468 | } |
---|
469 | |
---|
470 | static void gl_handle_mouse_motion(int x, int y) |
---|
471 | { |
---|
472 | caca_display_t *dp = gl_d; |
---|
473 | dp->drv.p->mouse_x = x / dp->drv.p->font_width; |
---|
474 | dp->drv.p->mouse_y = y / dp->drv.p->font_height; |
---|
475 | dp->mouse.x = dp->drv.p->mouse_x; |
---|
476 | dp->mouse.y = dp->drv.p->mouse_y; |
---|
477 | dp->drv.p->mouse_changed = 1; |
---|
478 | } |
---|
479 | |
---|
480 | #ifdef HAVE_GLUTCLOSEFUNC |
---|
481 | static void gl_handle_close(void) |
---|
482 | { |
---|
483 | caca_display_t *dp = gl_d; |
---|
484 | dp->drv.p->close = 1; |
---|
485 | } |
---|
486 | #endif |
---|
487 | |
---|
488 | static void _display(void) |
---|
489 | { |
---|
490 | caca_display_t *dp = gl_d; |
---|
491 | gl_display(dp); |
---|
492 | } |
---|
493 | |
---|
494 | static void gl_compute_font(caca_display_t *dp) |
---|
495 | { |
---|
496 | cucul_canvas_t *cv; |
---|
497 | uint32_t *image; |
---|
498 | int i, b, w, h, x, y; |
---|
499 | |
---|
500 | /* Count how many glyphs this font has */ |
---|
501 | dp->drv.p->blocks = cucul_get_font_blocks(dp->drv.p->f); |
---|
502 | |
---|
503 | for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2) |
---|
504 | b += (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]); |
---|
505 | |
---|
506 | /* Allocate a libcucul canvas and print all the glyphs on it */ |
---|
507 | cv = cucul_create_canvas(1, b); |
---|
508 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLACK); |
---|
509 | |
---|
510 | for(b = 0, i = 0; dp->drv.p->blocks[i + 1]; i += 2) |
---|
511 | { |
---|
512 | int j, n = (int)(dp->drv.p->blocks[i + 1] - dp->drv.p->blocks[i]); |
---|
513 | |
---|
514 | for(j = 0; j < n; j++) |
---|
515 | cucul_putchar(cv, 0, b + j, dp->drv.p->blocks[i] + j); |
---|
516 | |
---|
517 | b += n; |
---|
518 | } |
---|
519 | |
---|
520 | /* Draw the cucul canvas onto an image buffer */ |
---|
521 | image = malloc(b * dp->drv.p->font_height |
---|
522 | * dp->drv.p->font_width * sizeof(uint32_t)); |
---|
523 | cucul_render_canvas(cv, dp->drv.p->f, image, dp->drv.p->font_width, |
---|
524 | b * dp->drv.p->font_height, 4 * dp->drv.p->font_width); |
---|
525 | cucul_free_canvas(cv); |
---|
526 | |
---|
527 | /* Convert all glyphs in the image buffer to GL textures */ |
---|
528 | dp->drv.p->txid = malloc(b * sizeof(int)); |
---|
529 | |
---|
530 | w = dp->drv.p->font_width <= 16 ? dp->drv.p->font_width : 16; |
---|
531 | h = dp->drv.p->font_height <= 16 ? dp->drv.p->font_height : 16; |
---|
532 | |
---|
533 | for(i = 0; i < b; i++) |
---|
534 | { |
---|
535 | uint8_t tmp[16 * 4 * 16]; |
---|
536 | uint32_t *glyph = image + (int)(i * dp->drv.p->font_width |
---|
537 | * dp->drv.p->font_height); |
---|
538 | memset(tmp, 0, 16 * 4 * 16); |
---|
539 | |
---|
540 | for(y = 0; y < h; y++) |
---|
541 | { |
---|
542 | for(x = 0; x < w; x++) |
---|
543 | { |
---|
544 | uint32_t offset = x + (15 - y) * 16; |
---|
545 | uint8_t c = glyph[x + y * (int)dp->drv.p->font_width] >> 8; |
---|
546 | tmp[offset * 4] = c; |
---|
547 | tmp[offset * 4 + 1] = c; |
---|
548 | tmp[offset * 4 + 2] = c; |
---|
549 | tmp[offset * 4 + 3] = c; |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | glGenTextures(1, (GLuint*)&dp->drv.p->txid[i]); |
---|
554 | glBindTexture(GL_TEXTURE_2D, dp->drv.p->txid[i]); |
---|
555 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
556 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
557 | glTexImage2D(GL_TEXTURE_2D, 0, 4, 16, 16, 0, |
---|
558 | GL_RGBA, GL_UNSIGNED_BYTE, tmp); |
---|
559 | } |
---|
560 | |
---|
561 | free(image); |
---|
562 | } |
---|
563 | |
---|
564 | /* |
---|
565 | * Driver initialisation |
---|
566 | */ |
---|
567 | |
---|
568 | int gl_install(caca_display_t *dp) |
---|
569 | { |
---|
570 | #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION) |
---|
571 | if(!getenv("DISPLAY") || !*(getenv("DISPLAY"))) |
---|
572 | return -1; |
---|
573 | #endif |
---|
574 | |
---|
575 | dp->drv.driver = CACA_DRIVER_GL; |
---|
576 | |
---|
577 | dp->drv.init_graphics = gl_init_graphics; |
---|
578 | dp->drv.end_graphics = gl_end_graphics; |
---|
579 | dp->drv.set_display_title = gl_set_display_title; |
---|
580 | dp->drv.get_display_width = gl_get_display_width; |
---|
581 | dp->drv.get_display_height = gl_get_display_height; |
---|
582 | dp->drv.display = gl_display; |
---|
583 | dp->drv.handle_resize = gl_handle_resize; |
---|
584 | dp->drv.get_event = gl_get_event; |
---|
585 | dp->drv.set_mouse = gl_set_mouse; |
---|
586 | |
---|
587 | return 0; |
---|
588 | } |
---|
589 | |
---|
590 | #endif /* USE_GL */ |
---|
591 | |
---|