Nobuyoshi Nakada
6/11/2007 3:11:00 AM
Hi,
At Mon, 11 Jun 2007 04:28:33 +0900,
trebor777 wrote in [ruby-talk:255046]:
> Arf^^... nobody answered, but it's ok, I've solved my problem...
We had a development camp and RubyKaigi2007 last week.
> It's just because, i didnt' defined the initialize method as a singleton
> method (other tutorials never told me to..., and it has always worked so...)
> Anyway, it works now! Raaaah it was so simple xD
The initialize method must be an instance method. Rather
you'll need the allocator function. You should not override
Class.new unless you have special reasons.
static VALUE
bitmap_s_alloc(VALUE klass)
{
return Data_Wrap_Struct(klass, 0, SDL_FreeSurface, 0);
}
static VALUE
VALUE bitmap_init(int argc, VALUE *argv, VALUE self)
{
SDL_Surface *new_bitmap;
switch (argc) {
case 1:
new_bitmap = IMG_Load(StringValueCStr(argv[0]));
break;
case 2: {
unsigned int x = NUM2UINT(argv[0]), y = NUM2UINT(argv[1]);
new_bitmap = SDL_CreateRGBSurface(SDL_HWSURFACE, x, y, 32, 0, 0, 0, 0);
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)",
argc);
}
DATA_PTR(self) = new_bitmap;
return self;
}
void
Init_bitmap(void)
{
VALUE cBitmap = rb_define_class("Bitmap", rb_cData);
rb_define_alloc_func(cBitmap, bitmap_s_alloc);
rb_define_method(cBitmap, "initialize", bitmap_init, -1);
}
> > I got an argument type error on the class methods...(a.width for example):
> > "wrong argument type Bitmap <expected Data>"
The default allocator function would be used.
--
Nobu Nakada