PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Objects accessing eachother (OOP question)

Sorry, this is probably a pretty basic thing, but I have yet to run across it, and it seems like it's gonna be necessary to understand.

Lets assuming I have the following objects, each contained within another object:

Code:

+ GameManagerObject
+ CarManagerObject
+CarObject
+CarObject
+CarObject
+CarObject


GameManagerObject contains CarManagerObject, which contains a number of CarObjects.

So, in order to simplify things like number of surfaces and such, I'd like to use a single surface for the car graphic, though I may have 15 cars.
It seems like a good idea to ME to have that surface in CarManagerObject, and have each CarObject access that surface when it needs to draw itself to the backbuffer.

But the problem i've having is, this doesn't seem to me like the CarObject can even access the backbuffer OR the graphic surface without me passing pointer to them to it. Is there a way around this? I could imagine a LOT of stuff i'd like to be able to access, and passing pointers or whatever to EVERY method seems messy.

Any guidance is greatly appreciated!
Of course you are going to have to pass a pointer. I personally prefer to use some sort of rendering context class for the backbuffer ( basically a fancy wrapper around backbuffer) and a separate class for storing game images.
Each game object would then keep either a pointer or some sort of ID for the images associated with it and request at run time a pointer to the images it needs from the images repository class.

To access other classes (rendering context or images repository) generally you would have to either pass a pointer to your game objects class or create a global static class containing methods for obtaining pointers to various global classes and include header to it with every class definition.
There are many ways to do this of course.

I have a RasterizerObject that handles all of the drawing and keeps track of the backbuffer, etc.

Your other objects can store a pointer to the RasterizerObject and call routines in that object to draw stuff on the screen.

Pam
Pam wrote:I have a RasterizerObject that handles all of the drawing and keeps track of the backbuffer, etc.


PocketFrog also works that way. Feel free to take a look.
Thierry":2g4o1g4p]

Where do you think I got the idea :wink:

Pam
Thanks, while I can probably machette my way through it, it's helpful to know what is normal and what is messy, inefficient, or unconventional. I wanna make sure I'm following the correct path before it becomes a strong piece of my knowledge base :)

I suppose having the RasterizerObject makes sense... so I would just pass pointers to objects when instantiated of whatever I feel they will be needing to access? I suppose it does cut down on the amount of times I'll have to do such a pass.

Thanks, I think this gives me a lot to think about :)
I usually have a struct representing the System State. It contains objects and data that I want available to all components. Classes like the console and renderer, and data such as the current frame counter, length of current frame, current tick count, user input data (keystates and mouse position), etc. Then I pass a pointer to that struct to all functions.

Older games like Quake just make all that stuff global, which is bad for a number of reasons. Most importantly, some other mobile platforms do not support global variables.

Dan East