lwjglgamedev / lwjglbook-leg

Source code of the chapters of the book 3D Game Development with LWJGL 3
https://ahbejarano.gitbook.io/lwjglgamedev/
Apache License 2.0
560 stars 202 forks source link

GameLoop Thread #65

Closed thezaync closed 5 years ago

thezaync commented 5 years ago

When i run my thread with Thread.start() my screen not update

Edt1: Sorry my english Edt: I`m using Windows 10

public class Exemple02_GameLoop implements Runnable{

private long _windowId;
private static final short WIDTH = 800;
private static final short HEIGHT = 600;
private static final String WINDOW_TITLE = "Minha Janela";
private final Thread thread = new Thread(this,"Exemple02_GameLoop");

public static void main(String[] args) {
    Exemple02_GameLoop app = new Exemple02_GameLoop();
    app.initGLFW();
    app.createWindow(WIDTH,HEIGHT,WINDOW_TITLE);
    app.setVSync(true);
    app.start();

}

public void initGLFW() {
    if(!GLFW.glfwInit()) {
        System.err.println("Failed to inicialize GLFW!");
        System.exit(-1);
    }

    GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_FALSE);
    GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
}

public void createWindow(short width, short height, String windowTitle) {

    _windowId = GLFW.glfwCreateWindow(width, height, windowTitle, 0, 0);
    if(_windowId == 0) { System.err.println("Failed to create Window"); }

    GLFW.glfwMakeContextCurrent(_windowId);

    GL.createCapabilities();

    GLFW.glfwShowWindow(_windowId);

}

public void setVSync(boolean state) {

    byte interval = (byte) (state == true? 0:1);
    GLFW.glfwSwapInterval(interval);
    GLFW.glfwSwapBuffers(_windowId);
}

private synchronized void start() {
    thread.start();
}

@Override
public void run() {
    long lastLoopTime = System.nanoTime();
    final int TARGET_FPS = 60;
    final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
    long lastFpsTime = 0;
    while(!GLFW.glfwWindowShouldClose(_windowId)){
        long now = System.nanoTime();
        long updateLength = now - lastLoopTime;
        lastLoopTime = now;
        double delta = updateLength / ((double)OPTIMAL_TIME);

        lastFpsTime += updateLength;
        if(lastFpsTime >= 1000000000){
            lastFpsTime = 0;
        }

        render();
        update();
    }
    terminate();
}

private void render() {
    System.out.println("Renrering");
}

private void update() {
    GLFW.glfwPollEvents();
    GLFW.glfwSwapBuffers(_windowId);
}

private synchronized void terminate() {
    GLFW.glfwDestroyWindow(_windowId);
    GLFW.glfwTerminate();
}

}`

lwjglgamedev commented 5 years ago

There are several things wrong:

Hope it helps.

thezaync commented 5 years ago

Thank you.