Swappable renderer without C++ 'friend'

If this was C++, i’d be happily using the ‘friend’ keyword, as is i’m stuck as to how to do this…

So theres a ‘World’ class which holds the game state, character position etc. etc. and has all the game logic in it. Along with this we have a GLRenderer and a J2DRenderer, each rendering the world in its own way. Thus any generic info goes in World, anything drawing related (textures, sprites etc.) goes in the appropriate renderer.

Problem is the actual rendering code - each renderer obviously needs access to the World data, and not in an abstracted way, but in a raw data sense. This means that the two become tightly coupled, but this is unavoidable. But without a friend, the only solution is to make the World data public (ick, ugly and dangerous), add a whole bunch of getters (not much of an improvement really) or…?

Anyone any ideas?

…put it in the same package and make it package public only? Not that nice too, but better than completely public…at least that’s the way i’m doing it in these cases.

Sorry, I should of mentioned that I had thought of this, but the classes already reside in much more logical packages. Plus what happens when you get friend cases overlapping?

Unfortunatly this looks like being the best solution though.

Perhaps create managers for all your shared data? That is, make a TextureManager, EntityManager, etc. These managers are then singletons and can be accessed from anywhere. Then you either don’t need the single world class or it can be a wrapper to make population of the managers easier.

Just a thought.

You’re maybe thinking about it the wrong way. A world should be able to render itself:


class World {
public void render(WorldRenderer renderer) {...}
}

The WorldRenderer interface will provide the raw interface to rendering operations - “draw this set of sprites”, etc.

The implementations of WorldRenderer will take care of translating to the appropriate underlying API.

Cas :slight_smile: