# Bitmap objects

- acquire_bitmap — Locks the bitmap before drawing onto it.
- acquire_screen — Shortcut of acquire_bitmap(screen);
- add_clip_rect — Intersects a bitmap's clipping rectangle with the given area.
- bitmap_color_depth — Returns the color depth of the specified bitmap.
- bitmap_mask_color — Returns the mask color of the specified bitmap.
- create_bitmap — Creates a memory bitmap.
- create_bitmap_ex — Creates a memory bitmap specifying color depth.
- create_sub_bitmap — Creates a memory sub bitmap.
- create_system_bitmap — Creates a system memory bitmap.
- create_video_bitmap — Creates a video memory bitmap.
- destroy_bitmap — Destroys any type of created bitmap.
- get_clip_rect — Returns the clipping rectangle of a bitmap.
- get_clip_state — Tells if clipping is on for a bitmap.
- is_inside_bitmap — Tells if a point is inside a bitmap.
- is_linear_bitmap — Tells if a bitmap is linear.
- is_memory_bitmap — Tells if a bitmap is a memory bitmap.
- is_planar_bitmap — Tells if a bitmap is a planar screen bitmap.
- is_same_bitmap — Tells if two bitmaps describe the same drawing surface.
- is_screen_bitmap — Tells if a bitmap is the screen bitmap or sub bitmap.
- is_sub_bitmap — Tells if a bitmap is a sub bitmap.
- is_system_bitmap — Tells if a bitmap is a system bitmap or sub bitmap.
- is_video_bitmap — Tells if a bitmap is a screen bitmap, video memory or sub bitmap.
- lock_bitmap — Locks the memory used by a bitmap.
- release_bitmap — Releases a previously locked bitmap.
- release_screen — Shortcut of release_bitmap(screen);
- screen — Global pointer to the screen hardware video memory.
- SCREEN_H — Global define to obtain the size of the screen.
- SCREEN_W — Global define to obtain the size of the screen.
- set_clip_rect — Sets the clipping rectangle of a bitmap.
- set_clip_state — Turns on or off the clipping of a bitmap.
- VIRTUAL_H — Global define to obtain the virtual size of the screen.
- VIRTUAL_W — Global define to obtain the virtual size of the screen.

Once you have selected a graphics mode, you can draw things onto the display via the ``screen'` bitmap. All the Allegro graphics routines draw onto BITMAP structures, which are areas of memory containing rectangular images, stored as packed byte arrays (in 8-bit modes one byte per pixel, in 15- and 16-bit modes two bytes per pixel, in 24-bit modes 3 bytes per pixel and in 32-bit modes 4 bytes per pixel). You can create and manipulate bitmaps in system RAM, or you can write to the special ``screen'` bitmap which represents the video memory in your graphics card.

Read chapter "Direct access to video memory" for information on how to get direct access to the image memory in a bitmap.

Allegro supports several different types of bitmaps:

- The ``screen'` bitmap, which represents the hardware video memory. Ultimately you have to draw onto this in order for your image to be visible. It is destroyed by any subsequent calls to set_gfx_mode(), so you should never attempt to destroy it yourself.
- Memory bitmaps, which are located in system RAM and can be used to store graphics or as temporary drawing spaces for double buffered systems. These can be obtained by calling create_bitmap(), load_pcx(), or by loading a grabber datafile.
- Sub-bitmaps. These share image memory with a parent bitmap (which can be the screen, a video or system bitmap, a memory bitmap, or another sub-bitmap), so drawing onto them will also change their parent. They can be of any size and located anywhere within the parent bitmap, and can have their own clipping rectangles, so they are a useful way of dividing a bitmap into several smaller units, eg. splitting a large virtual screen into multiple sections (see examples/exscroll.c). Warning: Make sure not to destroy a bitmap before all of its sub-bitmaps, otherwise bad things will happen when you try to access one of these sub-bitmaps.
- Video memory bitmaps. These are created by the create_video_bitmap() function, and are usually implemented as sub-bitmaps of the screen object. They must be destroyed by destroy_bitmap() before any subsequent calls to set_gfx_mode().
- System bitmaps. These are created by the create_system_bitmap() function, and are a sort of halfway house between memory and video bitmaps. They live in system memory, so you aren't limited by the amount of video ram in your card, but they are stored in a platform-specific format that may enable better hardware acceleration than is possible with a normal memory bitmap (see the GFX_HW_SYS_TO_VRAM_BLIT and GFX_HW_SYS_TO_VRAM_BLIT_MASKED flags in gfx_capabilities). System bitmaps must be accessed in the same way as video bitmaps, using the bank switch functions and bmp_write*() macros. Not every platform implements this type of bitmap: if they aren't available, create_system_bitmap() will function identically to create_bitmap(). They must be destroyed by destroy_bitmap() before any subsequent calls to set_gfx_mode().

