0
\$\begingroup\$

I'm making a 2D shooter game. As of now, every entity "draws itself", meaning every renderable entity has a draw function. I know it is better if rendering is handled separately, but how can I achieve this? What should I put into the Renderer class?

\$\endgroup\$
  • \$\begingroup\$ I think you are referring to mode view controller. Google mvc \$\endgroup\$ – Evorlor Jan 24 '15 at 4:43
1
\$\begingroup\$

You only truly need a separate class if you're multithreading, so you can have separate constructors, one ran in the render thread. Even then its up to you whether or not you want to do that. The big reason is just organization.

More important than anything is to make sure your textures and render programs are static.
You also want to render all of a single type of renderer at the same time to avoid render state changes, possibly even rendering everything of the same type in the same draw pass.

\$\endgroup\$
0
\$\begingroup\$

It is not necessarily a better way to have a separate class to draw your entities. It is just one way how to implement how your entities get drawn.

But to answer your question. One way could be that each of your entities give access to the the data which are necessary to draw a entity. As soon as your entity provides such function then your rendering class just need to hold a reference to every available entity( e.g. In a vector) which should be drawn .

And then in every update cycle the rendering class will go through the vector access each entity and draw the entity in the same way the entities draw themselves now.

\$\endgroup\$
0
\$\begingroup\$

What I have found is that is good to make a handler class that has a list of GameObjects and draws, ticks(update) and can add new objects.

ex:

import java.awt.Graphics;
import java.util.LinkedList;


public class Handler {

  public LinkedList<GameObject> objects = new LinkedList<GameObject>();
  private GameObject temp;

  public void tick(){
    for(int c = 0; c < objects.size(); c++){
      temp = objects.get( c );
      temp.tick( objects );
    }
  }
  public void render(Graphics g){
    for(int c = 0; c < objects.size(); c++){
      temp = objects.get( c );
      temp.render( g );
    }
  }
  public void addObject(GameObject object){
    objects.add( object );
  }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.