summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--ext/ruby2d/ruby2d.c10
-rw-r--r--lib/ruby2d/image.rb2
-rw-r--r--test/testcard.rb3
3 files changed, 11 insertions, 4 deletions
diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c
index 3aad03e..d8d6f35 100644
--- a/ext/ruby2d/ruby2d.c
+++ b/ext/ruby2d/ruby2d.c
@@ -174,8 +174,14 @@ 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));
+
+ // Get width and height from Ruby class. If set, use it, else choose the
+ // native dimensions of the image.
+ R_VAL w = r_iv_get(self, "@width");
+ R_VAL h = r_iv_get(self, "@height");
+ r_iv_set(self, "@width" , r_test(w) ? w : INT2NUM(img->width));
+ r_iv_set(self, "@height", r_test(h) ? h : INT2NUM(img->height));
+
r_iv_set(self, "@data", r_data_wrap_struct(image, img));
return R_NIL;
}
diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb
index 89e9b47..0faee11 100644
--- a/lib/ruby2d/image.rb
+++ b/lib/ruby2d/image.rb
@@ -19,6 +19,8 @@ module Ruby2D
@x = opts[:x] || 0
@y = opts[:y] || 0
@z = opts[:z] || 0
+ @width = opts[:width] || nil
+ @height = opts[:height] || nil
@type_id = 4
diff --git a/test/testcard.rb b/test/testcard.rb
index 099c69e..4e629bc 100644
--- a/test/testcard.rb
+++ b/test/testcard.rb
@@ -203,8 +203,7 @@ Line.new(
Image.new(x: 590, y: 180, path: "#{media}/image.png")
Image.new(x: 590, y: 290, path: "#{media}/image.jpg")
Image.new(x: 590, y: 400, path: "#{media}/image.bmp")
-img_r = Image.new(x: 400, y: 200, path: "#{media}/colors.png")
-img_r.width, img_r.height = 50, 25
+img_r = Image.new(x: 400, y: 200, width: 50, height: 25, path: "#{media}/colors.png")
img_r.color = [1.0, 0.3, 0.3, 1.0]
img_g = Image.new(x: 400, y: 225, path: "#{media}/colors.png")
img_g.width, img_g.height = 25, 25