Code optimization
|
Welcome dear readers! This week I’m going to talk about optimization which is highly necessary but is usually overlooked by us unschooled programmers. I will optimize our code structure by removing unnecessary code strips, moving code for more efficient reading from our program as well as reading and understanding from the coder. Our group have only inexperienced programmers so even though we have tried to make an efficient engine we ended up with several hiccups and since we had time pressure to finish certain things in time we decided to make everything in a simple and “working” manner. We also used classes to handle behaviors for our objects which was at most well programming we were capable off at the time so our code structure turned into a messy program which worked. But this turned into a big problem when we wanted to change states and add a menu state since we had coded everything in main. So I started by creating my window where I displayed my game using the render window function from the SFML library in main and added a while running loop for the game then I added implemented our state manager another member in my group had made. With the help of the state managers set state function I set the state to game state inside my main. Now I moved all game code inside main to the game state where I had split them up like our sdl project using a constructor, deconstructor, Enter, Exit, Update, Draw, Set State and check collision. Everything before the while loop in main ended up inside the Enter or the variables inside the header. Everything that should be drawn I set inside the draw function and my collision check was moved inside check collision and then used inside update. All behaviors or changes occurring inside the game was set inside the update function and by returning false and setting the menu state as my next state got sent to the menu. Now we will be able to read the code more efficiently and find errors easier by going to the specific function instead of scrolling through main. My biggest problem with setting different states was to send the window to my states which I had to use a pointer on the window and use it as a parameter inside the constructor and send it on when I set my state. See you next week readers! |
