Frame per second counting in Android game - java

I'm looking for a method of counting Frames Per Second in game applications, the problem is that i dont have the source code for any of those apps, they dont have such functionality implemented inside. To be honest i dont really expect this to be possible but it was worth to ask.
And to be perfectly clear i understand that i need a basic knowledge and code of the main painting loop to count FPS.
Best Regards

What is the problem of making FPS counting when you have a main paint loop (don't you?).
I'm not familiar with Android features, but I'm sure it is done there the same in any game on any OS. The easiest way to make a game - is to make 2 threads. First one will be for logic actions (calculating trajectories, object position change over time, etc.). Second thread will take current game state (calculated by first thread) and draw it on the screen.
Here is the commented code on both threads with FPS counter:
//Class that contain current game state
class GameState {
private Point objectPosition;
public Point getObjectPosition() {
return objectPosition;
}
public void setObjectPosition(Point pos) {
this.objectPosition = pos;
}
}
//Runnable for thread that will calculate changes in game state
class GameLogics implements Runnable {
private GameState gameState; //State of the game
public GameLogics(GameState gameState) {
this.gameState = gameState;
}
public void run() {
//Main thread loop
while (true) { //Some exit check code should be here
synchronize (gameState) {
gameState.setObjectPosition(gameState.getObjectPosition()+1); //Move object by 1 unit
}
Thread.sleep(1000); //Wait 1 second until next movement
}
}
}
//Runnable for thread that will draw the game state on the screen
class GamePainter implements Runnable {
private Canvas canvas; //Some kind of canvas to draw object on
private GameState gameState; //State of the game
public GamePainter(Canvas canvas, GameState gameState) {
this.canvas = canvas;
this.gameState = gameState;
}
public void drawStateOnCanvas() {
//Some code that is rendering the gameState on canvas
canvas.drawLine(...);
canvas.drawOtherLine(...);
....
}
public void run() {
//Last time we updated our FPS
long lastFpsTime = 0;
//How many frames were in last FPS update
int frameCounter = 0;
//Main thread loop
//Main thread loop
while (true) { //Some exit check code should be here
synchronize (gameState) {
//Draw the state on the canvas
drawStateOnCanvas();
}
//Increment frame counter
frameCounter++;
int delay = (int)(System.getCurrentTimeMillis() - lastFpsTime);
//If last FPS was calculated more than 1 second ago
if (delay > 1000) {
//Calculate FPS
double FPS = (((double)frameCounter)/delay)*1000; //delay is in milliseconds, that's why *1000
frameCounter = 0; //Reset frame counter
lastFpsTime = System.getCurrentTimeMillis(); //Reset fps timer
log.debug("FPS: "+FPS);
}
}
}
}

I'm not too knowledgeable with Android Game development, but in Java2D (it's a bit hacky but it works...) I would xboot a jar with the edited JRE class which controls the rendering (eg Canvas) into the process.

This is a good read on game loop. It explains the various implementations and pros/cons of game loops.
And this is a good tutorial on implementing the gameloop + FPS counter in Android.

Related

Java game keeps crashing after about 2 minutes of runtime (Intellij)

