How to stop frames per second from dropping? - java

Okay, so why would my frames per second drop at a random time while playing the game? and what can I do to fix it. Below im going to show the code that I have with the engine that was built.
public Engine() {
Log.d("Engine","Engine constructor");
p_view = null;
p_canvas = null;
p_thread = null;
p_running = false;
p_paused = false;
p_resume = false;
p_paintDraw = null;
p_paintFont = null;
p_numPoints = 0;
p_typeface = null;
p_preferredFrameRate = 40;
p_sleepTime = 1000 / p_preferredFrameRate;
p_pauseCount = 0;
p_group = new LinkedList<Sprite>();
}
/**
* Runnable.run thread method (MAIN LOOP)
*/
#Override
public void run() {
Log.d("Engine","Engine.run start");
ListIterator<Sprite> iter=null, iterA=null, iterB=null;
Timer frameTimer = new Timer();
int frameCount=0;
int frameRate=0;
long startTime=0;
long timeDiff=0;
while (p_running) {
// Process frame only if not paused
if (p_paused) continue;
// Calculate frame rate
frameCount++;
startTime = frameTimer.getElapsed();
if (frameTimer.stopwatch(1000)) {
frameRate = frameCount;
frameCount = 0;
//reset touch input count
p_numPoints = 0;
}
// Call abstract update method in sub-class
update();
/**
* Test for collisions in the sprite group.
* Note that this takes place outside of rendering.
*/
iterA = p_group.listIterator();
while (iterA.hasNext()) {
Sprite sprA = (Sprite)iterA.next();
if (!sprA.getAlive()) continue;
if (!sprA.getCollidable()) continue;
/*
* Improvement to prevent double collision testing
*/
if (sprA.getCollided())
continue; //skip to next iterator
//iterate the list again
iterB = p_group.listIterator();
while (iterB.hasNext()) {
Sprite sprB = (Sprite)iterB.next();
if (!sprB.getAlive()) continue;
if (!sprB.getCollidable()) continue;
/*
* Improvement to prevent double collision testing
*/
if (sprB.getCollided())
continue; //skip to next iterator
//do not collide with itself
if (sprA == sprB) continue;
/*
* Ignore sprites with the same ID? This is an important
* consideration. Decide if your game requires it or not.
*/
if (sprA.getIdentifier() == sprB.getIdentifier())
continue;
if (collisionCheck(sprA, sprB)) {
sprA.setCollided(true);
sprA.setOffender(sprB);
sprB.setCollided(true);
sprB.setOffender(sprA);
break; //exit while
}
}
}
// begin drawing
if (beginDrawing()) {
// Call abstract draw method in sub-class
draw();
/**
* Draw the group entities with transforms
*/
iter = p_group.listIterator();
while (iter.hasNext()) {
Sprite spr = (Sprite)iter.next();
if (spr.getAlive()) {
spr.animate();
spr.draw();
}
}
/**
* Print some engine debug info.
*/
int x = p_canvas.getWidth()-150;
p_canvas.drawText("ENGINE", x, 20, p_paintFont);
p_canvas.drawText(toString(frameRate) + " FPS", x, 40,
p_paintFont);
p_canvas.drawText("Pauses: " + toString(p_pauseCount),
x, 60, p_paintFont);
// done drawing
endDrawing();
}
/*
* Do some cleanup: collision notification, removing
* 'dead' sprites from the list.
*/
iter = p_group.listIterator();
Sprite spr = null;
while (iter.hasNext()) {
spr = (Sprite)iter.next();
//remove from list if flagged
if (!spr.getAlive()) {
iter.remove();
continue;
}
//is collision enabled for this sprite?
if (spr.getCollidable()) {
//has this sprite collided with anything?
if (spr.getCollided()) {
//is the target a valid object?
if (spr.getOffender() != null) {
/*
* External func call: notify game of collision
* (with validated offender)
*/
collision(spr);
//reset offender
spr.setOffender(null);
}
//reset collided state
spr.setCollided(false);
}
}
}
// Calculate frame update time and sleep if necessary
timeDiff = frameTimer.getElapsed() - startTime;
long updatePeriod = p_sleepTime - timeDiff;
if (updatePeriod > 0) {
try {
Thread.sleep( updatePeriod ); // i notice this is called when frames are low
}
catch(InterruptedException e) {}
}
}//while
Log.d("Engine","Engine.run end");
System.exit(RESULT_OK);
}
public void setFrameRate(int rate) {
p_preferredFrameRate = rate;
p_sleepTime = 1000 / p_preferredFrameRate;
}
So this is not the entire engine, but it is everything that deals with the frames Why is it randomly dropping? What I am noticing that thread.sleep is being called when the frames drop below 15. I am using a nexus 5 to test this running on android 4.4.2, and My question here is How do I stop the frames from dropping that low and having it sleep especially after calling a recreate(); method when you lose the game?

