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.

Caca.xs 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /* What will I use my programming skill for? */
  2. #include "EXTERN.h"
  3. #include "perl.h"
  4. #include "XSUB.h"
  5. #include "caca.h"
  6. /* ref($sprite) eq 'HASH' && $sprite->{__address__} */
  7. void *
  8. address_of(SV *sprite)
  9. {
  10. /* make sure sprite is a hashref */
  11. if (SvTYPE(SvRV(sprite)) != SVt_PVHV) {
  12. return NULL;
  13. }
  14. return (struct caca_sprite *)
  15. SvIV(*hv_fetch((HV *) SvRV(sprite), "__address__", 11, 0));
  16. }
  17. /* turn a perl array of numbers into a c array */
  18. void *
  19. c_array(SV *p_array)
  20. {
  21. }
  22. MODULE = Term::Caca PACKAGE = Term::Caca
  23. # -==[- Basic functions -]==--------------------------------------------------
  24. void
  25. _init()
  26. CODE:
  27. caca_init();
  28. void
  29. _set_delay(usec)
  30. unsigned int usec
  31. CODE:
  32. caca_set_delay(usec);
  33. unsigned int
  34. _get_feature(feature)
  35. unsigned int feature
  36. CODE:
  37. RETVAL = caca_get_feature(feature);
  38. OUTPUT:
  39. RETVAL
  40. void
  41. _set_feature(feature)
  42. unsigned int feature
  43. CODE:
  44. caca_set_feature(feature);
  45. const char *
  46. _get_feature_name(feature)
  47. unsigned int feature
  48. CODE:
  49. RETVAL = caca_get_feature_name(feature);
  50. OUTPUT:
  51. RETVAL
  52. unsigned int
  53. _get_rendertime()
  54. CODE:
  55. RETVAL = caca_get_rendertime();
  56. OUTPUT:
  57. RETVAL
  58. unsigned int
  59. _get_width()
  60. CODE:
  61. RETVAL = caca_get_width();
  62. OUTPUT:
  63. RETVAL
  64. unsigned int
  65. _get_height()
  66. CODE:
  67. RETVAL = caca_get_height();
  68. OUTPUT:
  69. RETVAL
  70. int
  71. _set_window_title(title)
  72. const char *title
  73. CODE:
  74. RETVAL = caca_set_window_title(title);
  75. OUTPUT:
  76. RETVAL
  77. unsigned int
  78. _get_window_width()
  79. CODE:
  80. RETVAL = caca_get_window_width();
  81. OUTPUT:
  82. RETVAL
  83. unsigned int
  84. _get_window_height()
  85. CODE:
  86. RETVAL = caca_get_window_height();
  87. OUTPUT:
  88. RETVAL
  89. void
  90. _refresh()
  91. CODE:
  92. caca_refresh();
  93. void
  94. _end()
  95. CODE:
  96. caca_end();
  97. # -==[- Event handling -]==---------------------------------------------------
  98. unsigned int
  99. _get_event(event_mask)
  100. unsigned int event_mask
  101. CODE:
  102. RETVAL = caca_get_event(event_mask);
  103. OUTPUT:
  104. RETVAL
  105. unsigned int
  106. _get_mouse_x()
  107. CODE:
  108. RETVAL = caca_get_mouse_x();
  109. OUTPUT:
  110. RETVAL
  111. unsigned int
  112. _get_mouse_y()
  113. CODE:
  114. RETVAL = caca_get_mouse_y();
  115. OUTPUT:
  116. RETVAL
  117. unsigned int
  118. _wait_event(event_mask)
  119. unsigned int event_mask
  120. CODE:
  121. RETVAL = caca_wait_event(event_mask);
  122. OUTPUT:
  123. RETVAL
  124. # -==[- Character printing -]==-----------------------------------------------
  125. void
  126. _set_color(fgcolor, bgcolor)
  127. unsigned int fgcolor;
  128. unsigned int bgcolor;
  129. CODE:
  130. caca_set_color(fgcolor, bgcolor);
  131. unsigned int
  132. _get_fg_color()
  133. CODE:
  134. RETVAL = caca_get_fg_color();
  135. OUTPUT:
  136. RETVAL
  137. unsigned int
  138. _get_bg_color()
  139. CODE:
  140. RETVAL = caca_get_bg_color();
  141. OUTPUT:
  142. RETVAL
  143. const char *
  144. _get_color_name(color)
  145. unsigned int color
  146. CODE:
  147. RETVAL = caca_get_color_name(color);
  148. OUTPUT:
  149. RETVAL
  150. void
  151. _putchar(x, y, c)
  152. int x;
  153. int y;
  154. char c;
  155. CODE:
  156. caca_putchar(x, y, c);
  157. void
  158. _putstr(x, y, s)
  159. int x;
  160. int y;
  161. const char *s;
  162. CODE:
  163. caca_putstr(x, y, s);
  164. # skip caca_printf for now.
  165. # handle va_args on perl side.
  166. void
  167. _clear()
  168. CODE:
  169. caca_clear();
  170. # -==[- Primitives drawing -]==-----------------------------------------------
  171. void
  172. _draw_line(x1, y1, x2, y2, c)
  173. int x1;
  174. int y1;
  175. int x2;
  176. int y2;
  177. char c;
  178. CODE:
  179. caca_draw_line(x1, y1, x2, y2, c);
  180. void
  181. _draw_polyline(x, y, n, c)
  182. SV *x;
  183. SV *y;
  184. int n;
  185. char c;
  186. INIT:
  187. int *xc;
  188. int *yc;
  189. int i;
  190. /* make sure x and y are perl arrayrefs */
  191. if ( (SvTYPE(SvRV(x)) != SVt_PVAV)
  192. || (SvTYPE(SvRV(y)) != SVt_PVAV) )
  193. {
  194. XSRETURN_UNDEF;
  195. }
  196. /* create a C int array out of x and y */
  197. xc = (int *) malloc((n+1) * sizeof(int *));
  198. if (!xc) {
  199. XSRETURN_UNDEF;
  200. }
  201. yc = (int *) malloc((n+1) * sizeof(int *));
  202. if (!yc) {
  203. XSRETURN_UNDEF;
  204. }
  205. for (i = 0; i <= n; i++) {
  206. SV **integer;
  207. integer = av_fetch((AV *) SvRV(x), i, 0);
  208. if (integer) {
  209. xc[i] = SvIV(*integer);
  210. } else {
  211. xc[i] = 0;
  212. }
  213. integer = av_fetch((AV *) SvRV(y), i, 0);
  214. if (integer) {
  215. yc[i] = SvIV(*integer);
  216. } else {
  217. yc[i] = 0;
  218. }
  219. }
  220. CODE:
  221. caca_draw_polyline(xc, yc, n, c);
  222. free(yc);
  223. free(xc);
  224. void
  225. _draw_thin_line(x1, y1, x2, y2)
  226. int x1;
  227. int y1;
  228. int x2;
  229. int y2;
  230. CODE:
  231. caca_draw_thin_line(x1, y1, x2, y2);
  232. void
  233. _draw_thin_polyline(x, y, n)
  234. SV *x;
  235. SV *y;
  236. int n;
  237. INIT:
  238. int *xc;
  239. int *yc;
  240. int i;
  241. /* make sure x and y are perl arrayrefs */
  242. if ( (SvTYPE(SvRV(x)) != SVt_PVAV)
  243. || (SvTYPE(SvRV(y)) != SVt_PVAV) )
  244. {
  245. XSRETURN_UNDEF;
  246. }
  247. /* create a C int array out of x and y */
  248. xc = (int *) malloc((n+1) * sizeof(int *));
  249. if (!xc) {
  250. XSRETURN_UNDEF;
  251. }
  252. yc = (int *) malloc((n+1) * sizeof(int *));
  253. if (!yc) {
  254. XSRETURN_UNDEF;
  255. }
  256. for (i = 0; i <= n; i++) {
  257. SV **integer;
  258. integer = av_fetch((AV *) SvRV(x), i, 0);
  259. if (integer) {
  260. xc[i] = SvIV(*integer);
  261. } else {
  262. xc[i] = 0;
  263. }
  264. integer = av_fetch((AV *) SvRV(y), i, 0);
  265. if (integer) {
  266. yc[i] = SvIV(*integer);
  267. } else {
  268. yc[i] = 0;
  269. }
  270. }
  271. CODE:
  272. caca_draw_thin_polyline(xc, yc, n);
  273. free(yc);
  274. free(xc);
  275. void
  276. _draw_circle(x, y, r, c)
  277. int x;
  278. int y;
  279. int r;
  280. char c;
  281. CODE:
  282. caca_draw_circle(x, y, r, c);
  283. void
  284. _draw_ellipse(x0, y0, a, b, c)
  285. int x0;
  286. int y0;
  287. int a;
  288. int b;
  289. char c;
  290. CODE:
  291. caca_draw_ellipse(x0, y0, a, b, c);
  292. void
  293. _draw_thin_ellipse(x0, y0, a, b)
  294. int x0;
  295. int y0;
  296. int a;
  297. int b;
  298. CODE:
  299. caca_draw_thin_ellipse(x0, y0, a, b);
  300. void
  301. _fill_ellipse(x0, y0, a, b, c)
  302. int x0;
  303. int y0;
  304. int a;
  305. int b;
  306. char c;
  307. CODE:
  308. caca_fill_ellipse(x0, y0, a, b, c);
  309. void
  310. _draw_box(x0, y0, x1, y1, c)
  311. int x0;
  312. int y0;
  313. int x1;
  314. int y1;
  315. char c;
  316. CODE:
  317. caca_draw_box(x0, y0, x1, y1, c);
  318. void
  319. _draw_thin_box(x0, y0, x1, y1)
  320. int x0;
  321. int y0;
  322. int x1;
  323. int y1;
  324. CODE:
  325. caca_thin_box(x0, y0, x1, y1);
  326. void
  327. _fill_box(x0, y0, x1, y1, c)
  328. int x0;
  329. int y0;
  330. int x1;
  331. int y1;
  332. char c;
  333. CODE:
  334. caca_fill_box(x0, y0, x1, y1, c);
  335. void
  336. _draw_triangle(x0, y0, x1, y1, x2, y2, c)
  337. int x0;
  338. int y0;
  339. int x1;
  340. int y1;
  341. int x2;
  342. int y2;
  343. char c;
  344. CODE:
  345. caca_draw_triangle(x0, y0, x1, y1, x2, y2, c);
  346. void
  347. _draw_thin_triangle(x0, y0, x1, y1, x2, y2)
  348. int x0;
  349. int y0;
  350. int x1;
  351. int y1;
  352. int x2;
  353. int y2;
  354. CODE:
  355. caca_draw_thin_triangle(x0, y0, x1, y1, x2, y2);
  356. void
  357. _fill_triangle(x0, y0, x1, y1, x2, y2, c)
  358. int x0;
  359. int y0;
  360. int x1;
  361. int y1;
  362. int x2;
  363. int y2;
  364. char c;
  365. CODE:
  366. caca_fill_triangle(x0, y0, x1, y1, x2, y2, c);
  367. # -==[- Mathematical functions -]==-------------------------------------------
  368. int
  369. _rand(min, max)
  370. int min;
  371. int max;
  372. CODE:
  373. RETVAL = caca_rand(min, max);
  374. OUTPUT:
  375. RETVAL
  376. unsigned int
  377. _sqrt(n)
  378. unsigned int n;
  379. CODE:
  380. RETVAL = caca_sqrt(n);
  381. OUTPUT:
  382. RETVAL
  383. # -==[- Sprite handling -]==-
  384. SV *
  385. _load_sprite(file)
  386. const char *file
  387. INIT:
  388. struct caca_sprite *c_sprite;
  389. HV *sprite;
  390. CODE:
  391. if (!file) {
  392. XSRETURN_UNDEF;
  393. }
  394. c_sprite = caca_load_sprite(file);
  395. if (!c_sprite) {
  396. XSRETURN_UNDEF;
  397. } else {
  398. sprite = (HV *) sv_2mortal((SV *) newHV());
  399. if (!sprite) {
  400. XSRETURN_UNDEF;
  401. }
  402. hv_store(sprite, "__address__", 11, newSViv((int) c_sprite), 0);
  403. RETVAL = newRV((SV *) sprite);
  404. }
  405. OUTPUT:
  406. RETVAL
  407. int
  408. _get_sprite_frames(sprite)
  409. SV *sprite
  410. INIT:
  411. struct caca_sprite *c_sprite;
  412. c_sprite = address_of(sprite);
  413. if (!c_sprite) {
  414. XSRETURN_UNDEF;
  415. }
  416. CODE:
  417. RETVAL = caca_get_sprite_frames(c_sprite);
  418. OUTPUT:
  419. RETVAL
  420. int
  421. _get_sprite_width(sprite, f)
  422. SV *sprite;
  423. int f;
  424. INIT:
  425. struct caca_sprite *c_sprite;
  426. c_sprite = address_of(sprite);
  427. if (!c_sprite) {
  428. XSRETURN_UNDEF;
  429. }
  430. CODE:
  431. RETVAL = caca_get_sprite_width(c_sprite, f);
  432. OUTPUT:
  433. RETVAL
  434. int
  435. _get_sprite_height(sprite, f)
  436. SV *sprite;
  437. int f;
  438. INIT:
  439. struct caca_sprite *c_sprite;
  440. c_sprite = address_of(sprite);
  441. if (!c_sprite) {
  442. XSRETURN_UNDEF;
  443. }
  444. CODE:
  445. RETVAL = caca_get_sprite_height(c_sprite, f);
  446. OUTPUT:
  447. RETVAL
  448. int
  449. _get_sprite_dx(sprite, f)
  450. SV *sprite;
  451. int f;
  452. INIT:
  453. struct caca_sprite *c_sprite;
  454. c_sprite = address_of(sprite);
  455. if (!c_sprite) {
  456. XSRETURN_UNDEF;
  457. }
  458. CODE:
  459. RETVAL = caca_get_sprite_dx(c_sprite, f);
  460. OUTPUT:
  461. RETVAL
  462. int
  463. _get_sprite_dy(sprite, f)
  464. SV *sprite;
  465. int f;
  466. INIT:
  467. struct caca_sprite *c_sprite;
  468. c_sprite = address_of(sprite);
  469. if (!c_sprite) {
  470. XSRETURN_UNDEF;
  471. }
  472. CODE:
  473. RETVAL = caca_get_sprite_dy(c_sprite, f);
  474. OUTPUT:
  475. RETVAL
  476. void
  477. _draw_sprite(x, y, sprite, f)
  478. int x;
  479. int y;
  480. SV *sprite;
  481. int f;
  482. INIT:
  483. struct caca_sprite *c_sprite;
  484. c_sprite = address_of(sprite);
  485. if (!c_sprite) {
  486. XSRETURN_UNDEF;
  487. }
  488. CODE:
  489. caca_draw_sprite(x, y, c_sprite, f);
  490. void
  491. _free_sprite(sprite)
  492. SV *sprite;
  493. INIT:
  494. struct caca_sprite *c_sprite;
  495. c_sprite = address_of(sprite);
  496. if (!c_sprite) {
  497. XSRETURN_UNDEF;
  498. }
  499. CODE:
  500. caca_free_sprite(c_sprite);
  501. # -==[- Bitmap handling -]==--------------------------------------------------
  502. SV *
  503. _create_bitmap(bpp, w, h, pitch, rmask, gmask, bmask, amask)
  504. unsigned int bpp;
  505. unsigned int w;
  506. unsigned int h;
  507. unsigned int pitch;
  508. unsigned int rmask;
  509. unsigned int gmask;
  510. unsigned int bmask;
  511. unsigned int amask;
  512. INIT:
  513. struct caca_bitmap *c_bitmap;
  514. HV *bitmap;
  515. CODE:
  516. c_bitmap =
  517. caca_create_bitmap(bpp, w, h, pitch, rmask, gmask, bmask, amask);
  518. if (!c_bitmap) {
  519. XSRETURN_UNDEF;
  520. } else {
  521. bitmap = (HV *) sv_2mortal((SV *) newHV());
  522. if (!bitmap) {
  523. XSRETURN_UNDEF;
  524. }
  525. hv_store(bitmap, "__address__", 11, newSViv((int) c_bitmap), 0);
  526. hv_store(bitmap, "__bpp__", 7, newSViv((int) bpp ), 0);
  527. RETVAL = newRV((SV *) bitmap);
  528. }
  529. OUTPUT:
  530. RETVAL
  531. void
  532. _set_bitmap_palette(bitmap, red, green, blue, alpha)
  533. SV *bitmap;
  534. SV *red;
  535. SV *green;
  536. SV *blue;
  537. SV *alpha;
  538. INIT:
  539. struct caca_bitmap *c_bitmap;
  540. unsigned int *c_red;
  541. unsigned int *c_green;
  542. unsigned int *c_blue;
  543. unsigned int *c_alpha;
  544. c_bitmap = address_of(bitmap);
  545. if (!c_bitmap) {
  546. XSRETURN_UNDEF;
  547. }
  548. /* TODO: perl array to c array */
  549. c_red = c_array(red);
  550. c_green = c_array(green);
  551. c_blue = c_array(blue);
  552. c_alpha = c_array(alpha);
  553. CODE:
  554. caca_set_bitmap_palette(c_bitmap, c_red, c_green, c_blue, c_alpha);
  555. void
  556. _draw_bitmap(x1, y1, x2, y2, bitmap, pixels)
  557. int x1;
  558. int y1;
  559. int x2;
  560. int y2;
  561. SV *bitmap;
  562. SV *pixels;
  563. INIT:
  564. /* TODO: implement Tie::Scalar::Pointer for pixel support */
  565. CODE:
  566. void
  567. _free_bitmap(bitmap)
  568. SV *bitmap;
  569. INIT:
  570. struct caca_bitmap *c_bitmap;
  571. c_bitmap = address_of(bitmap);
  572. if (!c_bitmap) {
  573. XSRETURN_UNDEF;
  574. }
  575. CODE:
  576. caca_free_bitmap(c_bitmap);
  577. # vim:sw=2 sts=2 expandtab