I am using Intellij to program Java.
I am currently trying to make a top down tile based shooter.
My issue is that my game, after approximately 2 minutes, crashes with a popup saying "Java(TM) Platform SE Binary has stopped working. I recorded the time it took for it to crash 3 times: 1m57s, 1m59s, 1m58s.
The game is in a very simple state right now and I am not sure what could be causing the crash. All of the relevant code is in just two classes: GameFrame.java (extends JFrame) and GamePanel.java (which extends JPanel).
GameFrame.java:
package net.magnusfrater.tds.game;
import javax.swing.*;
public class GameFrame extends JFrame {
public static final int width = 1000;
public static final int height = width / 16 * 10;
private GamePanel gp;
public GameFrame () {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width,height);
setResizable(false);
setLocationRelativeTo(null);
setTitle("Time Based Fast Paced Top Down Shooter Demo");
gp = new GamePanel();
add(gp);
setVisible(true);
}
public static void main (String[] args) {
GameFrame gf = new GameFrame();
}
}
GamePanel.java
package net.magnusfrater.tds.game;
import net.magnusfrater.tds.input.Keyboard;
import javax.swing.*;
import java.awt.*;
public class GamePanel extends JPanel implements Runnable {
//panel
private Thread thread;
private static boolean running;
private boolean fpsLock;
//input
private Keyboard kb;
//game
private Game game;
public GamePanel () {
//panel
thread = new Thread(this, "Time Based Fast Paced Top Down Shooter Demo");
running = true;
fpsLock = true;
//input
//kb = new Keyboard();
//addKeyListener(kb);
//game
//game = new Game(1);
thread.start();
}
public void run () {
long iTimeNS = System.nanoTime();
int tickRate = 60;
long ns = 1000000000 / tickRate;
int ups = 0;
int fps = 0;
long iTimeS = System.nanoTime();
long s = 1000000000;
while (running) {
long fTimeNS = System.nanoTime();
if (fTimeNS - iTimeNS >= ns){
iTimeNS = System.nanoTime();
tick();
ups++;
if (fpsLock){
repaint();
fps++;
}
}
if (!fpsLock){
repaint();
fps++;
}
long fTimeS = System.nanoTime();
if (fTimeS - iTimeS >= s){
iTimeS = System.nanoTime();
System.out.println("ups: " + ups + "\tfps: " + fps);
ups = 0;
fps = 0;
}
}
System.exit(0);
}
public void tick () {
if (kb != null)
kb.tick();
if (game != null)
game.tick();
}
#Override
public void update (Graphics g) {
paint(g);
}
#Override
public void paint (Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0,0,GameFrame.width, GameFrame.height);
//if (game != null)
//game.paint(g);
}
public static void quitGame () {
running = false;
}
}
I originally thought that the issue was because of the way that I was loading images for spritesheets or maybe the way I was loading text files for the level design but after reworking both of those, the issue remained.
That left me curious and a little bit fed up so I tried finding out more about the explanation for the crash. First I read more from the popup but it didn't say anything useful: (See below)
Second I looked at the exit code given by Intellij: (See below)
I looked up what exit code 255 was but there wasn't anything useful. The best explanation I could find was that exit code 255 meant that the real exit code was out of range: (See below)
I was out of ideas at this point so I just started plain old googling everything I could think of. The problem with googling "Java(TM) Platform SE Binary has stopped working" is that almost every suggested link is a question about Minecraft. Limiting my search to Stack Overflow yielded me some results, but nothing conclusive. Some of the fixes I found were stuff I already tried (such as not handling input streams properly, not handling buffered reader properly, not disposing of elements, etc). I found these links but none of them were truly related to my issue:
(See below)
(See below)
(See below)
(See below)
(See below)
(See below)
The last fix I tried was to re-install Java SE Development Kit 8u101 AND Java SE Development Kit 8u102. I then restarted Intellij. I then restarted my computer.
Nothing worked.
At this point I think I'm just dumb. I've overlooked something easy I can tell. What am I missing?
(ps~ This is a possibly related issue. So if I run my game with almost no content in it with the fps not locked to 60, I get really absurd numbers of frames per second. I didn't think that fps as high as 7,000,000 was possible. I think. I don't know. Have I programmed that wrong as well? Here is a related picture of my ups/fps output: [see below])
(SEE HERE) So Stack Overflow doesn't allow members with a score within a certain threshold post more than 2 links and allows absolutely no posting of images. So here is a link to a google doc with all of the links and images I mentioned above:
https://docs.google.com/document/d/1XrBuVio19GmkFz0EfRzXVp5AJmM5zPfVO6vK3oS3Eaw/edit?usp=sharing
Try and set your -Xmx to something like 2G and see if it runs longer. If so, something is allocating memory and maybe you have that other setting set that exits instead of garbage collecting for some reason.
Also, try changing your code to limit things using Guava's RateLimiter.
…
// class level
final RateLimiter frameLimiter = RateLimiter.create(60.0);
final RateLimiter outputLimiter = RateLimiter.create(1.0);
…
// in run method
while (running) {
frameLimiter.acquire();
repaint();
fps++;
if (outputLimiter.tryAcquire()){
System.out.println("fps: " + fps);
fps = 0;
}
}
I've removed ups and tick(). You should do your work after repainting and I don't think you want to do more work than needed for the next frame, which at the soonest should be at your max rate. Later you'll need to add logic to handle skipping work when frames are being skipped. I might make more sense to increment the fps within repaint.
You could put the output in its own thread and only acquire that limiter if you synchronized the increments and resetting of fps.

Displaying JLabel movement

