Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Attempting to separate UPS and FPS, with implemented FPS cap  (Read 634 times)
0 Members and 1 Guest are viewing this topic.
Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Posted 2013-10-26 17:44:50 »

   Hello! I've been wrestling with this small task I've been trying to overcome. I want to be able to ask the player/user what FPS he wants to limit to, and to use that. I can unlock the FPS to go as fast as it can go, or to be locked at the current UPS (Ideally, 60). The problem is, I have no idea how to do anything separate (I.E. 30, 90, 120 FPS). I've looked over tutorials, tricks, tips, and some other articles on this website, and am still uncertain as to how I would apply it to my current game loop.
   Now, I'm not sure if this should be in the "Newbie/Debugging" or here, but regardless, here's my current run loop (Pastebin + direct copy): http://pastebin.com/UX5HifrL

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  
53  
54  
55  
// Run Method
  boolean isRunning = false;
   // Variables for the FPS and UPS counter
  private int ticks = 0;
   private int frames = 0;
   private int FPS = 0;
   private int UPS = 0;
   public double delta = 0;
   // Used in the "run" method to limit the frame rate to the UPS
  private boolean limitFrameRate = true;
   private boolean shouldRender;

   public synchronized void run() {
      long lastTime = System.nanoTime();
      double nsPerTick = 1000000000D / 60D;

      long lastTimer = System.currentTimeMillis();
      delta = 0D;

      while (isRunning) {
         long now = System.nanoTime();
         delta += (now - lastTime) / nsPerTick;
         lastTime = now;

         // If you want to limit frame rate, shouldRender = false
        shouldRender = false;

         // If the time between ticks = 1, then various things (shouldRender = true, keeps FPS locked at UPS)
        while (delta >= 1) {
            ticks++;
            tick();
            delta -= 1;
            shouldRender = true;
         }

         if (!limitFrameRate && ticks > 0)
            shouldRender = true;

         // If you should render, render!
        if (shouldRender) {
            frames++;
            render();
         }

         // Reset stuff every second for the new "FPS" and "UPS"
        if (System.currentTimeMillis() - lastTimer >= 1000) {
            lastTimer += 1000;
            FPS = frames;
            UPS = ticks;
            frames = 0;
            ticks = 0;
         }
      }
      stop();
   }


   I hesitate using "Thread.sleep", mostly because I don't want to break the functionality of my run loop as-is. I've been functioning on the render method being called either every tick/update, or just running all the time regardless. I guess that my question/request would be: What would be the code, or the process, to having varying FPS caps. Also, any other tips as to optimizing/using my game loop are very much appreciated. Thanks in advance!

Much obliged,
Ryan
Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Reply #1 - Posted 2013-10-27 16:24:20 »

   Is there nobody with any advice then? I'm still not having any luck myself. Just a link to an explanation or example would be nice. Again, thanks in advance.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2013-10-27 19:26:17 »

Here, take a look at this, its a great explanation of game loops:
http://youtu.be/rwjZDfcQ7Rc

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.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (38 views)
2014-08-08 02:01:56
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!