1 | /* |
---|
2 | * libcaca++ C++ bindings for libcaca |
---|
3 | * Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: caca++.cpp 2822 2008-09-27 14:11:36Z sam $ |
---|
7 | * |
---|
8 | * This library is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * This file contains the main functions used by \e libcaca++ applications to |
---|
17 | * initialise the library, get the screen properties, set the framerate and |
---|
18 | * so on. |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | |
---|
23 | #include <iostream> |
---|
24 | |
---|
25 | #include <stdio.h> // BUFSIZ |
---|
26 | #include <stdarg.h> // va_* |
---|
27 | |
---|
28 | #include "caca++.h" |
---|
29 | |
---|
30 | uint32_t Charset::utf8ToUtf32(char const *s, size_t *read) |
---|
31 | { |
---|
32 | return caca_utf8_to_utf32(s, read); |
---|
33 | } |
---|
34 | size_t Charset::utf32ToUtf8(char *buf, uint32_t ch) |
---|
35 | { |
---|
36 | return caca_utf32_to_utf8(buf, ch); |
---|
37 | } |
---|
38 | uint8_t Charset::utf32ToCp437(uint32_t ch) |
---|
39 | { |
---|
40 | return caca_utf32_to_cp437(ch); |
---|
41 | } |
---|
42 | uint32_t Charset::cp437ToUtf32(uint8_t ch) |
---|
43 | { |
---|
44 | return caca_cp437_to_utf32(ch); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | Canvas::Canvas() |
---|
49 | { |
---|
50 | cv = caca_create_canvas(0, 0); |
---|
51 | if(!cv) |
---|
52 | throw -1; |
---|
53 | } |
---|
54 | |
---|
55 | Canvas::Canvas(int width, int height) |
---|
56 | { |
---|
57 | cv = caca_create_canvas(width, height); |
---|
58 | if(!cv) throw -1; |
---|
59 | } |
---|
60 | |
---|
61 | Canvas::~Canvas() |
---|
62 | { |
---|
63 | if(cv) |
---|
64 | caca_free_canvas(cv); |
---|
65 | } |
---|
66 | |
---|
67 | caca_canvas_t *Canvas::get_caca_canvas_t() |
---|
68 | { |
---|
69 | return cv; |
---|
70 | } |
---|
71 | |
---|
72 | void Canvas::setSize(unsigned int width, unsigned int height) |
---|
73 | { |
---|
74 | caca_set_canvas_size(cv, width, height); |
---|
75 | } |
---|
76 | |
---|
77 | unsigned int Canvas::getWidth(void) |
---|
78 | { |
---|
79 | return caca_get_canvas_width(cv); |
---|
80 | } |
---|
81 | |
---|
82 | unsigned int Canvas::getHeight(void) |
---|
83 | { |
---|
84 | return caca_get_canvas_height(cv); |
---|
85 | } |
---|
86 | |
---|
87 | int Canvas::setColorANSI(uint8_t f, uint8_t b) |
---|
88 | { |
---|
89 | return caca_set_color_ansi(cv, f, b); |
---|
90 | } |
---|
91 | |
---|
92 | int Canvas::setColorARGB(unsigned int f, unsigned int b) |
---|
93 | { |
---|
94 | return caca_set_color_argb(cv, f, b); |
---|
95 | } |
---|
96 | |
---|
97 | void Canvas::putChar(int x, int y, uint32_t ch) |
---|
98 | { |
---|
99 | caca_put_char(cv, x, y, ch); |
---|
100 | } |
---|
101 | |
---|
102 | uint32_t Canvas::getChar(int x, int y) |
---|
103 | { |
---|
104 | return caca_get_char(cv, x, y); |
---|
105 | } |
---|
106 | |
---|
107 | void Canvas::putStr(int x, int y, char *str) |
---|
108 | { |
---|
109 | caca_put_str(cv, x, y, str); |
---|
110 | } |
---|
111 | |
---|
112 | void Canvas::Printf(int x, int y, char const * format, ...) |
---|
113 | { |
---|
114 | char tmp[BUFSIZ]; |
---|
115 | char *buf = tmp; |
---|
116 | va_list args; |
---|
117 | |
---|
118 | va_start(args, format); |
---|
119 | #if defined(HAVE_VSNPRINTF) |
---|
120 | vsnprintf(buf, getWidth() - x + 1, format, args); |
---|
121 | #else |
---|
122 | vsprintf(buf, format, args); |
---|
123 | #endif |
---|
124 | buf[getWidth() - x] = '\0'; |
---|
125 | va_end(args); |
---|
126 | |
---|
127 | putStr(x, y, buf); |
---|
128 | } |
---|
129 | |
---|
130 | void Canvas::Clear(void) |
---|
131 | { |
---|
132 | caca_clear_canvas(cv); |
---|
133 | } |
---|
134 | |
---|
135 | void Canvas::Blit(int x, int y, Canvas* c1, Canvas* c2) |
---|
136 | { |
---|
137 | caca_blit(cv, x, y, c1->get_caca_canvas_t(), |
---|
138 | c2 ? c2->get_caca_canvas_t() : NULL); |
---|
139 | } |
---|
140 | |
---|
141 | void Canvas::Invert() |
---|
142 | { |
---|
143 | caca_invert(cv); |
---|
144 | } |
---|
145 | |
---|
146 | void Canvas::Flip() |
---|
147 | { |
---|
148 | caca_flip(cv); |
---|
149 | } |
---|
150 | |
---|
151 | void Canvas::Flop() |
---|
152 | { |
---|
153 | caca_flop(cv); |
---|
154 | } |
---|
155 | |
---|
156 | void Canvas::Rotate180() |
---|
157 | { |
---|
158 | caca_rotate_180(cv); |
---|
159 | } |
---|
160 | |
---|
161 | void Canvas::RotateLeft() |
---|
162 | { |
---|
163 | caca_rotate_left(cv); |
---|
164 | } |
---|
165 | |
---|
166 | void Canvas::RotateRight() |
---|
167 | { |
---|
168 | caca_rotate_right(cv); |
---|
169 | } |
---|
170 | |
---|
171 | void Canvas::drawLine(int x1, int y1, int x2, int y2, uint32_t ch) |
---|
172 | { |
---|
173 | caca_draw_line(cv, x1, y1, x2, y2, ch); |
---|
174 | } |
---|
175 | |
---|
176 | void Canvas::drawPolyline(int const x[], int const y[], int f, uint32_t ch) |
---|
177 | { |
---|
178 | caca_draw_polyline(cv, x, y, f, ch); |
---|
179 | } |
---|
180 | |
---|
181 | void Canvas::drawThinLine(int x1, int y1, int x2, int y2) |
---|
182 | { |
---|
183 | caca_draw_thin_line(cv, x1, y1, x2, y2); |
---|
184 | } |
---|
185 | |
---|
186 | void Canvas::drawThinPolyline(int const x[], int const y[], int f) |
---|
187 | { |
---|
188 | caca_draw_thin_polyline(cv, x, y, f); |
---|
189 | } |
---|
190 | |
---|
191 | void Canvas::drawCircle(int x, int y, int d, uint32_t ch) |
---|
192 | { |
---|
193 | caca_draw_circle(cv, x, y, d, ch); |
---|
194 | } |
---|
195 | |
---|
196 | void Canvas::drawEllipse(int x, int y, int d1, int d2, uint32_t ch) |
---|
197 | { |
---|
198 | caca_draw_ellipse(cv, x, y, d1, d2, ch); |
---|
199 | } |
---|
200 | |
---|
201 | void Canvas::drawThinEllipse(int x, int y, int d1, int d2) |
---|
202 | { |
---|
203 | caca_draw_thin_ellipse(cv, x, y, d1, d2); |
---|
204 | } |
---|
205 | |
---|
206 | void Canvas::fillEllipse(int x, int y, int d1, int d2, uint32_t ch) |
---|
207 | { |
---|
208 | caca_fill_ellipse(cv, x, y, d1, d2, ch); |
---|
209 | } |
---|
210 | |
---|
211 | void Canvas::drawBox(int x, int y, int w, int h, uint32_t ch) |
---|
212 | { |
---|
213 | caca_draw_box(cv, x, y, w, h, ch); |
---|
214 | } |
---|
215 | |
---|
216 | void Canvas::drawThinBox(int x, int y, int w, int h) |
---|
217 | { |
---|
218 | caca_draw_thin_box(cv, x, y, w, h); |
---|
219 | } |
---|
220 | |
---|
221 | void Canvas::drawCP437Box(int x, int y, int w, int h) |
---|
222 | { |
---|
223 | caca_draw_cp437_box(cv, x, y, w, h); |
---|
224 | } |
---|
225 | |
---|
226 | void Canvas::fillBox(int x, int y, int w, int h, uint32_t ch) |
---|
227 | { |
---|
228 | caca_fill_box(cv, x, y, w, h, ch); |
---|
229 | } |
---|
230 | |
---|
231 | void Canvas::drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, uint32_t ch) |
---|
232 | { |
---|
233 | caca_draw_triangle(cv, x1, y1, x2, y2, x3, y3, ch); |
---|
234 | } |
---|
235 | |
---|
236 | void Canvas::drawThinTriangle(int x1, int y1, int x2, int y2, int x3, int y3) |
---|
237 | { |
---|
238 | caca_draw_thin_triangle(cv, x1, y1, x2, y2, x3, y3); |
---|
239 | } |
---|
240 | |
---|
241 | void Canvas::fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, uint32_t ch) |
---|
242 | { |
---|
243 | caca_fill_triangle(cv, x1, y1, x2, y2, x3, y3, ch); |
---|
244 | } |
---|
245 | |
---|
246 | int Canvas::Rand(int min, int max) |
---|
247 | { |
---|
248 | return caca_rand(min, max); |
---|
249 | } |
---|
250 | |
---|
251 | const char * Canvas::getVersion() |
---|
252 | { |
---|
253 | return caca_get_version(); |
---|
254 | } |
---|
255 | |
---|
256 | int Canvas::setAttr(uint32_t attr) |
---|
257 | { |
---|
258 | return caca_set_attr(cv, attr); |
---|
259 | } |
---|
260 | |
---|
261 | uint32_t Canvas::getAttr(int x, int y) |
---|
262 | { |
---|
263 | return caca_get_attr(cv, x, y); |
---|
264 | } |
---|
265 | |
---|
266 | int Canvas::setBoundaries(caca_canvas_t *, int x, int y, |
---|
267 | unsigned int w, unsigned int h) |
---|
268 | { |
---|
269 | return caca_set_canvas_boundaries(cv, x, y, h, w); |
---|
270 | } |
---|
271 | |
---|
272 | unsigned int Canvas::getFrameCount() |
---|
273 | { |
---|
274 | return caca_get_frame_count(cv); |
---|
275 | } |
---|
276 | int Canvas::setFrame(unsigned int f) |
---|
277 | { |
---|
278 | return caca_set_frame(cv, f); |
---|
279 | } |
---|
280 | int Canvas::createFrame(unsigned int f) |
---|
281 | { |
---|
282 | return caca_create_frame(cv, f); |
---|
283 | } |
---|
284 | int Canvas::freeFrame(unsigned int f) |
---|
285 | { |
---|
286 | return caca_create_frame(cv, f); |
---|
287 | } |
---|
288 | |
---|
289 | char const *const * Canvas::getImportList(void) |
---|
290 | { |
---|
291 | return caca_get_import_list(); |
---|
292 | } |
---|
293 | |
---|
294 | long int Canvas::importMemory(void const *buf, size_t len, char const *fmt) |
---|
295 | { |
---|
296 | return caca_import_memory(cv, buf, len, fmt); |
---|
297 | } |
---|
298 | |
---|
299 | long int Canvas::importFile(char const *file, char const *fmt) |
---|
300 | { |
---|
301 | return caca_import_file(cv, file, fmt); |
---|
302 | } |
---|
303 | |
---|
304 | char const *const * Canvas::getExportList(void) |
---|
305 | { |
---|
306 | return caca_get_export_list(); |
---|
307 | } |
---|
308 | |
---|
309 | void *Canvas::exportMemory(char const *fmt, size_t *len) |
---|
310 | { |
---|
311 | return caca_export_memory(cv, fmt, len); |
---|
312 | } |
---|
313 | |
---|
314 | Dither::Dither(unsigned int v1, unsigned int v2, unsigned int v3, unsigned int v4, unsigned int v5, unsigned int v6, unsigned int v7, unsigned int v8) |
---|
315 | { |
---|
316 | dither = caca_create_dither(v1, v2, v3, v4, v5, v6, v7, v8); |
---|
317 | } |
---|
318 | Dither::~Dither() |
---|
319 | { |
---|
320 | caca_free_dither(dither); |
---|
321 | } |
---|
322 | |
---|
323 | void Dither::setPalette(uint32_t r[], uint32_t g[], uint32_t b[], uint32_t a[]) |
---|
324 | { |
---|
325 | caca_set_dither_palette(dither, r, g, b, a); |
---|
326 | } |
---|
327 | |
---|
328 | void Dither::setBrightness(float f) |
---|
329 | { |
---|
330 | caca_set_dither_brightness(dither, f); |
---|
331 | } |
---|
332 | |
---|
333 | void Dither::setGamma(float f) |
---|
334 | { |
---|
335 | caca_set_dither_gamma(dither, f); |
---|
336 | } |
---|
337 | |
---|
338 | void Dither::setContrast(float f) |
---|
339 | { |
---|
340 | caca_set_dither_contrast(dither, f); |
---|
341 | } |
---|
342 | |
---|
343 | void Dither::setAntialias(char const *cv) |
---|
344 | { |
---|
345 | caca_set_dither_antialias(dither, cv); |
---|
346 | } |
---|
347 | |
---|
348 | char const *const * Dither::getAntialiasList() |
---|
349 | { |
---|
350 | return caca_get_dither_antialias_list(dither); |
---|
351 | } |
---|
352 | |
---|
353 | void Dither::setColor(char const *cv) |
---|
354 | { |
---|
355 | caca_set_dither_color(dither, cv); |
---|
356 | } |
---|
357 | |
---|
358 | char const *const * Dither::getColorList() |
---|
359 | { |
---|
360 | return caca_get_dither_color_list(dither); |
---|
361 | } |
---|
362 | |
---|
363 | void Dither::setCharset(char const *cv) |
---|
364 | { |
---|
365 | caca_set_dither_charset(dither, cv); |
---|
366 | } |
---|
367 | |
---|
368 | char const *const * Dither::getCharsetList() |
---|
369 | { |
---|
370 | return caca_get_dither_charset_list(dither); |
---|
371 | } |
---|
372 | |
---|
373 | void Dither::setMode(char const *cv) |
---|
374 | { |
---|
375 | caca_set_dither_algorithm(dither, cv); |
---|
376 | } |
---|
377 | |
---|
378 | char const *const * Dither::getModeList(void) |
---|
379 | { |
---|
380 | return caca_get_dither_algorithm_list(dither); |
---|
381 | } |
---|
382 | |
---|
383 | void Dither::Bitmap(Canvas *cv, int x, int y, int w, int h, void *v) |
---|
384 | { |
---|
385 | caca_dither_bitmap(cv->get_caca_canvas_t(), x, y, w, h, dither, v); |
---|
386 | } |
---|
387 | |
---|
388 | Font::Font(void const *s, unsigned int v) |
---|
389 | { |
---|
390 | font = caca_load_font(s, v); |
---|
391 | if(!font) throw -1; |
---|
392 | } |
---|
393 | |
---|
394 | char const *const * Font::getList(void) |
---|
395 | { |
---|
396 | return caca_get_font_list(); |
---|
397 | } |
---|
398 | |
---|
399 | unsigned int Font::getWidth() |
---|
400 | { |
---|
401 | return caca_get_font_width(font); |
---|
402 | } |
---|
403 | |
---|
404 | unsigned int Font::getHeight() |
---|
405 | { |
---|
406 | return caca_get_font_height(font); |
---|
407 | } |
---|
408 | |
---|
409 | void Font::renderCanvas(Canvas *cv, uint8_t *buf, unsigned int x, unsigned int y, unsigned int w) |
---|
410 | { |
---|
411 | caca_render_canvas(cv->get_caca_canvas_t(), font, buf, x, y, w); |
---|
412 | } |
---|
413 | |
---|
414 | uint32_t const *Font::getBlocks() |
---|
415 | { |
---|
416 | return caca_get_font_blocks(font); |
---|
417 | } |
---|
418 | |
---|
419 | Font::~Font() |
---|
420 | { |
---|
421 | caca_free_font(font); |
---|
422 | } |
---|
423 | |
---|
424 | Caca::Caca(Canvas *cv) |
---|
425 | { |
---|
426 | dp = caca_create_display(cv->get_caca_canvas_t()); |
---|
427 | if(!dp) |
---|
428 | throw -1; |
---|
429 | } |
---|
430 | |
---|
431 | Caca::~Caca() |
---|
432 | { |
---|
433 | caca_free_display(dp); |
---|
434 | } |
---|
435 | |
---|
436 | void Caca::Attach(Canvas *cv) |
---|
437 | { |
---|
438 | dp = caca_create_display(cv->get_caca_canvas_t()); |
---|
439 | if(!dp) |
---|
440 | throw -1; |
---|
441 | } |
---|
442 | |
---|
443 | void Caca::Detach() |
---|
444 | { |
---|
445 | caca_free_display(dp); |
---|
446 | } |
---|
447 | |
---|
448 | void Caca::setDisplayTime(unsigned int d) |
---|
449 | { |
---|
450 | caca_set_display_time(dp, d); |
---|
451 | } |
---|
452 | |
---|
453 | void Caca::Display() |
---|
454 | { |
---|
455 | caca_refresh_display(dp); |
---|
456 | } |
---|
457 | |
---|
458 | unsigned int Caca::getDisplayTime() |
---|
459 | { |
---|
460 | return caca_get_display_time(dp); |
---|
461 | } |
---|
462 | |
---|
463 | unsigned int Caca::getWidth() |
---|
464 | { |
---|
465 | return caca_get_display_width(dp); |
---|
466 | } |
---|
467 | |
---|
468 | unsigned int Caca::getHeight() |
---|
469 | { |
---|
470 | return caca_get_display_height(dp); |
---|
471 | } |
---|
472 | |
---|
473 | int Caca::setTitle(char const *s) |
---|
474 | { |
---|
475 | return caca_set_display_title(dp, s); |
---|
476 | } |
---|
477 | |
---|
478 | int Caca::getEvent(unsigned int g, Event *n, int aa) |
---|
479 | { |
---|
480 | return caca_get_event(dp, g, n ? &n->e : NULL, aa); |
---|
481 | } |
---|
482 | |
---|
483 | unsigned int Caca::getMouseX() |
---|
484 | { |
---|
485 | return caca_get_mouse_x(dp); |
---|
486 | } |
---|
487 | |
---|
488 | unsigned int Caca::getMouseY() |
---|
489 | { |
---|
490 | return caca_get_mouse_x(dp); |
---|
491 | } |
---|
492 | |
---|
493 | void Caca::setMouse(int v) |
---|
494 | { |
---|
495 | caca_set_mouse(dp, v); |
---|
496 | } |
---|
497 | |
---|
498 | const char * Caca::getVersion() |
---|
499 | { |
---|
500 | return caca_get_version(); |
---|
501 | } |
---|
502 | |
---|