Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm not sure whether this question should be posted here. I'm trying to make a basic generic game engine in Java. Here's what I have so far.

public abstract class Device {
    public abstract void startDevice();
    public abstract void closeDevice();
    public abstract int getWidth();
    public abstract int getHeight();
    public abstract void displayImage(Image im);
}
public class Game {
    public final Device device;
    public synchronized void gameStart() { ... }
}
public abstract class GameEntity implements Serializable {
    public abstract void create(Game g);
    public abstract void updateAndDraw(Game g, Graphics2D g2d);
    public abstract void destroy(Game g);
}

So far, I have implemented two concrete classes for Device, SwingDevice and J2meDevice, and it's working fine. Now the problem is I want to implement AndroidDevice. Everything will work just fine except for that I have enforced using java.awt.Image and java.awt.Graphics2D to paint Game's graphics offline. Then I forward this Image to the device for rendering.

Is there any way I can absolutely decouple the Device's graphics from the rest of the engine?

I have two solutions but I doubt they'll fully achieve what I need:

  • Stick with the java.awt.Image. Devices that support OpenGL will not fully utilize their hardware capabilities as drawing is done on CPU. And it might not work on platforms on which can't import java.awt (I doubt their existence).
  • Create my Graphics2D-like interface. This will make defining new devices harder and again it might hide some of the device's features.
share|improve this question
add comment

1 Answer 1

Decouple your game domain from the renderer. Then you can implement specific rendering functionality together with your specific device. The Visitor pattern is an excellent candidate for this.

class GameEntity {
      public abstract void create(Game g);
      public abstract void update(Game g);
      public void visit(Visitor v) { v.visit(this); }
}

class AndroidRenderer implements Visitor {
      public abstract void visit(GameEntity g) {
          String spriteKey = g.getSprite();
          AndroidAsset sprite = AndroidSpriteStore.getSprite(spriteKey);
          androidCanvas.drawImage(g.getX(), g.getY(), sprite);
      }
}

class AWTRenderer implements Visitor {
      public abstract void visit(GameEntity g) {
          String spriteKey = g.getSprite();
          Image sprite = AWTSpriteStore.getSprite(spriteKey);
          graphics.drawImage(g.getX(), g.getY(), sprite);
      }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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