Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
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  
  A game loop problem  (Read 637 times)
0 Members and 1 Guest are viewing this topic.
Offline SHC

Senior Member


Medals: 8



« Posted 2012-10-18 02:55:02 »

I had been trying to make a game engine and got nothing but problems. Here are the problems.

  • Different speeds on each run
  • Highly slowed rendering
  • The game freezes after 5-10 min

And here's my code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
/* Only run this in another thread */
public void gameLoop(){
    long UPS = 1000/30;
    long expectedTime = 1000/UPS;  // 30ms
   long elapsedTime = 0;
    while (running){
        long start = getCurrentTime(); // in ms
       if (elapsedTime<=expectedTime){
            updateGame(elapsedTime);
            displayGame();
        }
        long end = getCurrentTime();
        elapsedTime = end - start;
        long sleepTime = expectedTime - elapsedTime;
        if (sleepTime>0){
            try {
                Thread.sleep(sleepTime);
            } catch (Exception e){}
        }
    }
}


Could anyone shed some light please?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Reply #1 - Posted 2012-10-18 03:02:22 »

You could save yourself the headache and use the Sync class from LWJGL Smiley

http://java-game-lib.svn.sourceforge.net/viewvc/java-game-lib/trunk/LWJGL/src/java/org/lwjgl/opengl/Sync.java?revision=3803&view=markup

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline SHC

Senior Member


Medals: 8



« Reply #2 - Posted 2012-10-18 03:05:11 »

But I'm not using LWJGL. I'm trying to make an engine which is independent of any other engine.

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline actual

JGO Coder


Medals: 19



« Reply #3 - Posted 2012-10-18 03:23:54 »

I'll ask the standard question....is there any reason you are doing this rather than leveraging an existing engine? Are you building the engine as a learning experience? Do you think your engine has requirements that the pre-existing ones don't have? You say you've had nothing but problems, if so, why not use what already exists if it's getting in the way of making a game?


One thing you can do is use System.nanoTime() which is a more accurate clock (I believe, someone correct me if I am wrong). The rendering and game issue problem might not have anything to do with your game loop. The issue may be within your updateGame and displayGame methods.
Offline SHC

Senior Member


Medals: 8



« Reply #4 - Posted 2012-10-18 03:29:50 »

I was creating the engine just for learning purposes. What I want to is make each frame take same amount of time (30 ms) unlike the tutorials on game loop at dewitters and JGO.

And here's my method

1  
2  
3  
public long getCurrentTime(){
    return (System.nanoTime()/1000000);
}

Offline sproingie
« Reply #5 - Posted 2012-10-18 03:51:46 »

The Sync class from LWJGL is something you can pull out and use independently of LWJGL.  It has no dependencies on any other part of LWJGL except for some really trivial time-related code from Sys that you can also copy out.  Just throw in the requisite copyright statement and you're set.
Offline StumpyStrust
« Reply #6 - Posted 2012-10-18 05:39:21 »

Uhh lwjgl is not really an engine so.....  If you want to make an engine (which is what I am trying to do) that has any sort of performance outside of tetris like games, you really need opengl.
Offline davedes
« Reply #7 - Posted 2012-10-18 06:47:28 »

Is this the same as Display.sync?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Reply #8 - Posted 2012-10-18 10:05:21 »

Yes.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Roquen

JGO Ninja


Medals: 66



« Reply #9 - Posted 2012-10-18 11:15:06 »

1  
2  
3  
public long getCurrentTime(){
    return (System.nanoTime()/1000000);
}

This is not the way to do digital timers.
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Reply #10 - Posted 2012-10-18 11:17:10 »

This is not the way to do digital timers.
That isn't exactly helpful, eh? "You're doing it wrong!" Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Roquen

JGO Ninja


Medals: 66



« Reply #11 - Posted 2012-10-18 11:22:08 »

I'm too lazy to redo a write-up and hoping to prompt someone else to. Smiley

OK: Here's a mini-write up: http://www.java-gaming.org/topics/jvm-reports-timer-taking-longer-than-it-should/24117/msg/202078/view.html#msg202078
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (61 views)
2013-05-17 21:29:12

alaslipknot (70 views)
2013-05-16 21:24:48

gouessej (100 views)
2013-05-16 00:53:38

gouessej (98 views)
2013-05-16 00:17:58

theagentd (107 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (167 views)
2013-05-12 17:10:36

kutucuk (166 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.124 seconds with 20 queries.