Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (768)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
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  
  improving game loop  (Read 1692 times)
0 Members and 1 Guest are viewing this topic.
Offline pavul

Junior Devvie


Medals: 4
Exp: 4 years



« Posted 2018-04-05 15:16:33 »

hello guys, i am using below game loop for a long time now, i want to now if this is a correct approach or if i can improve a little bit more,
i would like to add a delta to update method but i dont know how that will impact



 
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  
[code]public final void run() 
    {
     
    long lastTime= System.nanoTime();
    double amountOfTicks=60.0;
    double ns= 1000000000 / amountOfTicks;
    double delta=0;
    long timer= System.currentTimeMillis();
//    int frames=0;
    while(running)
    {
   
        if(!pause)
        {
       
                long now= System.nanoTime();
                delta+=(now - lastTime)/ns;
                lastTime = now;
                while(delta >= 1)
                {
                   
                update();
                //render();
                delta--;
               
                }
               
                if(running)
                    render();
               
                //codigo para checar los frames
                frames++;
                if(System.currentTimeMillis() - timer > 1000)
                {
                timer +=1000;
                //System.out.println("FPS: "+frames);
                frames=0;
                }
             
               
        }//
               
    }//uail
[/code]

its better not know why it works, that know why is not working
Offline tariqbroadnax

Senior Devvie


Medals: 7
Exp: 3 years



« Reply #1 - Posted 2018-04-06 08:08:24 »

1. update with time step and not once per tick/frame
2. don't use System.currTimeMillis() if want to be good accuracy. In my experience, it leaves to being off by 1 or 2 frames per second.
3. i would change !paused variable to playing variable
4. if(running) render() is weird. Rendering is going to stop at the end of the iteration anyway
5. sleep?!?
6. Try not to worry too much about optimization. Although in this case, not sleeping is probably killing your CPU.


Here is my general game loop. Its not too different from yours. Copy if you want.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
long start, end = System.nanoTime(), period = (long)(1.0e9 / TARGET_FPS);
     
while(running)
{
   start = System.nanoTime();
   
   client.update((start - end) / 1.0e9);
         
   end = System.nanoTime();
     
   try
   {
      long remaining = period - (end - start),
             sleep_ms = remaining / 1000000;
           
      int sleep_ns = (int)(remaining - 1000000 * sleep_ms);
           
      if(sleep_ms > 0)
         Thread.sleep(sleep_ms, sleep_ns);
   }
   catch (InterruptedException e) {}
}
Offline pavul

Junior Devvie


Medals: 4
Exp: 4 years



« Reply #2 - Posted 2018-04-06 14:52:23 »

thank you i will try to implement that,

 delta is inside client.update( /*HERE*/)?

and render is after update?

its better not know why it works, that know why is not working
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2018-04-06 23:52:13 »

I recommend using the AnimationTimer from JavaFX. It reliably defaults to 60 fps, afaik, and is easy to use.

As an alternative (if not otherwise using JavaFX) using a ScheduledThreadPool with the game loop being contained in a Runnable. Goetz "Java Concurrency in Practice" (pg. 123). A util.Timer is not unreasonable as an alternative--and has an advantage of being possible to schedule to run TimerTasks at absolute time intervals, not just relative time intervals.

There is an interesting alternative structure for a game loop in a comment by @beeaware at the end of the JavaFX tutorial (which itself has a basic example). I would link to it directly but there doesn't seem to be a way to do this, so you will have to scroll down. beeaware makes use of Timelines, and integer counters, and seems to work fine with the game that beeaware showed recently in the showcase.

music and music apps: http://adonax.com
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1337 views)
2018-06-10 19:43:48

EgonOlsen (1228 views)
2018-06-10 19:43:44

EgonOlsen (939 views)
2018-06-10 19:43:20

DesertCoockie (1380 views)
2018-05-13 18:23:11

nelsongames (1112 views)
2018-04-24 18:15:36

nelsongames (1374 views)
2018-04-24 18:14:32

ivj94 (2119 views)
2018-03-24 14:47:39

ivj94 (1267 views)
2018-03-24 14:46:31

ivj94 (2284 views)
2018-03-24 14:43:53

Solater (807 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!