Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  Game loop design - your considerations  (Read 887 times)
0 Members and 1 Guest are viewing this topic.
Offline SHC
« Posted 2012-12-02 12:29:00 »

I had been using this run method as game loop.

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  
public void run(){
    // Initialize the resources
    Map.initMap();
    initResources();
    // Game loop initialization
    int SKIP_STEPS = 1000/Global.STEPS_FOR_SECOND;
    // The time
    long gameTime = getCurrentTime();
    int loops;
    // FPS counter
    int frames = 0;
    long lastFPSCount = getCurrentTime();
    // UPD counter
    int updates = 0;
    long lastUPDCount = getCurrentTime();
    // The current time
    long now;
    // Benchmarking
    long updStartTime = 0;
    long updEndTime = 0;
    long updTime = 0;
    while (running){
        now = getCurrentTime();
        loops = 0;
        updStartTime = getCurrentTime();
        while(now>gameTime && loops<Global.MAX_FRAMESKIP){
            updateGame(SKIP_STEPS);
            // calculate update count
            updates++;
            if (now - lastUPDCount > 1000){
                lastUPDCount = getCurrentTime();
                Global.ACTUAL_STEPS_FOR_SECOND = updates;
                Global.UPDATE_RATE = (int)((float)((float)Global.ACTUAL_STEPS_FOR_SECOND/(float)Global.STEPS_FOR_SECOND)*100);
                updates = 0;
            }
            gameTime += SKIP_STEPS;
            loops++;
        }
        updEndTime = getCurrentTime();
        updTime = updEndTime - updStartTime;
        System.out.println(updTime);
        interpolation = (now + SKIP_STEPS - gameTime)/SKIP_STEPS;
        displayGame();
        // FPS counter
        frames++;
        if (now - lastFPSCount > 1000) {
            lastFPSCount = now;
            Global.FRAMES_PER_SECOND = frames;
            frames = 0;
        }
    }
}


But the output of the bench marking is

1  
2  
3  
4  
5  
6  
7  
8  
9  
59
8
2
2
1
1
2
1
0


I couldn't understand why 0's appear as elapsed time for updates. Also the STEPS_PER_SECOND is 50. Thanks.

Offline ReBirth
« Reply #1 - Posted 2012-12-03 01:18:03 »

If the updateGame(SKIP_STEPS) doesnt do anything, it's normal right?

Offline SHC
« Reply #2 - Posted 2012-12-03 15:38:45 »

That method isn't empty, it moves the objects and checks collisions. The objects are nearly 100

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-12-04 10:38:43 »

After narrowing down your code
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
    while (running){
        now = getCurrentTime();
        loops = 0;
        updStartTime = getCurrentTime();
        while(now>gameTime && loops<Global.MAX_FRAMESKIP){
            updateGame(SKIP_STEPS);
            gameTime += SKIP_STEPS;
            loops++;
        }
        updEndTime = getCurrentTime();
        updTime = updEndTime - updStartTime;
        System.out.println(updTime);
    }

I suspect the 2nd while loop is not executed on later. The 'loops<Global.MAX_FRAMESKIP' looks pretty "stable". Try to debug the 'now' and 'gameTime'.

Offline SHC
« Reply #4 - Posted 2012-12-04 15:55:52 »

I tried printing them out every loop. Sometimes they're becoming equal. This other question I've asked helped me.

http://gamedev.stackexchange.com/questions/42010/passing-elapsed-time-to-the-update-function-from-the-game-loop/42018#42018

Thanks for the help

Offline ReBirth
« Reply #5 - Posted 2012-12-05 01:38:30 »

Quote
First, you should avoid calling getCurrentTime() multiple times per loop. You will potentially be getting different values at different times.
I just know about this, but yes it seems true Grin

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.

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

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

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

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

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

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

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

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

CopyableCougar4 (65 views)
2014-11-01 23:36:41

DarkCart (150 views)
2014-11-01 14:51:03
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!