I am attempting to create a simple animation in which a series of bubble rotate around a centre point. I have one version of animation where the bubbles spread from the centrepoint before they begin to rotate, which works fine, but as soon as I click one of the images (which sparks the animation) the screen freezes for a moment and then the bubbles appear in their end position, rather than showing each step they made.
What I have so far is:
while(bubble[1].getDegree() != 270)
{
long time = System.currentTimeMillis();
//the below if statement contains the function calls for
//the rotating bubble animations.
next();
draw();
// delay for each frame - time it took for one frame
time = (1000 / fps) - (System.currentTimeMillis() - time);
if (time > 0)
{
try
{
Thread.sleep(time);
}
catch(Exception e){}
}
}
public void draw()
{
for(int i = 1; i < bubble.length; i++)
{
iconLabel[i].setLocation(bubble[i].getX(), bubble[i].getY());
textLabel[i].setLocation((bubble[i].getX()+10),(bubble[i].getY()+10));
}
}
For clarity, the method "next()" merely changes the position of the bubble to the appropriate place, I know this to be functioning as I have had the animation work before but once I implemented the animation to JLabels it stopped working.
Any help would be appreciated.
The drawing is frozen because you block the event dispatch thread. Drawing is done in the same thread as the while loop, and since the loop prevents anything else happening while it's running, swing can get to drawing only after the loop is finished, so only the last position is drawn.
Use a swing Timer instead:
timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// whatever you need for the animation
updatePositions();
repaint();
}
});
timer.start();
And then call timer.stop() when all the frames you need have been processed.

Overriding paintComponent(); Drawing multiple buffered images

