Getting Started example Loop confusion

Started by T1663R, April 14, 2016, 17:43:14

Previous topic - Next topic

T1663R

Hey there,

so ive used to work quite a lot with libdx and got quite some nice results with it , but always felt it was "kinda overloaded" and wanted to try something else.
(i also used to code with Spritekit / Swift alot)
Now after a long coding break i just looked at lwjgl for the first time.
I did take the example from :
https://www.lwjgl.org/guide

Now im kinda confused with the way the loop is set up in the example app.
In libdx / spritekit you kinda get presented with your loop and mostly just have to "add your stuff".

I can think of many ways to do my loop.
Here the Loop method itself isnt really "Looping", you only have a while loop in there that does some stuff.
Which feels kinda weird.
On top of that, doesnt it make sense to kinda seperate your Drawing stuff from your Logic stuff ?

Now ofc i could do something like this :
    private void loop() {
        GL.createCapabilities();
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

        while ( glfwWindowShouldClose(window) == GLFW_FALSE ) {
            
            doLogicLoop();            

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
            glfwSwapBuffers(window); // swap the color buffers
            glfwPollEvents();
        }
    }


Yet im not really happy with this.
Now my question is, why is it set up this way ? Or did i overlook something ?
Or is it just cuz this is an example ?

please bare with me that my coding skills are somewhat rusty.
Thanks in advance :)

Cornix

Its set up this way because: How else would you do it?

You have to do anything. Its not like you can tell the computer to "maybe do something sometime". Your loop will usually:
1) Poll any events coming from the OS (inputs, etc)
2) Update the program logic / react to OS events
3) Rendering everything to screen
4) Put the thread to sleep for a smooth frame rate

This is how its done because this is the way its supposed to be done. Somebody decided to make it this way some time in the past.

If you want to separate your program logic from rendering you need to do this yourself. You can try to put it into a separate thread and do the necessary synchronization. I wouldnt recommend this for smaller projects though. The overhead of multi threading is most often much worse then any possible gains.

T1663R

Im fully aware of that.
Maybe i kinda asked wrong - sorry english is not my native language.

Kinda hard to explain what i mean.
But lets take your loop example, cuz it fits kinda nicely.

Try to apply your example loop to the code sniped loop in my post.
U would have to completely change it in order to have a clean seperation between your steps 1-4.

what i mean, wouldnt it be better to make the loop kinda like this :

    private void loop() {
            handleInput();            
            doLogic();
            render();
            other();
    }


and then let this loop beeing called repeatedly?
After i wrote the Thread i kinda realized that the question isnt the greatest, but i still was curios.
Sorry if my question is a little bit retarded, but it just kinda confused me.


EDIT
but yeah if i think about it it kinda makes sense that is set up that way, cuz the example isnt really supposed to do more.