Computer Tech
Allegro: Drawing Images - Printable Version

+- Computer Tech (https://computertech.createmybb3.com)
+-- Forum: Programming (https://computertech.createmybb3.com/forumdisplay.php?fid=6)
+--- Forum: C, C++ (https://computertech.createmybb3.com/forumdisplay.php?fid=7)
+--- Thread: Allegro: Drawing Images (/showthread.php?tid=145)



Allegro: Drawing Images - Scorch - 01-26-2011

I realise now at 1/25/11 this site isn't hugely active. However when it becomes active I want this section full of helpful threads.

This tutorial covers drawing in allegro.

For this tutorial you can use this image because I made itSmile, so no copyright infringement. Save as->.bmp
[Image: http://i992.photobucket.com/albums/af47/tnt-user/3DS%20MAX%20Models/Glass%20Tutorial/FinalRender.jpg]


You need to know allegro by default only accepts .bmp images. This isn't very important, but you need to be aware of it.

You can use a bitmap with a pointer.
[code]
BITMAP *Background;
[/code]

Remember to inintialize it after allegro_init().

The function is load_bitmap(par1, par2). It takes a image parameter and a custom pallette paramter you almost always will set to NULL.
You should also always check if the image isn't there to prevent crashes.
[code]
Background = load_bitmap("Background.bmp", NULL);
if(!Background)
{
allegro_message("No Background.bmp in this directory...");
return 0;
}
[/code]

After you load it you can draw it onto the screen using the function

draw_sprite(destination, bitmap, x, y);

Allegro has a color it renders invisible, hereafter called Magic Pink.
It contains the values of

Red - 225
Green - 0
Blue - 255

this color is useful for rendering out parts of an image.

Here's an example of drawing an image.
[code]
//Sprite drawing by Scorch
#include <allegro.h>


BITMAP *Background = NULL;//Set to NULL before allegro_init()

int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

Background = load_bitmap("Background.bmp", NULL);
if(!Background)//error checking is important;
{
allegro_message("No Background.bmp in this directory...");
return 0;
}

while(!key[KEY_ESC])
{
draw_sprite(screen, Background, 0, 0);

rest(50);//Wait before redrawing
}
return 0;
}
END_OF_MAIN()[/code]

However drawing to the screen isn't efficient for high fps with lots of objects. What you can do is use a buffer.

With 2 buffers working simultaneously you can keep a high fps and draw alot of objects. This is the basic concept.

Obj1
\
Buffer->Screen.
/
Obj2

Here's an example of that. I create an empty buffer to draw images onto.

[code]
//Sprite drawing by Scorch
#include <allegro.h>


BITMAP *Background = NULL;//Set to NULL before allegro_init()
BITMAP *Buffer = NULL;

int main()
{
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

Buffer = create_bitmap(SCREEN_W, SCREEN_H);//empty bitmap to draw onto

Background = load_bitmap("Background.bmp", NULL);
if(!Background)//error checking is important;
{
allegro_message("No Background.bmp in this directory...");
return 0;
}

while(!key[KEY_ESC])
{
draw_sprite(Buffer, Background, 0, 0);//draw to buffer
draw_sprite(screen, Buffer, 0, 0);//draw Buffer to screen

rest(50);//Wait before redrawing
}
return 0;
}
END_OF_MAIN()
[/code]

Here's the project file to review.


RE: Allegro: Drawing Images - Ironside - 01-26-2011

Another interesting tutorial. You've contributed a lot already Smile

( Fixed grammar, hope you don't mind.)
-[i]Scorch[/i]


RE: Allegro: Drawing Images - Scorch - 01-27-2011

Would you do any less in my position?


RE: Allegro: Drawing Images - Ironside - 01-27-2011

Would try to do as much as I can, but yet compliments have a good place at everything.