Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] crazy java fps counter results

Started by Japheth Oct 14, 2008 at 3:13 PM 2 replies 2.5k views
Original Post
Japheth
Japheth
I made an fps counter for a 2d game I'm making. I ran the counter on my desktop, which has a 9800 gx2 and a dual core 2.4 ghz processor. My fps was in the hundreds when I saw drawing lines and text and a small amount of images on the screen. However, when I got to the part where I draw lots of 40x40 tiles on the screen, the fps drops to 11. The same exact code running on my laptop, with an Intel 945GM and a 2.0 ghz processor, shows very close to 60 fps throughout. My problem is that I don't know how the fps for my desktop could be that much lower when it is clearly superior in terms of hardware. The code for fps counter and the main loop is below. The method I use for drawing the map tiles (they are pngs) is Graphics2D.drawImage(bufferedImage, x, y, null); and I draw right to the Graphics object that I pass to the render method. If anyone wants to see more code, I'll be glad to post it. I don't even know where to start fixing this problem. Is the fps counter somehow wrong? Should I try something more high res than System.currentTimeMillis()? Is there some more efficient way to draw images?
public LostHavenRPG(GraphicsDevice device) {
		try {
			GraphicsConfiguration gc = device.getDefaultConfiguration();
			frmMain = new Frame(gc);
			frmMain.setUndecorated(true);
			frmMain.setIgnoreRepaint(true);
			device.setFullScreenWindow(frmMain);
			
			if (device.isDisplayChangeSupported())
				chooseBestDisplayMode(device);

			frmMain.addMouseListener(this);
			frmMain.addKeyListener(this);
			frmMain.createBufferStrategy(2);
			BufferStrategy bufferStrategy = frmMain.getBufferStrategy();
			g = bufferStrategy.getDrawGraphics();
			
			gameState = GameState.Main;
			auxState = AuxState.None;
			done = false;
			showFps = false;
			frameCount = 0;
			lastFrameCount = 0;
			lastFpsUpdate = System.currentTimeMillis();
			playerX = 400;
			playerY = 300;
			
			loadGUI(bufferStrategy);
			loadMap();
			
			while (!done) {
				g = bufferStrategy.getDrawGraphics();
				handleGameEvents();
				render(g);
				g.dispose();
				bufferStrategy.show();
				frameCount++;
				if(System.currentTimeMillis()-1000 >= lastFpsUpdate) {
					lastFpsUpdate = System.currentTimeMillis();
					lastFrameCount = frameCount;
					frameCount = 0;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			device.setFullScreenWindow(null);
		}
	}
5MinuteGaming
5MinuteGaming
I'm guessing it has to do with your desktop being a dual core machine. Check your CPU usage while running the program, I'm pretty sure it will only use a single core thus your speed will greatly be reduced.

The graphics card on your desktop won't actually increase the rendering speed unless you turn on hardware acceleration and opengl rendering support for java. According to the link below you can do that as a command line option:

-Dsun.java2d.opengl=true

I found the following link in a quick google search for java hardware acceleration, I'm sure there are more in depth explanations on the particular features that might help solve your problems.

http://java.sun.com/j2se/1.5.0/docs/guide/2d/new_features.html

You also might want to try and put your rendering in a separate thread and do your own double buffering though I doubt this will make a difference. Just a thought though.

Good luck with solving this issue. I would like to know of any progress you have with this and what method(s) you use.
elFarto
elFarto
System.currentTimeMillis() on Windows has about a 16ms accuracy. System.nanoTime() / 1000000 is much more accurate.

Regards
elFarto
Japheth
Japheth
Thanks for the replies. I will probably use both of those things in the future. As a side note, I wonder if my fps was way off since I wasn't using an accurate timer. I forgot to mention, but my laptop was also dual core.

Anyway, I did figure out a fix. All the images I was loading were not in the same color mode as the screen, so I made a load image function that loads an image, makes an identical sized bufferedimage using GraphicsConfiguration.createCompatibleImage and then blitted the loaded image onto the new one. My fps suddenly jumped up!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.