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.
 
 
 
 
 
 

506 lines
9.4 KiB

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