Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  [Solved]So I think I have a problem with my fps  (Read 987 times)
0 Members and 1 Guest are viewing this topic.
Offline hapsburgabsinth

Junior Devvie





« Posted 2011-11-30 10:18:39 »

is my game loop right? :S

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void run() {
      while (running) {
         long time = System.currentTimeMillis();

         update();
         render();
         draw();
         
         long deltaTime = System.currentTimeMillis() - time;
         time = fps - deltaTime;
         currentFps = time;
         if (time > 0) {
            try {
               Thread.sleep(time);
            } catch (Exception e) {
               System.out.println(e);
            }
         }
      }
      setVisible(false);
   }


fps is set to 60
Offline roland
« Reply #1 - Posted 2011-11-30 10:33:05 »

your fps variable appeared out of nowhere.
or do you mean int fps = 1000/60?

If you don't care about an exact fps, this works:
then multiply velocities etc by the deltaTime variable.

deltaTime should be calculated before the processing so you can use it in the current frame.

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  
public void run() 
{
      long lastFPS = 0;
      int fps = 0;
      int fpsCount = 0;

      long lastTime = System.currentTimeMillis();
      while (running) {
         long time = System.currentTimeMillis();
         long deltaTime = time - lastTime;
         lastTime = time;
         
         if (time - lastFPS > 1000)
         {
               lastFPS = time;
               fps = fpsCount;
               fpsCount = 0;
         }
         else
         {
                  fpsCount++;
         }

         update();
         render();
         draw();
         
         int sleepTime = 1000/60; //sleep 16ms per frame = 60fps
            try {
               Thread.sleep(sleepTime);
            } catch (Exception e) {
               System.out.println(e);
            }
         }
      }
      setVisible(false);
   }
Offline hapsburgabsinth

Junior Devvie





« Reply #2 - Posted 2011-11-30 11:39:31 »

Well my problem isn't so much the calculation of the fps (I think). Maybe I wasn't clear... But I got a lot of lag in my game... And my cycle only takes about 0.1 sec... Maybe it not there the problem is? :S
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline roland
« Reply #3 - Posted 2011-11-30 12:03:11 »

did you try using the loop I posted? I think its because you are allowing your program to sleep long.
Also, 0.1 seconds for a cycle is slow. - most games run at about 60fps = (0.016 seconds). 10 frames per second will look laggy.
Offline hapsburgabsinth

Junior Devvie





« Reply #4 - Posted 2011-11-30 12:04:34 »

i meant 0.001 Tongue forgot two 0s there Tongue

I tried to make it work, but maybe I should give it another try... I'll be back with a post in a few mins
Offline hapsburgabsinth

Junior Devvie





« Reply #5 - Posted 2011-11-30 12:09:10 »

Well it dosen't help, baybe I should try post a link with the game, so you/others can see what I mean... Smiley

http://sudopwn.net/Files/Zompocalypse.zip
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-11-30 12:11:09 »

System.currentTimeMillis() has a very low resolution on some machines, especially Windows. This causes it to be quite unaccurate. Try using this trick in your main method before your game starts:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
new Thread() {
    {
        setDaemon(true);
        start();
    }

    public void run() {
        while(true) {
            try {
                Thread.sleep(Long.MAX_VALUE);
            }
            catch(Exception exc) {}
        }
    }
}

Offline hapsburgabsinth

Junior Devvie





« Reply #7 - Posted 2011-11-30 12:15:29 »

ra4king I'm using the method you said to me, about using the update method to check for inputs, and it like if the program doesn't react on every input, but only some times... Don't know if it's there the problem is?


tried your solution, but no luck
Offline hapsburgabsinth

Junior Devvie





« Reply #8 - Posted 2011-11-30 12:43:08 »

So I tried your loop, hopefully I understood it right. It looks like this now:

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  
public void run() {
      double startTime, endTime, deltaTime, time;
      double timeCounter = 0;
      int framesDrawn = 0;

      while (running) {
         startTime = System.currentTimeMillis();

         update();
         render();
         draw();

         endTime = System.currentTimeMillis();

         deltaTime = endTime - startTime;
         time = fps - deltaTime;

         timeCounter += deltaTime;
         if(timeCounter > 1000){
            currentFps = framesDrawn / 1000;
            timeCounter = 0;
            framesDrawn = 0;
         }else{
            framesDrawn++;
         }

         long sleepTime = (long) (1000 / 60);

         try {
            Thread.sleep(sleepTime);
         } catch (Exception e) {
            System.out.println(e);
         }

      }
      setVisible(false);
   }


but it says I have 1.7E-6 fps o.O
Offline hapsburgabsinth

Junior Devvie





« Reply #9 - Posted 2011-11-30 12:52:54 »

So did some testing on my game, and found out my loop takes 500+ milliseconds, that could be the problem for the lag... Tongue
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.

xFryIx (60 views)
2014-11-13 12:34:49

digdugdiggy (39 views)
2014-11-12 21:11:50

digdugdiggy (32 views)
2014-11-12 21:10:15

digdugdiggy (28 views)
2014-11-12 21:09:33

kovacsa (50 views)
2014-11-07 19:57:14

TehJavaDev (54 views)
2014-11-03 22:04:50

BurntPizza (53 views)
2014-11-03 18:54:52

moogie (68 views)
2014-11-03 06:22:04

CopyableCougar4 (67 views)
2014-11-01 23:36:41

DarkCart (153 views)
2014-11-01 14:51:03
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!