25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

caca-canvas.c 22 KiB

17 yıl önce
17 yıl önce
15 yıl önce
15 yıl önce
15 yıl önce
17 yıl önce
17 yıl önce
17 yıl önce
17 yıl önce
15 yıl önce
16 yıl önce
17 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * libcaca Ruby bindings
  3. * Copyright (c) 2007-2014 Pascal Terjan <pterjan@linuxfr.org>
  4. *
  5. * This library is free software. It comes without any warranty, to
  6. * the extent permitted by applicable law. You can redistribute it
  7. * and/or modify it under the terms of the Do What the Fuck You Want
  8. * to Public License, Version 2, as published by Sam Hocevar. See
  9. * http://www.wtfpl.net/ for more details.
  10. */
  11. #include <ruby.h>
  12. #include <caca.h>
  13. #include <errno.h>
  14. #include "caca-dither.h"
  15. #include "caca-font.h"
  16. #include "common.h"
  17. VALUE cCanvas;
  18. #define simple_func(x) \
  19. static VALUE x (VALUE self) \
  20. { \
  21. if( caca_##x (_SELF) <0) \
  22. rb_raise(rb_eRuntimeError, "%s", strerror(errno)); \
  23. \
  24. return self; \
  25. }
  26. #define get_int(x) \
  27. static VALUE get_##x (VALUE self) \
  28. { \
  29. return INT2NUM(caca_get_##x (_SELF)); \
  30. }
  31. static void canvas_free(void * p)
  32. {
  33. caca_free_canvas((caca_canvas_t *)p);
  34. }
  35. static VALUE canvas_alloc(VALUE klass)
  36. {
  37. VALUE obj;
  38. obj = Data_Wrap_Struct(klass, NULL, canvas_free, NULL);
  39. return obj;
  40. }
  41. VALUE canvas_create(caca_canvas_t *canvas)
  42. {
  43. return Data_Wrap_Struct(cCanvas, NULL, NULL, canvas);
  44. }
  45. static VALUE canvas_initialize(VALUE self, VALUE width, VALUE height)
  46. {
  47. caca_canvas_t *canvas;
  48. canvas = caca_create_canvas(NUM2INT(width), NUM2INT(height));
  49. if(canvas == NULL)
  50. {
  51. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  52. }
  53. _SELF = canvas;
  54. return self;
  55. }
  56. get_int(canvas_height)
  57. get_int(canvas_width)
  58. static VALUE set_canvas_width(VALUE self, VALUE width)
  59. {
  60. caca_set_canvas_size(_SELF, NUM2INT(width), caca_get_canvas_height(_SELF));
  61. return width;
  62. }
  63. static VALUE set_canvas_width2(VALUE self, VALUE width)
  64. {
  65. set_canvas_width(self, width);
  66. return self;
  67. }
  68. static VALUE set_canvas_height(VALUE self, VALUE height)
  69. {
  70. caca_set_canvas_size(_SELF, caca_get_canvas_width(_SELF), NUM2INT(height));
  71. return height;
  72. }
  73. static VALUE set_canvas_height2(VALUE self, VALUE height)
  74. {
  75. set_canvas_height(self, height);
  76. return self;
  77. }
  78. static VALUE set_canvas_size(VALUE self, VALUE height, VALUE width)
  79. {
  80. caca_set_canvas_size(_SELF, NUM2INT(width), NUM2INT(height));
  81. return self;
  82. }
  83. /****/
  84. static VALUE gotoxy(VALUE self, VALUE x, VALUE y)
  85. {
  86. if( caca_gotoxy(_SELF, NUM2INT(x), NUM2INT(y)) <0) {
  87. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  88. }
  89. return self;
  90. }
  91. static VALUE wherex(VALUE self)
  92. {
  93. return INT2NUM(caca_wherex(_SELF));
  94. }
  95. static VALUE wherey(VALUE self)
  96. {
  97. return INT2NUM(caca_wherey(_SELF));
  98. }
  99. simple_func(clear_canvas)
  100. static VALUE put_char(VALUE self, VALUE x, VALUE y, VALUE ch)
  101. {
  102. caca_put_char(_SELF, NUM2INT(x), NUM2INT(y), NUM2ULONG(ch));
  103. return self;
  104. }
  105. static VALUE get_char(VALUE self, VALUE x, VALUE y)
  106. {
  107. unsigned long int ch;
  108. ch = caca_get_char(_SELF, NUM2INT(x), NUM2INT(y));
  109. return INT2NUM(ch);
  110. }
  111. static VALUE put_str(VALUE self, VALUE x, VALUE y, VALUE str)
  112. {
  113. caca_put_str(_SELF, NUM2INT(x), NUM2INT(y), StringValuePtr(str));
  114. return self;
  115. }
  116. static VALUE get_attr(VALUE self, VALUE x, VALUE y)
  117. {
  118. unsigned long int ch;
  119. ch = caca_get_attr(_SELF, NUM2INT(x), NUM2INT(y));
  120. return INT2NUM(ch);
  121. }
  122. static VALUE set_attr(VALUE self, VALUE attr)
  123. {
  124. if(caca_set_attr(_SELF, NUM2ULONG(attr)) <0)
  125. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  126. return self;
  127. }
  128. static VALUE set_attr2(VALUE self, VALUE attr)
  129. {
  130. set_attr(self, attr);
  131. return self;
  132. }
  133. static VALUE put_attr(VALUE self, VALUE x, VALUE y, VALUE attr)
  134. {
  135. if(caca_put_attr(_SELF, NUM2INT(x), NUM2INT(y), NUM2ULONG(attr)) <0)
  136. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  137. return self;
  138. }
  139. static VALUE set_color_ansi(VALUE self, VALUE fg, VALUE bg)
  140. {
  141. if(caca_set_color_ansi(_SELF, NUM2INT(fg), NUM2INT(bg)) <0)
  142. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  143. return self;
  144. }
  145. static VALUE set_color_argb(VALUE self, VALUE fg, VALUE bg)
  146. {
  147. if(caca_set_color_argb(_SELF, NUM2UINT(fg), NUM2UINT(bg)) <0) {
  148. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  149. }
  150. return self;
  151. }
  152. static VALUE cprintf(int argc, VALUE* argv, VALUE self)
  153. {
  154. int x, y;
  155. VALUE rx, ry, format, rest, string;
  156. rb_scan_args(argc, argv, "3*", &rx, &ry, &format, &rest);
  157. x = NUM2INT(rx);
  158. y = NUM2INT(ry);
  159. string = rb_funcall2(rb_mKernel, rb_intern("sprintf"), argc-2, argv+2);
  160. caca_put_str(_SELF, x, y, StringValuePtr(string));
  161. return self;
  162. }
  163. get_int(canvas_handle_x)
  164. get_int(canvas_handle_y)
  165. static VALUE set_canvas_handle(VALUE self, VALUE x, VALUE y)
  166. {
  167. caca_set_canvas_handle(_SELF, NUM2INT(x), NUM2INT(y));
  168. return self;
  169. }
  170. static VALUE blit(int argc, VALUE* argv, VALUE self) {
  171. VALUE x, y, src, mask;
  172. caca_canvas_t *csrc, *cmask;
  173. rb_scan_args(argc, argv, "31", &x, &y, &src, &mask);
  174. Check_Type(x, T_FIXNUM);
  175. Check_Type(y, T_FIXNUM);
  176. if(CLASS_OF(src) != cCanvas)
  177. {
  178. rb_raise(rb_eArgError, "src is not a Caca::Canvas");
  179. }
  180. Data_Get_Struct(src, caca_canvas_t, csrc);
  181. if(!NIL_P(mask))
  182. {
  183. if(CLASS_OF(mask) != cCanvas)
  184. {
  185. rb_raise(rb_eArgError, "mask is not a Caca::Canvas");
  186. }
  187. Data_Get_Struct(mask, caca_canvas_t, cmask);
  188. }
  189. else
  190. cmask = NULL;
  191. if(caca_blit(_SELF, NUM2INT(x), NUM2INT(y), csrc, cmask)<0)
  192. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  193. return self;
  194. }
  195. static VALUE set_canvas_boundaries(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
  196. {
  197. if(caca_set_canvas_boundaries(_SELF, NUM2INT(x), NUM2INT(y), NUM2UINT(w), NUM2UINT(h))<0)
  198. {
  199. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  200. }
  201. return self;
  202. }
  203. /****/
  204. simple_func(invert)
  205. simple_func(flip)
  206. simple_func(flop)
  207. simple_func(rotate_180)
  208. simple_func(rotate_left)
  209. simple_func(rotate_right)
  210. simple_func(stretch_left)
  211. simple_func(stretch_right)
  212. /****/
  213. static VALUE draw_line(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE ch)
  214. {
  215. caca_draw_line(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2),NUM2ULONG(ch));
  216. return self;
  217. }
  218. static VALUE draw_polyline(VALUE self, VALUE points, VALUE ch)
  219. {
  220. int i, n;
  221. int *ax, *ay;
  222. int error = 0;
  223. VALUE v, x, y;
  224. n = RARRAY_LEN(points);
  225. ax = (int*)malloc(n*sizeof(int));
  226. if(!ax)
  227. rb_raise(rb_eNoMemError,"Out of memory");
  228. ay = (int*)malloc(n*sizeof(int));
  229. if(!ay)
  230. {
  231. free(ax);
  232. rb_raise(rb_eNoMemError,"Out of memory");
  233. }
  234. for(i=0; i<n; i++)
  235. {
  236. v = rb_ary_entry(points, i);
  237. if((TYPE(v) == T_ARRAY) && (RARRAY_LEN(v) == 2))
  238. {
  239. x = rb_ary_entry(v,0);
  240. y = rb_ary_entry(v,1);
  241. if(rb_obj_is_kind_of(x, rb_cInteger) &&
  242. rb_obj_is_kind_of(y, rb_cInteger))
  243. {
  244. ax[i] = NUM2INT(x);
  245. ay[i] = NUM2INT(y);
  246. } else
  247. error = 1;
  248. }
  249. else
  250. error = 1;
  251. }
  252. if(error)
  253. {
  254. free(ax);
  255. free(ay);
  256. rb_raise(rb_eArgError, "Invalid list of points");
  257. }
  258. n--;
  259. caca_draw_polyline(_SELF, ax, ay, n, NUM2ULONG(ch));
  260. free(ax);
  261. free(ay);
  262. return self;
  263. }
  264. static VALUE draw_thin_line(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2)
  265. {
  266. caca_draw_thin_line(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2));
  267. return self;
  268. }
  269. static VALUE draw_thin_polyline(VALUE self, VALUE points)
  270. {
  271. int i, n;
  272. int *ax, *ay;
  273. int error = 0;
  274. VALUE v, x, y;
  275. n = RARRAY_LEN(points);
  276. ax = (int*)malloc(n*sizeof(int));
  277. if(!ax)
  278. rb_raise(rb_eNoMemError,"Out of memory");
  279. ay = (int*)malloc(n*sizeof(int));
  280. if(!ay)
  281. {
  282. free(ax);
  283. rb_raise(rb_eNoMemError,"Out of memory");
  284. }
  285. for(i=0; i<n; i++)
  286. {
  287. v = rb_ary_entry(points, i);
  288. if((TYPE(v) == T_ARRAY) && (RARRAY_LEN(v) == 2))
  289. {
  290. x = rb_ary_entry(v,0);
  291. y = rb_ary_entry(v,1);
  292. if(rb_obj_is_kind_of(x, rb_cInteger) &&
  293. rb_obj_is_kind_of(y, rb_cInteger))
  294. {
  295. ax[i] = NUM2INT(x);
  296. ay[i] = NUM2INT(y);
  297. } else
  298. error = 1;
  299. }
  300. else
  301. error = 1;
  302. }
  303. if(error)
  304. {
  305. free(ax);
  306. free(ay);
  307. rb_raise(rb_eArgError, "Invalid list of points");
  308. }
  309. n--;
  310. caca_draw_thin_polyline(_SELF, ax, ay, n);
  311. free(ax);
  312. free(ay);
  313. return self;
  314. }
  315. static VALUE draw_circle(VALUE self, VALUE x, VALUE y, VALUE r, VALUE ch)
  316. {
  317. caca_draw_circle(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(r), NUM2ULONG(ch));
  318. return self;
  319. }
  320. static VALUE draw_ellipse(VALUE self, VALUE x, VALUE y, VALUE a, VALUE b, VALUE ch)
  321. {
  322. caca_draw_ellipse(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(a), NUM2INT(b), NUM2ULONG(ch));
  323. return self;
  324. }
  325. static VALUE draw_thin_ellipse(VALUE self, VALUE x, VALUE y, VALUE a, VALUE b)
  326. {
  327. caca_draw_thin_ellipse(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(a), NUM2INT(b));
  328. return self;
  329. }
  330. static VALUE fill_ellipse(VALUE self, VALUE x, VALUE y, VALUE a, VALUE b, VALUE ch)
  331. {
  332. caca_fill_ellipse(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(a), NUM2INT(b), NUM2ULONG(ch));
  333. return self;
  334. }
  335. static VALUE draw_box(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h, VALUE ch)
  336. {
  337. caca_draw_box(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h), NUM2ULONG(ch));
  338. return self;
  339. }
  340. static VALUE draw_thin_box(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
  341. {
  342. caca_draw_thin_box(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h));
  343. return self;
  344. }
  345. static VALUE draw_cp437_box(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
  346. {
  347. caca_draw_cp437_box(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h));
  348. return self;
  349. }
  350. static VALUE fill_box(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h, VALUE ch)
  351. {
  352. caca_fill_box(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h), NUM2ULONG(ch));
  353. return self;
  354. }
  355. static VALUE draw_triangle(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE x3, VALUE y3, VALUE ch)
  356. {
  357. caca_draw_triangle(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2), NUM2INT(x3), NUM2INT(y3), NUM2ULONG(ch));
  358. return self;
  359. }
  360. static VALUE draw_thin_triangle(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE x3, VALUE y3)
  361. {
  362. caca_draw_thin_triangle(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2), NUM2INT(x3), NUM2INT(y3));
  363. return self;
  364. }
  365. static VALUE fill_triangle(VALUE self, VALUE x1, VALUE y1, VALUE x2, VALUE y2, VALUE x3, VALUE y3, VALUE ch)
  366. {
  367. caca_fill_triangle(_SELF, NUM2INT(x1), NUM2INT(y1), NUM2INT(x2), NUM2INT(y2), NUM2INT(x3), NUM2INT(y3), NUM2ULONG(ch));
  368. return self;
  369. }
  370. static VALUE fill_triangle_textured(VALUE self, VALUE coords, VALUE texture, VALUE uv)
  371. {
  372. caca_canvas_t *ctexture;
  373. int i, l;
  374. int ccoords[6];
  375. float cuv[6];
  376. VALUE v;
  377. l = RARRAY_LEN(coords);
  378. if(l != 6 && l != 3)
  379. {
  380. rb_raise(rb_eArgError, "invalid coords list");
  381. }
  382. for(i=0; i<l; i++)
  383. {
  384. v = rb_ary_entry(coords, i);
  385. if(l==6)
  386. ccoords[i] = NUM2INT(v);
  387. else
  388. {
  389. if((TYPE(v) != T_ARRAY) || (RARRAY_LEN(v) != 2))
  390. rb_raise(rb_eArgError, "invalid coords list");
  391. ccoords[2*i] = NUM2INT(rb_ary_entry(v, 0));
  392. ccoords[2*i+1] = NUM2INT(rb_ary_entry(v, 1));
  393. }
  394. }
  395. l = RARRAY_LEN(uv);
  396. if(l != 6 && l != 3)
  397. {
  398. rb_raise(rb_eArgError, "invalid uv list");
  399. }
  400. for(i=0; i<l; i++)
  401. {
  402. v = rb_ary_entry(uv, i);
  403. if(l==6)
  404. cuv[i] = NUM2DBL(v);
  405. else
  406. {
  407. if((TYPE(v) != T_ARRAY) || (RARRAY_LEN(v) != 2))
  408. rb_raise(rb_eArgError, "invalid uv list");
  409. cuv[2*i] = NUM2DBL(rb_ary_entry(v, 0));
  410. cuv[2*i+1] = NUM2DBL(rb_ary_entry(v, 1));
  411. }
  412. }
  413. if(CLASS_OF(texture) != cCanvas)
  414. {
  415. rb_raise(rb_eArgError, "texture is not a Caca::Canvas");
  416. }
  417. Data_Get_Struct(texture, caca_canvas_t, ctexture);
  418. caca_fill_triangle_textured(_SELF, ccoords, ctexture, cuv);
  419. return self;
  420. }
  421. static VALUE dither_bitmap(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h, VALUE d, VALUE pixels)
  422. {
  423. if(CLASS_OF(d) != cDither)
  424. rb_raise(rb_eArgError, "d is not a Caca::Dither");
  425. Check_Type(pixels, T_STRING);
  426. caca_dither_bitmap(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h), DATA_PTR(d), StringValuePtr(pixels));
  427. return self;
  428. }
  429. /****/
  430. get_int(frame_count)
  431. static VALUE set_frame(VALUE self, VALUE id)
  432. {
  433. if(caca_set_frame(_SELF, NUM2INT(id))<0)
  434. rb_raise(rb_eArgError, "%s", strerror(errno));
  435. return self;
  436. }
  437. static VALUE set_frame2(VALUE self, VALUE id)
  438. {
  439. set_frame(self, id);
  440. return self;
  441. }
  442. static VALUE get_frame_name(VALUE self)
  443. {
  444. return rb_str_new2(caca_get_frame_name(_SELF));
  445. }
  446. static VALUE set_frame_name(VALUE self, VALUE name)
  447. {
  448. if(caca_set_frame_name(_SELF, StringValuePtr(name))<0)
  449. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  450. return self;
  451. }
  452. static VALUE set_frame_name2(VALUE self, VALUE name)
  453. {
  454. set_frame_name(self, name);
  455. return self;
  456. }
  457. static VALUE create_frame(VALUE self, VALUE id)
  458. {
  459. if(caca_create_frame(_SELF, NUM2INT(id))<0) {
  460. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  461. }
  462. return self;
  463. }
  464. static VALUE free_frame(VALUE self, VALUE id)
  465. {
  466. if(caca_free_frame(_SELF, NUM2INT(id))<0) {
  467. rb_raise(rb_eArgError, "%s", strerror(errno));
  468. }
  469. return self;
  470. }
  471. /****/
  472. static VALUE render_canvas(VALUE self, VALUE font, VALUE width, VALUE height, VALUE pitch)
  473. {
  474. void *buf;
  475. caca_font_t *f;
  476. VALUE b;
  477. if(CLASS_OF(font) != cFont)
  478. {
  479. rb_raise(rb_eArgError, "First argument is not a Caca::Font");
  480. }
  481. buf = malloc(width*height*4);
  482. if(buf == NULL)
  483. {
  484. rb_raise(rb_eNoMemError, "Out of memory");
  485. }
  486. f = DATA_PTR(font);
  487. caca_render_canvas(_SELF, f, buf, NUM2UINT(width), NUM2UINT(height), NUM2UINT(pitch));
  488. b = rb_str_new(buf, width*height*4);
  489. free(buf);
  490. return b;
  491. }
  492. static VALUE import_from_memory(VALUE self, VALUE data, VALUE format)
  493. {
  494. long int bytes;
  495. bytes = caca_import_canvas_from_memory (_SELF, StringValuePtr(data), RSTRING_LEN(StringValue(data)), StringValuePtr(format));
  496. if(bytes <= 0)
  497. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  498. return self;
  499. }
  500. static VALUE import_area_from_memory(VALUE self, VALUE x, VALUE y, VALUE data, VALUE format)
  501. {
  502. long int bytes;
  503. bytes = caca_import_area_from_memory (_SELF, NUM2INT(x), NUM2INT(y), StringValuePtr(data), RSTRING_LEN(StringValue(data)), StringValuePtr(format));
  504. if(bytes <= 0)
  505. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  506. return self;
  507. }
  508. static VALUE import_from_file(VALUE self, VALUE filename, VALUE format)
  509. {
  510. long int bytes;
  511. bytes = caca_import_canvas_from_file (_SELF, StringValuePtr(filename), StringValuePtr(format));
  512. if(bytes <= 0)
  513. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  514. return self;
  515. }
  516. static VALUE import_area_from_file(VALUE self, VALUE x, VALUE y, VALUE filename, VALUE format)
  517. {
  518. long int bytes;
  519. bytes = caca_import_area_from_file (_SELF, NUM2INT(x), NUM2INT(y), StringValuePtr(filename), StringValuePtr(format));
  520. if(bytes <= 0)
  521. rb_raise(rb_eRuntimeError, "%s", strerror(errno));
  522. return self;
  523. }
  524. static VALUE export_area_to_memory(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h, VALUE format)
  525. {
  526. size_t bytes;
  527. void *result;
  528. VALUE ret;
  529. result = caca_export_area_to_memory (_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h), StringValuePtr(format), &bytes);
  530. ret = rb_str_new(result, bytes);
  531. free(result);
  532. return ret;
  533. }
  534. static VALUE export_to_memory(VALUE self, VALUE format)
  535. {
  536. size_t bytes;
  537. void *result;
  538. VALUE ret;
  539. result = caca_export_canvas_to_memory (_SELF, StringValuePtr(format), &bytes);
  540. ret = rb_str_new(result, bytes);
  541. free(result);
  542. return ret;
  543. }
  544. get_singleton_double_list(export)
  545. get_singleton_double_list(import)
  546. /****/
  547. simple_func(disable_dirty_rect)
  548. simple_func(enable_dirty_rect)
  549. get_int(dirty_rect_count)
  550. static VALUE dirty_rect(VALUE self, VALUE n)
  551. {
  552. int x, y, width, height;
  553. VALUE ary;
  554. ary = rb_ary_new();
  555. caca_get_dirty_rect(_SELF, NUM2INT(n), &x, &y, &width, &height);
  556. rb_ary_push(ary, INT2NUM(x));
  557. rb_ary_push(ary, INT2NUM(y));
  558. rb_ary_push(ary, INT2NUM(width));
  559. rb_ary_push(ary, INT2NUM(height));
  560. return ary;
  561. }
  562. static VALUE dirty_rects(VALUE self)
  563. {
  564. int n = caca_get_dirty_rect_count(_SELF), i;
  565. VALUE ary;
  566. ary = rb_ary_new();
  567. for(i=0; i<n; i++)
  568. {
  569. rb_ary_push(ary, dirty_rect(self, INT2NUM(i)));
  570. }
  571. return ary;
  572. }
  573. /*FIXME Handle an array for the rect */
  574. static VALUE add_dirty_rect(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
  575. {
  576. caca_add_dirty_rect(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h));
  577. return self;
  578. }
  579. static VALUE remove_dirty_rect(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
  580. {
  581. caca_remove_dirty_rect(_SELF, NUM2INT(x), NUM2INT(y), NUM2INT(w), NUM2INT(h));
  582. return self;
  583. }
  584. simple_func(clear_dirty_rect_list)
  585. /****/
  586. void Init_caca_canvas(VALUE mCaca)
  587. {
  588. cCanvas = rb_define_class_under(mCaca, "Canvas", rb_cObject);
  589. rb_define_alloc_func(cCanvas, canvas_alloc);
  590. rb_define_method(cCanvas, "initialize", canvas_initialize, 2);
  591. rb_define_method(cCanvas, "width", get_canvas_width, 0);
  592. rb_define_method(cCanvas, "width=", set_canvas_width, 1);
  593. rb_define_method(cCanvas, "set_width", set_canvas_width2, 1);
  594. rb_define_method(cCanvas, "height", get_canvas_height, 0);
  595. rb_define_method(cCanvas, "height=", set_canvas_height, 1);
  596. rb_define_method(cCanvas, "set_height", set_canvas_height2, 1);
  597. rb_define_method(cCanvas, "set_size", set_canvas_size, 2);
  598. rb_define_method(cCanvas, "gotoxy", gotoxy, 2);
  599. rb_define_method(cCanvas, "wherex", wherex, 0);
  600. rb_define_method(cCanvas, "wherey", wherey, 0);
  601. rb_define_method(cCanvas, "handle_x", get_canvas_handle_x, 0);
  602. rb_define_method(cCanvas, "handle_y", get_canvas_handle_y, 0);
  603. rb_define_method(cCanvas, "set_handle", set_canvas_handle, 2);
  604. rb_define_method(cCanvas, "blit", blit, -1);
  605. rb_define_method(cCanvas, "set_boundaries", set_canvas_boundaries, 4);
  606. rb_define_method(cCanvas, "clear", clear_canvas, 0);
  607. rb_define_method(cCanvas, "put_char", put_char, 3);
  608. rb_define_method(cCanvas, "get_char", get_char, 2);
  609. rb_define_method(cCanvas, "put_str", put_str, 3);
  610. rb_define_method(cCanvas, "printf", cprintf, -1);
  611. rb_define_method(cCanvas, "get_attr", get_attr, 3);
  612. rb_define_method(cCanvas, "attr=", set_attr, 1);
  613. rb_define_method(cCanvas, "set_attr", set_attr2, 1);
  614. rb_define_method(cCanvas, "put_attr", put_attr, 3);
  615. rb_define_method(cCanvas, "set_color_ansi", set_color_ansi, 2);
  616. rb_define_method(cCanvas, "set_color_argb", set_color_argb, 2);
  617. rb_define_method(cCanvas, "invert", invert, 0);
  618. rb_define_method(cCanvas, "flip", flip, 0);
  619. rb_define_method(cCanvas, "flop", flop, 0);
  620. rb_define_method(cCanvas, "rotate_180", rotate_180, 0);
  621. rb_define_method(cCanvas, "rotate_left", rotate_left, 0);
  622. rb_define_method(cCanvas, "rotate_right", rotate_right, 0);
  623. rb_define_method(cCanvas, "stretch_left", stretch_left, 0);
  624. rb_define_method(cCanvas, "stretch_right", stretch_right, 0);
  625. rb_define_method(cCanvas, "draw_line", draw_line, 5);
  626. rb_define_method(cCanvas, "draw_polyline", draw_polyline, 2);
  627. rb_define_method(cCanvas, "draw_thin_line", draw_thin_line, 4);
  628. rb_define_method(cCanvas, "draw_thin_polyline", draw_thin_polyline, 1);
  629. rb_define_method(cCanvas, "draw_circle", draw_circle, 4);
  630. rb_define_method(cCanvas, "draw_ellipse", draw_ellipse, 5);
  631. rb_define_method(cCanvas, "draw_thin_ellipse", draw_thin_ellipse, 4);
  632. rb_define_method(cCanvas, "fill_ellipse", fill_ellipse, 5);
  633. rb_define_method(cCanvas, "draw_box", draw_box, 5);
  634. rb_define_method(cCanvas, "draw_thin_box", draw_thin_box, 4);
  635. rb_define_method(cCanvas, "draw_cp437_box", draw_cp437_box, 4);
  636. rb_define_method(cCanvas, "fill_box", fill_box, 5);
  637. rb_define_method(cCanvas, "draw_triangle", draw_triangle, 7);
  638. rb_define_method(cCanvas, "draw_thin_triangle", draw_thin_triangle, 6);
  639. rb_define_method(cCanvas, "fill_triangle", fill_triangle, 7);
  640. rb_define_method(cCanvas, "fill_triangle_textured", fill_triangle_textured, 4);
  641. rb_define_method(cCanvas, "dither_bitmap", dither_bitmap, 6);
  642. rb_define_method(cCanvas, "frame_count", get_frame_count, 0);
  643. rb_define_method(cCanvas, "frame=", set_frame, 1);
  644. rb_define_method(cCanvas, "set_frame", set_frame2, 1);
  645. rb_define_method(cCanvas, "frame_name", get_frame_name, 0);
  646. rb_define_method(cCanvas, "frame_name=", set_frame_name, 1);
  647. rb_define_method(cCanvas, "set_frame_name", set_frame_name2, 1);
  648. rb_define_method(cCanvas, "create_frame", create_frame, 1);
  649. rb_define_method(cCanvas, "free_frame", free_frame, 1);
  650. rb_define_method(cCanvas, "render", render_canvas, 4);
  651. rb_define_method(cCanvas, "import_from_memory", import_from_memory, 2);
  652. rb_define_method(cCanvas, "import_area_from_memory", import_area_from_memory, 4);
  653. rb_define_method(cCanvas, "import_from_file", import_from_file, 2);
  654. rb_define_method(cCanvas, "import_area_from_file", import_area_from_file, 4);
  655. rb_define_method(cCanvas, "export_to_memory", export_to_memory, 1);
  656. rb_define_method(cCanvas, "export_area_to_memory", export_area_to_memory, 5);
  657. rb_define_singleton_method(cCanvas, "export_list", export_list, 0);
  658. rb_define_singleton_method(cCanvas, "import_list", import_list, 0);
  659. rb_define_method(cCanvas, "disable_dirty_rect", disable_dirty_rect, 0);
  660. rb_define_method(cCanvas, "enable_dirty_rect", enable_dirty_rect, 0);
  661. rb_define_method(cCanvas, "dirty_rect_count", get_dirty_rect_count, 0);
  662. rb_define_method(cCanvas, "dirty_rect", dirty_rect, 1);
  663. rb_define_method(cCanvas, "dirty_rects", dirty_rects, 0);
  664. rb_define_method(cCanvas, "add_dirty_rect", add_dirty_rect, 4);
  665. rb_define_method(cCanvas, "remove_dirty_rect", remove_dirty_rect, 4);
  666. rb_define_method(cCanvas, "clear_dirty_rect_list", clear_dirty_rect_list, 0);
  667. }