From 3cd7a36b88ae7cc4441417cf44eb1c44ea131f81 Mon Sep 17 00:00:00 2001 From: Tom Black Date: Wed, 26 Apr 2017 17:01:47 -0400 Subject: 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. --- ext/ruby2d/ruby2d-opal.rb | 9 ++++++++- ext/ruby2d/ruby2d.c | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'ext') 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; } -- cgit v1.2.3