Does this opengl render loop suck?

public class Example {
	
	public Example() {
		//do some stuff for initilization
		GLCanvas canvas = new GLCanvas(/*Parameters*/);

		initScene();
		set2dRendering();
        	display.asyncExec(new Runnable() {
	       		 public void run() {
	    			if (!canvas.isDisposed()) {
	    				canvas.setCurrent();
	    				try {
	    					GLContext.useContext(canvas);
	    	    		 	} catch(LWJGLException e) { e.printStackTrace(); }
	    				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); //clears the last rendering and does a new one
	    				GL11.glLoadIdentity();
	    				render();
	    				canvas.swapBuffers();
	    				display.asyncExec(this);
	    			}
	        	}
        	});
		
	}
}
	

This is a hacked down version of the loop from Snippet95.java (a code snipet from www.eclipse.org). This loop seems incredibly greedy. Is this really the right (best) way to do this?

two things come to mind

  • you’re doing an awfull lot in the constructor - I personally delegate stuff to an init method. Constructors failing can exhibit strange behaviour
  • the render loop doesn’t yield or pause - might slab in a Display.sync(60);

also you shouldn’t have to set/use context each loop - once for the thread instance should be fine

Awesome! That Display.sync(60) was what I was looking for, thanks a ton.