Skip to content

Commit

Permalink
Libretro: Fix frame limiting on the libretro core
Browse files Browse the repository at this point in the history
Despite looking like frames were being limited, JBenchmark 2 pointed
out that it was still rendering over a thousand frames per second,
which meant tons of performance wasted since only 60 of those
were displayed... now it only really renders the required
amount of frames, just like AWT and SDL.
  • Loading branch information
AShiningRay committed Sep 21, 2024
1 parent 4dcabf1 commit 770bb16
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/org/recompile/freej2me/Libretro.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.awt.Image;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

Expand Down Expand Up @@ -159,7 +158,19 @@ public void run()
{
try
{
gc.drawImage(Mobile.getPlatform().getLCD(), 0, 0, lcdWidth, lcdHeight, null);
if(limitFPS>0)
{
requiredFrametime = 1000 / limitFPS;
elapsedTime = System.currentTimeMillis() - lastRenderTime;
sleepTime = requiredFrametime - elapsedTime;

if (sleepTime > 0) { Thread.sleep(sleepTime); }

gc.drawImage(Mobile.getPlatform().getLCD(), 0, 0, lcdWidth, lcdHeight, null);

lastRenderTime = System.currentTimeMillis();
}
else { gc.drawImage(Mobile.getPlatform().getLCD(), 0, 0, lcdWidth, lcdHeight, null); }
}
catch (Exception e) { }
}
Expand Down Expand Up @@ -394,7 +405,7 @@ public void run()
// Send Frame Libretro //
try
{
int[] data;
final int[] data;

if(config.isRunning)
{
Expand All @@ -404,7 +415,7 @@ public void run()
{
data = surface.getRGB(0, 0, lcdWidth, lcdHeight, null, 0, lcdWidth);
}
int bufferLength = data.length*3;
final int bufferLength = data.length*3;
int cb = 0;
for(int i=0; i<data.length; i++)
{
Expand All @@ -418,28 +429,10 @@ public void run()
frameHeader[2] = (byte)((lcdWidth)&0xFF);
frameHeader[3] = (byte)((lcdHeight>>8)&0xFF);
frameHeader[4] = (byte)((lcdHeight)&0xFF);
//frameHeader[5] = rotate - set from config

if(limitFPS>0)
{
requiredFrametime = 1000 / limitFPS;
elapsedTime = System.currentTimeMillis() - lastRenderTime;
sleepTime = requiredFrametime - elapsedTime;

if (sleepTime > 0) { Thread.sleep(sleepTime); }

System.out.write(frameHeader, 0, 6);
System.out.write(frameBuffer, 0, bufferLength);
System.out.flush();

lastRenderTime = System.currentTimeMillis();
}
else
{
System.out.write(frameHeader, 0, 6);
System.out.write(frameBuffer, 0, bufferLength);
System.out.flush();
}
System.out.write(frameHeader, 0, 6);
System.out.write(frameBuffer, 0, bufferLength);
System.out.flush();
}
catch (Exception e)
{
Expand Down

0 comments on commit 770bb16

Please sign in to comment.