Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  How would I stop my game loop from running at thousands of FPS?  (Read 607 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-10-04 15:23:10 »

Hey,

For the past few months I've been messing around on a little project of mine and now I'm just getting to the point where I can start using the game loop again. I haven't even looked at this loop in a few months so I'm not sure how to fix it up the way I want just yet. Could someone explain how I could go about limiting the FPS to stop it from running at a few thousand FPS?

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  
public void run() {
      long lastTime = System.nanoTime();
      long timer = System.currentTimeMillis();
      final double NANO_SECONDS = 1000000000.0 / 60.0;
      double delta = 0.0;
      int frames = 0; // (FPS)Counts how many frames can be rendered per second.
      int updates = 0; // Counts how many times update(); can be called per second.
     
      new TESTING();

      while (running) {
         long now = System.nanoTime();
         delta += (now - lastTime) / NANO_SECONDS;
         lastTime = now;
         while(delta >= 1)
         {
            updateLogic();
            updates++;
            delta--;
         }
         renderScreen();
         frames++;
         
         if((System.currentTimeMillis() - timer) > 1000)
         {
            timer += 1000;
            FRAME.setTitle(updates + "UPS, " + frames + "FPS");
            updates = 0;
            frames = 0;
         }
      }
      stop();
   }


I'm fairly sure that I based this game loop off of TheCherno's youtube tutorials but it was quite a while back so I'm not sure.

Thanks in advance for any replies.
Offline Simn
« Reply #1 - Posted 2013-10-04 15:33:08 »

Benny has it covered quite nice hereWink

- Simn
Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #2 - Posted 2013-10-04 15:53:41 »

The question is why would you want to limit your FPS? If it's for regulating how fast the actual game runs, then you're approaching it from the wrong angle. If it's because of processor starvation, you would probably do well to add a Thread.yield() statement in your loop. The game loops article is fairly comprehensive and should get you back on track no matter what your chosen approach is.

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DQQAYME
« Reply #3 - Posted 2013-10-04 15:57:51 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
//D.T = System.nanoTime();
   static private void Syncronize(int fps){
      long pred_Frame_Time = 1000L * 1000 * 1000 / fps;
      long next_Frame_Start_Time = pred_Frame_Time + Frame_End_Nanos_Syn;
      long curent_Time = D.T();
      long dif_Time_In_Nano = next_Frame_Start_Time - curent_Time - 100000;
     
      if(dif_Time_In_Nano > 0){
         long mily_Sleap = dif_Time_In_Nano / 1000000;
         int nano_Sleap = (int) (dif_Time_In_Nano % 1000000);
         try {
            Thread.sleep(mily_Sleap, nano_Sleap);
         } catch (InterruptedException e1){
            e1.printStackTrace();
         }
      }
      Frame_End_Nanos_Syn = D.T();
   }

Offline TheSpaceHedgehog

Senior Newbie


Projects: 1


Space is the place!


« Reply #4 - Posted 2013-10-11 11:54:30 »

If your using Slick, you can use:

VSync - Sets FPS to monitor refresh rate - more here
1  
appGameContainer.setVSync(true);


Directly set FPS rate - Sets the FPS rate directly, at, say 60
1  
appGameContainer.setTargetFrameRate(60);



As for vanilla Java or LWJGL, these articles may help:
http://stackoverflow.com/questions/3102888/game-development-how-to-limit-fps
http://stackoverflow.com/questions/6217663/limiting-fps-for-my-game
http://lwjgl.org/wiki/index.php?title=LWJGL_Basics_4_(Timing)
http://www.koonsolo.com/news/dewitters-gameloop/


Finally, it depends on the type of game you're making. If it's really, really simple then this may be a good idea - otherwise it might be better to leave it as it is  Wink

Exception - A game like Limbo
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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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!