A89: Re: Re: Re: Scroller Engine


[Prev][Next][Index][Thread]

A89: Re: Re: Re: Scroller Engine




Hi!

| I generally program C++, so I felt I would apply the idea to standard C to
| help in organization.

Of course.  I wasn't saying that OOP techniques are bad, even on
calculators.  It's just that OOP programming depends on some specific
details such as virtual methods; if those fall away, it's sometimes hard to
still do what you want.

| Lemme think about that one, have to see what your talking about :)

To be more concrete:

typedef struct {
    unsigned long typemask;
    int x, y;
    int width, height;
    // add and modify whatever you want here
} TVisualObject;
typedef TVisualObject *PVisualObject;

typedef struct {
    TVisualObject base;
    int deltax, deltay;
} TFlyingObject;
typedef TFlyingObject *PFlyingObject;

I hope I got the syntax right; C sometimes really confuses me.  In this
case, you can cast a value of type PFlyingObject (a pointer to a flying
object) to PVisualObject (any visual object) without any trouble (I hope).
This works because the field "base" comes first in the structure, meaning
that it also comes first in memory, and a pointer to the "base" field of a
"TFlyingObject" is actually the same as a pointer to a "TFlyingObject"
itself.

I just made up that mask thing; I thought about the following: If an object
is a "TFlyingObject", the first 4 bits could be set to 1; if it is, for
example, a "TStaticObject" (which isn't shown here), these 4 bits could have
a value of 2, and so on.  That way you could have at most 16 parallel
'classes', and a hierarchy depth of at most 8 (4 bytes in a long, 2 values
in a byte).  Maybe this could be improved; there's plenty of room for that.
The basic concept is just that you can tell from that mask if an object is a
specific type or any of its subtypes.  Maybe this isn't even needed at all.
I'm just applying the RTTI concept. :-)

| > For the database, I would have a pointer to a variable-sized array.
|
| Like char *objectdatabase[]; ?

Actually, more like the following:

typedef TVisualObject TVisualObjects[];
typedef TVisualObjects *PVisualObjects;

typedef struct {
    int count;
    PVisualObjects objects;
} TObjectDataBase;

(I don't know if there has to be a '0' between the brackets; the difference
is documented somewhere.)

Then you could resize the "objects" field of your database depending on how
many objects there actually are.

| never used delphi, so I'm kinda vague on what your talking about :)

I just meant that reallocating memory supposedly takes quite a bit of time.
That means that the number of times that memory has to be reallocated should
be reduced.  For example, the database could reserve space for 8 pointers
even if there is only 1 in the list.

| I was just thinking of having the function run, then having it wait, then
| again. If i just have the function recurse itself over and over, then I
| would assume you would see speed differences... I'll check into that one.

Maybe.

| where did that come >from? :)

>From the >from in your previous paragraph (double alliteration!!!), in the
first email.

Bye,
Sebastian Reichelt




References: