Created
January 15, 2015 03:17
-
-
Save williamahartman/5584f037ed2748f57432 to your computer and use it in GitHub Desktop.
Display framerate in a LibGDX graphics window. Call update() and render() in your main render method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.g2d.BitmapFont; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.utils.Disposable; | |
import com.badlogic.gdx.utils.TimeUtils; | |
/** | |
* A nicer class for showing framerate that doesn't spam the console | |
* like Logger.log() | |
* | |
* @author William Hartman | |
*/ | |
public class FrameRate implements Disposable{ | |
long lastTimeCounted; | |
private float sinceChange; | |
private float frameRate; | |
private BitmapFont font; | |
private SpriteBatch batch; | |
private OrthographicCamera cam; | |
public FrameRate() { | |
lastTimeCounted = TimeUtils.millis(); | |
sinceChange = 0; | |
frameRate = Gdx.graphics.getFramesPerSecond(); | |
font = new BitmapFont(); | |
batch = new SpriteBatch(); | |
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | |
} | |
public void resize(int screenWidth, int screenHeight) { | |
cam = new OrthographicCamera(screenWidth, screenHeight); | |
cam.translate(screenWidth / 2, screenHeight / 2); | |
cam.update(); | |
batch.setProjectionMatrix(cam.combined); | |
} | |
public void update() { | |
long delta = TimeUtils.timeSinceMillis(lastTimeCounted); | |
lastTimeCounted = TimeUtils.millis(); | |
sinceChange += delta; | |
if(sinceChange >= 1000) { | |
sinceChange = 0; | |
frameRate = Gdx.graphics.getFramesPerSecond(); | |
} | |
} | |
public void render() { | |
batch.begin(); | |
font.draw(batch, (int)frameRate + " fps", 3, Gdx.graphics.getHeight() - 3); | |
batch.end(); | |
} | |
public void dispose() { | |
font.dispose(); | |
batch.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx