| 1 | /* |
|---|
| 2 | * libcucul++ C++ bindings for libcucul |
|---|
| 3 | * Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This library is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the Do What The Fuck You Want To |
|---|
| 10 | * 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 libcucul++ applications |
|---|
| 16 | * to initialise a drawing context. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | |
|---|
| 21 | #include <stdio.h> // BUFSIZ |
|---|
| 22 | #include <stdarg.h> // va_* |
|---|
| 23 | |
|---|
| 24 | #include "cucul++.h" |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | unsigned long int Charset::utf8ToUtf32(char const *s, unsigned int *read) |
|---|
| 28 | { |
|---|
| 29 | return cucul_utf8_to_utf32(s, read); |
|---|
| 30 | } |
|---|
| 31 | unsigned int Charset::utf32ToUtf8(char *buf, unsigned long int ch) |
|---|
| 32 | { |
|---|
| 33 | return cucul_utf32_to_utf8(buf, ch); |
|---|
| 34 | } |
|---|
| 35 | unsigned char Charset::utf32ToCp437(unsigned long int ch) |
|---|
| 36 | { |
|---|
| 37 | return cucul_utf32_to_cp437(ch); |
|---|
| 38 | } |
|---|
| 39 | unsigned long int Charset::cp437ToUtf32(unsigned char ch) |
|---|
| 40 | { |
|---|
| 41 | return cucul_cp437_to_utf32(ch); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | Cucul::Cucul() |
|---|
| 46 | { |
|---|
| 47 | cv = cucul_create_canvas(0, 0); |
|---|
| 48 | if(!cv) |
|---|
| 49 | throw -1; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | Cucul::Cucul(int width, int height) |
|---|
| 53 | { |
|---|
| 54 | cv = cucul_create_canvas(width, height); |
|---|
| 55 | if(!cv) throw -1; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | Cucul::~Cucul() |
|---|
| 59 | { |
|---|
| 60 | if(cv) |
|---|
| 61 | cucul_free_canvas(cv); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | cucul_canvas_t *Cucul::get_cucul_canvas_t() |
|---|
| 65 | { |
|---|
| 66 | return cv; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | void Cucul::setSize(unsigned int width, unsigned int height) |
|---|
| 70 | { |
|---|
| 71 | cucul_set_canvas_size(cv, width, height); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | unsigned int Cucul::getWidth(void) |
|---|
| 75 | { |
|---|
| 76 | return cucul_get_canvas_width(cv); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | unsigned int Cucul::getHeight(void) |
|---|
| 80 | { |
|---|
| 81 | return cucul_get_canvas_height(cv); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | int Cucul::setColorANSI(unsigned char f, unsigned char b) |
|---|
| 85 | { |
|---|
| 86 | return cucul_set_color_ansi(cv, f, b); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | int Cucul::setColorARGB(unsigned int f, unsigned int b) |
|---|
| 90 | { |
|---|
| 91 | return cucul_set_color_argb(cv, f, b); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void Cucul::putChar(int x, int y, unsigned long int ch) |
|---|
| 95 | { |
|---|
| 96 | cucul_put_char(cv, x, y, ch); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | unsigned long int Cucul::getChar(int x, int y) |
|---|
| 100 | { |
|---|
| 101 | return cucul_get_char(cv, x, y); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void Cucul::putStr(int x, int y, char *str) |
|---|
| 105 | { |
|---|
| 106 | cucul_put_str(cv, x, y, str); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void Cucul::Printf(int x, int y, char const * format, ...) |
|---|
| 110 | { |
|---|
| 111 | char tmp[BUFSIZ]; |
|---|
| 112 | char *buf = tmp; |
|---|
| 113 | va_list args; |
|---|
| 114 | |
|---|
| 115 | va_start(args, format); |
|---|
| 116 | #if defined(HAVE_VSNPRINTF) |
|---|
| 117 | vsnprintf(buf, getWidth() - x + 1, format, args); |
|---|
| 118 | #else |
|---|
| 119 | vsprintf(buf, format, args); |
|---|
| 120 | #endif |
|---|
| 121 | buf[getWidth() - x] = '\0'; |
|---|
| 122 | va_end(args); |
|---|
| 123 | |
|---|
| 124 | putStr(x, y, buf); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | void Cucul::Clear(void) |
|---|
| 128 | { |
|---|
| 129 | cucul_clear_canvas(cv); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void Cucul::Blit(int x, int y, Cucul* c1, Cucul* c2) |
|---|
| 133 | { |
|---|
| 134 | cucul_blit(cv, x, y, c1->get_cucul_canvas_t(), |
|---|
| 135 | c2 ? c2->get_cucul_canvas_t() : NULL); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | void Cucul::Invert() |
|---|
| 139 | { |
|---|
| 140 | cucul_invert(cv); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | void Cucul::Flip() |
|---|
| 144 | { |
|---|
| 145 | cucul_flip(cv); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void Cucul::Flop() |
|---|
| 149 | { |
|---|
| 150 | cucul_flop(cv); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | void Cucul::Rotate() |
|---|
| 154 | { |
|---|
| 155 | cucul_rotate(cv); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | void Cucul::drawLine(int x1, int y1, int x2, int y2, unsigned long int ch) |
|---|
| 159 | { |
|---|
| 160 | cucul_draw_line(cv, x1, y1, x2, y2, ch); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | void Cucul::drawPolyline(int const x[], int const y[], int f, unsigned long int ch) |
|---|
| 164 | { |
|---|
| 165 | cucul_draw_polyline(cv, x, y, f, ch); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | void Cucul::drawThinLine(int x1, int y1, int x2, int y2) |
|---|
| 169 | { |
|---|
| 170 | cucul_draw_thin_line(cv, x1, y1, x2, y2); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | void Cucul::drawThinPolyline(int const x[], int const y[], int f) |
|---|
| 174 | { |
|---|
| 175 | cucul_draw_thin_polyline(cv, x, y, f); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | void Cucul::drawCircle(int x, int y, int d, unsigned long int ch) |
|---|
| 179 | { |
|---|
| 180 | cucul_draw_circle(cv, x, y, d, ch); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | void Cucul::drawEllipse(int x, int y, int d1, int d2, unsigned long int ch) |
|---|
| 184 | { |
|---|
| 185 | cucul_draw_ellipse(cv, x, y, d1, d2, ch); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | void Cucul::drawThinEllipse(int x, int y, int d1, int d2) |
|---|
| 189 | { |
|---|
| 190 | cucul_draw_thin_ellipse(cv, x, y, d1, d2); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | void Cucul::fillEllipse(int x, int y, int d1, int d2, unsigned long int ch) |
|---|
| 194 | { |
|---|
| 195 | cucul_fill_ellipse(cv, x, y, d1, d2, ch); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | void Cucul::drawBox(int x, int y, int w, int h, unsigned long int ch) |
|---|
| 199 | { |
|---|
| 200 | cucul_draw_box(cv, x, y, w, h, ch); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | void Cucul::drawThinBox(int x, int y, int w, int h) |
|---|
| 204 | { |
|---|
| 205 | cucul_draw_thin_box(cv, x, y, w, h); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | void Cucul::drawCP437Box(int x, int y, int w, int h) |
|---|
| 209 | { |
|---|
| 210 | cucul_draw_cp437_box(cv, x, y, w, h); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | void Cucul::fillBox(int x, int y, int w, int h, unsigned long int ch) |
|---|
| 214 | { |
|---|
| 215 | cucul_fill_box(cv, x, y, w, h, ch); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | void Cucul::drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, unsigned long int ch) |
|---|
| 219 | { |
|---|
| 220 | cucul_draw_triangle(cv, x1, y1, x2, y2, x3, y3, ch); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | void Cucul::drawThinTriangle(int x1, int y1, int x2, int y2, int x3, int y3) |
|---|
| 224 | { |
|---|
| 225 | cucul_draw_thin_triangle(cv, x1, y1, x2, y2, x3, y3); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | void Cucul::fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, unsigned long int ch) |
|---|
| 229 | { |
|---|
| 230 | cucul_fill_triangle(cv, x1, y1, x2, y2, x3, y3, ch); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | int Cucul::Rand(int min, int max) |
|---|
| 234 | { |
|---|
| 235 | return cucul_rand(min, max); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | int Cucul::setAttr(unsigned long int attr) |
|---|
| 239 | { |
|---|
| 240 | return cucul_set_attr(cv, attr); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | unsigned long int Cucul::getAttr(int x, int y) |
|---|
| 244 | { |
|---|
| 245 | return cucul_get_attr(cv, x, y); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | int Cucul::setBoundaries(cucul_canvas_t *, int x, int y, |
|---|
| 249 | unsigned int w, unsigned int h) |
|---|
| 250 | { |
|---|
| 251 | return cucul_set_canvas_boundaries(cv, x, y, h, w); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | unsigned int Cucul::getFrameCount() |
|---|
| 255 | { |
|---|
| 256 | return cucul_get_frame_count(cv); |
|---|
| 257 | } |
|---|
| 258 | int Cucul::setFrame(unsigned int f) |
|---|
| 259 | { |
|---|
| 260 | return cucul_set_frame(cv, f); |
|---|
| 261 | } |
|---|
| 262 | int Cucul::createFrame(unsigned int f) |
|---|
| 263 | { |
|---|
| 264 | return cucul_create_frame(cv, f); |
|---|
| 265 | } |
|---|
| 266 | int Cucul::freeFrame(unsigned int f) |
|---|
| 267 | { |
|---|
| 268 | return cucul_create_frame(cv, f); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | char const *const * Cucul::getImportList(void) |
|---|
| 272 | { |
|---|
| 273 | return cucul_get_import_list(); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | long int Cucul::importMemory(void const *buf, unsigned long int len, char const *fmt) |
|---|
| 277 | { |
|---|
| 278 | return cucul_import_memory(cv, buf, len, fmt); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | long int Cucul::importFile(char const *file, char const *fmt) |
|---|
| 282 | { |
|---|
| 283 | return cucul_import_file(cv, file, fmt); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | char const *const * Cucul::getExportList(void) |
|---|
| 287 | { |
|---|
| 288 | return cucul_get_export_list(); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | void *Cucul::exportMemory(char const *fmt, unsigned long int *len) |
|---|
| 292 | { |
|---|
| 293 | return cucul_export_memory(cv, fmt, len); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | 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) |
|---|
| 297 | { |
|---|
| 298 | dither = cucul_create_dither(v1, v2, v3, v4, v5, v6, v7, v8); |
|---|
| 299 | } |
|---|
| 300 | Dither::~Dither() |
|---|
| 301 | { |
|---|
| 302 | cucul_free_dither(dither); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | void Dither::setPalette(unsigned int r[], unsigned int g[], unsigned int b[], unsigned int a[]) |
|---|
| 306 | { |
|---|
| 307 | cucul_set_dither_palette(dither, r, g, b, a); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | void Dither::setBrightness(float f) |
|---|
| 311 | { |
|---|
| 312 | cucul_set_dither_brightness(dither, f); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | void Dither::setGamma(float f) |
|---|
| 316 | { |
|---|
| 317 | cucul_set_dither_gamma(dither, f); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | void Dither::setContrast(float f) |
|---|
| 321 | { |
|---|
| 322 | cucul_set_dither_contrast(dither, f); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | void Dither::setInvert(int i) |
|---|
| 326 | { |
|---|
| 327 | cucul_set_dither_invert(dither, i); |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | void Dither::setAntialias(char const *cv) |
|---|
| 331 | { |
|---|
| 332 | cucul_set_dither_antialias(dither, cv); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | char const *const * Dither::getAntialiasList() |
|---|
| 336 | { |
|---|
| 337 | return cucul_get_dither_antialias_list(dither); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | void Dither::setColor(char const *cv) |
|---|
| 341 | { |
|---|
| 342 | cucul_set_dither_color(dither, cv); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | char const *const * Dither::getColorList() |
|---|
| 346 | { |
|---|
| 347 | return cucul_get_dither_color_list(dither); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | void Dither::setCharset(char const *cv) |
|---|
| 351 | { |
|---|
| 352 | cucul_set_dither_charset(dither, cv); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | char const *const * Dither::getCharsetList() |
|---|
| 356 | { |
|---|
| 357 | return cucul_get_dither_charset_list(dither); |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | void Dither::setMode(char const *cv) |
|---|
| 361 | { |
|---|
| 362 | cucul_set_dither_mode(dither, cv); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | char const *const * Dither::getModeList(void) |
|---|
| 366 | { |
|---|
| 367 | return cucul_get_dither_mode_list(dither); |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | void Dither::Bitmap(Cucul *cv, int x, int y, int w, int h, void *v) |
|---|
| 371 | { |
|---|
| 372 | cucul_dither_bitmap(cv->get_cucul_canvas_t(), x, y, w, h, dither, v); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | Font::Font(void const *s, unsigned int v) |
|---|
| 376 | { |
|---|
| 377 | font = cucul_load_font(s, v); |
|---|
| 378 | if(!font) throw -1; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | char const *const * Font::getList(void) |
|---|
| 382 | { |
|---|
| 383 | return cucul_get_font_list(); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | unsigned int Font::getWidth() |
|---|
| 387 | { |
|---|
| 388 | return cucul_get_font_width(font); |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | unsigned int Font::getHeight() |
|---|
| 392 | { |
|---|
| 393 | return cucul_get_font_height(font); |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | void Font::renderCanvas(Cucul *cv, unsigned char *buf, unsigned int x, unsigned int y, unsigned int w) |
|---|
| 397 | { |
|---|
| 398 | cucul_render_canvas(cv->get_cucul_canvas_t(), font, buf, x, y, w); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | unsigned long int const *Font::getBlocks() |
|---|
| 402 | { |
|---|
| 403 | return cucul_get_font_blocks(font); |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | Font::~Font() |
|---|
| 407 | { |
|---|
| 408 | cucul_free_font(font); |
|---|
| 409 | } |
|---|
| 410 | |
|---|