Blitz2D Intermediates: Speed Optimization
by Maher Farag (Sphinx)

Introduction :

At first forgive me for my bad English as Arabic is my mother-tongue. Also I am not good at writing so prepare yourself to solve some puzzles ;) Even though Blitz is our language and we all understand it with ease. This article is aimed to help you optimize your code for speed and again do not expect some algorithms to make your code runs faster or to help you some AI problems, though, it will give/teach some tricks to optimize the speed of your code, so here we go:

1- Use variables of type Integer :

As we all know Blitz has three types of variables: Integers, Float and String. Try using Integers as much as you can because is the fastest in processing especially in calculations.

2- Use constants instead of variables :

That applies only for values that you will never alter during game execution, as constants are much faster than variables and Blitz does not need to look up for its value as it knows it already. For example it is better to make PI=3.14 a constant not a variable.

3- Do not use Text command :

Instead try using bitmap fonts to display messages for the user as Text command inside the main game loop affects the game speed dramatically.

4- Display Color Depth :

Using 16 bit color depth for your graphics mode is much faster than 24bits and 32bits.


5- Use Rect instead of Line :

Believe it or not Rect is much much faster at drawing Horizontal and vertical lines than Line command itself.

6- Do not use GetColor :

Using Getcolor inside game loop will slow it down, instead try using ReadPixel , though you will need to calculate the color manually.

7- No need for CLS :

If you are going to DrawBlock/TileBlock the background then no need to use CLS; it is useless in this situation.

8- Pre-calculate :

Well, if you are going to rotate or resize an image then it is much faster to do that outside the main game loop and save the rotated/resized images in an ,say, array and then use this array handle inside the game loop. The same applies for pre-calculating Cos/Sin values to make attack waves in your game, as this will slow the game down if you calculate it in real time.

9- Save your self some Function calls :

If you are going to use a Blitz intrinsic function like ImageHeight() and ImageWidth()...etc in loops then it is better to assign it to a variable and then use the variable in your loops instead, well look at this :


The following code is faster:


10- Divide your formulas :

In my game Pharaohs' Curse I am using the following code to get the current tile where the player is located :


The following code is faster (especially when you have many variables which stores the tile located to the left, right, up, down , top-middle, bottom-middle....etc of the player)


11- Un-necessary Math Functions :

Well, look at this 


that is faster:


12- Change between 0 and 1 :

If a variable is going to be changed between 0 and one then you better use :


instead of 


Same applies to Boolean variable as you can use: bVariable= Not bVariable

13- Counting :

If you have a variable which needs to be iterated from 1 to ,say, 20 then it is better to use :


instead of


14- Loop Step :

If you have a loop like the following :


Try use the following


15- Use conditions wisely :


Try the following, it is faster :


16- Join conditions :

From the previous example, the following is faster


17- Un-necessary If statements :

Look at this :


It is better and faster to use :


18- Select Insted of Ifs :

Try using Select instead of Ifs as it is better, faster and more readable


Instead


19- Gosub instead of Functions :

Using Gosub is faster than using Functions even though I sacrifice speed for more readable code as Function make it easy to read your code.

20- Do not use Goto :

As much as you can try avoid using Goto it is notoriously known of causing bugs and unstructured code (Pascal programmers, are you there?!!).

Lastly :

Well, I hope this help you gain some tricks to make your game runs much faster, till we meet again with the next article where I will explain how to write your real time REPLAY feature for you game, I have implemented this cool feature in my game Pharaohs' Curse which helps you see the computer solve a given level/stage for you.

 

Well, Bye for now.

Maher Farag

http://www.ancientsoft.com

 


For a printable copy of this article, please click HERE.


This site is CopyrightŠ 2000-2004, BlitzCoder. All rights reserved.