Additional Coding Stuff

Started by Blizzard, March 23, 2011, 03:13:12 pm

Previous topic - Next topic

Blizzard

March 23, 2011, 03:13:12 pm Last Edit: March 27, 2011, 08:03:43 am by Blizzard
There are a couple of things that I want to mention quickly because they will be useful to you when you code.

1. VALUE.

Ruby objects are actually allocated all over the memory and I assume that VALUE is just simply the memory address of the Ruby object. It is a typdef'ed unsigned long after all. In any case, refer to VALUE always as "Ruby object".



2. Ruby in C

Since Ruby was written in C and C is not object oriented. Because of this our Ruby interfaced objects are actually never created in the conventional way.  They are always created manually and constructors and destructors are never called. Keep in mind that we will remove all constructors and destructors at one point. Also, all our objects must not have value type variables. All of them have to be pointers. And all objects have to be created using the rb_new functions (e.g. you have to return a pixel Color, then you have to used Color::rb_new to create that Color Ruby object before you return it).



3. ClassName::wrap()

This method will return the calling object's corresponding Ruby object.



4. RB_VAR2CPP(VALUE_NAME, CLASS_NAME, VARIABLE_NAME)

This macro allows you to quickly turn a Ruby object into a C++ object. It is basically calling Data_Get_Struct.

RB_VAR2CPP(rb_color, Color, color); // rb_color is of type VALUE
color->red; // color is of type Color*, it is the C++ version of rb_color




5. RB_SELF2CPP(CLASS_NAME, VARIABLE_NAME)

It's the same as RB_VAR2CPP, except that it automatically converts "self" instead of a given Ruby object. It just makes coding a bit shorter and easier to understand.



6. ClassName::createRubyInterface()

You most probably already know this method. It contains the definitions for Ruby how to work with this class.



7. ClassName::rb_inspect()

We don't really need a custom inspect method except for a few basic classes such as Color, Rect, etc. Those have a custom representation in RGSS as well.



8. ClassName::gc_mark()

Represents that mark function to call for every object during the GC mark-phase. More info can be found here



9. ClassName::gc_free()

Represents that free function to call for every object during the GC collect-phase. More info can be found here



10. return self / Qnil

Be careful when you return self and when you return Qnil. Returning self is usually very rare, most ruby methods should return Qnil.



11. RB_GENERATE_SETTER(TYPE1, NAME1, TYPE2, NAME2)

This macro allows you to quickly generate the code for a setter when it's an object. Basically you can look up the generation code in CodeSnippets.h.
This will allow you to literally write one line what would have been 7 because of this under 5. The point is that it's repeating code so I have made a macro that does that for you. Here is an example how it is used.

    VALUE Sprite::rb_setBitmap(VALUE self, VALUE value)
   {
       RB_GENERATE_SETTER(Sprite, sprite, Bitmap, bitmap);
       return value;
   }


This will work with a "Sprite* sprite" and "Bitmap* bitmap", while using value and setting sprite->bitmap and sprite->rb_bitmap. Notice how Sprite, sprite, Bitmap and bitmap are the parameters for the macro.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.