source: libcaca/trunk/cxx/caca++.cpp @ 4369

Last change on this file since 4369 was 4369, checked in by Sam Hocevar, 12 years ago

Add the copyright unit test and update copyright information everywhere.

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