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.
 
 
 
 
 
 

810 rivejä
22 KiB

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