extern BITMAP *screen;
Global pointer to a bitmap, sized VIRTUAL_W x VIRTUAL_H. This is created by set_gfx_mode(), and represents the hardware video memory. Only a part of this bitmap will actually be visible, sized SCREEN_W x SCREEN_H. Normally this is the top left corner of the larger virtual screen, so you can ignore the extra invisible virtual size of the bitmap if you aren't interested in hardware scrolling or page flipping. To move the visible window to other parts of the screen bitmap, call scroll_screen(). Initially the clipping rectangle will be limited to the physical screen size, so if you want to draw onto a larger virtual screen space outside this rectangle, you will need to adjust the clipping.

For example, to draw a pixel onto the screen you would write:

>           putpixel(screen, x, y, color);

Or to implement a double-buffered system:

>           /* Make a bitmap in RAM. */
>           BITMAP *bmp = create_bitmap(320, 200);
>           /* Clean the memory bitmap. */
>           clear_bitmap(bmp);
>           /* Draw onto the memory bitmap. */
>           putpixel(bmp, x, y, color);
>           /* Copy it to the screen. */
>           blit(bmp, screen, 0, 0, 0, 0, 320, 200);

Warning: be very careful when using this pointer at the same time as any bitmaps created by the create_video_bitmap() function (see the description of this function for more detailed information). And never try to destroy it with destroy_bitmap().

> See also: set_gfx_mode, is_screen_bitmap, create_video_bitmap, scroll_screen.

> Examples using this: Available Allegro examples.

#define SCREEN_W;
#define SCREEN_H;
Global defines that return the width and height of the screen, or zero if the screen has not been initialised yet. Example:

>           char buf[100];
>           ...
>           uszprintf(buf, sizeof(buf),
>                     "The screen size is %d x %d pixels",
>                     SCREEN_W, SCREEN_H);

> See also: screen, set_gfx_mode, VIRTUAL_W, VIRTUAL_H.

> Examples using this: Available Allegro examples.

#define VIRTUAL_W;
#define VIRTUAL_H;
Global defines that return the width and height of the virtual screen, or zero if the screen has not been initialised yet. Example:

>           char buf[100];
>           ...
>           uszprintf(buf, sizeof(buf),
>                     "The virtual screen size is %d x %d pixels",
>                     SCREEN_W, SCREEN_H);

> See also: screen, set_gfx_mode, SCREEN_W, SCREEN_H.

BITMAP *create_bitmap(int width, int height);
Creates a memory bitmap sized width by height. The bitmap will have clipping turned on, and the clipping rectangle set to the full size of the bitmap. The image memory will not be cleared, so it will probably contain garbage: you should clear the bitmap before using it. This routine always uses the global pixel format, as specified by calling set_color_depth(). The minimum height of the BITMAP must be 1 and width can't be negative. Example:

>           /* Create a 10 pixel tall bitmap, as wide as the screen. */
>           BITMAP *bmp = create_bitmap(SCREEN_W, 10);
>           if (!bmp)
>              abort_on_error("Couldn't create bitmap!");
>           /* Use the bitmap. */
>           ...
>           /* Destroy it when we don't need it any more. */
>           destroy_bitmap(bmp);

Return value: Returns a pointer to the created bitmap, or NULL if the bitmap could not be created. Remember to free this bitmap later to avoid memory leaks.

> See also: create_bitmap_ex, create_sub_bitmap, create_video_bitmap, create_system_bitmap, destroy_bitmap, set_color_depth, is_memory_bitmap, clear_bitmap, clear_to_color.

> Examples using this: Available Allegro examples.

BITMAP *create_bitmap_ex(int color_depth, int width, int height);
Creates a bitmap in a specific color depth (8, 15, 16, 24 or 32 bits per pixel). Example:

>           /* Create screen sized bitmap in 32 bits per pixel. */
>           BITMAP *bmp = create_bitmap_ex(32, SCREEN_W, SCREEN_H);
>           if (!bmp)
>              abort_on_error("Couldn't create bitmap!");
>           /* Use the bitmap. */
>           ...
>           /* Destroy it when we don't need it any more. */
>           destroy_bitmap(bmp);

