Skip to content
Snippets Groups Projects
Commit a88e6cb1 authored by Magnus Alexander Strømseng's avatar Magnus Alexander Strømseng
Browse files

limit and log fps

parent 4e465f19
No related branches found
No related tags found
1 merge request!71Resolve "Limit and log fps"
Pipeline #275634 passed
package com.mygdx.game; package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.utils.ScreenUtils; import com.badlogic.gdx.utils.ScreenUtils;
import com.mygdx.game.controller.SoundManager; import com.mygdx.game.controller.SoundManager;
import com.mygdx.game.service.APIClient.APIClientProvider; import com.mygdx.game.service.APIClient.APIClientProvider;
...@@ -8,12 +9,14 @@ import com.mygdx.game.service.APIClient.APIClientProvider; ...@@ -8,12 +9,14 @@ import com.mygdx.game.service.APIClient.APIClientProvider;
public class ChessGame extends ApplicationAdapter { public class ChessGame extends ApplicationAdapter {
public static final float WIDTH = 540; public static final float WIDTH = 540;
public static final float HEIGHT = 960; public static final float HEIGHT = 960;
private int fps = 60;
APIClientProvider apiProvider; APIClientProvider apiProvider;
public ChessGame() { public ChessGame() {
super(); super();
} }
FPSLogger logger = new FPSLogger();
public ChessGame(APIClientProvider apiProvider) { public ChessGame(APIClientProvider apiProvider) {
super(); super();
...@@ -29,6 +32,8 @@ public class ChessGame extends ApplicationAdapter { ...@@ -29,6 +32,8 @@ public class ChessGame extends ApplicationAdapter {
@Override @Override
public void render() { public void render() {
ScreenUtils.clear(0.3f, 0, 0, 1); ScreenUtils.clear(0.3f, 0, 0, 1);
sleep(fps);
logger.log(); // Log fps
GameManager.getInstance().render(); GameManager.getInstance().render();
} }
...@@ -41,4 +46,21 @@ public class ChessGame extends ApplicationAdapter { ...@@ -41,4 +46,21 @@ public class ChessGame extends ApplicationAdapter {
public void resize(int width, int height) { public void resize(int width, int height) {
GameManager.getInstance().resize(width, height); GameManager.getInstance().resize(width, height);
} }
private long diff, start = System.currentTimeMillis();
public void sleep(int fps) {
if(fps>0){
diff = System.currentTimeMillis() - start;
long targetDelay = 1000/fps;
if (diff < targetDelay) {
try{
Thread.sleep(targetDelay - diff);
} catch (InterruptedException e) {}
}
start = System.currentTimeMillis();
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment