- 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?
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?