Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Allegro: Drawing Images
Author Message
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #1
Allegro: Drawing Images
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: 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;

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;
}

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()

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()

Here's the project file to review.


Attached File(s)
.zip  Drawing Images.zip (Size: 1.7 KB / Downloads: 10)
(This post was last modified: 01-26-2011 04:51 PM by Scorch.)
01-26-2011 04:20 AM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner
**********

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #2
RE: Allegro: Drawing Images
Another interesting tutorial. You've contributed a lot already Smile

( Fixed grammar, hope you don't mind.)
-Scorch
(This post was last modified: 01-27-2011 03:18 AM by Scorch.)
01-26-2011 02:11 PM
Find all posts by this user Quote this message in a reply
Scorch Offline
Intermediate C++
********

Trade Count: (0)
Posts: 83
Joined: Jan 2011
Reputation: 15
Post: #3
RE: Allegro: Drawing Images
Would you do any less in my position?
01-27-2011 12:59 AM
Find all posts by this user Quote this message in a reply
Ironside Offline
Site Owner
**********

Trade Count: (0)
Posts: 170
Joined: Nov 2010
Reputation: 7
Post: #4
RE: Allegro: Drawing Images
Would try to do as much as I can, but yet compliments have a good place at everything.
(This post was last modified: 01-27-2011 02:14 PM by Ironside.)
01-27-2011 02:13 PM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: