Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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 791 times)
0 Members and 1 Guest are viewing this topic.
Online SHC
« Posted 2012-12-02 13: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 02:18:03 »

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

Online SHC
« Reply #2 - Posted 2012-12-03 16: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 11: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'.

Online SHC
« Reply #4 - Posted 2012-12-04 16: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 02: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.

xsi3rr4x (47 views)
2014-04-15 18:08:23

BurntPizza (43 views)
2014-04-15 03:46:01

UprightPath (59 views)
2014-04-14 17:39:50

UprightPath (41 views)
2014-04-14 17:35:47

Porlus (57 views)
2014-04-14 15:48:38

tom_mai78101 (80 views)
2014-04-10 04:04:31

BurntPizza (139 views)
2014-04-08 23:06:04

tom_mai78101 (239 views)
2014-04-05 13:34:39

trollwarrior1 (199 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!