Skip to content

Commit

Permalink
FreeJ2ME: Move frame cap code into the main Runnable
Browse files Browse the repository at this point in the history
This makes sure that frame limiting works even with zb3's revamped
paint() logic, which has the added bonus of lower overhead and
increased performance when framerate is uncapped (most users
won't notice a difference with framerate capped anyway).

This however, only applies to AWT, since Anbu and Libretro don't
work the same way and do not benefit from the revamped logic, as
their drawing method is different and frame cap was always working
there.
  • Loading branch information
AShiningRay committed Sep 20, 2024
1 parent cde86ae commit 0bf1ff6
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/org/recompile/freej2me/FreeJ2ME.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,24 @@ public void run()
/* Whenever AWT GUI notifies that its menu options were changed, update settings */
if(awtGUI.hasChanged()) { settingsChanged(); awtGUI.clearChanged(); }

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

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

lcd.repaint();

lastRenderTime = System.currentTimeMillis();
}
else { lcd.repaint(); }

} catch (Exception e) { System.out.println(e.getMessage()); }

//lcd.paint(lcd.getGraphics());
}
});
Expand Down Expand Up @@ -556,41 +573,27 @@ public void update(Graphics g) {

public void paint(Graphics g)
{
try
if (config.isRunning)
{
if(limitFPS>0)
{
requiredFrametime = 1000 / limitFPS;
elapsedTime = System.currentTimeMillis() - lastRenderTime;
sleepTime = requiredFrametime - elapsedTime;

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

if (config.isRunning)
if(!rotateDisplay) { g.drawImage(config.getLCD(), cx, cy, cw, ch, null); }
else
{
if(!rotateDisplay) { g.drawImage(config.getLCD(), cx, cy, cw, ch, null); }
else
{
// If rotated, simply redraw the config menu with different width and height
g.drawImage(config.getLCD(), cy, cx, cw, ch, null);
}
// If rotated, simply redraw the config menu with different width and height
g.drawImage(config.getLCD(), cy, cx, cw, ch, null);
}
}
else
{
if(!rotateDisplay) { g.drawImage(Mobile.getPlatform().getLCD(), cx, cy, cw, ch, null); }
else
{
if(!rotateDisplay) { g.drawImage(Mobile.getPlatform().getLCD(), cx, cy, cw, ch, null); }
else
{
final Graphics2D cgc = (Graphics2D)this.getGraphics();
// Rotate the FB 90 degrees counterclockwise with an adjusted pivot
cgc.rotate(Math.toRadians(-90), ch/2, ch/2);
// Draw the rotated FB with adjusted cy and cx values
cgc.drawImage(Mobile.getPlatform().getLCD(), 0, cx, ch, cw, null);
}
final Graphics2D cgc = (Graphics2D)this.getGraphics();
// Rotate the FB 90 degrees counterclockwise with an adjusted pivot
cgc.rotate(Math.toRadians(-90), ch/2, ch/2);
// Draw the rotated FB with adjusted cy and cx values
cgc.drawImage(Mobile.getPlatform().getLCD(), 0, cx, ch, cw, null);
}
lastRenderTime = System.currentTimeMillis();
}
catch (Exception e) { System.out.println(e.getMessage()); }
}
}
}

0 comments on commit 0bf1ff6

Please sign in to comment.