Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ sprite_new(VALUE rcv, SEL sel, VALUE name)
return rb_class_wrap_new((void *)sprite, rcv);
}

/// @method #new_with_image(data, len)
/// Creates a sprite from +data+ stream buffer image data and +len+ length
/// @param data [String] image data stream buffer
/// @param len [Integer] data length
/// @return [Sprite]

static VALUE
sprite_new_with_image(VALUE rcv, SEL sel, VALUE data, VALUE len)
{
cocos2d::Sprite *sprite = NULL;
cocos2d::Texture2D *texture = new cocos2d::Texture2D();
cocos2d::Image *image = new cocos2d::Image();

image->initWithImageData((unsigned char *)RSTRING_PTR(StringValue(data)), len);
texture->initWithImage(image);
sprite = cocos2d::Sprite::createWithTexture(texture);

assert(sprite != NULL); // TODO raise exception
return rb_class_wrap_new((void *)sprite, rcv);
}

/// @group Actions

static VALUE
Expand Down Expand Up @@ -181,7 +202,7 @@ need_physics(VALUE rcv)
{
auto physics = SPRITE(rcv)->getPhysicsBody();
if (physics == NULL) {
rb_raise(rb_eRuntimeError, "receiver does not have a physics body");
rb_raise(rb_eRuntimeError, "receiver does not have a physics body");
}
return physics;
}
Expand Down Expand Up @@ -405,6 +426,7 @@ Init_Sprite(void)

rb_define_singleton_method(rb_cSprite, "load", sprite_load, 1);
rb_define_singleton_method(rb_cSprite, "new", sprite_new, 1);
rb_define_singleton_method(rb_cSprite, "new_with_image", sprite_new_with_image, 2);
rb_define_method(rb_cSprite, "move_by", sprite_move_by, 2);
rb_define_method(rb_cSprite, "move_to", sprite_move_to, 2);
rb_define_method(rb_cSprite, "blink", sprite_blink, 2);
Expand Down