Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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 1066 times)
0 Members and 1 Guest are viewing this topic.
Online SHC
« Posted 2012-10-18 00: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: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-10-18 01: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
Online SHC
« Reply #2 - Posted 2012-10-18 01: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!
Legends of Yore - The Casual Retro Roguelike
Offline actual

JGO Coder


Medals: 23



« Reply #3 - Posted 2012-10-18 01: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.
Online SHC
« Reply #4 - Posted 2012-10-18 01: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

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-10-18 01: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 03: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 04:47:28 »

Is this the same as Display.sync?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


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

Yes.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online Roquen
« Reply #9 - Posted 2012-10-18 09: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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-10-18 09: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
Online Roquen
« Reply #11 - Posted 2012-10-18 09: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.

 

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

The first screenshot will be displayed as a thumbnail.

atombrot (24 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!