I'm trying to make a tower defense game. So far I've made the JFrame (called GameFrame) where everything is displayed, added a background image (.PNG) and I'm trying to create the monsters and make them "move" over the path I've created. So, I created a GlassPane class which I set as glassPane in the GameFrame.
Here is the code for the GlassPane:
public class GlassPane extends JComponent implements ActionListener{
private ArrayList<MyPoint> path; //list of Points-pixel positions- (x,y pair) that monsters will follow
private ArrayList<Monster> wave; //list of monsters that will try to reach the rift
private Timer timer; //javax.swing.Timer
private boolean waveEnd;//signing that the wave has ended(ignore it)
public GlassPane(){
super();
createPath();//method is below
wave = new ArrayList<Monster>();//monsters added in nextWave()
timer = new Timer(4,this);//timer in order to slow down and smooth the movement of monsters
waveEnd = false;
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for(Monster m:wave){
if(m.isVisible())//visible is a private variable in Monster class(defines whether i want to paint that monster or not)
{
g.drawImage(m.getImage(), m.getX(), m.getY(), this);
}
}
}
public void nextWave(){
waveEnd = false;
for(int i =0;i<20;i++)
wave.add(new Monster());
wave.get(0).setVisible(true);//sets the first monster to be visible(paintable)
int visibles = 1;//index 0 is visible,so index 1 is next to become visible
while(!waveEnd){
if(visibles<19 && wave.get(0).getPathIndex()%50 == 0){
//meaning until all 20 monsters are visible(indexes 0-19),"every 50 steps(pixels) the first monster moves"
wave.get(visibles).setVisible(true);
//make another monster visible and start moving it
visibles++;//visible(moving) monsters increased by 1
timer.start();//"move all visible monsters one step forward with a small delay until the next movement to make it soft"
}
JOptionPane.showMessageDialog(new JFrame(), "Finished!","All monsters reached the rift!",JOptionPane.INFORMATION_MESSAGE);//let me know then wave is finished
}
private void createPath(){
//just making a lsit of pixels that I want monsters to follow
path = new ArrayList<MyPoint>();
for(int x=0;x<175;x++){
path.add(new MyPoint(x,0));
}
for(int y=0;y<175;y++){
path.add(new MyPoint(174,y));
}
for(int x=175;x<325;x++){
path.add(new MyPoint(x,174));
}
for(int y=175;y<425;y++){
path.add(new MyPoint(299,y));
}
for(int x=325;x<575;x++){
path.add(new MyPoint(x,424));
}
for(int y=424;y>175;y--){
path.add(new MyPoint(574,y));
}
for(int x=575;x<1001;x++){
path.add(new MyPoint(x,174));
}
}
#Override
public void actionPerformed(ActionEvent arg0) {
for(Monster m:wave)
if(m.isVisible())
m.move(path);
//"move every visible monster by one step"
if(m.get(19).getPathIndex() == 1674)//1674 is the path's last index
waveEnd = true;//if the last monster reaches the end,then all have
//meaning the wave is finished
this.repaint();//refresh any changes to the monsters' positions
}
}
The problem is that only the first monster is moving through the path, meaning only that one is painted when I invoke repaint(). What am I doing wrong in paintComponent(Graphics g)? Because I believe that's where my mistake is... How can I draw all "available" images each time one after another?
Note: They are buffered images in .PNG format if that matters at all.
I though about adding the Monster class code but I believe explaining the methods' function is enough to understand what's going on. Should someone need more details let me know. Thanks in advance.
I think you have to put parantheses around your loops.
The shorthand only loops over the first line after the loop
//Output:
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//BBBBBBBBBBBBBBBBBB
for(Monster m:wave)
System.out.println("AAAAAAAAAAAAAA");
System.out.println("BBBBBBBBBBBBBBB");
//Output:
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//AAAAAAAAAAAAAAAAAA
//BBBBBBBBBBBBBBBBBB
//BBBBBBBBBBBBBBBBBB
//BBBBBBBBBBBBBBBBBB
//BBBBBBBBBBBBBBBBBB
for(Monster m:wave)
{
System.out.println("AAAAAAAAAAAAAA");
System.out.println("BBBBBBBBBBBBBBB");
}

Code works sometime but not others (memory or thread issue)

I am having this odd issue, and I am not sure what is causing it. Some times the issue isn't even there. From what I am guessing, is that this is a Java memory issue or some sort of threading issue.
I have a Ship and the ship shoots Bullets If I hold down the Space key the ship shoots the bullets. I have the bullets set to fire off every 200 milliseconds. Some times they shoot fine and move at the same speed! Other times, they shoot they move at different speeds. What could cause this?
package JGame.Actions;
import JGame.GameObject.GameObject;
import javax.swing.AbstractAction;
public class MoveAction extends Action implements Runnable{
protected GameObject obj;
protected int endX = 0, endY = 0;
protected int moveAmount = 0;
protected Thread thread;
public void moveToY(GameObject obj, int y, int amount, AbstractAction complete){
this.obj = obj;
this.endY = y;
this.moveAmount = amount;
this.complete = complete;
thread = new Thread(this);
thread.start();
}
public void run(){
try{
boolean run = true;
while(run){
int objY = obj.getY();
if(objY > this.endY){
obj.setY(obj.getY() - 1);
}else if(objY < this.endY){
obj.setY(obj.getY() + 1);
}else{
run = false;
this.actionComplete();
}
thread.sleep(moveAmount);
}
}catch(Exception e){
}
}
}
Action Complete:
package JGame.Actions;
import javax.swing.AbstractAction;
public class Action {
protected boolean actionComplete = false;
protected AbstractAction complete;
public void actionComplete(){
complete.actionPerformed(null);
}
}
In my code I call moveToY it is a very simple call but sometime the Bullets move at different speeds (wrong), and others they move at the same speed (right). I don't know if it would help to mention that as the bullets move sometimes they slow down for a second or two then speed back up to the correct speed.
Edit: Main Thread
The following is my main thread with the paintComponent
#Override
public void run(){
try{
while(true){
// Check for key press events
Iterator actions = KeyboardMap.map.entrySet().iterator();
while(actions.hasNext()){
Map.Entry ap = (Map.Entry)actions.next();
Mapping mp = (Mapping)ap.getValue();
if(mp.pressed){
mp.run();
}
}
// Check for click mouse events
Iterator actions2 = MouseMap.map.entrySet().iterator();
while(actions2.hasNext()){
Map.Entry ap = (Map.Entry)actions2.next();
Mapping mp = (Mapping)ap.getValue();
if(mp.pressed){
mp.run();
}
}
for(GameObject go : gameObjects){
if(!go.getLeaveScreen()){
int goWidth = go.getWidth();
int goHeight = go.getHeight();
int goX = go.getX();
int goY = go.getY();
int gameWidth = Game.width;
int gameHeight = Game.height;
if(goX + goWidth >= gameWidth){
go.setX(gameWidth - goWidth);
}
if(goX <= 0){
go.setX(0);
}
if(goY + goHeight >= gameHeight){
go.setY(gameHeight - goHeight);
}
if(goY <= 0){
go.setY(0);
}
}
}
this.repaint();
Thread.sleep(roomSpeed);
}
}catch(Exception e){
}
}
public void paintComponent(Graphics g){
try{
g.drawImage(bg, 0, 0, this);
for(int i = 0; i < gameObjects.size(); i++){
GameObject go = gameObjects.get(i);
g.drawImage(go.getSprite(), go.getX(), go.getY(), this);
}
}catch(Exception e){
}
}
Well, I guess you have a large number of concurrent threads running (one by moving bullet), and you expect each thread to wake up after exactly moveAmount milliseconds. You can't have such a guarantee, because the thread scheduler allows each thread to run for some time one at a time, and you might thus have glitches.
Another problem is that you seem to execute modification on Swing components out of the event dispatch thread, which is clearly forbidden by Swing's threading policy.
The most important thing to change in your code is don't model time with Thread.sleep. Use a Swing Timer, which you'll schedule to execute every moveAmount milliseconds and, as a bonus, the code is executed on the Event Dispatch Thread with no effort on your part.
Inform yourself about the usage of multiple threads and how the processing structure of a game is made correctly.
The most important info for your case: You should only use ONE thread to process/move all your bullets and then render them.

animating a drawline in Android

I wanted to animate a line on the view. Ex: A progressive line starting at (0,120) and progresses upto (100,120). I've already tried this with plain canvas.drawLine() but was unable to apply an animation while drawing a line.
What I wanted to see is once the view is loaded a set of straight lines(of varying length) being drawn on the view. I don't want them as static lines. I wanted to see these lines when they are being drawn on the canvas.
I've tried couple of other options like, using the shape drawables and applying animations from the anim/xmls. Most of them didn't solve my problem. Extensive googling show the animations which are applied to the views directly. Here I dont want to create multiple image views as I'll end up having 6 lines on the view and i need to animate each of the line with different colors.
In future I should be able to move the lines up and down change there colors and do other kind of animations on each one of the lines.
Remembering the above constraints can you suggest me how to write a custom implementation for the same.
well are you not just using canvas.drawline(...), or canvas.drawlines(...), Your View could have the variables which represent the lines, maybe an arraylist of Points or something. I am currently trying to draw a line with moving values on a game I am making. I simply just change the values put into the drawline function, and my line animates as those values change. However it seams whenever the line has a slope closer to 1 my loop slow down and my other animations slow along with it. Are you having a similar problem when animating the lines? or are you just not able to get them drawn?
it sounds like your problem is in your game loop... I use a thread to loop through my draw and update methods, as I never was able to get the invalidate() thing working...
so below is a class called mainthread, it is created in your surfaceview in this case the surfaceview is called DrawView...
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MainThread extends Thread {
// desired fps
private final static int MAX_FPS = 75;
// maximum number of frames to be skipped
private final static int MAX_FRAME_SKIPS = 0;
// the frame period
private final static int FRAME_PERIOD = 1000 / MAX_FPS;
// Surface holder that can access the physical surface
private SurfaceHolder surfaceHolder;
// The actual view that handles inputs
// and draws to the surface
private DrawView gamePanel;
// flag to hold game state
public boolean running;
public void setRunning(boolean running) {
this.running = running;
}
public MainThread(SurfaceHolder surfaceHolder, DrawView gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
#Override
public void run() {
Canvas canvas;
long beginTime; // the time when the cycle begun
long timeDiff; // the time it took for the cycle to execute
int sleepTime; // ms to sleep (<0 if we're behind)
int framesSkipped; // number of frames being skipped
sleepTime = 0;
while (running) {
canvas = null;
// try locking the canvas for exclusive pixel editing
// in the surface
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
beginTime = System.currentTimeMillis();
framesSkipped = 0; // resetting the frames skipped
// update game state
this.gamePanel.doUpdate();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.doDraw(canvas);
// calculate how long did the cycle take
timeDiff = System.currentTimeMillis() - beginTime;
// calculate sleep time
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
// if sleepTime > 0 we're OK
try {
// send the thread to sleep for a short period
// very useful for battery saving
Thread.sleep(sleepTime);
} catch (InterruptedException e) {}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
// we need to catch up
this.gamePanel.doUpdate(); // update without rendering
sleepTime += FRAME_PERIOD; // add frame period to check if in next frame
framesSkipped++;
}
}
} catch(ClassCastException cce) {
}finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
}
using this thread it will call your update and draw methods approprietly, for onTouch just override the surfaceview (DrawView)'s onTouch method to access it normally. in order to stop the thread for what ever reason just set running to false.

Categories

Resources