Share this post

Link to post

Share on other sites

Yes, the compiler internally handles it like a pointer, but a reference is more efficient (no checks for NULL when casting for example).

The global Screen object has the advantage that the reference can be resolved at compile time resulting in code without indirection.The disadvantage is some kind of inflexibility, e.g. if you plan to have more than one screen.

Share this post

Link to post

Share on other sites

a singleton-like setup might work for you. What I mean by that is, don't make your screen a singleton class, because it may make a lot of sense down the line to be able to have multiple screens at the same time and all of that kinda stuff ... but create another class, something like ScreenManager or something, that simply provides the "global access" part of the singleton puzzle.

So you have a Screen class which supports whatever functions a screen supports.

And you have a World class, which supports managing all the things a world manages. And also has a function to draw the world onto a screen.

Then, for now, you simply a a very small ScreenManager class, which (in the first version) simple provides 2 functions: GetCurrentScreen(), and SetCurrentScreen() - and internally stores a pointer to a screen object.

Then your main creates the screen at app start time, sets it as the current screen, and deletes it at app cleanup time.

Then your World class simply uses GetCurrentScreen() whenever it needs a screen.

...

Later, when you want to have multiple different views of the same world, or multiple worlds or screens, you refactor a little bit of your code, for instance you can make drawWorld take the Screen it draws on as a parameter ... then your main or game can simple pass the current screen when it calls drawWorld. Another option would be to have a WorldView class, which has the logic for drawing a view on the screen, and has a pointer to a screen, and a pointer to a world ... then when you call Draw() on the WorldView object, it calls view.drawWorld(screen); or something like that ...

0

Share this post

Link to post

Share on other sites

Whoa, that was an intense description. But I think I understood it. I think I'm still gonna go with the reference object however. Because, I am only making a smalltime programming-practice game. But thanks anyway.