Just take out the Thread.Sleep statement altogether. Or just pass "0" into the Sleep call (if you are trying to yield cycles to other threads and processes). On each cycle of the run() loop, calculate how much time actually elapsed and update your simulation based on this value. In other words, don't try to force a hard-coded frame rate.
The device has an upperbound on frame rate (60fps on my device). So you'll likely be sleeping a little during each loop anyway.

Related

Graph plotter with timer in java

I'm working on a grapher that gets an expression like x^2 + t from the user and then will ask the user for range of x and t .
t in here is the timer variable.
so in x^2 + t the user for example will choose the -10 to 10 for x and 1 to 5 for t . now by clicking the draw button in GUI program the code will start plotting the expression from minimum t (1 in here) and after each second(or any time period) will increase the t value by one and draw the expression with new t (2 ,3 until it reaches the maximum range).
how can make the event handler to do this? I have found a way to draw multiple graphs but I can't make a delay so the minimum to maximum .
I know I should use timer but I don't know how to use in this part of the code
the Link for the whole code
this is the part of code in plotter class that should be changed :
// Grapher
drawButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String testSpring = null;
String tVarString = null;
for (int i = 0; i < 5; i++) {
testSpring = inputExpression;
tVarString = String.valueOf(i);
testSpring = testSpring.replaceAll("t", tVarString);
Equation equation = new Equation(testSpring);
graph.addEquation(equation);
}
}
catch (EquationSyntaxException e) {
e.printStackTrace();
}
}
});
This is the picture of the program :
my priority is to make the program run just by clicking draw button
but It would be better if this timer could be influence JSlider
so the min and max of t would be min and max of Jslider and by clicking draw it would start drawing by every time slider knob would point at a value for t
Take a look at How to use Swing Timers
This will allow you to setup a callback at a regular interval, which is executed from within the context of the EDT, making it safe to update the UI from within
public void actionPerformed(ActionEvent arg0) {
Timer timer = new Timer(1000, new ActionListener() {
private int iteration;
#Override
public void adtionPerformed(ActionEvent evt) {
try {
String testSpring = null;
String tVarString = null;
testSpring = inputExpression;
tVarString = String.valueOf(iteration);
testSpring = testSpring.replaceAll("t", tVarString);
Equation equation = new Equation(testSpring);
graph.addEquation(equation);
} catch (EquationSyntaxException e) {
e.printStackTrace();
} finally {
iteration++
if (iteration > 4) {
((Timer)evt.getSource()).stop();
}
}
}
});
timer.start();
}
});

GameLoop, What To Put For Delta?

I was just wondering for a quick question, what do you put in for delta exactly. The parameter double delta is (as the developer stated, in seconds of logic updates). If I wanted the loop to run 20 times a second, would I have it set to .2 or something like that? I am a bit confused on the logic updates (in seconds) part.
Anyways if you want to check any more of the game loops provided then check out the page here http://entropyinteractive.com/2011/02/game-engine-design-the-game-loop/
public abstract class GameLoop
{
private boolean runFlag = false;
/**
* Begin the game loop
* #param delta time between logic updates (in seconds)
*/
public void run(double delta)
{
runFlag = true;
startup();
// convert the time to seconds
double nextTime = (double)System.nanoTime() / 1000000000.0;
while(runFlag)
{
// convert the time to seconds
double currTime = (double)System.nanoTime() / 1000000000.0;
if(currTime >= nextTime)
{
// assign the time for the next update
nextTime += delta;
update();
draw();
}
else
{
// calculate the time to sleep
int sleepTime = (int)(1000.0 * (nextTime - currTime));
// sanity check
if(sleepTime > 0)
{
// sleep until the next update
try
{
Thread.sleep(sleepTime);
}
catch(InterruptedException e)
{
// do nothing
}
}
}
}
shutdown();
}
public void stop()
{
runFlag = false;
}
public abstract void startup();
public abstract void shutdown();
public abstract void update();
public abstract void draw();
}
You input the time in milliseconds that a single logic-loop should be based on.
If you want 20 times per second then 1/20 second is the number which is not 0.2 but 0.05.
You can write it more intuitively (IMO) by writing "1.0 / 20" then you don't have to convert back and forth and can just replace 20 with the frequency.

