Android computing game FPS

Source: Internet
Author: User

Today, when I wrote a game that needed to calculate FPS, I found a loon Java game computing FPS class on the Internet and modified it as follows (copyright belongs to the original author ):

Package COM. PX. FPS; <br/> Import Java. text. decimalformat; <br/>/** <br/> */<br/>/** <br/> * <p> title: loonframework </P> <br/> * <p> Description: </P> <br/> * <p> copyright: Copyright (c) 2007 </P> <br/> * <p> company: loonframework </P> <br/> * @ author chenpeng <br/> * @ email ceponline@yahoo.com.cn <br/> * @ version 0.1 <br/> */ <br/> public class cfpsmaker <br/> {<br/>/** <br/> * sets the animation running quantity. Count the number of frames after a frame <br/> */<br/> Public static final int FPS = 8; </P> <p>/** <br/> * converts the data to a running cycle. <br/> * Unit: NS (nanoseconds) <br/> */<br/> Public static final long period = (long) (1.0/FPs * 1000000000 ); <br/>/** <br/> * maximum FPS interval, converted to 1 S = 10 ^ 9ns <br/> * Unit: NS <br/> */<br/> Public static long fps_max_interval = 000000000l; </P> <p>/** <br/> * Actual FPS value <br/> */<br/> private double nowfps = 0.0; </P> <p>/** <br/> * Total FPS interval <br/> * In NS <br/> */<br/> private long interval = 0l; <br/> private long time; <br/>/** <br/> * Total Number of running workers <br/> */<br/> private long framecount = 0; </P> <p>/** <br/> * Format decimal places <br/> */<br/> private decimalformat df = new decimalformat ("0.0 "); </P> <p>/** <br/> * manufacturing FPS data <br/> */<br/> Public void makefps () <br/>{< br/> framecount ++; <br/> interval + = period; <br/> // when the actual interval meets Time. <Br/> If (interval> = fps_max_interval) <br/>{< br/> // nanotime () returns the current value of the most accurate available system timer, in milliseconds <br/> long timenow = system. nanotime (); <br/> // obtain the time distance so far <br/> long realtime = timenow-time; // unit: NS <br/> // converts it to the actual FPS value <br/> nowfps = (double) framecount/realtime) * fps_max_interval; </P> <p> // change value <br/> framecount = 0l; <br/> interval = 0l; <br/> time = timenow; <br/>}</P> <p> Public long getframecount () <br/>{< br/> return framecount; <br/>}</P> <p> Public void setframecount (long framecount) <br/>{< br/> This. framecount = framecount; <br/>}</P> <p> Public long getinterval () <br/>{< br/> return interval; <br/>}</P> <p> Public void setinterval (long interval) <br/>{< br/> This. interval = interval; <br/>}</P> <p> Public double getnowfps () <br/>{< br/> return nowfps; <br/>}</P> <p> Public void setnowfps (double nowfps) <br/>{< br/> This. nowfps = nowfps; <br/>}</P> <p> Public long gettime () <br/>{< br/> return time; <br/>}</P> <p> Public void settime (long time) <br/>{< br/> This. time = time; <br/>}</P> <p> Public String getfps () <br/>{< br/> return DF. format (nowfps); <br/>}< br/>}

 

Simple usage:

First, initialize a fpsmaker instance.

Cfpsmaker fpsmaker; <br/> fpsmaker = new cfpsmaker (); <br/>

Then, initialize the following current time when calculating FPS:

Fpsmaker. setnowfps (system. nanotime ());

Finally, calculate and display the frame rate of the current game in the game loop:

Fpsmaker. makefps (); <br/> gamemanager. drawfps (fpsmaker. getfps () + "FPS ");

Over!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.