Return value: Returns a pointer to the created bitmap, or NULL if the bitmap could not be created. Remember to free this bitmap later to avoid memory leaks.

> See also: create_bitmap, create_sub_bitmap, create_video_bitmap, create_system_bitmap, destroy_bitmap, is_memory_bitmap, clear_bitmap, clear_to_color.

> Examples using this: ex12bit, exlights, exrgbhsv, extrans.

BITMAP *create_sub_bitmap(BITMAP *parent, int x, y, width, height);
Creates a sub-bitmap, ie. a bitmap sharing drawing memory with a pre-existing bitmap, but possibly with a different size and clipping settings. When creating a sub-bitmap of the mode-X screen, the x position must be a multiple of four. The sub-bitmap width and height can extend beyond the right and bottom edges of the parent (they will be clipped), but the origin point must lie within the parent region.

Return value: Returns a pointer to the created sub bitmap, or NULL if the sub bitmap could not be created. Remember to free the sub bitmap before freeing the parent bitmap to avoid memory leaks and potential crashes accessing memory which has been freed.

> See also: create_bitmap, create_bitmap_ex, destroy_bitmap, is_sub_bitmap, clear_bitmap, clear_to_color.

> Examples using this: expat, exscroll, exswitch.

BITMAP *create_video_bitmap(int width, int height);
Allocates a video memory bitmap of the specified size. This can be used to allocate offscreen video memory for storing source graphics ready for a hardware accelerated blitting operation, or to create multiple video memory pages which can then be displayed by calling show_video_bitmap(). Read the introduction of this chapter for a comparison with other types of bitmaps and other specific details.

Warning: video memory bitmaps are usually allocated from the same space as the screen bitmap, so they may overlap with it; it is therefore not a good idea to use the global screen at the same time as any surfaces returned by this function.

Return value: Returns a pointer to the bitmap on success, or NULL if you have run out of video ram. Remember to destroy this bitmap before any subsequent call to set_gfx_mode().

> See also: create_bitmap, create_bitmap_ex, create_system_bitmap, create_sub_bitmap, destroy_bitmap, screen, show_video_bitmap, gfx_capabilities, is_video_bitmap, clear_bitmap, clear_to_color.

> Examples using this: ex3buf, exaccel, exflip, exupdate.

BITMAP *create_system_bitmap(int width, int height);
Allocates a system memory bitmap of the specified size. Read the introduction of this chapter for a comparison with other types of bitmaps and other specific details.

Return value: Returns a pointer to the bitmap on success, NULL otherwise. Remember to destroy this bitmap before any subsequent call to set_gfx_mode().

> See also: create_bitmap, create_bitmap_ex, create_video_bitmap, create_sub_bitmap, destroy_bitmap, is_system_bitmap, clear_bitmap, clear_to_color.

> Examples using this: exupdate.

void destroy_bitmap(BITMAP *bitmap);
Destroys a memory bitmap, sub-bitmap, video memory bitmap, or system bitmap when you are finished with it. If you pass a NULL pointer this function won't do anything. See above for the restrictions as to when you are allowed to destroy the various types of bitmaps.

The bitmap must not have a mouse cursor shown on it at the time it is destroyed.

> See also: create_bitmap, load_bitmap, show_mouse.

> Examples using this: Available Allegro examples.

void lock_bitmap(BITMAP *bitmap);
Under DOS, locks all the memory used by a bitmap. You don't normally need to call this function unless you are doing very weird things in your program.

int bitmap_color_depth(BITMAP *bmp);
Returns the color depth of the specified bitmap (8, 15, 16, 24, or 32). Example:

>           switch (bitmap_color_depth(screen)) {
>              case 8:
>                 /* Access screen using optimized 8-bit code. */
>                 break;
>              default:
>                 /* Use generic slow functions. */
>                 break;
>           }

> See also: set_color_depth, bitmap_mask_color.

> Examples using this: ex3d, exlights, exscn3d, exswitch, extrans, exupdate, exzbuf.

int bitmap_mask_color(BITMAP *bmp);
Returns the mask color for the specified bitmap (the value which is skipped when drawing sprites). For 256-color bitmaps this is zero, and for truecolor bitmaps it is bright pink (maximum red and blue, zero green). A frequent use of this function is to clear a bitmap with the mask color so you can later use this bitmap with masked_blit() or draw_sprite() after drawing other stuff on it. Example:

