選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

caca-canvas.c 22 KiB

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