Skip to content

MainGameLoop

ivanorsolic edited this page Apr 17, 2016 · 3 revisions

This one is pretty much straightforward. This is a class that tests the engine. #First commit Inside we can find these methods :

DisplayManager.createDisplay();
DisplayManager.updateDisplay();
DisplayManager.closeDisplay();

which are documented at the DisplayManager page.

while(!Display.isCloseRequested()){
    DisplayManager.updateDisplay();
}

This is the main game loop. This is where all of the objects will be displayed, where the rendering will be taking place, etc. The Display.isCloseRequested checks whether the user has pressed X in the top right corner of the Display, and if so, it is set to true. So in our while loop, we loop until the Display.isCloseRequested is set to true, and then we close the display.

#Update 2 Changelog:

  • Added Loader, Renderer and RawModel class
  • Implemented the new methods into the main game loop

We are adding the renderer and the loader into the main game loop so we've created two new objects:

Loader loader = new Loader();
Renderer renderer = new Renderer();

We added a cleanup method to clean the loader of all the VBOs and VAOs we've created (see the Loader class for more info):

loader.cleanup();

We added a method into the main loop to prepare the renderer every single frame:

renderer.prepare()

We load vertices to a RawModel using:

RawModel model = loader.loadToVAO(vertices);

And we render it with:

renderer.render(model);

##For quick reference, here is the full code with some comments:

package engineTester;

import org.lwjgl.opengl.Display;

import renderEngine.DisplayManager;
import renderEngine.Loader;
import renderEngine.RawModel;
import renderEngine.Renderer;

//Testing Class
public class MainGameLoop {

	public static void main(String[] args) {
		
		//Opening the display using a method from DisplayManager
		DisplayManager.createDisplay();
		
		Loader loader = new Loader();
		Renderer renderer = new Renderer();
		float[] vertices = {
				  -0.5f, 0.5f, 0,
				  -0.5f, -0.5f, 0,
				  0.5f, -0.5f, 0,
				  0.5f, 0.5f, 0f
				};
				  
		int[] indices = {
				  0,1,3,
				  3,1,2
				};
		//game loop - this is where all the objects are displayed
		//where all the rendering is happening, etc.
		//!Display.isCloseRequested <-- run until the user presses X
		RawModel model = loader.loadToVAO(vertices, indices);
		while(!Display.isCloseRequested()){
			renderer.prepare();
			//game logic
			//render
			renderer.render(model);
			
			//update the display every frame
			DisplayManager.updateDisplay();
			
		}
		
		loader.cleanUp();
		//closing the display
		DisplayManager.closeDisplay();
	}

}

Clone this wiki locally