#include <allegro.h>
BITMAP *xSprite;
BITMAP *oSprite;
int main(){
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
xSprite = load_bitmap( "x.bmp", NULL);
oSprite = load_bitmap( "o.bmp", NULL);
acquire_screen();
line( screen, 200, 0, 200, 480, makecol( 255, 255, 255));
line( screen, 400, 0, 400, 480, makecol( 255, 255, 255));
line( screen, 0, 150, 680, 150, makecol( 255, 255, 255));
line( screen, 0, 300, 680, 300, makecol( 255, 255, 255));
draw_sprite( screen, xSprite, 0, 0);
draw_sprite( screen, oSprite, 200, 0);
draw_sprite( screen, xSprite, 400, 0);
draw_sprite( screen, oSprite, 0, 150);
draw_sprite( screen, xSprite, 200, 150);
draw_sprite( screen, oSprite, 400, 150);
draw_sprite( screen, oSprite, 0, 300);
draw_sprite( screen, xSprite, 200, 300);
draw_sprite( screen, oSprite, 400, 300);
release_screen();
readkey();
return 0;
}
END_OF_MAIN();
|
|
This program will draw a filled in Tic-Tac-Toe board to screen. Lets break it down.
The first new line would be "BITMAP *xSprite;", this line introduces a new data type, BITMAP. The * before the name of the variable means its a "pointer" so instead of containing a value like normal variables it holds a place in memory that points to some value. Don't worry if your somewhat lost with pointers, they're not all that important just yet. A bitmap is basically a list of numbers which represent different colors which combined make an image.
set_color_depth() simply sets how many bits are to be used when making a pixel. In this case I set it to 16. This is important when it comes to choosing the transparent color in your drawings. At 16 bits and above the RGB (Red, Green, Blue) value would be 255, 0, 255 for the transparent pixel. 8 bit uses 0, 0, 0.
The next new line is "xSprite = load_bitmap( "x.bmp", NULL);". This line stores the place in memory where the image is located to "xSprite". If you don't understand that, don't worry, just remember to use what ever variable you load the memory address into (in this case xSprite) whenever you want to use this image.
The last new function in this program is draw_sprite(). This function will draw an image ( .bmp, .pcx, .lba, and .tgm are the file types supported by Allegro) to a bitmap, in this case the screen. The first parameter is where to draw the sprite, the second is a pointer to the sprite that you wish to draw, and the third and fourth parameter define the coordinates to where you want your sprite to be displayed.
|