This assignment was mostly about learning how Lua works and how we can use it for our projects. The other minor requirement was that we had to modify the graphics code to render 2 triangles on the screen instead of one.
One of the first things I noticed when going through the examples provided by JP was the use of gotos for error handling. I had been under the impression that gotos are always bad and should never be used. After looking at the code samples, this led me to do some research of my own and I learned that carefully using goto this way to cleanly exit on error leads to much cleaner code. (I would like to post some code here but apparently EAE blogs system doesn’t allow us to install plugins)
Lets talk about Lua
The most significant aspect of this assignment was learning how Lua and the C API for Lua works. We touched on Lua in the last semester where we used it to store only very basic data. At the time I felt that it was rushed and there was no proper technique of integrating it into the engine. This time we are using it to run our AssetBuilder.
The Lua C Api is a strange animal. It uses a abstract stack to pass data (and function calls) between Lua and C. The API gives you full control over the stack, this means there are no hand rails and you have to be careful how you manipulate the stack. If you accidentally push one extra value from C and forget to tell Lua about it, bad things will happen. I did find this cumbersome and was looking at alternatives. More on that later.
The AssetBuilder now only provides helper functions to Lua, while a Lua script called BuildAssets.lua contains all the logic. I can see the benefit of doing this. This allows designers/artists to author build processes for assets in Lua without having to make changes to the C++ code. Additionally it helps us keep the AssetBuilder as a “tool” that the scripts can use to do what they want. I appreciate that our first foray with Lua in this class has been used to do something useful.
The BuildAssets.lua lives in a separate “Scripts” directory because its neither compile-able code nor an asset.
The Lua C API isn’t exactly user friendly. So I started searching for C++ Lua binding libraries out there. Apparently there are a lot. One of the more modern and nice ones that I found is Selene. It makes it fairly easy and convinient to bind C/C++ to Lua. If we end up using Lua extensively in this class (or if I just want to use it to do more stuff) I might try out Selene.
Drawing two triangles
The second part of this assignment was to modify the graphics code to draw two triangles instead of one. It took a while to find out what needed to be changed, but the changes themselves were trivial. The vertices for the first triangle were hardcoded, as well as teh size of the vertex buffer. So all we had to do was hardcode another triangle and increase the size of the buffer. We had to do this separately for OpenGL and Direct3D, so had to make sure we get the vertices in the right order.
You can download it here.
Press Esc to exit.