Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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