java - Game Loop control -
i'm having tremendous difficulty understanding piece of code controls loop of game... can't understand purpose of while loop "while(unprocessedseconds > secondsforeachtick)" , why fps counter if inside of if(tickcounter % 60 ==0) follows on code:
public void run() { int frames = 0; double unprocessedseconds = 0; long previoustime = system.nanotime(); double secondsforeachtick = 1/60.0; int tickcount = 0; //render counter boolean ticked = false; while (running) { long currenttime = system.nanotime(); long passedtime = currenttime - previoustime; previoustime = currenttime; unprocessedseconds = unprocessedseconds + passedtime / 1000000000.0; int count = 0; while(unprocessedseconds > secondsforeachtick) { tick(); count++; unprocessedseconds -= secondsforeachtick; ticked = true; tickcount++; if(tickcount % 60 == 0){ system.out.println(frames + " fps"); previoustime += 1000; frames = 0; } } system.out.println("iterações loop: "+count); if(ticked) { render(); frames++; ticked = false; } } }
that inner while cycle ensures game update based on fixed time, rather frame time. unprocessed time getting collected , greater time frame 1 tick, calles tick()
.
the tickcount
counts ticks (the tick time 1/60 sec, 60 tick/sec), , 1 sec elapsed prints collected frames (so fps). also, ticked
signals may changed in game, new render required.
Comments
Post a Comment