source: libcaca/trunk/perl/lib/Term/Caca.xs @ 379

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