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.

caca++.cpp 9.5 KiB

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