Javafx - Syncronization delay - card flips

I'm actually working on a memory game and I'm stuck at the point where I should write the gameplay-part of the game.
So:
I have an array of N card objects. Each object has an attribute called cardNum - an identifier. I think I should write an actionListener on that array, so when I flip a card, it puts the flipped card's cardNum in an array of two elements and if the two elements of the array are equal, a pair is found.
The problem is that I just don't know how to get the last flipped card's cardNum.
Any help would be appreciated.
Here's the way I tried:
private void easyGame(Card[] cards) {
int flippedCards = 0;
int card1;
while(flippedCards != 24) {
for(int i=0; i<cards.length; i++) {
if(cards[i].getIsFlipped())
flippedCards ++;
}
if(flippedCards % 2 == 0 && flippedCards > 0)
for(int i=0; i<cards.length; i++) {
card1 = getCardIndByCardNum(cards[i].getCardNum(), cards, i);
if(!cards[card1].getIsFlipped()) {
for(int j=0; j<cards.length; j++) {
if(cards[i].getIsFlipped())
cards[i].flip();
}
flippedCards = 0;
break;
}
}
}
}
The problem is that if I call this method, the game won't be drawn. May I use use threads somehow?
EDIT
Here is how I get the indexes of the clicked cards, and I call it in the UI:
private void setCardHandlers() {
for(final Card card : cards) {
card.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
clickedCardInd = getChildren().indexOf(card)-1;
}
});
}
}
Than here is how I am using it:
setOnMouseReleased(new EventHandler<MouseEvent> () {
#Override
public void handle(MouseEvent t) {
int cardIndex = clickedCardInd; // get index of what user clicked
clickedCardInd = -1;
if (cardIndex != -1 && moveRequestedFlag) { // our controller waits for the move
// get the position and report
moveRequestedFlag = false; // we handled the move
//System.out.println(cardIndex);
nextMove.setMove(cardIndex); // this will unblock controller's thread
}
}
});
It has a delay on fliping cards, also in the easyGame the requestMove method sets both indexes to the same.
I would recommend splitting you responsibilities a bit into Model/View/Controller modules, which, in simplest case would look like :
Model - your game current state and data, i.e. cards array Cards mCards = new Cards[24];
View - your UI, that can reflect current state of mCards(model) on screen in Main thread
Controller - your main game logic. This is most complex part, responsible for
requesting/handling user move,
updating mCards(model) based on user move,
Requesting UI to re-draw.
Contoroller's code (easyGame method) should run on separate thread to not block the UI.
Below I sketched a skeleton code that should fit your requirements :
class Game {
/*
* controller - main logic
*/
void startEasyGame() {
// initialize cards array, shuffle if needed
// we start with zero cards flipped
int flippedCards = 0;
// main loop
while (flippedCards != mCards.length) {
// 1. show updated UI
mBoard.showUpdatedCards();
// 2. request player move
// and block current thread to wait till move is done
// the result of the move - index of the card
int index1 = requestMove();
// temporarily flip first card face-up
mCards[index1].flip();
// show it on screen
mBoard.showUpdatedCards();
// same for second card
int index2 = requestMove();
mCards[index2].flip();
mBoard.showUpdatedCards();
// 3. check the result
if (mCards[index1].getCardNum() == mCards[index2].getCardNum()) {
// hooray, correct guess, update count
// possibly show some encouraging feedback to user
flippedCards += 2;
} else {
// incorrect, flip cards back face down
mCards[index1].flip();
mCards[index2].flip();
}
} // end of while loop
// game ended -> show score and time
mBoard.showResult();
}
}
EDIT
Extra details on how to await for result from UI thread :
int requestMove() {
// 1. show user prompt to make a move
// ...
// 2. construct latch to wait for move done on UI thread
mBoard.moveRequestedFlag = true;
NextMove nextMove = new NextMove();
mBoard.nextMove = nextMove;
// 3. await for move and get the result
return nextMove.getMove();
}
then, somewhere in UI code :
// handling card onClick somewhere on UI thread
if (mBoard.moveRequestedFlag) { // our controller waits for the move
// get the position and report
int cardIndex = ... // get index of what user clicked
mBoard.moveReqestedFlag = false; // we handled the move
mBoard.nextMove.setMove(cardIndex); // this will unblock controller's thread
}
and NextMove utility class to sync threads :
public class NextMove {
private volatile int mCardIndex;
private final CountDownLatch mMoveReady = new CountDownLatch(1);
public int getMove() throws InterruptedException {
mMoveReady.await();
return mCardIndex;
}
public synchronized void setMove(int selectedCardIndex) {
if (mMoveReady.getCount() > 0) {
mCardIndex = selectedCardIndex;
mMoveReady.countDown();
}
}
}

