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.

преди 17 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 16 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * libcaca++ C++ bindings for libcaca
  3. * Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  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. * This file contains the main functions used by \e libcaca++ applications to
  16. * initialise the library, get the screen properties, set the framerate and
  17. * so on.
  18. */
  19. #include "config.h"
  20. #include <iostream>
  21. #include <stdio.h> // BUFSIZ
  22. #include <stdarg.h> // va_*
  23. #include "caca++.h"
  24. uint32_t Charset::utf8ToUtf32(char const *s, size_t *read)
  25. {
  26. return caca_utf8_to_utf32(s, read);
  27. }
  28. size_t Charset::utf32ToUtf8(char *buf, uint32_t ch)
  29. {
  30. return caca_utf32_to_utf8(buf, ch);
  31. }
  32. uint8_t Charset::utf32ToCp437(uint32_t ch)
  33. {
  34. return caca_utf32_to_cp437(ch);
  35. }
  36. uint32_t Charset::cp437ToUtf32(uint8_t ch)
  37. {
  38. return caca_cp437_to_utf32(ch);
  39. }
  40. Canvas::Canvas()
  41. {
  42. cv = caca_create_canvas(0, 0);
  43. if(!cv)
  44. throw -1;
  45. }
  46. Canvas::Canvas(int width, int height)
  47. {
  48. cv = caca_create_canvas(width, height);
  49. if(!cv) throw -1;
  50. }
  51. Canvas::~Canvas()
  52. {
  53. if(cv)
  54. caca_free_canvas(cv);
  55. }
  56. caca_canvas_t *Canvas::get_caca_canvas_t()
  57. {
  58. return cv;
  59. }
  60. void Canvas::setSize(unsigned int width, unsigned int height)
  61. {
  62. caca_set_canvas_size(cv, width, height);
  63. }
  64. unsigned int Canvas::getWidth(void)
  65. {
  66. return caca_get_canvas_width(cv);
  67. }
  68. unsigned int Canvas::getHeight(void)
  69. {
  70. return caca_get_canvas_height(cv);
  71. }
  72. int Canvas::setColorANSI(uint8_t f, uint8_t b)
  73. {
  74. return caca_set_color_ansi(cv, f, b);
  75. }
  76. int Canvas::setColorARGB(unsigned int f, unsigned int b)
  77. {
  78. return caca_set_color_argb(cv, f, b);
  79. }
  80. void Canvas::putChar(int x, int y, uint32_t ch)
  81. {
  82. caca_put_char(cv, x, y, ch);
  83. }
  84. uint32_t Canvas::getChar(int x, int y)
  85. {
  86. return caca_get_char(cv, x, y);
  87. }
  88. void Canvas::putStr(int x, int y, char *str)
  89. {
  90. caca_put_str(cv, x, y, str);
  91. }
  92. void Canvas::Printf(int x, int y, char const * format, ...)
  93. {
  94. char tmp[BUFSIZ];
  95. char *buf = tmp;
  96. va_list args;
  97. va_start(args, format);
  98. #if defined(HAVE_VSNPRINTF)
  99. vsnprintf(buf, getWidth() - x + 1, format, args);
  100. #else
  101. vsprintf(buf, format, args);
  102. #endif
  103. buf[getWidth() - x] = '\0';
  104. va_end(args);
  105. putStr(x, y, buf);
  106. }
  107. void Canvas::Clear(void)
  108. {
  109. caca_clear_canvas(cv);
  110. }
  111. void Canvas::Blit(int x, int y, Canvas* c1, Canvas* c2)
  112. {
  113. caca_blit(cv, x, y, c1->get_caca_canvas_t(),
  114. c2 ? c2->get_caca_canvas_t() : NULL);
  115. }
  116. void Canvas::Invert()
  117. {
  118. caca_invert(cv);
  119. }
  120. void Canvas::Flip()
  121. {
  122. caca_flip(cv);
  123. }
  124. void Canvas::Flop()
  125. {
  126. caca_flop(cv);
  127. }
  128. void Canvas::Rotate180()
  129. {
  130. caca_rotate_180(cv);
  131. }
  132. void Canvas::RotateLeft()
  133. {
  134. caca_rotate_left(cv);
  135. }
  136. void Canvas::RotateRight()
  137. {
  138. caca_rotate_right(cv);
  139. }
  140. void Canvas::drawLine(int x1, int y1, int x2, int y2, uint32_t ch)
  141. {
  142. caca_draw_line(cv, x1, y1, x2, y2, ch);
  143. }
  144. void Canvas::drawPolyline(int const x[], int const y[], int f, uint32_t ch)
  145. {
  146. caca_draw_polyline(cv, x, y, f, ch);
  147. }
  148. void Canvas::drawThinLine(int x1, int y1, int x2, int y2)
  149. {
  150. caca_draw_thin_line(cv, x1, y1, x2, y2);
  151. }
  152. void Canvas::drawThinPolyline(int const x[], int const y[], int f)
  153. {
  154. caca_draw_thin_polyline(cv, x, y, f);
  155. }
  156. void Canvas::drawCircle(int x, int y, int d, uint32_t ch)
  157. {
  158. caca_draw_circle(cv, x, y, d, ch);
  159. }
  160. void Canvas::drawEllipse(int x, int y, int d1, int d2, uint32_t ch)
  161. {
  162. caca_draw_ellipse(cv, x, y, d1, d2, ch);
  163. }
  164. void Canvas::drawThinEllipse(int x, int y, int d1, int d2)
  165. {
  166. caca_draw_thin_ellipse(cv, x, y, d1, d2);
  167. }
  168. void Canvas::fillEllipse(int x, int y, int d1, int d2, uint32_t ch)
  169. {
  170. caca_fill_ellipse(cv, x, y, d1, d2, ch);
  171. }
  172. void Canvas::drawBox(int x, int y, int w, int h, uint32_t ch)
  173. {
  174. caca_draw_box(cv, x, y, w, h, ch);
  175. }
  176. void Canvas::drawThinBox(int x, int y, int w, int h)
  177. {
  178. caca_draw_thin_box(cv, x, y, w, h);
  179. }
  180. void Canvas::drawCP437Box(int x, int y, int w, int h)
  181. {
  182. caca_draw_cp437_box(cv, x, y, w, h);
  183. }
  184. void Canvas::fillBox(int x, int y, int w, int h, uint32_t ch)
  185. {
  186. caca_fill_box(cv, x, y, w, h, ch);
  187. }
  188. void Canvas::drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, uint32_t ch)
  189. {
  190. caca_draw_triangle(cv, x1, y1, x2, y2, x3, y3, ch);
  191. }
  192. void Canvas::drawThinTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
  193. {
  194. caca_draw_thin_triangle(cv, x1, y1, x2, y2, x3, y3);
  195. }
  196. void Canvas::fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, uint32_t ch)
  197. {
  198. caca_fill_triangle(cv, x1, y1, x2, y2, x3, y3, ch);
  199. }
  200. int Canvas::Rand(int min, int max)
  201. {
  202. return caca_rand(min, max);
  203. }
  204. const char * Canvas::getVersion()
  205. {
  206. return caca_get_version();
  207. }
  208. int Canvas::setAttr(uint32_t attr)
  209. {
  210. return caca_set_attr(cv, attr);
  211. }
  212. uint32_t Canvas::getAttr(int x, int y)
  213. {
  214. return caca_get_attr(cv, x, y);
  215. }
  216. int Canvas::setBoundaries(caca_canvas_t *, int x, int y,
  217. unsigned int w, unsigned int h)
  218. {
  219. return caca_set_canvas_boundaries(cv, x, y, h, w);
  220. }
  221. unsigned int Canvas::getFrameCount()
  222. {
  223. return caca_get_frame_count(cv);
  224. }
  225. int Canvas::setFrame(unsigned int f)
  226. {
  227. return caca_set_frame(cv, f);
  228. }
  229. int Canvas::createFrame(unsigned int f)
  230. {
  231. return caca_create_frame(cv, f);
  232. }
  233. int Canvas::freeFrame(unsigned int f)
  234. {
  235. return caca_create_frame(cv, f);
  236. }
  237. char const *const * Canvas::getImportList(void)
  238. {
  239. return caca_get_import_list();
  240. }
  241. long int Canvas::importFromMemory(void const *buf, size_t len, char const *fmt)
  242. {
  243. return caca_import_canvas_from_memory(cv, buf, len, fmt);
  244. }
  245. long int Canvas::importFromFile(char const *file, char const *fmt)
  246. {
  247. return caca_import_canvas_from_file(cv, file, fmt);
  248. }
  249. char const *const * Canvas::getExportList(void)
  250. {
  251. return caca_get_export_list();
  252. }
  253. void *Canvas::exportToMemory(char const *fmt, size_t *len)
  254. {
  255. return caca_export_canvas_to_memory(cv, fmt, len);
  256. }
  257. 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)
  258. {
  259. dither = caca_create_dither(v1, v2, v3, v4, v5, v6, v7, v8);
  260. }
  261. Dither::~Dither()
  262. {
  263. caca_free_dither(dither);
  264. }
  265. void Dither::setPalette(uint32_t r[], uint32_t g[], uint32_t b[], uint32_t a[])
  266. {
  267. caca_set_dither_palette(dither, r, g, b, a);
  268. }
  269. void Dither::setBrightness(float f)
  270. {
  271. caca_set_dither_brightness(dither, f);
  272. }
  273. void Dither::setGamma(float f)
  274. {
  275. caca_set_dither_gamma(dither, f);
  276. }
  277. void Dither::setContrast(float f)
  278. {
  279. caca_set_dither_contrast(dither, f);
  280. }
  281. void Dither::setAntialias(char const *cv)
  282. {
  283. caca_set_dither_antialias(dither, cv);
  284. }
  285. char const *const * Dither::getAntialiasList()
  286. {
  287. return caca_get_dither_antialias_list(dither);
  288. }
  289. void Dither::setColor(char const *cv)
  290. {
  291. caca_set_dither_color(dither, cv);
  292. }
  293. char const *const * Dither::getColorList()
  294. {
  295. return caca_get_dither_color_list(dither);
  296. }
  297. void Dither::setCharset(char const *cv)
  298. {
  299. caca_set_dither_charset(dither, cv);
  300. }
  301. char const *const * Dither::getCharsetList()
  302. {
  303. return caca_get_dither_charset_list(dither);
  304. }
  305. void Dither::setMode(char const *cv)
  306. {
  307. caca_set_dither_algorithm(dither, cv);
  308. }
  309. char const *const * Dither::getModeList(void)
  310. {
  311. return caca_get_dither_algorithm_list(dither);
  312. }
  313. void Dither::Bitmap(Canvas *cv, int x, int y, int w, int h, void *v)
  314. {
  315. caca_dither_bitmap(cv->get_caca_canvas_t(), x, y, w, h, dither, v);
  316. }
  317. Font::Font(void const *s, unsigned int v)
  318. {
  319. font = caca_load_font(s, v);
  320. if(!font) throw -1;
  321. }
  322. char const *const * Font::getList(void)
  323. {
  324. return caca_get_font_list();
  325. }
  326. unsigned int Font::getWidth()
  327. {
  328. return caca_get_font_width(font);
  329. }
  330. unsigned int Font::getHeight()
  331. {
  332. return caca_get_font_height(font);
  333. }
  334. void Font::renderCanvas(Canvas *cv, uint8_t *buf, unsigned int x, unsigned int y, unsigned int w)
  335. {
  336. caca_render_canvas(cv->get_caca_canvas_t(), font, buf, x, y, w);
  337. }
  338. uint32_t const *Font::getBlocks()
  339. {
  340. return caca_get_font_blocks(font);
  341. }
  342. Font::~Font()
  343. {
  344. caca_free_font(font);
  345. }
  346. Caca::Caca(Canvas *cv)
  347. {
  348. dp = caca_create_display(cv->get_caca_canvas_t());
  349. if(!dp)
  350. throw -1;
  351. }
  352. Caca::~Caca()
  353. {
  354. caca_free_display(dp);
  355. }
  356. void Caca::Attach(Canvas *cv)
  357. {
  358. dp = caca_create_display(cv->get_caca_canvas_t());
  359. if(!dp)
  360. throw -1;
  361. }
  362. void Caca::Detach()
  363. {
  364. caca_free_display(dp);
  365. }
  366. void Caca::setDisplayTime(unsigned int d)
  367. {
  368. caca_set_display_time(dp, d);
  369. }
  370. void Caca::Display()
  371. {
  372. caca_refresh_display(dp);
  373. }
  374. unsigned int Caca::getDisplayTime()
  375. {
  376. return caca_get_display_time(dp);
  377. }
  378. unsigned int Caca::getWidth()
  379. {
  380. return caca_get_display_width(dp);
  381. }
  382. unsigned int Caca::getHeight()
  383. {
  384. return caca_get_display_height(dp);
  385. }
  386. int Caca::setTitle(char const *s)
  387. {
  388. return caca_set_display_title(dp, s);
  389. }
  390. int Caca::getEvent(unsigned int g, Event *n, int aa)
  391. {
  392. return caca_get_event(dp, g, n ? &n->e : NULL, aa);
  393. }
  394. unsigned int Caca::getMouseX()
  395. {
  396. return caca_get_mouse_x(dp);
  397. }
  398. unsigned int Caca::getMouseY()
  399. {
  400. return caca_get_mouse_x(dp);
  401. }
  402. void Caca::setMouse(int v)
  403. {
  404. caca_set_mouse(dp, v);
  405. }
  406. const char * Caca::getVersion()
  407. {
  408. return caca_get_version();
  409. }