Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  High resolution timer issues under Vista  (Read 890 times)
0 Members and 1 Guest are viewing this topic.
Offline Drus

Junior Newbie





« Posted 2008-07-22 04:56:43 »

Hello everyone,

I've been making a small 2D game for a few years now in Windows XP. I recently bought a new laptop with Vista included and continued development on Vista instead. I have been running into a timing issue that I did not encounter in XP. From what I have read, a high resolution timer is enabled in Vista whenever a "multi-media" application is open. I've noticed that Internet Explorer counts as such an application.

I use a loop in a separate thread to call a draw method that draws everything. This loop has a Thread.sleep(x) to limit the cpu use. If I use x = 10 and have no other applications open, my game runs fine with around 8% cpu use on average (In reality, the above line sleeps for about 16 milliseconds). But when I open up IE to enable the high resolution timer, the cpu use goes up to 25%.

Ok, so its sleeping for 10 milliseconds instead of 16, meaning its drawing more often, so of course the performance would be more demanding. However I tried x = 16, 15, 17, etc, but the cpu usage is still abnormally high when the high resolution timer is on. Also, the FPS counter that I keep in my draw method reports an inconsistant and higher rate when the high resolution timer is on compared to when it is off. The only way to really fix this was to have x = 100, but of course the framerate becomes unacceptable for a simple 2D game in this case.

Has anyone run into this issue? Should I be handling the drawing differently? Any suggestions are appreciated.

Thank you.
Offline DragonsRage

Senior Newbie





« Reply #1 - Posted 2008-07-22 05:17:37 »

Are you explicitly calling Thread.sleep(10)? If you are, what might be happening is that your new laptop is faster than what you were previously using, and so your drawing takes less time, increasing your fps. In order to get a balanced fps try something like below.


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  
public void gameLoop()
    {
        long delta;
        //number of milliseconds between each frame
       //assuming 60 frames per second
       long frameDelay = 1000 / 60;
       
        while (isRunning)
        {
            //get the time before drawing
           delta = System.currentTimeMillis();
           
            drawing();
            logic();
            otherHappyStuff();
           
            //subtract the start time from the current
           //to find out the total time elapsed
           delta = System.currentTimeMillis() - delta;
           
            //subtract the elapsed time from the time
           //we have between each frame
           delta = frameDelay - delta;
           
            //Math.max to avoid trying to sleep for
           //a negative number, this happens
           //if it takes to long to draw.
           delta = Math.max(5, delta);
           
            try {
                Thread.sleep(delta);
            } catch (InterruptedException e) {}
        }
    }


If this isn't the source of your problem, post an example code of your drawing Thread and whatnot so we can all take a look at it. Also I am developing two 2D games on my laptop running Vista Ultimate and I have not experienced any problems. I even just ran a test to see if I am experiencing the same thing and I am not.

Secrets and lies

     () ()
     (O.o)
     (>.<)
Offline Drus

Junior Newbie





« Reply #2 - Posted 2008-07-22 07:07:56 »

I have implemented your provided code and I am getting more consistent framerates and lower cpu usage. Thank you.

However, there is still a difference between the cpu usages, but it is minor. I did some tests and I noticed that javaw.exe uses between 5 - 14% cpu, averaging around 10, along with a reported FPS of 62, with the high resolution timer, and 0 - 8% at 59 FPS without the timer. Instead of IE, I used a simple MidiSystem.getSequencer() call at the start which seems to turns on the high resolution timer. I removed the calls to logic updating to make sure that the rendering was the only thing running.

I forgot to mention in my original post that I have an XP Home desktop (Intel E6600 2.4 ghz cpu) as well as the Vista Home Premium laptop (Intel T7500 2.2ghz cpu) previously mentioned. I also have my old laptop (Celeron M 1.4ghz) ready for some slow machine testing now and then. From previous tests with my old code, the game runs as expected (i.e. exactly the same) on both slow and fast XP computers, with 64 FPS reported consistently with minimal (<4%) cpu use on both. I only have issues with Vista and Ubuntu (Consistent performance under Linux is a completely different story that I will address later).

I'm probably going to leave it as it is right now, but suggestions are still appreciated.

I just find it inconvenient that drawing a few images, nothing fancy, to a 800x600 screen would be so expensive on modern computers.

Here is the code I use for the graphics thread:
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  
public void run() {
      long frameDelay = 1000 / 60;
      long delta;
     
      while(true) {
         // FPS counter code removed for simplicity
       
         delta = System.currentTimeMillis();
         
         if (isLoaded) {
            Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
            draw(g);
         }
         
         delta = System.currentTimeMillis() - delta;
         delta = frameDelay - delta;
         
         delta = Math.max(5, delta);
         
         try {
            Thread.sleep(delta);
         } catch(Exception e) {System.out.println("interrupt");}
      }
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DragonsRage

Senior Newbie





« Reply #3 - Posted 2008-07-22 17:40:02 »

I am glad the code helped. Smiley I still have no idea whats causing the difference in cpu though, or how to do anything about it, so hopefully one of the other members here will have some insight.

Secrets and lies

     () ()
     (O.o)
     (>.<)
Pages: [1]
  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.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (58 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (41 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!