>           /* Replace mask color with another color. */
>           for (y = 0; y < bmp->h; y++)
>              for (x = 0; x < bmp->w; x++)
>                 if (getpixel(bmp, x, y) == bitmap_mask_color(bmp))
>                    putpixel(bmp, x, y, another_color);

> See also: MASK_COLOR_8, set_color_depth, bitmap_color_depth.

> Examples using this: ex3d, exmouse, expat.

int is_same_bitmap(BITMAP *bmp1, BITMAP *bmp2);
Returns TRUE if the two bitmaps describe the same drawing surface, ie. the pointers are equal, one is a sub-bitmap of the other, or they are both sub-bitmaps of a common parent.

> See also: create_sub_bitmap.

int is_planar_bitmap(BITMAP *bmp);
Returns TRUE if bmp is a planar (mode-X or Xtended mode) screen bitmap.

> See also: is_linear_bitmap, is_memory_bitmap.

int is_linear_bitmap(BITMAP *bmp);
Returns TRUE if bmp is a linear bitmap, i.e. a bitmap that can be accessed linearly within each scanline (for example a memory bitmap, the DOS VGA or SVGA screen, Windows bitmaps, etc). Linear bitmaps can be used with the _putpixel(), _getpixel(), bmp_write_line(), and bmp_read_line() functions.

Historically there were only linear and planar bitmaps for Allegro, so is_linear_bitmap() is actually an alias for !is_planar_bitmap().

> See also: is_planar_bitmap, is_memory_bitmap.

int is_memory_bitmap(BITMAP *bmp);
Returns TRUE if bmp is a memory bitmap, ie. it was created by calling create_bitmap() or loaded from a grabber datafile or image file. Memory bitmaps can be accessed directly via the line pointers in the bitmap structure, eg. bmp->line[y][x] = color.

> See also: is_linear_bitmap, is_planar_bitmap.

int is_screen_bitmap(BITMAP *bmp);
Returns TRUE if bmp is the screen bitmap, or a sub-bitmap of the screen.

> See also: screen, create_sub_bitmap.

int is_video_bitmap(BITMAP *bmp);
Returns TRUE if bmp is the screen bitmap, a video memory bitmap, or a sub-bitmap of either.

> See also: screen, create_video_bitmap, create_sub_bitmap.

int is_system_bitmap(BITMAP *bmp);
Returns TRUE if bmp is a system bitmap object, or a sub-bitmap of one.

> See also: create_system_bitmap, create_sub_bitmap.

int is_sub_bitmap(BITMAP *bmp);
Returns TRUE if bmp is a sub-bitmap.

> See also: create_sub_bitmap.

void acquire_bitmap(BITMAP *bmp);
Acquires the specified video bitmap prior to drawing onto it. You never need to call the function explicitly as it is low level, and will only give you a speed up if you know what you are doing. Using it wrongly may cause slowdown, or even lock up your program.

Note: You do never need to use acquire_bitmap on a memory bitmap, i.e. a normal bitmap created with create_bitmap. It will simply do nothing in that case.

It still can be useful, because e.g. under the current DirectDraw driver of Allegro, most drawing functions need to lock a video bitmap before drawing to it. But doing this is very slow, so you will get much better performance if you acquire the screen just once at the start of your main redraw function, then call multiple drawing operations which need the bitmap locked, and only release it when done.

Multiple acquire calls may be nested, but you must make sure to match up the acquire_bitmap and release_bitmap calls. Be warned that DirectX and X11 programs activate a mutex lock whenever a surface is locked, which prevents them from getting any input messages, so you must be sure to release all your bitmaps before using any timer, keyboard, or other non-graphics routines!

Note that if you are using hardware accelerated VRAM->VRAM functions, you should not call acquire_bitmap(). Such functions need an unlocked target bitmap under DirectX, so there is now just the opposite case from before - if the bitmap is already locked with acquire_bitmap, the drawing operation has to unlock it.

