Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 830 times)
0 Members and 1 Guest are viewing this topic.
Offline 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?

Offline 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'.

Offline 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.

pw (25 views)
2014-07-24 01:59:36

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

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

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

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

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

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 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!