Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Is this even a valid way of getting Ticks Per Second?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Is this even a valid way of getting Ticks Per Second?

    Hello all. I've been recently working a bunch on my game, and I've seen a lot of simple game examples calculate Ticks/Frames per seconds in some odd way that uses a lot of variables. And I have a small piece of code in my game that calculates how many ticks passed every second. It seems really easy and I was wondering why no-one else used the method. Here is mine:

    //...
    int ticks = 0;
     
    Thread tickThread = new Thread(new Runnable(){
      public void run(){
        while(true){
          try{
            Thread.sleep(1000);
          }catch(Exception e){}
          //Then show user ticks that have
         //passed.
        }
      }
    });
    public Renderer(){
      tickThread.start();
    }
     
    //... inside update method
    public void update(){
      ticks++;
    }

    It pretty much uses a thread sleeping for 1000 milliseconds, then shows the user the amount of ticks that have passed. update() is called every game loop.
    So, is there an easier way? I've never seen anyone use this method. In LWJGL it is very accurate.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Is this even a valid way of getting Ticks Per Second?

    I'm not sure how you'd use this code, as it seems incomplete. For example, I don't see any reset of the ticks value. I'd suggest putting together an SSCCE that demonstrates exactly how it would be used, if you really want useful replies.

    But by introducing threading, you're introducing uncertainty into your fps count. For example, it's theoretically possible for this thread to return from the Thread.sleep() call, but not actually get control until much later. That probably won't happen, but you will see some wiggle in your data because of that problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Valid Java, compile
    By AnilS23 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2012, 10:51 AM
  2. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  3. Valid user input
    By ChristopherLowe in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  4. Help on a HW assignment if password is valid or invalid?
    By DeadlySwordz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2011, 09:48 AM
  5. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM