ext/rays/image.cpp in rays-0.1.3 vs ext/rays/image.cpp in rays-0.1.4

- old
+ new

@@ -10,16 +10,17 @@ using namespace Rucy; using Rays::coord; +static Class cImage; + + namespace Rays { - static Class cImage; - Class image_class () { return cImage; } @@ -31,56 +32,64 @@ namespace Rucy { Value - value (const Rays::Image& image) + value (const Rays::Image& obj) { - return new_type<Rays::Image>( - Rays::image_class(), new Rays::Image(image)); + return new_type(cImage, new Rays::Image(obj)); } + Value + value (const Rays::Image* obj) + { + return obj ? value(*obj) : nil(); + } + }// Rucy -#define this to<Rays::Image*>(self) +#define THIS to<Rays::Image*>(self) -#define CHECK CHECK_OBJECT(self, Rays::Image, Rays::image_class()) +#define CHECK RUCY_CHECK_OBJECT(self, Rays::Image, cImage) static RUBY_DEF_ALLOC(alloc, klass) { - return new_type<Rays::Image>(klass, new Rays::Image); + return new_type<Rays::Image>(klass); } RUBY_END static RUBY_DEFN(initialize) { - CHECK_OBJ(self, Rays::Image, Rays::image_class()); - if (argc != 0 && argc != 1 && argc != 2 && argc != 3) - arg_count_error("Image#initialize", argc, 0, 1, 2, 3); + RUCY_CHECK_OBJ(self, Rays::Image, cImage); + if (argc < 1 || 3 < argc) + arg_count_error("Image#initialize", argc, 1, 2, 3); + if (argc == 0) return self; - if (argv[1].is_kind_of(Rays::bitmap_class())) + if (argv[0].is_kind_of(Rays::bitmap_class())) { - if (argc != 1 && argc != 2) - arg_count_error("Image#initialize", argc, 0, 1, 2, 3); + if (argc < 1 || 2 < argc) + arg_count_error("Image#initialize", argc, 1, 2); - const Rays::Bitmap& bitmap = *to<Rays::Bitmap*>(argv[1]); - bool alphaonly = (argc == 2) ? to<bool>(argv[2]) : false; - *this = Rays::Image(bitmap, alphaonly); + const Rays::Bitmap* bitmap = to<Rays::Bitmap*>(argv[0]); + if (!bitmap) argument_error(); + + bool alphaonly = (argc == 2) ? to<bool>(argv[1]) : false; + *THIS = Rays::Image(*bitmap, alphaonly); } else { int width = to<int>(argv[0]); int height = to<int>(argv[1]); uint colorspace = (argc == 3) ? to<uint>(argv[2]) : (uint) Rays::RGBA; - *this = Rays::Image(width, height, (Rays::ColorSpaceType) colorspace); + *THIS = Rays::Image(width, height, (Rays::ColorSpaceType) colorspace); } return self; } RUBY_END @@ -88,47 +97,47 @@ static RUBY_DEF0(width) { CHECK; - return value(this->width()); + return value(THIS->width()); } RUBY_END static RUBY_DEF0(height) { CHECK; - return value(this->height()); + return value(THIS->height()); } RUBY_END static RUBY_DEF0(color_space) { CHECK; - return value(this->color_space().type()); + return value(THIS->color_space().type()); } RUBY_END static RUBY_DEF0(bitmap) { CHECK; - return value(this->bitmap()); + return value(THIS->bitmap()); } RUBY_END static RUBY_DEF0(texture) { CHECK; - return value(this->texture()); + return value(THIS->texture()); } RUBY_END static @@ -140,11 +149,11 @@ bool alphaonly = (argc == 2) ? to<bool>(argv[1]) : false; Rays::Image img; if (!Rays::load_image(&img, path.c_str(), alphaonly)) { - error( + rays_error( "Image.load('%s', %s) failed.", path.c_str(), alphaonly ? "true" : "false"); } return value(img); @@ -153,19 +162,17 @@ void Init_image () { - Module m = define_module("Rays"); + Module mRays = define_module("Rays"); - Class c = m.define_class("Image"); - Rays::cImage = c; - - c.define_alloc_func(alloc); - c.define_method("initialize", initialize); - c.define_method("width", width); - c.define_method("height", height); - c.define_method("color_space", color_space); - c.define_method("bitmap", bitmap); - c.define_method("texture", texture); - c.define_function("load", load); + cImage = mRays.define_class("Image"); + cImage.define_alloc_func(alloc); + cImage.define_private_method("initialize", initialize); + cImage.define_method("width", width); + cImage.define_method("height", height); + cImage.define_method("color_space", color_space); + cImage.define_method("bitmap", bitmap); + cImage.define_method("texture", texture); + cImage.define_function("load", load); }