Sign in to follow this  

Small Design Question

This topic is 3044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing an Escape Velocity clone, and I had a question regarding when/what calls the actual code to display a game object. Should I parse through all of the game objects and call a render() and make my camera a global variable so the game object can compare itself to the camera's box or should I pass a pointer to each of those objects to the camera so it can make the decision if the object should be rendered or not? Or should some other class be doing this? Thanks

Share this post


Link to post
Share on other sites
As a general rule, each object should have its own draw() function. This gives you an ideal place to put special drawing logic (e.g. faking lighting effects, or engine glows, or "ships" that are aggregated of several loosely-connected components, etc.). So you will be iterating over objects and calling their draw() functions. They will of course need to know where the camera is to know where on the screen they should be drawn.

For purposes of determining if an object should be drawn at all, I would suggest handling that in the data structure that has your objects in it. For example, if your data structure is a list, then your code would look something like this:
screenBoundingBox = camera.getBoundingBox()
for object in listOfGameObjects:
if object.getBoundingBox().intersects(screenBoundingBox):
object.draw(camera)

Share this post


Link to post
Share on other sites
I partially agree with the above poster, as long as the mentioned logic is within an object who's only task is rendering, ie. the renderer.

The renderer should be somewhat of a container that you register all your renderable objects with, and then has some function Render() which draws all the objects that are currently registered. Internally the renderer will use logic very similar to the above poster. However it has no idea what the objects are or how they're used, only that they need to be rendered (via their renderable interface.)

The renderer will contain the active camera and pass it to each object as illustrated above. It will also manage all the materials and shaders and vertex buffers and all that.



Renderer renderer;
RenderableScene world, screen;
Camera camera;

SpaceShip ship1, ship2;
OtherObject obj;
MenuRenderable rMenu;
bool showMenu;

void Initialize()
{
camera.SetDefaultValues();

world.AddRenderable(ship1);
world.AddRenderable(ship2);
world.AddRenderable(obj);
world.SetCamera(camera.SceneView);

screen.AddRenderable(rMenu);
screen.SetCamera(camera.ScreenView);

renderer.AddScene(world);
renderer.AddScene(screen);
}

void GameLoop()
{
UpdateGameLogic();

//hide or show menu
screen.SetVisible(showMenu);

renderer.Render();
}

Share this post


Link to post
Share on other sites

This topic is 3044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this