Skip to main content.
May 24th, 2005

C++ adventures into the land of method’s calling

Monday, back to work. Whole day very focused on some major code rewrite. Something I started Friday.
I have this M3D module that I use for handling 3D at the data-base level (no rendering, although I wrote some PC renderers that use M3D). Basically I use this library to handle basic geometry. Vertices with color, skinning, texture, normals. Triangles and strips. making strips, converting strips back to triangles, removing unused materials, sort the data as to remove state changes for rendering. Also some experiments on mesh complexity reduction and so on.
Basically it’s the interface that I use to handle parsed data from file formats like XSI and 3DS.
For this rewrite I wanted to try and approach C++. It’s actually an experiment in itself. For the time being, my main concern with C++ is writing style more than performance, as I think I can already handle performance well enough.
My current style is base dont he following rules, that I need to verify with time.

Let’s say I have an scene, an object and a group. For every scene there are N objects and for every object there are M groups. A group is practically a 3D mesh. So, an object is a collection of meshes (plus other things that I’ll leave out for now).
In M3D, I have m3d_object_t, m3d_scene_t and so on. Functions have prefixes that indicate their scope. They are basically like C++ methods. So, functions that operate on m3d_object_t structure will all start with m3d_object_. For example: m3d_object_scale( m3d_object_t *op ). For the scene then it’s m3d_scene_scale( m3d_scene_t *sp ).
And here start the troubles with C++. m3d_object_scale( op ) would become op->scale(), m3d_scene_scale( sp ) becomes sp->scale( sp ). And so on for groups and all the rest.
Now, while this is neat, at the practical level, it turns a nightmare when I need to find out exactly where in any piece of code I scale the scene or I scale an object. This is especially important for libraries such as this, that are used by a number of different programs.
My current solution is to use a shorter prefix. So, for the class m4d_scene_c I have sp->m4s_scale(). m4d is the library name, m4s stands for an m4d Scene.
This allows me to keep track of where I scale a scene, an object or a group.

Then it comes the issue of the private methods and additional modules. For private methods, it’s probably OK to lose the prefix. They aren’t supposed to be used externally anyway. I’m still not sure though.
In any case, C++ forces to have a lot of private methods, some have a specific domain and some sort of prefix is still probably useful.
Let’s say that I have a separate CPP file for welding close vertices of a group. In the file I will put the m4g_weldverts() method, but also some private functions. In C i would declare those static and forget about them. In C++ I need to declare them into the class, so it can get a bit more confusing.
The solution for now is to use some sort of prefix. In this case, a function that is strictly used by the m4d_weldverts() would be called, for example, weldverts_flag_weldable_verts(). Basically with the weldverts_ prefix.

Still, I sort of hate the idea of having to declare the private methods, but we’ll see how it goes.

It’s almost 3AM.. zzzzzzzzzzzzz

Posted by Davide Pasca in Uncategorized

This entry was posted on Tuesday, May 24th, 2005 at 2:56 am and is filed under Uncategorized. You can follow any responses to this entry through the comments RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Responses to “C++ adventures into the land of method’s calling”

  1. rince says:

    Question you need to ask yourself is what c++ functionality do you need to justify dynamic objects like that.

    Are you going to build object hierarchies with virtual methods? etc.. etc… ?

    If not and you just want to give your code a c++ look then why not have something like

    class scene_m3d
    {
    public:
    static scale( scene_m3d *pScene );
    .
    .
    .

    };

    and then in your code

    scene_m3d::scale( pScene );

  2. ragin' lion says:

    I must be seeing things … Kaz & C++ code? Oh dear! 8P

    Rince I see that your favorite “static” method comes to the rescue again!

    I agree with Rince Mr. Kaz, how you use the C++ features should depend on the situation/context. Of course, based on Rince’s example, one could ask what’s the point of making those things into a class?

  3. Davide Pasca says:

    Avoid the this seems a bit drastic for one that is trying to make a compromise 8)
    Recently in C, I’ve been calling the main object as T. Something like:
    m3d_scene_load( m3d_scene_t *T )
    ..seems pretty clean, and not confusing if considering that the main object is really just one.
    In C++ instead I put an underscore in front of the class members.
    In C, I use the underscore to classify globals, so it sort of makes sense (class members are global to the method in which they are used).
    An underscore in front is nice because doesn’t add clutter.
    Some may say that underscore in front may reflect some hidden global variable for the OS.. yeah whatever !

  4. rince says:

    You still havent answered my question, mf!!!

    What features of c++ are you going to use?

    So far I didn’t see anything in your post that justified using c++.

    If you are just going to rewrite c code into c++ then static functions are the closest you can get.

  5. Davide Pasca says:

    I’m not sure what I’m trying to do, because it’s an experiment 8)
    First of all, I wanted to see if I could use the class->method() notation in a way that is not so confusing to me.

    One more thing I noticed is that I’m not so sure if I want to write a function for every value I want to get and set. It’s kind of nice to directly access to arrays for example. In the end they are pretty simple structures, no need to wrap them with methods. That also works best at debug time.

  6. rince says:

    Yeah the get and set is another issue.

    If I have that then I make the variable public otherwise you end up with pages and pages of ‘get’ and ’set’ functions in your class declaration.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>