Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sprite_new_with_image to create a Sprite from raw image data #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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