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!Optimization

One comment

  1. karlmalm · March 8, 2016

    Right, time for this week’s comment! You describe a very useful way of making your code more readable and less cluttered, but I am missing a few details. Your structure resembles the one we see from our earlier project as you mentioned, but I would still have liked to see that structure detailed in code (the image works, but is a bit abstracted from what I’d like to see).

    I am also entirely confused by this sentence: “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” I think you mean something like “Our usage of classes to handle object behaviour was an example of good programming practise we were capable of producing at the time, resulting in a program that worked but which was messy in terms of structure.”

    Something that is missing and is possibly the most important thing I want to see is the WHY of the week’s work. Why is it more efficient to split the program into several chunks, why is it better to not write everything in main (I can guess, but I’d rather see it explained)? Removing unneeded code is self-explanatory, but the rest is not. More efficient reading from the program, does that mean the program compiles faster or that it executes certain code faster when certain functions are called? Why?

    Since the entire topic of the post is optimization, I would also have liked to see a few examples of just how much faster it works when compared to the code you used before. Not in percentages or seconds it takes (though that certainly would have been impressive), but a flow chart or image that showed how much code was loaded before each time the game updated, and how much less work needed to be done when you got finished optimizing. Or even a screencap of the amount of RAM used before optimization to after, if it was a significant gain.

    Like

Leave a comment