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 (534)
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  
  Java game runs with different delays ( sleep ) on WinXP compared to Linux Ubuntu  (Read 1523 times)
0 Members and 1 Guest are viewing this topic.
Offline NewOnJava

Junior Member




1970 born Java game hobbyer.


« Posted 2009-07-03 22:22:46 »

Hello!

I have a game on a build, my game is action with smoke and fire -> http://temp4321.dy.fi/dist4/appletloader.php

Why my game runs slow on WinXP and OK with Ubuntu 9.04.
I have no knowledge about timers or any delays. i have added the following code to help understand my problem.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
class coordinateThread extends Thread
    {
        public void run()
        {
            while ( true )
            {
                long endtime = System.nanoTime()+2000000;

                while ( endtime > System.nanoTime () )
                    try
                    {
                        Thread.sleep( 0, 200000 );
                    } catch (InterruptedException ex)
                    {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }


This code is my only extra Thread on my game, it is build to make game coordinate changes smoother.
As this is only my second Java game, and first with LWJGL, i need this help.

With Ubuntu my game runs ok, but with my WINXP my game runs real slow even as the screen update is smooth ??
So, With XP my game runs far too slow, why is this i have no experience on timers or any delays ??

How to fix this problem, what is the best answer, is my approach to Thread.sleep() totally wrong ??

//----

Thanks,,
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #1 - Posted 2009-07-03 23:44:51 »

I didnt even know that Thread.sleep() could ahve to numbers passed on to it....

what does it do having 2 numbers?
Offline bienator

Senior Member




OutOfCoffeeException


« Reply #2 - Posted 2009-07-03 23:46:07 »

1  
Warning - while you were typing a new reply has been posted. You may wish to review your post. 

<ignored>  Grin

hi,
sleeping, waiting, yealding is platform dependent - precision may vary, and yes windows has lower timer precision than linux.

The easiest way to render with constant framerate is to enable vsync and render fast as possible (without sleep). The gpu driver will do the rest for you and limit the framerate to the screen refresh rate (e.g 60hz is very common for LCD screens).

gl.glEnableSwapInterval(1);

Note: user can overwrite this setting in the driver configuration

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

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2009-07-03 23:55:31 »

In LWJGL, it's Display.setVSyncEnabled(true) to enable vsync.

Also, consider using the Display.sync() method to time the main loop. Or at least use Sys.getTime() and Sys.getTimerResolution() rather than System.nanoTime().

Cas Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2009-07-04 00:00:43 »

  Thread.sleep( 0, 200000 );


Hm. Well... look at the Sun implementation...

ms += (ns < 500000) ? 0 : 1;


So you're basically calling Thread.sleep(0);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline NewOnJava

Junior Member




1970 born Java game hobbyer.


« Reply #5 - Posted 2009-07-04 00:02:51 »

Hello!

I think i get it working with my Athlon on WinXP + Ubuntu9.04
it works now a real better, i removed the nanotime(), and installed import org.lwjgl.util.timer;
With LWJGL timer, my thread runs a lot better, i keep this timer for until i found even better.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
//------------------------------------------------------------------------------
   class coordinateThread extends Thread
    {
        public void run()
        {
            while ( true )
            {
                timer.tick();
                float endtime = (float)(timer.getTime()+0.002f);

                while ( endtime > timer.getTime() )
                try
                    {                        
                        Thread.sleep( 0, 99999 );
                        timer.tick();
                    } catch (InterruptedException ex)
                    {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }


//----

Thanks,,
Offline NewOnJava

Junior Member




1970 born Java game hobbyer.


« Reply #6 - Posted 2009-07-04 00:05:29 »

  Thread.sleep( 0, 200000 );


Hm. Well... look at the Sun implementation...

ms += (ns < 500000) ? 0 : 1;


So you're basically calling Thread.sleep(0);

Hmm!

Should i change my sleep to Thread.sleep(1); ??
what is the best way to sleep a while on Java Thread ??

----

Quote
Also, consider using the Display.sync() method to time the main loop.

I have Display.sync (40); on my code.

----

Should i go counting timer ticks and multiply my sin + cos with timer values, to get real clock based moving.
I just dont really like, when game goes like a atomclock, smooth it have to be, still, atomclock is a bit too much.

( edit. )
I started to count my sin + cos for plane moves with LWJGL.TIMER, now my game is very very smooth.
Demo game here -> http://temp4321.dy.fi/dist4/appletloader.php

//----

Thanks,,
Offline lithos

Junior Member


Medals: 1
Projects: 1



« Reply #7 - Posted 2009-07-04 02:54:25 »

I'm using thread.yield and then not processing anything that is 0ms.   Only downside is that unless you're using calculus for physics you're going to be having slightly different accelerations(running really fast V.  really slow) if you're just using delta-time.

___

The fun thing with using thread.sleep on windows or pretty much anywhere is that even telling the thread to sleep for 30mins could instead get it to sleep for 0 seconds.   While I don't ever see this happening the documentation is almost worded to say "don't be surprised or blame us if it does".

There are no such things as bugs...  Only happy accidents.
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 (35 views)
2014-07-24 01:59:36

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

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

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

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

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

danieldean (39 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!