Java 2d game design

badkarma1399

Senior member
Feb 21, 2007
688
2
0
So I've been working on 2D platformer engine using java. Everything was going fine until I realized that my game engine is a total resource hog. Basically, I've narrowed down the issue to when the game is creating its buffered image. Whenever i try to copy a sprite to the image, my game gets incredible slow down, up to 50% cpu usage on a C2D E6600.

link to java program: http://www.filefactory.com/dlf...92d309841/j/7333146811
its an executable jar, but i think you need java 1.6 installed to run it.

Also, a code snippet. Basically, this object acts as the games buffered image. Sprites are drawn to it via the addGraphic method. The sprite is then drawn to the image, theGameImage. At the end of the game loop, the objects getImage method is called. another class retrieves the image and displays it on screen.

public class gameImage
{


private Image theGameImage;


public gameImage() throws IOException
{
theGameImage = ImageIO.read(getClass().getResourceAsStream("/grass.png"));
}

public void addGraphic(Image img, Point p)
{
theGameImage.getGraphics().drawImage(img, (int)p.getX(), (int)p.getY(), null);
}

public void addText(String str, Point p)
{
theGameImage.getGraphics().drawString(str, (int) p.getX(), (int) p.getY());
}

public void clearImage() {}

public Image getImage()
{
return theGameImage;
}

}

the line: theGameImage.getGraphics().drawImage(img, (int)p.getX(), (int)p.getY(), null);
seems to be the cause of most of the slowdown. Whenever i comment it out, the game speeds up greatly.

The game itself is stand alone, and it displays through a window i created myself using in part JFrame.

So my question...any one have any ideas on how to speed the gfx rendering up?
 

exdeath

Lifer
Jan 29, 2004
13,679
10
81
What resolution is your frame buffer (your 'image')? I hope you're not trying to do 1600x1200 32 bit software graphics, especially in Java.

And when you copy your sprite to the frame buffer are you looping pixels or using some kind of high speed block transfer? And is it 1:1 scaling or is it sampling? Are all the images the same internal pixel format? Format conversion and sampling is bad enough in native machine code, 10 times worse inside an emulator.

And Java is about the worst language you can use for graphics due to it's emulated sandbox nature.

I have a feeling its the conversion, probably blitting a 256 color gif (common sprite format) onto a 32 bit (or worse unaligned unpadded 24 bit) background image and it's having to covert every pixel one at a time instead of using block transfers. Even worse would be blitting a RGB image to a 8 bit index image because it will have to create a palette and palette match for the source image each and every time, and that involves sqrts and all kinds of nasty stuff.

Try up sampling all your images to the same format before the game starts and see what it does then. Not knowing all this stuff is happening is the price that is paid for abstraction.
 

Atheus

Diamond Member
Jun 7, 2005
7,313
2
0
I believe you can access graphics hardware in Java these days, if you're not running as an applet that is - I've seen some very speedy high res 2D game demos floating around.

exdeath is right though, Java is not the best for games. I once wrote a rigid body dynamics system in 2D Java and it was damn slow with more than a half dozen solid objects or a hundred particles on screen.