Jump to content
  • Advertisement
Sign in to follow this  
phil05

Java game loop questions

This topic is 4809 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

I don't understand how game loops are done in Java. This run method is by a seperate thread for my Pong game. Few questions are: How can I keep 30 fps within this? How can the keyboard input be part of this loop if it has its own method outside? The timing would be off due to that. It was easy to do in C++ but I'm still unsure of how its done in Java. Any help is appreciated.
	public void run()
	{
		for ( ; ; )
		{  
			// Repaint the screen.
			repaint();
				 
			// Check if the user minimized window.
			if (stopFlag)
				break;
		}
	}
	 
	
	public void keyPressed(KeyEvent ke)
	{
		int key = ke.getKeyCode();
		
		switch (key)
		{
			case KeyEvent.VK_UP:
			{
				LPad.setY(LPad.getY() - 15);				
			}
			break;
			
			case KeyEvent.VK_DOWN:
			{
				LPad.setY(LPad.getY() + 15);
			}
			break;
		}
		
		repaint();
	}

Share this post


Link to post
Share on other sites
Advertisement
Google "How to set FPS in Java Game Programming" Try that. I wish I knew Java so I could help ya more. But I helped ya I think. Try it and see what happens. Java harder than C++? C++ is hard though. Google those others is all I can say. I hope I don't sound like a jerk or anything. I would really enjoy helping you. I thought maybe it was you couldn't figure out loops.

Share this post


Link to post
Share on other sites
Quote:
Original post by phil05
I don't understand how game loops are done in Java. This run method is by a seperate thread for my Pong game. Few questions are: How can I keep 30 fps within this?


In the same way as in C++: by checking the amount of time elapsed for the current loop iteration, and pausing the thread for the remaining time desired for the frame. In Java, timing is available via System.currentTimeMillis(), and millisecond time values can be fed to the sleep() method of the thread that you wish to pause (i.e. you should just be able to put "sleep(whatever)", since you appear to be extending Thread). Note that sleep() can throw Exceptions: java.lang.InterruptedException (checked; impossible to be thrown unless you're doing complicated threading stuff, but you'll need to catch it) and NegativeSleepTimeException (if the requested time is negative; not checked, but it sure is a pain to have your thread die because of this).

Quote:
How can the keyboard input be part of this loop if it has its own method outside?


To keep things serialized, I recommend using the keyPressed() etc. callbacks (note that these methods will be processed by a different thread than the one executing the run() code) to set some class member indicating the current "key state", which you can then check as part of the run() processing.

Also, just as a comment, you might not want to terminate the current thread just because the window is closed. Consider an architecture like:


public void run() {
while (running) {
checkTime();
if (!paused) {
doMainLogic();
repaint();
}
sleepForRemainingTime();
}
cleanUpProgram();
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
System.currentMillis() sucks, cannot measure low elapses (I think min is 50ms = 5 FPS max). System.nanoTime from Tiger is better, Java3D timer even more. Also, if you want consistent update thread, you should calculate excesses, oversleep times and update without rendering accordingly.

Second, drop AWT repainting requests, JVM has tendency to "combine" (read: discard) if more than a few request are pending on the thread. Implement manual doublebuffering using BufferedImage.

To zahlman:
Testing keys won't work if few keys are pressed fast enough, cause update thread won't have time to process them.

Queue is solution, but push on queue only events that are different than the one peeked from queue, to avoid late key processings.

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!