Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Removed..  (Read 3736 times)
0 Members and 1 Guest are viewing this topic.
Offline Jari Särö

Junior Devvie


Medals: 1



« Posted 2011-09-06 09:32:53 »

Removed..
Online kappa
« League of Dukes »

JGO Kernel


Medals: 78
Projects: 15


★★★★★


« Reply #1 - Posted 2011-09-06 09:54:00 »

1000 milliseconds in one second.
You want 25 frames per second.

Therefore 1000/25 = 40ms is the total time each frame that should be spent on producing & sleeping that frame.

So the time you should sleep each frame is something like:

40ms - timeSpentProducingFrame = sleepTime (provided it doesn't go below 0).
Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #2 - Posted 2011-09-06 11:55:27 »

Removed..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2011-09-06 12:02:41 »

Not sleep(40) but sleep(40-timeSpentProducingFrame), I think this what kappa would to say.
Active rendering can help slow computer in resulting same framerate.

Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #4 - Posted 2011-09-06 12:07:43 »

you have to put on a variable the time you started your iteration, then do logic and draw, take the difference from the start of iteration, then sleep (40- iterationTime) .

1  
2  
3  
4  
5  
6  
7  
8  
9  
long startOfIteration= System.nanoTime();

//do logic
//do draw

long delay = (System.nanoTime() - startOfIteration)/1000000 - 40;

if (delay>0)
   Thread.sleep(delay);

Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #5 - Posted 2011-09-07 01:32:09 »

Removed..
Offline philfrei
« Reply #6 - Posted 2011-09-07 01:56:14 »

Don't forget that there are still a lot of OS's with slow clock interrupts out there, like Win XP. The accuracy of the sleep is only within +/- 15 msec, unless you do the trick to get a high speed clock mode:

http://www.java-gaming.org/topics/slight-jerkiness/24311/msg/206914/view.html#msg206914

"It's after the end of the world! Don't you know that yet?"
Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #7 - Posted 2011-09-07 04:35:33 »

Removed..
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-09-08 03:40:18 »

Setting that thread as a daemon will make it run in the background and will automatically end when the applet is closed.
The only thing you need to know about sleep(xx) is that is inaccurate without using that thread sleeping forever trick. Otherwise, you're good to go.
For a really smooth game loop, it is best if you use a busy Thread.yield loop like this:
1  
2  
3  
long now = System.nanoTime();
while(System.nanoTime()-now < sleepTime)
    Thread.yield();

This causes the game loop to be really smooth, however this hogs the CPU. For better performance with a slightly less smooth loop, use Thread.sleep(1) instead.

Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #9 - Posted 2011-09-08 06:07:56 »

Removed..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #10 - Posted 2011-09-09 09:00:17 »

Removed..
Offline cylab

JGO Ninja


Medals: 55



« Reply #11 - Posted 2011-09-09 09:10:49 »

It probably is, but still doesn't achieve 1ms accuracy. Try the same without the accuracy thread and you might even have much bigger differences. Hence the tight loop checking the actual time spend by the sleep on every iteration.

Mathias - I Know What [you] Did Last Summer!
Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #12 - Posted 2011-09-09 09:39:49 »

Removed..
Offline cylab

JGO Ninja


Medals: 55



« Reply #13 - Posted 2011-09-09 10:34:29 »

There is no difference with using this Thread and using the same loops without the Thread ?

Is this a question or have you tried it?

Mathias - I Know What [you] Did Last Summer!
Offline pitbuller
« Reply #14 - Posted 2011-09-09 10:39:36 »

Why you are trying to clutter your game code with those sleep(2)
Only set sleep in one place and only if needed. There isn't any reason for minimum sleep. Just use example what raking provided and you are safe. Don't try to use any kind of magic here. Its just a game loop and done millions times before. Use what is already tested and proved to behave smoothly.
Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #15 - Posted 2011-09-09 11:06:41 »

Removed..
Offline philfrei
« Reply #16 - Posted 2011-09-09 16:28:11 »

I agree, your timing numbers make it look like the XP is still using its clock interrupt of about 15msec. I can think of two possible reasons that you are not showing better accuracy with your TimerAccuracyThread running.

1) [Unlikely] Maybe this tactic only works with Win XP sp 3. My system is sp3, not 2, so I can't say for sure it works on sp2.

(2) Maybe you are measuring your accuracy using System.currentTimeMillis(). It will remain at 15msec accuracy, even with TimerAccuracyThread running. To measure elapsed time correctly (if you are not already doing so) use System.nanoTime() instead.

Below is a class I wrote which I use to get timing metrics. The main() method will demonstrate the difference between measuring with currentTimeMillis() and nanoTime(). Try running it with and without TimerAccuracyThread.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
public class TimeMetric {

   private long start, end;
   private int fpsCounter;
   
   public void fps()
   {
      // reset every second
      if (System.nanoTime() - start > 1000000000)
      {
         start = System.nanoTime();
         System.out.println("fps:" + fpsCounter);
         fpsCounter = 0;
      }
      else
      {
         fpsCounter++;
      }
   }
   
   public void peg()
   {
      start = System.nanoTime();
   }
   
   public void dur()
   {
      System.out.print("dur:");
      end = System.nanoTime();
      System.out.format("%.6f%n", (end - start)/1000000.0);
   }
   
   public static void main(String[] args) throws InterruptedException
   {
      long start = 0;
      long end = 0;
      for (int i = 5; i < 100; i += 5)
      {
         System.out.print("Sleep Int: " + i + "   ");
         start = System.currentTimeMillis();
         Thread.sleep(i);
         end = System.currentTimeMillis();
         System.out.println("  difMillis: " + (end - start));
         int j = 0;
         while ( j < Math.random()* 10000) j++;
         start = System.nanoTime();
         Thread.sleep(i);
         end = System.nanoTime();
         System.out.print("                    difNanos: ");
         System.out.format("%.1f%n", (end - start)/1000000.0);
         System.out.println();
      }
   }
}

