Jump to content
  • Advertisement
Sign in to follow this  
breath20

[java] problem with FPS

This topic is 3705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im working on a very basic game, (just trying to get my feet wet). basically i followed the example out of the book "Java Game Programming Second Edition" The only real big change i made was changing the app from an applet to a Jframe. Now to the problem i'm having. When i load an image for a sprite it completely kills my FPS. As long as i don't include the images, then the game runs out a very high FPS. I thought i might be loading the image more then once, but from what i can tell im not. i would really appreciate any help that i can get on this matter, i have been stuck for some time now and have run out of ideas. here is the key points for the graphics, i chopped the code down, so it does not include everything i have. public class gamePanel extends JFrame implements Runnable, KeyListener { private Thread gameloop; private BufferedImage backbuffer; private Graphics2D g2d; private static final int ASTEROIDS = 20; private Sprite[] ast = new Sprite[ASTEROIDS]; AffineTransform identity = new AffineTransform(); Random rand = new Random(); private volatile boolean running = true; public gamePanel() { super(); init(); gameStart(); } public void init() { backbuffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB); g2d = backbuffer.createGraphics(); for (int n = 0; n < ASTEROIDS; n++) { ast[n] = new Sprite(this, g2d); ast[n].setAlive(true); ast[n].load("asteroid.png"); ast[n].setPosition(new Point2D(rand.nextInt(this.SCREENWIDTH), rand.nextInt(this.SCREENHEIGHT))); ast[n].setFaceAngle(rand.nextInt(360)); ast[n].setMoveAngle(rand.nextInt(360)); ast[n].setRotationRate(rand.nextDouble()); double ang = ast[n].moveAngle() - 90; double velx = calcAngleMoveX(ang); double vely = calcAngleMoveY(ang); ast[n].setVelocity(new Point2D(velx, vely)); } } private void drawAsteroids() { for (int n = 0; n < ASTEROIDS; n++) { if (ast[n].isAlive()) { ast[n].transform(); ast[n].draw(); } } } public void paint(Graphics g) { g.drawImage(backbuffer, 0, 0, this); g.dispose(); } public void start() { gameloop = new Thread(this); gameloop.start(); } public void run() { while (running) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } gameUpdate(); screenUpdate(); this.repaint(); } System.exit(0); } private void gameUpdate() { updateAsteroids(); } private void screenUpdate() { g2d.setColor(Color.BLACK); g2d.fillRect(0, 0, this.SCREENWIDTH, this.SCREENHEIGHT); drawAsteroids(); g2d.setColor(Color.WHITE); g2d.drawString("FPS : " + frameRate, 5, 10); } // end of screenUpdate() private void updateAsteroids() { for (int i = 0; i < ASTEROIDS; i++) { if (ast.isAlive()) { ast.updatePosition(); ast.updateRotation(); int w = ast.imageWidth() - 1; int h = ast.imageHeight() - 1; double newx = ast.position().X(); double newy = ast.position().Y(); if (ast.position().X() < -w) { newx = this.SCREENWIDTH + w; } else if (ast.position().X() > this.SCREENWIDTH + w) { newx = -w; } if (ast.position().Y() < -h) { newy = this.SCREENHEIGHT + h; } else if (ast.position().Y() > this.SCREENHEIGHT + h) { newy = -h; } ast.setPosition(new Point2D(newx, newy)); ast.setState(SPRITE_NORMAL); } } } private void gameStart() { if (gameloop == null) { gameloop = new Thread(this); gameloop.start(); } } } this is the class that loads the image, my sprite class just calls ImageEntity to load and store the iamge. public class ImageEntity extends BaseGameEntity { protected Image image; protected AffineTransform at; protected Graphics2D g2d; protected JFrame frame; public ImageEntity(JFrame a) { super(); this.frame = a; setImage(null); setAlive(true); } public Image getImage() { return this.image; } public void setImage(Image image) { this.image = image; double x = this.frame.getSize().width / 2 - width() / 2; double y = this.frame.getSize().height / 2 - height() / 2; at = AffineTransform.getTranslateInstance(x, y); } private URL getURL(String filename) { URL url = null; try { url = this.getClass().getResource(filename); } catch (Exception e) { } return url; } public void load(String fileName) { try { this.image = ImageIO.read(getURL(fileName)); double x = this.frame.getSize().width /2 - width() /2; double y = this.frame.getSize().height /2 - height() /2; at = AffineTransform.getTranslateInstance(x,y); } catch(Exception e) { } } public void transform() { at.setToIdentity(); at.translate((int)getX() + width()/2, (int)getY() + height() /2); at.rotate(Math.toRadians(getFaceAngle())); at.translate(-width()/2, -height()/2); } public void draw() { g2d.drawImage(getImage(), at, frame); } } Thanks for help. Breath

Share this post


Link to post
Share on other sites
Advertisement
What actually happens to your FPS (e.g. 1000 -> 10)? I can't see anything in the code you posted that would actually slow it down to a crawl, but maybe there is something in the other parts of your code.

On another topic, using the repaint() method isn't usually good for games, so you may want to check out the Active Rendering article for a better example.

Share this post


Link to post
Share on other sites
The problem is the image you are loading is not compatible with the screen resolution. To make any image you load compatible, do this:

//load your image
Image loadedImage = ImageIO.read("someimage.png");

//create a compatable image
BufferedImage compatibleImage = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(loadedImage.getWidth(), loadedImage.getHeight());

//copy loaded image to compatible image
compatibleImage.getGraphics().drawImage(loadedImage, 0, 0, null);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!