how to lock my game at 30 fps

I am developing a game in an applet and I want to lock it at 30 fps. It is a plataformgame.I want to add a full-screen mode to it too, howcan I do the same thing?Thanks
[210 byte] By [luisofta] at [2007-9-29 13:13:58]
# 1

Something like ....

int oldTime = hiresTimer.getTime();

render();

int newTime = hiresTimer.getTime();

int renderTime = newTime-oldTime;

if(renderTime<1000/30)

{

Thread.sleep(1000/30-renderTime);

}

Abusea at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 2
as for fullscreen, simply use window.getGraphicsConfiguration().getDevice().setFullscreenWindow(window);
Abusea at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 3
Cheers Abuse, I had no hiresTimer existed, I am incredibly though :)How smooth will that keep the updates?
KarateMarca at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 4

You can make a hi-res timer with 1.4.2 using the "hidden" class sun.misc.Perfpublic class HiResTimer() {

Perf hiResTimer;

long freq;

public HiResTimer() {

hiResTimer = Perf.getPerf();

freq = hiResTimer.highResFrequency();

}

public long currentTimeMillis() {

return hiResTimer.highResCounter() * 1000 / freq;

}

}

jboeinga at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 5
what is this class? I didnt find any docs about it
luisofta at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 6
For some reason the words 齣dea?and 齦azy?vanished from my above post, fantastic :)Cheers jboeing, secret classes hey, christ what happens if you read the java src backwards!!!}:-D
KarateMarca at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 7
I up give
KarateMarca at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 8
how do you import sun.misc.Perf? It does compile when I useimport sun.misc.Perf but I get an error in my main class saying java.lang.NoClassDefFoundError: ... when I run myprogram.
afarmanda at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 9
Nevermind that, it seems no compiled code runs at all over here and it was running fine before.
afarmanda at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 10

Recipee for constant frame rate if you are using threads:

If you are using threads and you will work at a constant frame rate you have first to calculate the time a frame should remain on screen in milliseconds: delayPerFrame = 1000/frameRate.

You have to use the thread's function .sleep(x) and x is delayPerframe - time_you_use_to_draw_frame_stuff.

Of course this time will change while execution, therefore you need a timer. Java's timer is not precise enough for this purpose and the resolution of System.getTimeMillis() is also not high enough. So you have to write a timer on your own.

Idea:

Use a separate Thread with an infinite loop where a long-counter is increased and the thread sleeps for 1 millisecond.

Code:

Public class PreciseTimer implements Runnable{

private Thread preciseTimerThread = new Thread(this, "precise");

private long milliSeconds=0;

private boolean endTimer = false;

public PreciseTimer()

{

preciseTimerThread.start();

}

public void start(){

milliSeconds=0;

}

public long getMilliSeconds(){

if (milliSeconds>maxMilliSeconds){milliSeconds=maxMilliSeconds;}

return (milliSeconds);

}

public void endTimer(){

endTimer=true;

}

public void run(){

while (!endTimer){

milliSeconds+=1;

try{preciseTimerThread.sleep(1);}catch(java.lang.InterruptedException e){}

}

}

}

in your drawing thread you put:

PreciseTimer preciseTimer = new PreciseTimer();

//main loop begins

{

preciseTimer.start()

//draw frame...

try{

'thisthread'.sleep(delayPerFrame-preciseTimer.getMilliSeconds ())

catch(..)...

}

//main loop ends

preciseTimer.endTimer();

You should also check that for sleep(x), x remains positive.

I also used this method in my java game resulting in a constant frame rate. The precise timer-thread will not really affect the performance.

Good luck!

exxiona at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 11
waht value should I use for "maxMilliSeconds" ?
luisofta at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 12
what should I do when the value becomes negative?
luisofta at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...
# 13

just use maxMilliSeconds to define the maximal value the timer returns. if you use maxMilliSeconds=delayPerFrame, it cannot get negative.

public class PreciseTimer implements Runnable{

private Thread preciseTimerThread = new Thread(this, "precise");

private long milliSeconds=0;

private boolean endTimer = false;

private int maxMilliSeconds=0;

public PreciseTimer()

{

preciseTimerThread.start();

}

public PreciseTimer(int max)

{

maxMilliSeconds=max;

preciseTimerThread.start();

}

public void start(){

milliSeconds=0;

}

public long getMilliSeconds(){

if (milliSeconds>maxMilliSeconds){milliSeconds=maxMilliSeconds;}

return (milliSeconds);

}

public void endTimer(){

endTimer=true;

}

public void run(){

while (!endTimer){

milliSeconds++;

try{preciseTimerThread.sleep(1);}catch(java.lang.InterruptedException e){}

}

}

}

use PreciseTimer preciseTimer = new PreciseTimer(delayPerFrame); to generate new Instance.

exxiona at 2007-7-15 3:25:21 > top of Java-index,Other Topics,Java Game Development...