Skip to content

Commit

Permalink
FPS cap
Browse files Browse the repository at this point in the history
  • Loading branch information
usernameak committed Sep 14, 2024
1 parent b42e68c commit 2ff004a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/net/sktemu/ams/AppDeviceProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class AppDeviceProfile {
private int screenWidth = 240;
private int screenHeight = 320;
private int maxFps = 0;
private boolean secureUtilWorkaround = false;

public void loadDeviceProfile(File file) throws IOException {
Expand All @@ -25,6 +26,10 @@ public void loadDeviceProfile(File file) throws IOException {
screenHeight = Integer.parseInt(propertyTable.getProperty("screenHeight", "320"));
} catch (NumberFormatException ignored) {
}
try {
maxFps = Integer.parseInt(propertyTable.getProperty("maxFps", "0"));
} catch (NumberFormatException ignored) {
}

secureUtilWorkaround = Boolean.parseBoolean(propertyTable.getProperty("secureUtilWorkaround", "false"));
}
Expand All @@ -34,6 +39,7 @@ public void saveDeviceProfile(File file) throws IOException {

propertyTable.setProperty("screenWidth", Integer.toString(screenWidth));
propertyTable.setProperty("screenHeight", Integer.toString(screenHeight));
propertyTable.setProperty("maxFps", Integer.toString(maxFps));
propertyTable.setProperty("secureUtilWorkaround", Boolean.toString(secureUtilWorkaround));

try (OutputStream stream = new FileOutputStream(file);
Expand All @@ -58,6 +64,14 @@ public void setScreenHeight(int screenHeight) {
this.screenHeight = screenHeight;
}

public int getMaxFps() {
return maxFps;
}

public void setMaxFps(int maxFps) {
this.maxFps = maxFps;
}

public boolean getSecureUtilWorkaround() {
return secureUtilWorkaround;
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/net/sktemu/ams/AppInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class AppInstance implements AutoCloseable {

private ExecutorService appThreadExecutor;

private final Object frameLimiterLock = new Object();

private long lastPresentTime = 0;

static {
AmsSysPropManager.init();
}
Expand Down Expand Up @@ -180,6 +184,30 @@ public Graphics getMidpGraphics() {
}

public void blitGraphics() {
int maxFps = getAppModel().getDeviceProfile().getMaxFps();

if (maxFps > 0) {
synchronized (frameLimiterLock) {
long presentTime = System.nanoTime();
long deltaTime = 1_000_000_000L / maxFps - (presentTime - lastPresentTime);

if (deltaTime > 0) {
try {
Thread.sleep(deltaTime / 1_000_000L, (int) (deltaTime % 1_000_000));
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
return;
}
}

if (deltaTime < -20_000_000L) {
lastPresentTime = presentTime - 20; // overrun
} else {
lastPresentTime = presentTime + deltaTime;
}
}
}

synchronized (emuCanvas.getBufferedImage()) {
backbufferImage.copyData(emuCanvas.getBufferedImage().getRaster());
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/sktemu/launcher/DeviceProfileEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ public DeviceProfileEditor(Frame owner, AppModel appModel) {
AppDeviceProfile deviceProfile = appModel.getDeviceProfile();

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new MigLayout("", "[][80,grow,fill]", "[][][][nogrid]"));
mainPanel.setLayout(new MigLayout("", "[][80,grow,fill]", "[][][][][nogrid]"));

JTextField tfScreenWidth = new JTextField(Integer.toString(deviceProfile.getScreenWidth()));
JTextField tfScreenHeight = new JTextField(Integer.toString(deviceProfile.getScreenHeight()));
JTextField tfMaxFps = new JTextField(Integer.toString(deviceProfile.getMaxFps()));
JCheckBox cbSecureUtilWorkaround = new JCheckBox("SecureUtil Workaround", deviceProfile.getSecureUtilWorkaround());

mainPanel.add(new JLabel("Screen Width:"));
mainPanel.add(tfScreenWidth, "wrap");
mainPanel.add(new JLabel("Screen Height:"));
mainPanel.add(tfScreenHeight, "wrap");
mainPanel.add(new JLabel("Max FPS:"));
mainPanel.add(tfMaxFps, "wrap");
mainPanel.add(cbSecureUtilWorkaround, "spanx 2,wrap");

Action okAction = new AbstractAction("OK") {
Expand All @@ -35,6 +38,7 @@ public void actionPerformed(ActionEvent e) {
try {
deviceProfile.setScreenWidth(Integer.parseInt(tfScreenWidth.getText()));
deviceProfile.setScreenHeight(Integer.parseInt(tfScreenHeight.getText()));
deviceProfile.setMaxFps(Integer.parseInt(tfMaxFps.getText()));
deviceProfile.setSecureUtilWorkaround(cbSecureUtilWorkaround.isSelected());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(
Expand Down

0 comments on commit 2ff004a

Please sign in to comment.