Main method loop isn't working right.

I’m just trying to cap the fps at 60, and get a fps counter, but my loop seems to be eating a ton of my resources:


public static void main(String[] args) {
    	
    	joglplat m = new joglplat();
    	while(true){
    		fpscounter = 1000;
    		long startTime = System.nanoTime() / 1000000;
    		//startTime = System.currentTimeMillis();
    		
    		//System.out.println("st2: " + startTime);

    		try
			{	
				m.controls();
	    		m.update();
	    		m.repaint();
	    			
	    		fpscounter = ((System.nanoTime() / 1000000)-startTime);
	    		
				do {
		           Thread.yield();
		           } while ((System.nanoTime() / 1000000)-startTime < 17);
	
			}
    		
    		catch(Exception e){
    			e.printStackTrace();
    		}
    		
    		
    		//System.out.println("framerate: " + (60 - fpscounter));
    	}
    }

What exactly does this do?

m.controls();
m.update();
m.repaint();

// Json

probably:

gets input
updates all of the entities
paints

this is just a guess :wink:

Replace thread.yield with thread.sleep(1) and cpu usage will go down.

But there’s nothing wrong with using up all the cpu. And yield isn’t necessarily bad - many people use a game loop just like yours that yields over and over. search the forums for examples of different peoples’ game loops.

Hope that helps,
Keith

The reason I was asking was because the things in those methods might be what is eating up the resources.

You could always try commenting things out until you find the bad guy :smiley:

// Json

thread.yield will use the rest of your spare cpu, so its always at max 100%.

my gameloops yields every frame

works for me

Yes its perfectly fine to use if you don’t mind your game using your cpu to 100%

Ah, this worked. Thanks very much. ;D

Hi!

joglplat m = new joglplat();

Are you trying to use JOGL for your game? ;D

Yup! ;D