You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

425 lines
8.1 KiB

  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. * This file contains the main functions used by \e libcucul++ applications
  15. * to initialise a drawing context.
  16. */
  17. #include "config.h"
  18. #include <stdio.h> // BUFSIZ
  19. #include <stdarg.h> // va_*
  20. #include "cucul++.h"
  21. unsigned long int Charset::utf8ToUtf32(char const *s, unsigned int *read)
  22. {
  23. return cucul_utf8_to_utf32(s, read);
  24. }
  25. unsigned int Charset::utf32ToUtf8(char *buf, unsigned long int ch)
  26. {
  27. return cucul_utf32_to_utf8(buf, ch);
  28. }
  29. unsigned char Charset::utf32ToCp437(unsigned long int ch)
  30. {
  31. return cucul_utf32_to_cp437(ch);
  32. }
  33. unsigned long int Charset::cp437ToUtf32(unsigned char ch)
  34. {
  35. return cucul_cp437_to_utf32(ch);
  36. }
  37. Cucul::Cucul()
  38. {
  39. cv = cucul_create_canvas(0, 0);
  40. if(!cv)
  41. throw -1;
  42. }
  43. Cucul::Cucul(int width, int height)
  44. {
  45. cv = cucul_create_canvas(width, height);
  46. if(!cv) throw -1;
  47. }
  48. Cucul::Cucul(Buffer *b, char const *format)
  49. {
  50. cv = cucul_import_canvas(b->getBuffer(), format);
  51. if(!cv) throw -1;
  52. }
  53. Cucul::~Cucul()
  54. {
  55. if(cv)
  56. cucul_free_canvas(cv);
  57. }
  58. cucul_canvas_t *Cucul::get_cucul_canvas_t()
  59. {
  60. return cv;
  61. }
  62. void Cucul::setSize(unsigned int width, unsigned int height)
  63. {
  64. cucul_set_canvas_size(cv, width, height);
  65. }
  66. unsigned int Cucul::getWidth(void)
  67. {
  68. return cucul_get_canvas_width(cv);
  69. }
  70. unsigned int Cucul::getHeight(void)
  71. {
  72. return cucul_get_canvas_height(cv);
  73. }
  74. int Cucul::setColorANSI(unsigned char f, unsigned char b)
  75. {
  76. return cucul_set_color_ansi(cv, f, b);
  77. }
  78. int Cucul::setColorARGB(unsigned int f, unsigned int b)
  79. {
  80. return cucul_set_color_argb(cv, f, b);
  81. }
  82. void Cucul::putChar(int x, int y, char ch)
  83. {
  84. cucul_putchar(cv, x, y, ch);
  85. }
  86. void Cucul::putStr(int x, int y, char *str)
  87. {
  88. cucul_putstr(cv, x, y, str);
  89. }
  90. void Cucul::Printf(int x, int y, char const * format,...)
  91. {
  92. char tmp[BUFSIZ];
  93. char *buf = tmp;
  94. va_list args;
  95. va_start(args, format);
  96. #if defined(HAVE_VSNPRINTF)
  97. vsnprintf(buf, getWidth() - x + 1, format, args);
  98. #else
  99. vsprintf(buf, format, args);
  100. #endif
  101. buf[getWidth() - x] = '\0';
  102. va_end(args);
  103. putStr(x, y, buf);
  104. }
  105. void Cucul::Clear(void)
  106. {
  107. cucul_clear_canvas(cv);
  108. }
  109. void Cucul::Blit(int x, int y, Cucul* c1, Cucul* c2)
  110. {
  111. cucul_blit(cv, x, y, c1->get_cucul_canvas_t(), c2->get_cucul_canvas_t());
  112. }
  113. void Cucul::Invert()
  114. {
  115. cucul_invert(cv);
  116. }
  117. void Cucul::Flip()
  118. {
  119. cucul_flip(cv);
  120. }
  121. void Cucul::Flop()
  122. {
  123. cucul_flop(cv);
  124. }
  125. void Cucul::Rotate()
  126. {
  127. cucul_rotate(cv);
  128. }
  129. void Cucul::drawLine(int x1, int y1, int x2, int y2, char const *ch)
  130. {
  131. cucul_draw_line(cv, x1, y1, x2, y2, ch);
  132. }
  133. void Cucul::drawPolyline(int const x[], int const y[], int f, char const *ch)
  134. {
  135. cucul_draw_polyline(cv, x, y, f, ch);
  136. }
  137. void Cucul::drawThinLine(int x1, int y1, int x2, int y2)
  138. {
  139. cucul_draw_thin_line(cv, x1, y1, x2, y2);
  140. }
  141. void Cucul::drawThinPolyline(int const x[], int const y[], int f)
  142. {
  143. cucul_draw_thin_polyline(cv, x, y, f);
  144. }
  145. void Cucul::drawCircle(int x, int y, int d, char const *ch)
  146. {
  147. cucul_draw_circle(cv, x, y, d, ch);
  148. }
  149. void Cucul::drawEllipse(int x, int y, int d1, int d2, char const *ch)
  150. {
  151. cucul_draw_ellipse(cv, x, y, d1, d2, ch);
  152. }
  153. void Cucul::drawThinEllipse(int x, int y, int d1, int d2)
  154. {
  155. cucul_draw_thin_ellipse(cv, x, y, d1, d2);
  156. }
  157. void Cucul::fillEllipse(int x, int y, int d1, int d2, char const *ch)
  158. {
  159. cucul_fill_ellipse(cv, x, y, d1, d2, ch);
  160. }
  161. void Cucul::drawBox(int x, int y, int w, int h, char const *ch)
  162. {
  163. cucul_draw_box(cv, x, y, w, h, ch);
  164. }
  165. void Cucul::drawThinBox(int x, int y, int w, int h)
  166. {
  167. cucul_draw_thin_box(cv, x, y, w, h);
  168. }
  169. void Cucul::fillBox(int x, int y, int w, int h, char const *ch)
  170. {
  171. cucul_fill_box(cv, x, y, w, h, ch);
  172. }
  173. void Cucul::drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, char const *ch)
  174. {
  175. cucul_draw_triangle(cv, x1, y1, x2, y2, x3, y3, ch);
  176. }
  177. void Cucul::drawThinTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
  178. {
  179. cucul_draw_thin_triangle(cv, x1, y1, x2, y2, x3, y3);
  180. }
  181. void Cucul::fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, const char *ch)
  182. {
  183. cucul_fill_triangle(cv, x1, y1, x2, y2, x3, y3, ch);
  184. }
  185. int Cucul::Rand(int min, int max)
  186. {
  187. return cucul_rand(min, max);
  188. }
  189. int Cucul::setAttr(unsigned long int attr)
  190. {
  191. return cucul_set_attr(cv, attr);
  192. }
  193. unsigned long int Cucul::getAttr(int x, int y)
  194. {
  195. return cucul_get_attr(cv, x, y);
  196. }
  197. int Cucul::setBoundaries(cucul_canvas_t *, int x, int y,
  198. unsigned int w, unsigned int h)
  199. {
  200. return cucul_set_canvas_boundaries(cv, x, y, h, w);
  201. }
  202. unsigned int Cucul::getFrameCount()
  203. {
  204. return cucul_get_canvas_frame_count(cv);
  205. }
  206. int Cucul::setFrame(unsigned int f)
  207. {
  208. return cucul_set_canvas_frame(cv, f);
  209. }
  210. int Cucul::createFrame(unsigned int f)
  211. {
  212. return cucul_create_canvas_frame(cv, f);
  213. }
  214. int Cucul::freeFrame(unsigned int f)
  215. {
  216. return cucul_create_canvas_frame(cv, f);
  217. }
  218. 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)
  219. {
  220. dither = cucul_create_dither(v1, v2, v3, v4, v5, v6, v7, v8);
  221. }
  222. Dither::~Dither()
  223. {
  224. cucul_free_dither(dither);
  225. }
  226. void Dither::setPalette(unsigned int r[], unsigned int g[], unsigned int b[], unsigned int a[])
  227. {
  228. cucul_set_dither_palette(dither, r, g, b, a);
  229. }
  230. void Dither::setBrightness(float f)
  231. {
  232. cucul_set_dither_brightness(dither, f);
  233. }
  234. void Dither::setGamma(float f)
  235. {
  236. cucul_set_dither_gamma(dither, f);
  237. }
  238. void Dither::setContrast(float f)
  239. {
  240. cucul_set_dither_contrast(dither, f);
  241. }
  242. void Dither::setInvert(int i)
  243. {
  244. cucul_set_dither_invert(dither, i);
  245. }
  246. void Dither::setAntialias(char const *cv)
  247. {
  248. cucul_set_dither_antialias(dither, cv);
  249. }
  250. char const *const * Dither::getAntialiasList()
  251. {
  252. return cucul_get_dither_antialias_list(dither);
  253. }
  254. void Dither::setColor(char const *cv)
  255. {
  256. cucul_set_dither_color(dither, cv);
  257. }
  258. char const *const * Dither::getColorList()
  259. {
  260. return cucul_get_dither_color_list(dither);
  261. }
  262. void Dither::setCharset(char const *cv)
  263. {
  264. cucul_set_dither_charset(dither, cv);
  265. }
  266. char const *const * Dither::getCharsetList()
  267. {
  268. return cucul_get_dither_charset_list(dither);
  269. }
  270. void Dither::setMode(char const *cv)
  271. {
  272. cucul_set_dither_mode(dither, cv);
  273. }
  274. char const *const * Dither::getModeList(void)
  275. {
  276. return cucul_get_dither_mode_list(dither);
  277. }
  278. void Dither::Bitmap(Cucul *cv, int x, int y, int w, int h, void *v)
  279. {
  280. cucul_dither_bitmap(cv->get_cucul_canvas_t(), x, y, w, h, dither, v);
  281. }
  282. Font::Font(void const *s, unsigned int v)
  283. {
  284. font = cucul_load_font(s, v);
  285. if(!font) throw -1;
  286. }
  287. char const *const * Font::getList(void)
  288. {
  289. return cucul_get_font_list();
  290. }
  291. unsigned int Font::getWidth()
  292. {
  293. return cucul_get_font_width(font);
  294. }
  295. unsigned int Font::getHeight()
  296. {
  297. return cucul_get_font_height(font);
  298. }
  299. void Font::renderCanvas(Cucul *cv, unsigned char *buf, unsigned int x, unsigned int y, unsigned int w)
  300. {
  301. cucul_render_canvas(cv->get_cucul_canvas_t(), font, buf, x, y, w);
  302. }
  303. unsigned long int const *Font::getBlocks()
  304. {
  305. return cucul_get_font_blocks(font);
  306. }
  307. Font::~Font()
  308. {
  309. cucul_free_font(font);
  310. }
  311. Buffer::Buffer()
  312. {
  313. buffer_ = NULL;
  314. }
  315. Buffer::~Buffer()
  316. {
  317. if(buffer_)
  318. cucul_free_buffer(buffer_);
  319. }
  320. char const *const * Buffer::getExportList(void)
  321. {
  322. return cucul_get_export_list();
  323. }
  324. void *Buffer::getData(void)
  325. {
  326. return cucul_get_buffer_data(buffer_);
  327. }
  328. void Buffer::loadMemory(void *buf, unsigned long int size)
  329. {
  330. buffer_ = cucul_load_memory(buf, size);
  331. if(buffer_ == NULL)
  332. throw -1;
  333. }
  334. void Buffer::loadFile(char const *filename)
  335. {
  336. buffer_ = cucul_load_file(filename);
  337. if(buffer_ == NULL)
  338. throw -1;
  339. }
  340. unsigned long int Buffer::getSize()
  341. {
  342. return cucul_get_buffer_size(buffer_);
  343. }
  344. cucul_buffer *Buffer::getBuffer()
  345. {
  346. return buffer_;
  347. }