Skip to content

Commit

Permalink
MobilePlatform: Implement initial support for a fps counter overlay
Browse files Browse the repository at this point in the history
Still won't work on SDL (as it needs that UI overhaul) and Libretro
already has its own fps counter. As of right now, the overlay can
flash intermittently in some jars if the framerate is unlocked
(Asphalt 4 - K800i), but solid otherwise.
  • Loading branch information
AShiningRay committed Oct 31, 2024
1 parent c288996 commit 3b3a9f9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/org/recompile/freej2me/AWTGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import javax.microedition.media.Manager;

import org.recompile.mobile.Mobile;

public final class AWTGUI
{
/* This is used to indicate to FreeJ2ME that it has to call "settingsChanged()" to apply changes made here */
Expand Down Expand Up @@ -149,6 +151,7 @@ public final class AWTGUI
final CheckboxMenuItem enableRotation = new CheckboxMenuItem("Rotate Screen", false);
final CheckboxMenuItem useCustomMidi = new CheckboxMenuItem("Use custom midi soundfont", false);
final CheckboxMenuItem halveCanvasRes = new CheckboxMenuItem("Halve Canvas Resolution", false);
final CheckboxMenuItem showFPS = new CheckboxMenuItem("Show FPS Counter", false);

final CheckboxMenuItem stdLayout = new CheckboxMenuItem("Standard", true);
final CheckboxMenuItem nokiaLayout = new CheckboxMenuItem("Nokia", false);
Expand Down Expand Up @@ -401,6 +404,15 @@ public void itemStateChanged(ItemEvent e)
}
});

showFPS.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(showFPS.getState()){ Mobile.getPlatform().setShowFPS(true); }
else{ Mobile.getPlatform().setShowFPS(false); }
}
});

stdLayout.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
Expand Down Expand Up @@ -593,6 +605,7 @@ private void buildMenuBar()
optionMenu.add(useCustomMidi);
optionMenu.add(resChangeMenuItem);
optionMenu.add(halveCanvasRes);
optionMenu.add(showFPS);
optionMenu.add(phoneType);
optionMenu.add(fpsCap);
optionMenu.add(mapInputs);
Expand Down
38 changes: 36 additions & 2 deletions src/org/recompile/mobile/MobilePlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import java.util.concurrent.locks.LockSupport;
import java.io.InputStream;

import java.awt.Color;
import java.awt.event.KeyEvent;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Graphics3D;
Expand All @@ -47,11 +49,17 @@ public class MobilePlatform
public int lcdHeight;

// Frame Limit Variables
private long lastRenderTime = 0;
private long lastRenderTime = System.nanoTime();
private long requiredFrametime = 0;
private long elapsedTime = 0;
private long sleepTime = 0;

// Whether the user has toggled the ShowFPS option
private boolean showFPS = false;
private int frameCount = 0;
private long lastFpsTime = System.nanoTime();
private int fps = 0;

public MIDletLoader loader;
private EventQueue eventQueue;
private Displayable d;
Expand Down Expand Up @@ -224,7 +232,8 @@ public void flushGraphics(Image img, int x, int y, int width, int height)
{
limitFps();
gc.flushGraphics(img, x, y, width, height);


if(showFPS) { showFPS();}
painter.run();

//System.gc();
Expand All @@ -235,6 +244,7 @@ public void repaint(Image img, int x, int y, int width, int height)
limitFps();
gc.flushGraphics(img, x, y, width, height);

if(showFPS) { showFPS();}
painter.run();

//System.gc();
Expand Down Expand Up @@ -351,4 +361,28 @@ private void limitFps()

lastRenderTime = System.nanoTime();
}

// For now, the logic here works by updating the framerate counter every second
private void showFPS()
{
frameCount++;

if (System.nanoTime() - lastFpsTime >= 1_000_000_000)
{ // If 1 second or more has passed in nanoseconds
fps = frameCount; // Set fps to frames counted in the last second to then show it in the overlay.
frameCount = 0; // begin counting again from this point onwards until the next second passes
lastFpsTime = System.nanoTime(); // Reset last FPS time
}

// Set the overlay background
gc.getGraphics2D().setColor(new Color(0, 0, 105, 150)); // Semi-transparent dark blue
gc.fillRect(2, 2, 80, 20); // Background rectangle

// Draw the FPS text
gc.getGraphics2D().setColor(new Color(255, 175, 0, 255)); // Text color is orange
gc.setFont(Font.getDefaultFont());
gc.drawString("FPS: " + fps, 5, 4, Graphics.TOP|Graphics.LEFT); // Draw text at position (5, 5, from the top-left corner)
}

public void setShowFPS(boolean show) { showFPS = show; }
}

0 comments on commit 3b3a9f9

Please sign in to comment.