Hello there! I’m new here, and also in java world.
I’m currently working on a tile-based game. The problem is: When i make a runnable jar file and open it in my computer, i have 55-60 FPS, but if one of my friends open it, the FPS don’t pass 18. I have to mention that my PC is slower (in hardware) than theirs.
Here is my Core class:
package com.game;
import java.awt.*;
import com.game.gui.*;
public abstract class Core implements Runnable {
private boolean running;
public static GameScreen screen;
public boolean paused;
public int fps;
boolean game_is_running = true;
long startTime, currTime;
public void run() {
init();
gameLoop();
startTime = System.currentTimeMillis();
currTime = System.currentTimeMillis();
}
public void init() {
screen = new GameScreen();
running = true;
}
public void gameLoop() {
int TICKS_PER_SECOND = 60;
int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
int MAX_FRAMESKIP = 10;
long next_game_tick = currTime-startTime;
int loops;
fps = 0;
int frames = 0;
long totalTime = 0;
long curTime = System.currentTimeMillis();
long lastTime = curTime;
while(running) {
lastTime = curTime;
curTime = System.currentTimeMillis();
totalTime += curTime - lastTime;
if( totalTime > 1000 ) {
totalTime -= 1000;
fps = frames;
frames = 0;
}
frames++;
currTime = System.currentTimeMillis();
loops = 0;
while( currTime - startTime > next_game_tick && loops < MAX_FRAMESKIP) {
update(); //updates entity position
next_game_tick += SKIP_TICKS;
loops++;
}
Graphics2D g = screen.getGraphics();
render(g);
g.dispose();
screen.update();
}
}
public abstract void render(Graphics2D g);
public void update() {}
}