GameLoop Thread
thezaync opened this issue · 2 comments
thezaync commented
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
There are several things wrong:
- I think you are not enabling vsync (it gets a 0).
- Even if you enable it, you are not drawing anything so vsync will do nothing. You can try erasing the colour buffer or something.
- Execute everything in the main thread. There is no need to create a new thread (I've updated first chapters recently with these changes). If you chose to use another Thread, you must be on Windows, and initialize OpenGL in that thread (you are doing it right now in the main Thread).
Hope it helps.
thezaync commented
Thank you.