Navigation Menu

Skip to content

Commit

Permalink
properly clean up @post_step_blocks after step
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn42 committed Apr 23, 2013
1 parent c078d93 commit d4236ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ext/chipmunk/rb_cpSpace.c
Expand Up @@ -314,6 +314,10 @@ rb_cpSpaceAddPostStepCallback(int argc, VALUE *argv, VALUE self) {
VALUE blocks = rb_iv_get(self, "@post_step_blocks");

rb_scan_args(argc, argv, "10&", &obj, &block);
if(!RTEST(block)) {
rb_raise(rb_eArgError, "Cannot omit the block.");
}

rb_hash_aset(blocks, obj, block);

cpSpaceAddPostStepCallback(SPACE(self),
Expand Down Expand Up @@ -539,6 +543,7 @@ rb_cpSpaceShapeQuery(int argc, VALUE *argv, VALUE self) {
static VALUE
rb_cpSpaceStep(VALUE self, VALUE dt) {
cpSpaceStep(SPACE(self), NUM2DBL(dt));
rb_iv_set(self, "@post_step_blocks", rb_hash_new());
return Qnil;
}

Expand Down
18 changes: 18 additions & 0 deletions spec/space_spec.rb
Expand Up @@ -128,6 +128,23 @@
end


it 'can handle junk object for callback' do
space = CP::Space.new
bod = CP::Body.new 90, 76
shapy = CP::Shape::Circle.new bod, 40, CP::ZERO_VEC_2
shapy.collision_type = :foo

bod_one = CP::Body.new 90, 76
shapy_one = CP::Shape::Circle.new bod_one, 40, CP::ZERO_VEC_2
shapy_one.collision_type = :bar
space.add_shape shapy
space.add_shape shapy_one

space.add_collision_func :foo, :bar, 1
# just asserting that junk doesn't get registered
space.step 1
end

it 'can have old style callbacks' do
space = CP::Space.new
bod = CP::Body.new 90, 76
Expand Down Expand Up @@ -179,6 +196,7 @@
space.step 1

called.should be_true
space.instance_variable_get("@post_step_blocks").should be_empty
end

it 'can register for post step callbacks' do
Expand Down

0 comments on commit d4236ec

Please sign in to comment.