Best Method For Rendering?

I’m trying to figure out the best method to rendering data on screen… and ideally NOT by just using drawImage.

Does anyone have a particular way of doing it that is scalable, reliable and has good performance?

I’m not only referring to the act of shifting the data, I’m also referring to the whole architecture. What calls what, in what order, and how are the Objects organized?

dont use Java2D.

Here’s what I do for Java2D

At the beginning of my main render method, the Graphics object is sent to my Screen class.
After this, the game is rendered, which also renders all of the entities and tiles and things. Here’s what a typical render method looks like for an entity:


public void render() {
     Screen.draw(x, y, 0, false, false);
}

Where the parameters are x, y, sprite, xFlip and yFlip.
My Screen.draw method:


	public static void draw(int x, int y, int sprite, boolean flipX, boolean flipY) {
		BufferedImage img = sheet.sprites[sprite];
		
		if (flipX) {
			AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
			tx.translate(-img.getWidth(), 0);
			AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
			img = op.filter(img, null);
		}
		if (flipY) {
			AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
			tx.translate(0, -img.getHeight());
			AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
			img = op.filter(img, null);
		}
		
		draw2(img, x, y, img.getWidth(), img.getHeight());
	}
	
	public static void draw2(BufferedImage img, int x, int y, int width, int height) {
		g.drawImage(img, x, y, width, height, null);
	}

At the end of the rendering cycle, my main render method gets the Graphics object back from the Screen class.

This prevents having to pass around the Graphics object to all of my classes that will be rendering, and it lets me condense my rendering logic.

Hope this helps somehow.
| Nathan

Static draw methods?!? O____O :o

Oh God… I can already tell I’ve been doing something wrong.

Lay it on me… what’s wrong with using static draw methods?

| Nathan

Well if you want best performance use something other then Java2D like the other post said.

But if you are still using it, there is not a whole lot faster then just drawImage from what I understand.

One key thing to be careful of is to not create AffineTransforms or AlphaComposites every frame. By having a static composite or transform for that class I saw a huge boost in performance.


private static AlphaComposite alphaComp;

alphaComp = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, (fade));
		g2d.setComposite(alphaComp);
[/code

This is what I do. I am prob doing something wrong here but it works so take it or leave it. 

Why make it static? I’m pretty sure you have an instance of the Screen class at all times :slight_smile:

No, I don’t. At the beginning a call


new Font();

and Font extends Screen.

I am switching stuff around now so that this isn’t static, I guess. Still waiting for a reply from ra4king :slight_smile:

| Nathan

Well design-wise…ever heard of OOP? :slight_smile:

Java2D is more than fine.

With a static method, you need to be more aware of “when” you call it. The Graphics object could be null or disposed of.
Passing in the graphic object instead will give you more control over it.

Nothing. It’s fine. Can be slightly cosmetically ugly.