[Fix] Timestep

[Add] FPS counter.
This commit is contained in:
Ritchie Cunningham 2022-03-07 15:01:12 +00:00
parent 0ba6a9dcc0
commit 675bb53d31
6 changed files with 77 additions and 27 deletions

Binary file not shown.

Binary file not shown.

BIN
res/kpixel.ttf Normal file

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 248 KiB

After

Width:  |  Height:  |  Size: 248 KiB

View File

@ -1,12 +1,18 @@
package tfg;
import org.jsfml.graphics.Color;
import org.jsfml.graphics.Font;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.graphics.Text;
import org.jsfml.system.Clock;
import org.jsfml.system.Vector2i;
import org.jsfml.window.Keyboard;
import org.jsfml.window.Keyboard.Key;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
@ -20,6 +26,10 @@ public class Game {
private final Vector2i renderWindowDimensions = new Vector2i(640, 480);
private Player player;
private ArrayList<Map> maps = new ArrayList<Map>();
private boolean windowFocused = true;
private Font pixel = new Font();
private int fps;
private static boolean limitFPS = false;
public static void main(String[] args) {
Game g = new Game(); /* Create temp object of self. */
@ -32,10 +42,17 @@ public class Game {
}
public void handleInitialization() {
try {
pixel.loadFromFile(Paths.get("res/kpixel.ttf"));
} catch(IOException ex) {
ex.printStackTrace();
}
renderWindow.create(new VideoMode(renderWindowDimensions.x,
renderWindowDimensions.y), renderWindowTitle);
renderWindow.setFramerateLimit(60);
if(limitFPS) {
renderWindow.setFramerateLimit(60);
}
player = new Player();
@ -51,10 +68,31 @@ public class Game {
*/
public void run() {
handleInitialization();
int framesDrawn = 0;
float updateRate = 20.0f; /* 20hz */
long maxUpdates = 1;
Clock updateClock = new Clock();
Clock frameClock = new Clock();
updateClock.restart();
long nextUpdate = updateClock.getElapsedTime().asMilliseconds();
while(renderWindow.isOpen()) {
long updates = 0;
handleInput();
handleLogic();
long updateTime = updateClock.getElapsedTime().asMicroseconds();
while ((updateTime-nextUpdate) >= updateRate && updates++ < maxUpdates) {
handleLogic();
nextUpdate += updateRate;
}
handleDrawing();
framesDrawn++;
float elapsedTime = frameClock.getElapsedTime().asSeconds();
if(elapsedTime >= 1.0f) {
fps = (int)(framesDrawn/elapsedTime);
framesDrawn = 0;
frameClock.restart();
}
}
}
@ -69,32 +107,39 @@ public class Game {
}
}
if(Keyboard.isKeyPressed(Key.W)) {
player.move(Direction.NORTH);
} else if(Keyboard.isKeyPressed(Key.S)) {
player.move(Direction.SOUTH);
} else if(Keyboard.isKeyPressed(Key.A)) {
player.move(Direction.WEST);
} else if(Keyboard.isKeyPressed(Key.D)) {
player.move(Direction.EAST);
} else if(Keyboard.isKeyPressed(Key.ESCAPE)) {
renderWindow.close();
}
if(Keyboard.isKeyPressed(Key.N)) {
player.resetLocation();
player.changeMap(getRandomMap());
if(windowFocused) {
if(Keyboard.isKeyPressed(Key.W)) {
player.move(Direction.NORTH);
} else if(Keyboard.isKeyPressed(Key.S)) {
player.move(Direction.SOUTH);
} else if(Keyboard.isKeyPressed(Key.A)) {
player.move(Direction.WEST);
} else if(Keyboard.isKeyPressed(Key.D)) {
player.move(Direction.EAST);
} else if(Keyboard.isKeyPressed(Key.ESCAPE)) {
renderWindow.close();
}
if(Keyboard.isKeyPressed(Key.N)) {
player.resetLocation();
player.changeMap(getRandomMap());
}
}
}
public void handleLogic() {
player.update();
}
public void handleDrawing() {
Text fpsCount = new Text("FPS: " + fps, pixel, 24);
fpsCount.setColor(Color.YELLOW);
fpsCount.setStyle(Text.BOLD);
fpsCount.setPosition(0,0);
renderWindow.clear();
renderWindow.draw(player.getMap());
renderWindow.draw(player);
renderWindow.draw(fpsCount);
renderWindow.display();
}

View File

@ -32,6 +32,7 @@ public class Player implements Drawable {
private int animationFrame = 0;
private SoundBuffer cannotMoveBuffer = new SoundBuffer();
private Sound cannotMove = new Sound();
private Vector2f tempPosition = new Vector2f(0,0); /* Horrible Hack. */
public Player() {
playerLoc = new Location(0, 0);
@ -60,7 +61,7 @@ public class Player implements Drawable {
if(currentMap.isValidLocation(newLoc)) {
currentAction = PlayerAction.MOVING;
newPlayerLoc = newLoc;
} else if(cannotMove.getStatus() != SoundSource.Status.PLAYING) {
} else if(cannotMove.getStatus() == SoundSource.Status.STOPPED) {
cannotMove.play();
}
}
@ -94,8 +95,9 @@ public class Player implements Drawable {
return textureCoordsRect;
}
public void draw(RenderTarget target, RenderStates states) {
Vector2f spritePosition = new Vector2f(0,0);
public void update() {
/* Dirty dirty hack and should be fixed asap. */
tempPosition = new Vector2f(0,0);
Vector2i currentPlayerPosition = playerLoc.getPosition();
if(currentAction == PlayerAction.MOVING) {
if(frameCounter >= animationSpeed) {
@ -104,7 +106,7 @@ public class Player implements Drawable {
playerLoc = newPlayerLoc;
newPlayerLoc = null;
Vector2i newPlayerPosition = playerLoc.getPosition();
spritePosition = new Vector2f(newPlayerPosition.x, newPlayerPosition.y);
tempPosition = new Vector2f(newPlayerPosition.x, newPlayerPosition.y);
animationFrame = 0;
} else {
float additionX = 0.0f;
@ -132,17 +134,20 @@ public class Player implements Drawable {
} else if(change >= .75f && change <= 1.0f) {
animationFrame = 3;
}
spritePosition = new Vector2f(currentPlayerPosition.x + additionX,
tempPosition = new Vector2f(currentPlayerPosition.x + additionX,
currentPlayerPosition.y + additionY);
}
frameCounter++;
} else {
spritePosition = new Vector2f(currentPlayerPosition.x, currentPlayerPosition.y);
tempPosition = new Vector2f(currentPlayerPosition.x, currentPlayerPosition.y);
}
}
public void draw(RenderTarget target, RenderStates states) {
playerSprite.setPosition(
new Vector2f(spritePosition.x*playerSize.x,
(spritePosition.y*playerSize.x) - (playerSize.y - playerSize.x)));
new Vector2f(tempPosition.x*playerSize.x,
(tempPosition.y*playerSize.x)-(playerSize.y-playerSize.x)));
playerSprite.setTextureRect(getTextureCoords());
RenderStates newStates = new RenderStates(playerSpritesheetTexture);
playerSprite.draw(target, newStates);