Browse files

Add Chipmunk::Space

  • Loading branch information...
1 parent 9e17989 commit 80a53249e633f101350d6a9c54430ccd8d116f68 @jtpalmer committed Apr 22, 2012
Showing with 148 additions and 1 deletion.
  1. +2 −1 .gitignore
  2. +42 −0 lib/Chipmunk/Space.pm
  3. +33 −0 lib/Chipmunk/Space.xs
  4. +21 −0 t/space.t
  5. +50 −0 typemap
View
3 .gitignore
@@ -5,5 +5,6 @@
/META.*
/_build
/blib
-/lib/**.c
+/lib/*.c
+/lib/Chipmunk/*.c
View
42 lib/Chipmunk/Space.pm
@@ -0,0 +1,42 @@
+package Chipmunk::Space;
+use 5.008001;
+use strict;
+use warnings;
+use Alien::Chipmunk;
+
+our $VERSION = '0.000001';
+$VERSION = eval $VERSION;
+
+require XSLoader;
+XSLoader::load('Chipmunk::Space', $VERSION);
+
+1;
+
+__END__
+
+=head1 NAME
+
+Chipmunk - Perl bindings for the Chipmunk Physics library
+
+=head1 SYNOPSIS
+
+ use Chipmunk;
+
+=head1 DESCRIPTION
+
+Perl bindings for the Chipmunk Physics library.
+
+=head1 AUTHOR
+
+Jeffrey T. Palmer, E<lt>jtpalmer@cpan.org<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2012 by Jeffrey Palmer
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.14.2 or,
+at your option, any later version of Perl 5 you may have available.
+
+=cut
+
View
33 lib/Chipmunk/Space.xs
@@ -0,0 +1,33 @@
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+#include "ppport.h"
+
+#include <chipmunk.h>
+
+MODULE = Chipmunk::Space PACKAGE = Chipmunk::Space PREFIX = cps_
+PROTOTYPES: ENABLE
+
+cpSpace *
+cps_new(CLASS)
+ char *CLASS
+ CODE:
+ RETVAL = cpSpaceNew();
+ OUTPUT:
+ RETVAL
+
+void
+cps_set_gravity(space, gravity)
+ cpSpace *space
+ cpVect gravity
+ CODE:
+ cpSpaceSetGravity(space, gravity);
+
+cpVect
+cps_get_gravity(space)
+ cpSpace *space
+ CODE:
+ RETVAL = cpSpaceGetGravity(space);
+ OUTPUT:
+ RETVAL
+
View
21 t/space.t
@@ -0,0 +1,21 @@
+#!perl
+use strict;
+use warnings;
+use Test::More;
+use Chipmunk::Space;
+
+my $space = Chipmunk::Space->new();
+
+isa_ok( $space, 'Chipmunk::Space' );
+
+my $gravity = [ 1, 2 ];
+
+$space->set_gravity($gravity);
+
+use Data::Dumper;
+print Dumper($space->get_gravity);
+
+is_deeply( $space->get_gravity(), $gravity, 'get_gravity' );
+
+done_testing();
+
View
50 typemap
@@ -0,0 +1,50 @@
+TYPEMAP
+int T_IV
+float T_NV
+cpSpace * O_OBJECT
+cpVect O_OBJ_VECT
+
+INPUT
+
+O_OBJECT
+ if (sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG)) {
+ $var = ($type)SvIV((SV *)SvRV($arg));
+ } else if ($arg == 0) {
+ XSRETURN(0);
+ } else {
+ XSRETURN_UNDEF;
+ }
+
+O_OBJ_VECT
+ if (SvTYPE(SvRV($arg)) == SVt_PVAV) {
+ AV *input = (AV *)SvRV($arg);
+ if (av_len(input) != 1) {
+ printf(\"av_len = %d\\n\", av_len(input));
+ XSRETURN_UNDEF;
+ }
+ float x, y;
+ x = (float)SvNV(*av_fetch(input, 0, 0));
+ y = (float)SvNV(*av_fetch(input, 1, 0));
+ printf(\"gravity: %f %f\\n\", x, y);
+ $var = cpv(x, y);
+ } else if ($arg == 0) {
+ XSRETURN(0);
+ } else {
+ XSRETURN_UNDEF;
+ }
+
+OUTPUT
+
+O_OBJECT
+ if ($var) {
+ sv_setref_pv( $arg, CLASS, (void*)$var );
+ } else {
+ XSRETURN_UNDEF;
+ }
+
+O_OBJ_VECT
+ AV *output = newAV();
+ av_push(output, newSVnv($var.x));
+ av_push(output, newSVnv($var.y));
+ $arg = newRV_inc((SV *)output);
+

0 comments on commit 80a5324

Please sign in to comment.