summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2017-04-26 17:01:47 -0400
committerTom Black <[email protected]>2017-04-28 18:25:06 -0400
commit3cd7a36b88ae7cc4441417cf44eb1c44ea131f81 (patch)
tree16a7aaca93166342ae1ca05218d2a29b5fadd023
parent7bf37309d9c948df6947caa7d2b56d448a48e7cb (diff)
downloadruby2d-3cd7a36b88ae7cc4441417cf44eb1c44ea131f81.tar.gz
ruby2d-3cd7a36b88ae7cc4441417cf44eb1c44ea131f81.zip
Store image width and height on creation
Since images are loaded asynchronously on the web and the width and height might have already been set by the user, set them only if they are nil.
-rw-r--r--ext/ruby2d/ruby2d-opal.rb9
-rw-r--r--ext/ruby2d/ruby2d.c2
2 files changed, 10 insertions, 1 deletions
diff --git a/ext/ruby2d/ruby2d-opal.rb b/ext/ruby2d/ruby2d-opal.rb
index d4bea68..666a354 100644
--- a/ext/ruby2d/ruby2d-opal.rb
+++ b/ext/ruby2d/ruby2d-opal.rb
@@ -176,7 +176,14 @@ function render() {
module Ruby2D
class Image
def init(path)
- `#{self}.data = S2D.CreateImage(path);`
+ `#{self}.data = S2D.CreateImage(path, function() {
+ if (#{@width} == Opal.nil) {
+ #{@width} = #{self}.data.width;
+ }
+ if (#{@height} == Opal.nil) {
+ #{@height} = #{self}.data.height;
+ }
+ });`
end
end
diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c
index d91e0dc..ae39fbb 100644
--- a/ext/ruby2d/ruby2d.c
+++ b/ext/ruby2d/ruby2d.c
@@ -173,6 +173,8 @@ static R_VAL ruby2d_image_init(R_VAL self, R_VAL path) {
sprintf(S2D_msg, "Init image: %s", RSTRING_PTR(path));
S2D_Log(S2D_msg, S2D_INFO);
S2D_Image *img = S2D_CreateImage(RSTRING_PTR(path));
+ r_iv_set(self, "@width", INT2NUM(img->width));
+ r_iv_set(self, "@height", INT2NUM(img->height));
r_iv_set(self, "@data", r_data_wrap_struct(image, img));
return R_NIL;
}