"It's after the end of the world! Don't you know that yet?"
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #17 - Posted 2011-09-10 08:18:10 »

Why so many calls to Thread.yield() O___O ?!

Offline pitbuller
« Reply #18 - Posted 2011-09-10 22:28:48 »

Dont clutter the code. Use sleep/yield only in on place inside of loop and dont put it after the draw calls. If you are doing turn based game use sleep if something with real time components then use yield.
Offline pitbuller
« Reply #19 - Posted 2011-09-20 15:33:31 »

Where you have "learn" that yield/sleep after image drawing thing.  It's just plain wrong. Computers are most efficient when they are running on full speed. Best method is then: draw fast as possible and sleep after that until next frame is needed to render.

Sama suomeksi: Renderöi mahdollisimman nopeasti ja lepuuta sitten. Älä sotke koodiasia turhilla yieldeillä render metodissa.
Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #20 - Posted 2011-09-20 16:27:57 »

Removed..
Offline Waterwolf

Junior Devvie


Medals: 3



« Reply #21 - Posted 2011-09-20 16:50:53 »

cpu and gpu load,

If something happens to users computer it will propably happen when cpu or gpu load is hi, there is no reason to run cpu or gpu at 70%-100%, if it can be run 20%-40%, even if heavy load is just a short time.

I will set as many sleep and yield moments as i can to my code, and i feel im more respected that way when i speak about how my code runs, but im not starting to argue about this, i just want to know if there is any common times i should yield after every bufferedimage or quad, i would love to use sleep but it is not possible with present java to sleep so short times, so i have yield loops with nanotimer().

Thread.yield() uses a lot more CPU than Thread.sleep(). Also you'll get a lot more "respect" by using proper coding conventions and doing it right.

Also "i would love to use sleep but it is not possible with present java to sleep so short times", can't this be fixed with infinite sleeping thread trick.
Offline JL235

JGO Coder


Medals: 10



« Reply #22 - Posted 2011-09-20 17:57:54 »

Yield != sleep. It essentially means "take this thread out of the running queue, search for runnable threads, put them on the queue if they are available, and if they aren't available, continue running this thread". That is a of work you are adding to your while loop, and it does not prevent that thread from still eating up all CPU usage (where as sleep does).

Yielding also has dramatically different effects on different operating systems. In Windows Vista and 7, the OS will generally sleep your thread more often (it is a lot more difficult for threads to starve each other from Vista onwards). On Linux and XP, it won't. As a result parts of your app can freeze on one OS, and not on another, all because you used yield.

In my own code I sleep for the length of an ideal frame, minus the length that the last frame took (I think this was already suggested above). In practice the ideal frame time is around 15, a tad faster then 60fps. This always worked well for me, and I'd recommend it over yielding, spinning or sleep(1)'s. I would only use something more advanced if this does not work for you!

There is a good reason why Windows, and Mac OS, don't use high resolution timers by default. It is the same reason why Java, and especially JavaScript, also don't support a high resolutions for their timers. It is because it significantly drains the battery life on laptops and other devices. Chrome developers experimented with having a high-resolution timer, and found they used up an extra 25% of battery time. That is a quarter of the life spent solely on keeping the timer accurate. In one of the Mac OS X updates Apple reduced the internal timer, as it significantly increased battery time. IE 9 even has different resolutions, depending on if you are plugged into the mains or not.

Spinning or repeatedly yielding will use up more battery power!

Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #23 - Posted 2011-09-20 18:46:57 »

Removed..
Offline sproingie

JGO Kernel


Medals: 202



« Reply #24 - Posted 2011-09-20 20:00:57 »

Yield need not pause, or in fact do anything at all.  Do take note of what jdk7 says about yield:

http://download.oracle.com/javase/7/docs/api/java/lang/Thread.html#yield%28%29

Quote
public static void yield()

A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.

Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilise a CPU. Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.

It is rarely appropriate to use this method. It may be useful for debugging or testing purposes, where it may help to reproduce bugs due to race conditions. It may also be useful when designing concurrency control constructs such as the ones in the java.util.concurrent.locks package.
Offline Jari Särö

Junior Devvie


Medals: 1



« Reply #25 - Posted 2011-09-20 22:44:16 »

Removed..
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #26 - Posted 2011-09-20 22:48:54 »

The only thing you should be worrying about is:
1. update logic
2. draw everything
3. sleep for (desired amount - time the logic and drawing took)
4. goto 1

Btw: The graphics card isn't exactly....human. It doesn't need to "rest" Tongue

Offline JL235

JGO Coder


Medals: 10



« Reply #27 - Posted 2011-09-20 22:52:49 »

I do have this 200-400 maybe more bufferedimages on drawing row, largest is 900x900px and some are somewhat a 200x200px and 100x100px and a lot of 32x32px, and i do not know any other way to let graphics card rest a bit after every bimage than my good friend yield loop with nanotimer ?
Why do you need to let the graphics card rest?

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #28 - Posted 2011-09-21 00:12:20 »

Frame rate limiting is only useful on laptops. It's either to reduce power consumption or reduce graphics card heat. My little laptop likes to be 89 degrees if I don't set my settings carefully. Only power consumption is relevant in this case of course, but letting a Java2D program run wild doesn't sound that bad...

Myomyomyo.
Offline pitbuller
« Reply #29 - Posted 2011-09-21 06:17:45 »

GPU will get less heat(less malfunction) if you just draw everything on short period and rest until next frame.
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (34 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!