Note: For backwards compatibility, the unlocking behavior of such functions is permanent. That is, if you call acquire_bitmap first, then call e.g. an accelerated blit, the DirectX bitmap will be unlocked internally (it won't affect the nesting counter of acquire/release calls).

There is no clear cross-platform way in this Allegro version to know which drawing operations need a locked/unlocked state. For example a normal rectfill most probably is accelerated under DirectX, and therefore needs the screen unlocked, but an XOR rectfill, or one with blending activated, most probably is not, and therefore locks the screen. And while the DirectX driver will do automatic unlocking, there is no such thing under X11, where the function is used to synchronize X11 calls from different threads. Your best bet is to never use acquire_bitmap - changes are you are doing something in the wrong way if you think you need it.

Warning: This function can be very dangerous to use, since the whole program may get locked while the bitmap is locked. So the lock should only be held for a short time, and you should not call anything but drawing operations onto the locked video bitmap while a lock is in place. Especially don't call things like show_mouse (or scare_mouse which calls that) or readkey, since it will most likely deadlock your entire program.

> See also: release_bitmap, acquire_screen, release_screen.

> Examples using this: ex3buf, exaccel, expat, exquat, exscroll, exswitch, exupdate.

void release_bitmap(BITMAP *bmp);
Releases a bitmap that was previously locked by calling acquire_bitmap(). If the bitmap was locked multiple times, you must release it the same number of times before it will truly be unlocked.

> See also: acquire_bitmap, acquire_screen, release_screen.

> Examples using this: ex3buf, exaccel, expat, exquat, exscroll, exswitch, exupdate.

void acquire_screen();
Shortcut version of acquire_bitmap(screen);

> See also: acquire_bitmap, release_bitmap, release_screen.

> Examples using this: Available Allegro examples.

void release_screen();
Shortcut version of release_bitmap(screen);

> See also: acquire_bitmap, release_bitmap, acquire_screen.

> Examples using this: Available Allegro examples.

void set_clip_rect(BITMAP *bitmap, int x1, int y1, int x2, int y2);
Each bitmap has an associated clipping rectangle, which is the area of the image that it is OK to draw onto. Nothing will be drawn to positions outside this space. This function sets the clipping rectangle for the specified bitmap. Pass the coordinates of the top-left and bottom-right corners of the clipping rectangle in this order; these are both inclusive, i.e. set_clip_rect(bitmap, 16, 16, 32, 32) will allow drawing to (16, 16) and (32, 32), but not to (15, 15) and (33, 33).

Drawing operations will be performed (at least partially) on the bitmap as long as the first coordinates of its clipping rectangle are not greater than the second coordinates and its intersection with the actual image is non-empty. If either condition is not fulfilled, drawing will be turned off for the bitmap, e.g.

>           set_clip_rect(bmp, 0, 0, -1, -1); /* disable drawing on bmp */

Note that passing "out-of-bitmap" coordinates is allowed, but they are likely to be altered (and so the coordinates returned by get_clip_rect() will be different). However, such modifications are guaranteed to preserve the external effect of the clipping rectangle, that is not to modify the actual area of the image that it is OK to draw onto.

> See also: get_clip_rect, add_clip_rect, set_clip_state, get_clip_state.

> Examples using this: ex12bit, excamera.

void get_clip_rect(BITMAP *bitmap, int *x1, int *y1, int *x2, int *y2);
Returns the clipping rectangle for the specified bitmap.

> See also: set_clip_rect, add_clip_rect, set_clip_state, get_clip_state.

void add_clip_rect(BITMAP *bitmap, int x1, int y1, int x2, int y2);
Sets the clipping rectangle of the specified bitmap as the intersection of its current clipping rectangle and the rectangle described by the four coordinates.

> See also: set_clip_rect, get_clip_rect, set_clip_state, get_clip_state.

void set_clip_state(BITMAP *bitmap, int state)
Turns on (if state is non-zero) or off (if state is zero) clipping for the specified bitmap. Turning clipping off may slightly speed up some drawing operations (usually a negligible difference, although every little helps) but will result in your program dying a horrible death if you try to draw beyond the edges of the bitmap.

> See also: set_clip_rect, get_clip_rect, add_clip_rect, get_clip_state.

int get_clip_state(BITMAP *bitmap)
Returns non-zero if clipping is turned on for the specified bitmap and zero otherwise.

> See also: set_clip_rect, get_clip_rect, add_clip_rect, set_clip_state.

int is_inside_bitmap(BITMAP *bmp, int x, int y, int clip);
Returns non-zero if point (x, y) lies inside the bitmap. If ``clip'` is non-zero, the function compares the coordinates with the clipping rectangle, that is it returns non-zero if the point lies inside the clipping rectangle or if clipping is disabled for the bitmap. If ``clip'` is zero, the function compares the coordinates with the actual dimensions of the bitmap.

> See also: set_clip_rect, set_clip_state, getpixel.

Back to contents