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

Last change on this file since 3495 was 3495, checked in by Sam Hocevar, 11 years ago

Add caca_export_area_to_memory() to export only a selected part of the
current canvas. This is useful to export dirty rectangles. Accordingly,
add caca_import_area_from_memory() and caca_import_area_from_file().

  • Property svn:keywords set to Id
File size: 9.3 KB
Line 
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 3495 2009-05-21 20:55:21Z 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
30uint32_t Charset::utf8ToUtf32(char const *s, size_t *read)
31{
32    return caca_utf8_to_utf32(s, read);
33}
34size_t Charset::utf32ToUtf8(char *buf, uint32_t ch)
35{
36    return caca_utf32_to_utf8(buf, ch);
37}
38uint8_t Charset::utf32ToCp437(uint32_t ch)
39{
40    return caca_utf32_to_cp437(ch);
41}
42uint32_t Charset::cp437ToUtf32(uint8_t ch)
43{
44    return caca_cp437_to_utf32(ch);
45}
46
47
48Canvas::Canvas()
49{
50    cv = caca_create_canvas(0, 0);
51    if(!cv)
52        throw -1;
53}
54
55Canvas::Canvas(int width, int height)
56{
57    cv = caca_create_canvas(width, height);
58    if(!cv) throw -1;
59}
60
61Canvas::~Canvas()
62{
63    if(cv)
64        caca_free_canvas(cv);
65}
66
67caca_canvas_t *Canvas::get_caca_canvas_t()
68{
69    return cv;
70}
71
72void Canvas::setSize(unsigned int width, unsigned int height)
73{
74    caca_set_canvas_size(cv, width, height);
75}
76
77unsigned int Canvas::getWidth(void)
78{
79    return caca_get_canvas_width(cv);
80}
81
82unsigned int Canvas::getHeight(void)
83{
84    return caca_get_canvas_height(cv);
85}
86
87int Canvas::setColorANSI(uint8_t f, uint8_t b)
88{
89    return caca_set_color_ansi(cv, f, b);
90}
91
92int  Canvas::setColorARGB(unsigned int f, unsigned int b)
93{
94    return caca_set_color_argb(cv, f, b);
95}
96
97void Canvas::putChar(int x, int y, uint32_t ch)
98{
99    caca_put_char(cv, x, y, ch);
100}
101
102uint32_t Canvas::getChar(int x, int y)
103{
104    return caca_get_char(cv, x, y);
105}
106
107void Canvas::putStr(int x, int y, char *str)
108{
109    caca_put_str(cv, x, y, str);
110}
111
112void 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
130void Canvas::Clear(void)
131{
132    caca_clear_canvas(cv);
133}
134
135void 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
141void Canvas::Invert()
142{
143    caca_invert(cv);
144}
145
146void Canvas::Flip()
147{
148    caca_flip(cv);
149}
150
151void Canvas::Flop()
152{
153    caca_flop(cv);
154}
155
156void Canvas::Rotate180()
157{
158    caca_rotate_180(cv);
159}
160
161void Canvas::RotateLeft()
162{
163    caca_rotate_left(cv);
164}
165
166void Canvas::RotateRight()
167{
168    caca_rotate_right(cv);
169}
170
171void 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
176void 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
181void Canvas::drawThinLine(int x1, int y1, int x2, int y2)
182{
183    caca_draw_thin_line(cv, x1, y1, x2, y2);
184}
185
186void Canvas::drawThinPolyline(int const x[], int const y[], int f)
187{
188    caca_draw_thin_polyline(cv, x, y, f);
189}
190
191void Canvas::drawCircle(int x, int y, int d, uint32_t ch)
192{
193    caca_draw_circle(cv, x, y, d, ch);
194}
195
196void 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
201void Canvas::drawThinEllipse(int x, int y, int d1, int d2)
202{
203    caca_draw_thin_ellipse(cv, x, y, d1, d2);
204}
205
206void 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
211void 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
216void Canvas::drawThinBox(int x, int y, int w, int h)
217{
218    caca_draw_thin_box(cv, x, y, w, h);
219}
220
221void Canvas::drawCP437Box(int x, int y, int w, int h)
222{
223    caca_draw_cp437_box(cv, x, y, w, h);
224}
225
226void 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
231void 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
236void 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
241void 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
246int Canvas::Rand(int min, int max)
247{
248    return caca_rand(min, max);
249}
250
251const char * Canvas::getVersion()
252{
253    return caca_get_version();
254}
255
256int Canvas::setAttr(uint32_t attr)
257{
258    return caca_set_attr(cv, attr);
259}
260
261uint32_t Canvas::getAttr(int x, int y)
262{
263    return caca_get_attr(cv, x, y);
264}
265
266int 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
272unsigned int Canvas::getFrameCount()
273{
274    return caca_get_frame_count(cv);
275}
276int Canvas::setFrame(unsigned int f)
277{
278    return caca_set_frame(cv, f);
279}
280int Canvas::createFrame(unsigned int f)
281{
282    return caca_create_frame(cv, f);
283}
284int Canvas::freeFrame(unsigned int f)
285{
286    return caca_create_frame(cv, f);
287}
288
289char const *const * Canvas::getImportList(void)
290{
291    return caca_get_import_list();
292}
293
294long int Canvas::importFromMemory(void const *buf, size_t len, char const *fmt)
295{
296    return caca_import_canvas_from_memory(cv, buf, len, fmt);
297}
298
299long int Canvas::importFromFile(char const *file, char const *fmt)
300{
301    return caca_import_canvas_from_file(cv, file, fmt);
302}
303
304char const *const * Canvas::getExportList(void)
305{
306    return caca_get_export_list();
307}
308
309void *Canvas::exportToMemory(char const *fmt, size_t *len)
310{
311    return caca_export_canvas_to_memory(cv, fmt, len);
312}
313
314Dither::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}
318Dither::~Dither()
319{
320    caca_free_dither(dither);
321}
322
323void 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
328void Dither::setBrightness(float f)
329{
330    caca_set_dither_brightness(dither, f);
331}
332
333void Dither::setGamma(float f)
334{
335    caca_set_dither_gamma(dither, f);
336}
337
338void Dither::setContrast(float f)
339{
340    caca_set_dither_contrast(dither, f);
341}
342
343void Dither::setAntialias(char const *cv)
344{
345    caca_set_dither_antialias(dither, cv);
346}
347
348char const *const * Dither::getAntialiasList()
349{
350    return caca_get_dither_antialias_list(dither);
351}
352
353void Dither::setColor(char const *cv)
354{
355    caca_set_dither_color(dither, cv);
356}
357
358char const *const * Dither::getColorList()
359{
360    return caca_get_dither_color_list(dither);
361}
362
363void Dither::setCharset(char const *cv)
364{
365    caca_set_dither_charset(dither, cv);
366}
367
368char const *const * Dither::getCharsetList()
369{
370    return caca_get_dither_charset_list(dither);
371}
372
373void Dither::setMode(char const *cv)
374{
375    caca_set_dither_algorithm(dither, cv);
376}
377
378char const *const * Dither::getModeList(void)
379{
380    return caca_get_dither_algorithm_list(dither);
381}
382
383void 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
388Font::Font(void const *s, unsigned int v)
389{
390    font = caca_load_font(s, v);
391    if(!font) throw -1;
392}
393
394char const *const * Font::getList(void)
395{
396    return caca_get_font_list();
397}
398
399unsigned int Font::getWidth()
400{
401    return caca_get_font_width(font);
402}
403
404unsigned int Font::getHeight()
405{
406    return caca_get_font_height(font);
407}
408
409void 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
414uint32_t const *Font::getBlocks()
415{
416    return caca_get_font_blocks(font);
417}
418
419Font::~Font()
420{
421    caca_free_font(font);
422}
423
424Caca::Caca(Canvas *cv)
425{
426    dp = caca_create_display(cv->get_caca_canvas_t());
427    if(!dp)
428        throw -1;
429}
430
431Caca::~Caca()
432{
433    caca_free_display(dp);
434}
435
436void Caca::Attach(Canvas *cv)
437{
438    dp = caca_create_display(cv->get_caca_canvas_t());
439    if(!dp)
440        throw -1;
441}
442
443void Caca::Detach()
444{
445    caca_free_display(dp);
446}
447
448void Caca::setDisplayTime(unsigned int d)
449{
450    caca_set_display_time(dp, d);
451}
452
453void Caca::Display()
454{
455    caca_refresh_display(dp);
456}
457
458unsigned int Caca::getDisplayTime()
459{
460    return caca_get_display_time(dp);
461}
462
463unsigned int Caca::getWidth()
464{
465    return caca_get_display_width(dp);
466}
467
468unsigned int Caca::getHeight()
469{
470    return caca_get_display_height(dp);
471}
472
473int Caca::setTitle(char const *s)
474{
475    return caca_set_display_title(dp, s);
476}
477
478int Caca::getEvent(unsigned int g, Event *n, int aa)
479{
480    return caca_get_event(dp, g, n ? &n->e : NULL, aa);
481}
482
483unsigned int Caca::getMouseX()
484{
485    return caca_get_mouse_x(dp);
486}
487
488unsigned int Caca::getMouseY()
489{
490    return caca_get_mouse_x(dp);
491}
492
493void Caca::setMouse(int v)
494{
495    caca_set_mouse(dp, v);
496}
497
498const char * Caca::getVersion()
499{
500    return caca_get_version();
501}
502
Note: See TracBrowser for help on using the repository browser.