0

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:

  1. When removing the second while loop, it loops correctly in the first loop. Which is obvious really.
  2. 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!

Dubstepzedd
  • 148
  • 1
  • 9
  • 2
    Can you explain what you actually see happening? Because "it doesn't work" is just an opinion, not a problem: the code does exactly what it should do because that's the only thing it can do, so: what did you want to do, what do you see it doing instead, and what have you already checked (with what results) before you decided to post here? – Mike 'Pomax' Kamermans Mar 26 '20 at 20:30
  • I'll add it to "What's the problem?"! Posted a bit fast, sorry! – Dubstepzedd Mar 26 '20 at 20:32
  • Edited it, is that enough info? @Mike'Pomax'Kamermans – Dubstepzedd Mar 26 '20 at 20:39
  • 3
    first because of integer division 1/60 == 0. So notProcessTime >= UPDATE_TIME should always be true, hence the loop never ends. The only reason i can imagine that it ends would be a floatingpoint underflow. Anyway, make UPDATE_TIME double and initialize it with `1/60.0` – k5_ Mar 26 '20 at 20:44
  • Why did I make it an integer... I am sorry for wasting your time. I totally didn't think of that. I copied most of the code but some parts I edited and this part got affected. Thanks for the help though! – Dubstepzedd Mar 26 '20 at 20:48
  • 1
    Not really related to your problem, but calling `Thread.run()` just calls the `run()` method of thread. It doesn't actually start a thread. For that you want `Thread.start()` – k5_ Mar 26 '20 at 21:11
  • I thought Thread.run() made it run on a single thread? And start on multiple? @k5_ – Dubstepzedd Mar 26 '20 at 22:04
  • 2
    @LiamAndersson `Thread.run()` acts like it's just calling a normal method and will wait until it's finished until moving on, meanwhile `Thread.start()` simultaneously runs both the code after `Thread.start()` and the code in the `run()` method. – Nosrep Mar 26 '20 at 22:31
  • @Nosrep Ah okay, thanks – Dubstepzedd Mar 26 '20 at 22:34

0 Answers0