OiRc
OiRc

Reputation: 1622

separate logic and GUI in java

I'm implementing a game in Java, using the classes shown below to control the game logic. I try to be very clear when explaining my question.

I thought of making a utility class singleton that implements all the draw() of each state, but it is very nasty to insert all in a single class.

Example:

instead of calling gameState.draw(graphic); I call Render.getRenderInstance().draw(graphic,gameState); this solutions works, but I don't like it.

So how can I divide the draw method from the rest of the logic in a pretty way?

Some advice? Thank you.

Upvotes: 10

Views: 1784

Answers (1)

luke1985
luke1985

Reputation: 2354

The usual approach is to divide the game into two parts:

  • the logic, which looks to be done OK in your case

  • the scene, where things are drawn

Here in pseudocode is how I do it:

the main loop:
  gamestate.update()
  scene.render();

In the scene you should have all your graphics operations. You should be able to add new graphics to the scene using a call similar to this:

scene.addObject(SomeGraphic graphic);

So the scene would render that graphic every main loop iteration.

To let this happen you should keep a list or another collection inside the Scene class and render every object every tick. So the actuall scene.render() would look like so:

public void render() {
  for(SomeGraphic graphic : objects) {
    graphic.draw()
  }
}

You will be able then to control what is on the scene from the game logic - you will be able to add and delete objects etc.

Upvotes: 3

Related Questions