Chapter 4-The Render Class
Because having everything in the main class is mind-numbingly painful.
So, how are we all feeling about our understanding of LWJGL? We feeling good, feeling sassy? Alright, let's get ready to move a chunk of the stuff we wrote out of the Boot class. Why?
Because you never, ever, EVER keep all of your code in the main class. When you're sorting Legos for a big build, do you just dump everything into a pile? No, it's hard as hell. You sort things to some meaningful degree. Yellows with yellows, minifigs with minifigs, and with different piles for different parts of the build.
I've given you a starting point by using packages. By now you've noticed that anything that involves the render code goes into the root render package or one of it's children. Now, we're going to be making a new class in this package named Render. Can you guess what it's going to be used for?
Before we get started, you'll need to import the following:
Now, right now this class is going to be very simple. Our engine is still in its infancy, and at the point where we can develop it into either a 2D or 3D game. To that end, we'll only need two methods: cleanup() and render(Mesh mesh). While we're at it, include a ShaderTextured in there too.
We've already talked about the glClear(...) method before. As a reminder, every render cycle we need to erase everything that was drawn before, otherwise it just draws the objects over the previous frame. The result winds up being something a little like this:
Now, next we're going to be copying the rendering code from our main game loop and dropping it into the render(...) method.
Once again, we start our shader, bind the VAO for our mesh, enable the position and texture attributes, and set the active texture. Everything's pretty much the same with one change: our Mesh class has a new method, getTexture(). So, let's go back to the Mesh class and see what's changed:
So, we've added a new integer for the Mesh class that stores the texture of the mesh. The reason we have the addTexture(...) return a Mesh after setting the texture is so we can simple set the texture in the same line of code as our initialization for the Mesh. Since we already know how the getTexture() method works from its name and the other methods in the class, let's go back to the Boot class.
That looks a LOT cleaner now, right? As I mentioned before, with the way the addTexture(...) method is set up, we can just add that to the end of our initialization for our mesh. With the Render class handling all of the render code now, we just need to tell it to cleanup() the last frame and render(...) the new one.
In the next chapter, we'll be further cleaning up our codebase by moving the GLFW code out of the Boot class.
Last updated