Finally had time and motivation today to get back to my old OpenGL project. Today I wanted to add a “win” and “lose” screen and just tidy up the Screens (which are really states) in general.
For some reason I had a stack in main where I would push screens onto if I needed to change. However I wanted the screen flow to be like this: “Main Menu” -> “Game” -> “Lose” -> “Main Menu”. So I didn’t want to put the lose screen over the game screen in the stack since I didn’t want to return to the game screen when the lose screen was done.
I figured that it made much more sense to just have a pointer to the current screen.
The way I currently change screens is by analysing the return value of the update function. If the return value is the current screen I do nothing, if the return value is null then the game loop ends. If the return value is a different screen then I delete the old current screen and replace it with the screen that was returned. (Why did I not do this in the first place?)
So, I added the Win and Lose screens – which was a trivial task, they looked almost identical to the MainMenu screen.
So much so, in fact, that I want to refactor the Screens a bit next time. I want to move the common code (so far just the calls to set the projection matrix in the shaders) down into the base Screen class. I also thought it would be a good idea to have some sort of list of Sprites and Texts in the base Screen class. Then the Draw function can just loop through them all and draw them (maybe even more ideally they would have an ordering I can specify so things will be drawn in the correct order).
Also, I’m currently not cleaning up a single thing, so I have memory leaks all over the place. I will add a Cleanup function to all Screens so that I can call it before I delete the Screen itself in the main loop.
The setup of each Text also seems convoluted and repetitive, but I’m not sure I want to change that for this project. Perhaps I will just iterate on it for the next project I’m planning.
The code of my project is located here – for those who’d like to look at what I’ve changed.
For the next project I’d like to experiment a bit more with the Component system. I think I will take inspiration from Unity’s GameObject and try to emulate that in my own engine. This was suggested to me last year when I started this project, but I lacked the experience and confidence to try it out. I now feel WAY more confident (and even excited) about piecing together a component system on my own – so I’m pretty happy with how my knowledge has progressed from last year.