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

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

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