I don't like posting these kind of problems. But this time I really don't know what happened.
I wrote this code on PC and it worked fine. However on my Macbook it doesn't work. It gets stuck in the second loop it seems like? Is there a way to prevent that? Is the Macbook not quick enough?
Code:
package com.mittspel;
public class Handler implements Runnable {
Thread thread;
boolean running = true;
boolean render = false;
int UPDATE_TIME = 1/60;
Handler() {
thread = new Thread(this);
thread.run();
}
public void run() {
double startTime = System.nanoTime() / Math.pow(10, 9);
double endTime = 0;
double passedTime = 0;
double notProcessedTime = 0; //So we dont skip frames.
double frameTime = 0;
int frames = 0;
int fps = 0;
while(running) {
render = false;
endTime = System.nanoTime() / Math.pow(10, 9);
passedTime = endTime - startTime;
frameTime += passedTime;
notProcessedTime += passedTime;
System.out.println("First loop");
do {
notProcessedTime -= UPDATE_TIME;
render = true;
frames++;
if(frameTime == 1.0) {
fps = frames;
System.out.println(fps);
frames = 0;
frameTime = 0;
fps = 0;
}
System.out.println("Second Loop");
}
while(notProcessedTime >= UPDATE_TIME);
if(render) {
//TODO: Render stuff
}
}
}
public void start() {
}
public static void main(String[] args) {
Handler handler = new Handler();
handler.start();
}
}
What's the problem? The code above is supposed to be a "beginning" to a game-loop. The first loop runs when the game is "running", while the second loop is for when the "notProcessedTime" exceeds 1/60. Why? Because I want the code there to update in 60fps. In the while loop I also remove the UPDATE_TIME from notProcessedTime, which short said means that I don't want the code to skip frames. Later in the second loop I want to print the fps. Which should be 60fps if it runs correctly.
What have I tried before posting?
I tried making some "System.out.println" in the loops and removed code. It gave me the following results:
- When removing the second while loop, it loops correctly in the first loop. Which is obvious really.
- When inserting "System.out.println" in both loops (as code shows above) I found that it only prints once in the first loop then only loops the second one to infinity.
I predict after debugging it myself that it gets stuck in the second while loop. As the code worked on PC, I wonder if it has something to do with hardware? I'm stuck and I need help. I have debugged it and can't come to any better conclusion than this.
I struggled with inserting the code above, sorry for that!