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

Last change on this file since 3955 was 3955, checked in by Jean-Yves Lamoureux, 10 years ago
  • Updated C++ bindings with Canvas::fillTriangleTextured()
  • Property svn:keywords set to Id
File size: 9.4 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 3955 2009-11-19 11:21:16Z jylam $
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
246void Canvas::fillTriangleTextured(int coords[6], Canvas *tex, float uv[6])
247{
248    caca_fill_triangle_textured(cv, coords, tex->cv, uv);
249}
250
251int Canvas::Rand(int min, int max)
252{
253    return caca_rand(min, max);
254}
255
256const char * Canvas::getVersion()
257{
258    return caca_get_version();
259}
260
261int Canvas::setAttr(uint32_t attr)
262{
263    return caca_set_attr(cv, attr);
264}
265
266uint32_t Canvas::getAttr(int x, int y)
267{
268    return caca_get_attr(cv, x, y);
269}
270
271int Canvas::setBoundaries(caca_canvas_t *, int x, int y,
272                         unsigned int w, unsigned int h)
273{
274    return caca_set_canvas_boundaries(cv, x, y, h, w);
275}
276
277unsigned int Canvas::getFrameCount()
278{
279    return caca_get_frame_count(cv);
280}
281int Canvas::setFrame(unsigned int f)
282{
283    return caca_set_frame(cv, f);
284}
285int Canvas::createFrame(unsigned int f)
286{
287    return caca_create_frame(cv, f);
288}
289int Canvas::freeFrame(unsigned int f)
290{
291    return caca_create_frame(cv, f);
292}
293
294char const *const * Canvas::getImportList(void)
295{
296    return caca_get_import_list();
297}
298
299long int Canvas::importFromMemory(void const *buf, size_t len, char const *fmt)
300{
301    return caca_import_canvas_from_memory(cv, buf, len, fmt);
302}
303
304long int Canvas::importFromFile(char const *file, char const *fmt)
305{
306    return caca_import_canvas_from_file(cv, file, fmt);
307}
308
309char const *const * Canvas::getExportList(void)
310{
311    return caca_get_export_list();
312}
313
314void *Canvas::exportToMemory(char const *fmt, size_t *len)
315{
316    return caca_export_canvas_to_memory(cv, fmt, len);
317}
318
319Dither::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)
320{
321    dither = caca_create_dither(v1, v2, v3, v4, v5, v6, v7, v8);
322}
323Dither::~Dither()
324{
325    caca_free_dither(dither);
326}
327
328void Dither::setPalette(uint32_t r[], uint32_t g[], uint32_t b[], uint32_t a[])
329{
330    caca_set_dither_palette(dither, r, g, b, a);
331}
332
333void Dither::setBrightness(float f)
334{
335    caca_set_dither_brightness(dither, f);
336}
337
338void Dither::setGamma(float f)
339{
340    caca_set_dither_gamma(dither, f);
341}
342
343void Dither::setContrast(float f)
344{
345    caca_set_dither_contrast(dither, f);
346}
347
348void Dither::setAntialias(char const *cv)
349{
350    caca_set_dither_antialias(dither, cv);
351}
352
353char const *const * Dither::getAntialiasList()
354{
355    return caca_get_dither_antialias_list(dither);
356}
357
358void Dither::setColor(char const *cv)
359{
360    caca_set_dither_color(dither, cv);
361}
362
363char const *const * Dither::getColorList()
364{
365    return caca_get_dither_color_list(dither);
366}
367
368void Dither::setCharset(char const *cv)
369{
370    caca_set_dither_charset(dither, cv);
371}
372
373char const *const * Dither::getCharsetList()
374{
375    return caca_get_dither_charset_list(dither);
376}
377
378void Dither::setMode(char const *cv)
379{
380    caca_set_dither_algorithm(dither, cv);
381}
382
383char const *const * Dither::getModeList(void)
384{
385    return caca_get_dither_algorithm_list(dither);
386}
387
388void Dither::Bitmap(Canvas *cv, int x, int y, int w, int h, void *v)
389{
390    caca_dither_bitmap(cv->get_caca_canvas_t(), x, y, w, h, dither, v);
391}
392
393Font::Font(void const *s, unsigned int v)
394{
395    font = caca_load_font(s, v);
396    if(!font) throw -1;
397}
398
399char const *const * Font::getList(void)
400{
401    return caca_get_font_list();
402}
403
404unsigned int Font::getWidth()
405{
406    return caca_get_font_width(font);
407}
408
409unsigned int Font::getHeight()
410{
411    return caca_get_font_height(font);
412}
413
414void Font::renderCanvas(Canvas *cv, uint8_t *buf, unsigned int x, unsigned int y, unsigned int w)
415{
416    caca_render_canvas(cv->get_caca_canvas_t(), font, buf, x, y, w);
417}
418
419uint32_t const *Font::getBlocks()
420{
421    return caca_get_font_blocks(font);
422}
423
424Font::~Font()
425{
426    caca_free_font(font);
427}
428
429Caca::Caca(Canvas *cv)
430{
431    dp = caca_create_display(cv->get_caca_canvas_t());
432    if(!dp)
433        throw -1;
434}
435
436Caca::~Caca()
437{
438    caca_free_display(dp);
439}
440
441void Caca::Attach(Canvas *cv)
442{
443    dp = caca_create_display(cv->get_caca_canvas_t());
444    if(!dp)
445        throw -1;
446}
447
448void Caca::Detach()
449{
450    caca_free_display(dp);
451}
452
453void Caca::setDisplayTime(unsigned int d)
454{
455    caca_set_display_time(dp, d);
456}
457
458void Caca::Display()
459{
460    caca_refresh_display(dp);
461}
462
463unsigned int Caca::getDisplayTime()
464{
465    return caca_get_display_time(dp);
466}
467
468unsigned int Caca::getWidth()
469{
470    return caca_get_display_width(dp);
471}
472
473unsigned int Caca::getHeight()
474{
475    return caca_get_display_height(dp);
476}
477
478int Caca::setTitle(char const *s)
479{
480    return caca_set_display_title(dp, s);
481}
482
483int Caca::getEvent(unsigned int g, Event *n, int aa)
484{
485    return caca_get_event(dp, g, n ? &n->e : NULL, aa);
486}
487
488unsigned int Caca::getMouseX()
489{
490    return caca_get_mouse_x(dp);
491}
492
493unsigned int Caca::getMouseY()
494{
495    return caca_get_mouse_x(dp);
496}
497
498void Caca::setMouse(int v)
499{
500    caca_set_mouse(dp, v);
501}
502
503const char * Caca::getVersion()
504{
505    return caca_get_version();
506}
507
Note: See TracBrowser for help on using the repository browser.