Reasons for my drawing thread to suddenly stop?

#Override
public void run() {
super.run();
final float timePerFrame = 1000 / Options.MAX_FPS;
float timeDiff;
while(!isInterrupted() && isRunning) {
timeDiff = System.currentTimeMillis();
mController.refresh();
timeDiff = timePerFrame - (timeDiff - System.currentTimeMillis());
try {
Thread.sleep(Math.max(Math.round(timeDiff), 0));
} catch(InterruptedException e) {
// do nothing
e.printStackTrace();
}
}
Log.e("GameThread", "Thread dead.");
}
This is what my thread is basically doing.
Options.MAX_FPS = 30;
To limit the frames per second, to have some sort of sleeping time between each refresh. The refresh method looks like this:
public synchronized void refresh() {
int mRotation = Math.round((360 + ((-1)*mAccelerometer[0] * 4.5f)) % 360);
mObject.setRotation(mRotation);
// get canvas from surfaceview
if(mSurfaceHolder != null) {
// do all calculations.
if(Looper.myLooper() == Looper.getMainLooper()) {
log("on main thread!");
} else {
log("Not on main thread");
}
Canvas mCanvas = mSurfaceHolder.lockCanvas();
if (mCanvas != null) {
// shift x offset
mScreenWidth = mCanvas.getWidth();
mScreenHeight = mCanvas.getHeight();
// draw black background, clear everything.
mCanvas.drawRect(new Rect(0,0,mScreenWidth, mScreenHeight), mBlackPaint);
// shift & draw all viewobjects if still visible
for(ViewObject view: mViewObjectList.toArray(new ViewObject[mViewObjectList.size()])) {
view.shiftX(-1 * Options.Gameplay.Environment.MOVING_SPEED);
if(view.getX() + view.getWidth() < 0) {
mViewObjectList.remove(view);
}
if(view.getCurrentBitmap() != null)
mCanvas.drawBitmap(view.getCurrentBitmap(), new Rect(0,0,view.getCurrentBitmap().getWidth(), view.getCurrentBitmap().getHeight()), new Rect(view.getX(), view.getY(), view.getX()+ view.getWidth(), view.getY()+view.getHeight()), new Paint());
view.nextFrame();
}
// draw object
final Bitmap sBitmap = mObject.getCurrentBitmap();
mObject.nextFrame();
if(sBitmap != null) {
mCanvas.drawBitmap(sBitmap, new Rect(0, 0, sBitmap.getWidth(), sBitmap.getHeight()), new Rect(mObject.getX(), mObject.getY(), sBitmap.getWidth(), sBitmap.getHeight()), new Paint());
} else log("bitmap = null!");
}
mSurfaceHolder.unlockCanvasAndPost(mCanvas);
}
}
The thread keeps running for some time, background elements are being drawn (even though my rotation does not work quite yet, but that's another story..), the background "seems" to move (like a side-scroller), but at some random point in time, without any ADB LogCat messages (not limited to the App but the whole LogCat output) - the thread simply stops. No more drawing. Nothing. I am not calling interrupt or setting isRunning to false, as the "Thread dead." message is not written into LogCat as well..
I do not know what's happening.
Thanks for your help.
You have:
timeDiff = System.currentTimeMillis();
// refresh, which takes some time I guess
timeDiff = timePerFrame - (timeDiff - System.currentTimeMillis());
so System.currentTimeMillis() will be later and therefore bigger than timeDiff. The term in brackets will be negative - so you will be adding onto the timePerFrame and timeDiff will grow not reduce.

Smooth movement in Java

I'm making a simulation in a 3D environment. So far, I have the movements of all the creatures, but it is not "smooth". I've tried quite a few things but was horribly wrong. Now I just have no idea what to do. I was thinking of implementing a vector (not vector class) but don't really know how.
import env3d.EnvObject;
import java.util.ArrayList;
abstract public class Creature extends EnvObject
{
/**
* Constructor for objects of class Creature
*/
public Creature(double x, double y, double z)
{
setX(x);
setY(y);
setZ(z);
setScale(1);
}
public void move(ArrayList<Creature> creatures, ArrayList<Creature> dead_creatures)
{
double rand = Math.random();
if (rand < 0.25) {
setX(getX()+getScale());
setRotateY(90);
} else if (rand < 0.5) {
setX(getX()-getScale());
setRotateY(270);
} else if (rand < 0.75) {
setZ(getZ()+getScale());
setRotateY(0);
} else if (rand < 1) {
setZ(getZ()-getScale());
setRotateY(180);
}
if (getX() < getScale()) setX(getScale());
if (getX() > 50-getScale()) setX(50 - getScale());
if (getZ() < getScale()) setZ(getScale());
if (getZ() > 50-getScale()) setZ(50 - getScale());
// collision detection
if (this instanceof Fox) {
for (Creature c : creatures) {
if (c.distance(this) < c.getScale()+this.getScale() && c instanceof Tux) {
dead_creatures.add(c);
}
}
}
}
}
import env3d.Env;
import java.util.ArrayList;
/**
* A predator and prey simulation. Fox is the predator and Tux is the prey.
*/
public class Game
{
private Env env;
private boolean finished;
private ArrayList<Creature> creatures;
/**
* Constructor for the Game class. It sets up the foxes and tuxes.
*/
public Game()
{
// we use a separate ArrayList to keep track of each animal.
// our room is 50 x 50.
creatures = new ArrayList<Creature>();
for (int i = 0; i < 55; i++) {
if (i < 5) {
creatures.add(new Fox((int)(Math.random()*48)+1, 1, (int)(Math.random()*48)+1));
} else {
creatures.add(new Tux((int)(Math.random()*48)+1, 1, (int)(Math.random()*48)+1));
}
}
}
/**
* Play the game
*/
public void play()
{
finished = false;
// Create the new environment. Must be done in the same
// method as the game loop
env = new Env();
// Make the room 50 x 50.
env.setRoom(new Room());
// Add all the animals into to the environment for display
for (Creature c : creatures) {
env.addObject(c);
}
// Sets up the camera
env.setCameraXYZ(25, 50, 55);
env.setCameraPitch(-63);
// Turn off the default controls
env.setDefaultControl(false);
// A list to keep track of dead tuxes.
ArrayList<Creature> dead_creatures = new ArrayList<Creature>();
// The main game loop
while (!finished) {
if (env.getKey() == 1) {
finished = true;
}
// Move each fox and tux.
for (Creature c : creatures) {
c.move(creatures, dead_creatures);
}
// Clean up of the dead tuxes.
for (Creature c : dead_creatures) {
env.removeObject(c);
creatures.remove(c);
}
// we clear the ArrayList for the next loop. We could create a new one
// every loop but that would be very inefficient.
dead_creatures.clear();
// Update display
env.advanceOneFrame();
}
// Just a little clean up
env.exit();
}
/**
* Main method to launch the program.
*/
public static void main(String args[]) {
(new Game()).play();
}
}
You haven't shown enough of your program. Basically, if you want animation to be smooth, and you want to do it yourself (as opposed to using JavaFX or something), then you need to do lots of inter-frames. So rather than advancing an entire timer tick, advance a 10th of a timer tick, move everything on a screen a tiny bit, and then advance again. You should have the background redraw happening every 10th of a second for smooth animation.
As vy32 mentioned, we need to see more of your code. But it looks like you are missing timing code.
What you probably want to do is check the time each iteration of your game loop and then sleep for a certain amount of time to achieve some desired frame rate. Otherwise your game loop will run hundreds of thousands of times a second.
Alternatively, you should be advancing your creatures by a distance that is proportional to the amount of time that has elapsed since the previous frame.
Here is an example of a very simple regulated loop ("fps" is the desired framerate):
private long frameLength = 1000000000 / fps;
public void run() {
long ns = System.nanoTime();
while (!finished) {
//Do one frame of work
step();
//Wait until the time for this frame has elapsed
try {
ns += frameLength;
Thread.sleep(Math.max(0, (ns - System.nanoTime())/10000000));
} catch (InterruptedException e) {
break;
}
}
}
It should be very easy to retrofit this into your game loop.

Categories

Resources