Wednesday, January 6, 2010

Intro to Allegro. [Part 1]

After the previous post of setting up allegro in your pc what you want to do now is start writing codes in allegro.

Before I even teach you what to code I must tell you that you should at least have some idea about programming and C language. If you don’t then you might want to refer to this.

Ok after all that let us get to Allegro 101 [Part 1].

Just like in C you start the allegro programming with the header file. But before even including <stdio.h> or <iostream> header files, allegro does not need the two basic header that we are so accustomed to. In fact allegro can do with just <allegro.h> header file. So the start of your program is going to be with this line:-

#include <allegro.h>

After that line depending on the complexity of your program either custom function can appear or the main function can be written. But since we are in 101 class let us start with the main function.

Your line normally should be:-

int main()

{

After that line you would need to start the allegro master function(no it’s not called that, I made that up). You would need to initialize allegro library. To do that you would do:-

allegro_init();

With that done what I always do just to make sure allegro library is up and running is test the simple program from allegro. Normally it goes something like this:-

allegro_message(“Hello world”);

return 0;

}

END_OF_MAIN();

Woah!! what is all that thing over there?? Don’t panic, the first line is the message box that allegro creates and displays “hello world” without the quote of course. The last line(macro) is something you need to declare because of the cross-platform compatibility. If forgetting to write that line could result in crashing of the program.

 

Now that you know that END_OF_MAIN(); is essential part of the allegro just as allegro_init() make yourself a note that you would never forget this part.

Let us go through something more in this 101 class shall we? Because “hello world” is just too dull to do anything. Let us import image into our allegro window.

I will provide you with code first, go through it and try to understand as much as possible then go through the explanation below.

#include <allegro.h>
int main()
{
         allegro_init(); 
         install_keyboard();
         set_color_depth(32);
         set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
         BITMAP *image;
         image = load_bitmap(“mypic.bmp”,NULL);
         blit(image,screen,0,0,0,0,image->w,image->h);
         read_key();
         destroy_bitmap(image);
         return 0;
}
END_OF_MAIN();

 

Ok by now you should know till allegro_init(). Now let’s move further down shall we?

The install_keyboard() function does what it says. It installs keyboard to your program so that you can make use of the keyboard. That was easy but let us move further down.

set_color_depth(32);

If you have any slight idea about how computers generate color in the screen then you would have no problem with this else you would scratch your head on what this even means. The number 32 isn’t given just randomly. In fact you can just write either 8,15,16,24,32. What it does is it sets the pixel format to your screen. Higher the number greater the detail can be shown in the image inside your allegro program. 8 bit is something you would play back in NES. Anyway it depends on how vast you want the users to be. 8 bit is almost supported by all while 32 might not.

set_gfx_mode(GFX_AUTODETECT,640,480,0,0);

After setting the color depth, you should tell the allegro what kind of driver you are using as a graphic device to allegro. Luckily you don’t have to manually put the graphic driver, allegro does that for you with GFX_AUTODETECT and 640 and 480 are the setting of the screen width and height (640 and 480 are chose because it is the standard monitor resolution of many and even if it isn’t I can guarantee that majority of computer supports this resolution even if they have 1920X1200 resolution). The later 0,0 was required for virtual screens but is not required anymore hence it can be left with 0,0.

BITMAP *image;

image = load_bitmap(“my_pic.bmp”,NULL);

The next thing after that is BITMAP pointer for holding your image. BITMAP sets the address for your “image” variable to load the image which it does in the next line. Think of it like int but only much more powerful.

load_bitmap() function loads the given image to your image pointer variable which in our case is image. load_bitmap only takes BMP,TGA,LBM and PCX file format till now. So before you start loading up those 10 MP Jpeg file you better watch out.

blit(image,screen,0,0,0,0,image->w,image->h);

The blit line is the crucial and yet most complicated part of this program so far. What some beginner might think this as is it is responsible for displaying the image into the screen, but is not so.

What blit does is copy from source to the given destiny, in the code above lit is copying from image to screen (your monitor) and the rest of the parameters that follow are the clipping of the source width and height 0,0 means no clipping. If you entered something like this 10,3 then your image will be clipped 10 pixels wide and 3 pixels height. The other 0,0 is the position where you would want to place your image. 0,0  here means to the top left side of your screen. If given other variable then the image is placed according to the x and y coordinate of the screen. Left generally means negative x coordinate and Up generally means negative y coordinate and vice versa for Right and Down. The last two parameters are the width and height of the screen that you would actually want to display on your screen. You could also write SCREEN_W,SCREEN_H to display your whole screen.

read_key();

This line just waits for you to press some keys before exiting.

destroy_bitmap();

Because you take up memory after every compilation of the program it is not just a good idea but a must to destroy the memory that you took on your last compilation so that you clear up the memory and there is no memory leak.

The rest are usual standard procedure for ending your program.

Now go play with it and try to add that are not supposed to be added and ask questions to yourself about the possibilities.

Technorati Tags: ,,

1 comment: