Show exsprite.c syntax highlighted
/*
* Example program for the Allegro library, by Grzegorz Ludorowski.
*
* This example demonstrates how to use datafiles, various sprite drawing
* routines and flicker-free animation.
*
* A short explanation for beginners.
* Why did I do animate () routine in that way?
* As you probably know, VIDEO RAM is much slower than "normal" RAM, so
* it's advisable to reduce VRAM blits to a minimum.
* So, drawing sprite on the al_screen (I mean in VRAM) and then clearing
* a background for him is not very fast. I've used a different method
* which is much faster, but require a bit more memory.
* I clear a buffer (it's a normal AL_BITMAP), then I draw sprite to it, and
* after all I al_blit only one time this buffer to the al_screen. So, I'm using
* a single VRAM al_blit instead of blitting/clearing background and drawing
* a sprite on it. It's a good method even when I have to restore.
* background And of course, it completely remove flickering effect.
* When one uses a big (ie. 800x600 background) and draws something on
* it, it's wise to use a copy of background somewhere in memory and
* restore background using this "virtual background". When blitting from
* VRAM in SVGA modes, it's probably, that drawing routines have to switch
* banks on video card. I think, I don't have to remind how slow is it.
*/
#include "allegro.h"
#include "running.h"
/* pointer to data file */
AL_DATAFILE *running_data;
/* current sprite frame number */
int frame_number = 0;
/* pointer to a sprite buffer, where sprite will be drawn */
AL_BITMAP *sprite_buffer;
/* a boolean - if true, skip to next part */
int next;
void animate(void)
{
/* waits for vertical retrace interrupt */
al_vsync();
al_vsync();
/* blits sprite buffer to al_screen */
al_blit(sprite_buffer, al_screen, 0, 0, 120, 80, 82, 82);
/* clears sprite buffer with color 0 */
al_clear_bitmap(sprite_buffer);
/* if SPACE al_key pressed set a next flag */
if (al_key_pressed())
next = TRUE;
else
next = FALSE;
/* increase frame number, or if it's equal 9 (last frame) set it to 0 */
if (frame_number == 9)
frame_number = 0;
else
frame_number++;
}
int main(int argc, char *argv[])
{
AL_BITMAP *running;
char datafile_name[256];
int angle = 0;
allegro_init();
al_install_keyboard();
if (al_set_gfx_mode(AL_GFX_SAFE, 320, 200, 0, 0) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Unable to set any graphic mode\n%s\n", al_error);
return 1;
}
/* we still don't have a palette, don't let Allegro twist colors */
al_set_color_conversion(AL_COLORCONV_NONE);
/* loads datafile and sets user palette saved in datafile */
al_replace_filename(datafile_name, argv[0], "running.dat", sizeof(datafile_name));
running_data = al_load_datafile(datafile_name);
if (!running_data) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Error loading %s!\n", datafile_name);
return 1;
}
/* select the palette which was loaded from the datafile */
al_set_palette(running_data[AL_PALETTE_001].dat);
/* aha, set a palette and let Allegro convert colors when blitting */
al_set_color_conversion(AL_COLORCONV_TOTAL);
/* create and clear a bitmap for sprite buffering */
sprite_buffer = al_create_bitmap(82, 82);
al_clear_bitmap(sprite_buffer);
/* create another bitmap for color conversion from the datafile */
running = al_create_bitmap(82, 82);
/* write current sprite drawing method */
al_put_text(al_screen, al_font_8x8, "Press a al_key for next part...", 40, 10, al_palette_color[1]);
al_put_text(al_screen, al_font_8x8, "Using al_draw_sprite", 1, 190, al_palette_color[15]);
do {
al_blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
al_draw_sprite(sprite_buffer, running, 0, 0);
animate();
} while (!next);
al_clear_keybuf();
al_draw_rect_fill(al_screen, 0, 190, 320, 200, 0);
al_put_text(al_screen, al_font_8x8, "Using al_draw_sprite_h_flip", 1, 190, al_palette_color[15]);
do {
al_blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
al_draw_sprite_h_flip(sprite_buffer, running, 0, 0);
animate();
} while (!next);
al_clear_keybuf();
al_draw_rect_fill(al_screen, 0, 190, 320, 200, 0);
al_put_text(al_screen, al_font_8x8, "Using al_draw_sprite_v_flip", 1, 190, al_palette_color[15]);
do {
al_blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
al_draw_sprite_v_flip(sprite_buffer, running, 0, 0);
animate();
} while (!next);
al_clear_keybuf();
al_draw_rect_fill(al_screen, 0, 190, 320, 200, 0);
al_put_text(al_screen, al_font_8x8, "Using al_draw_sprite_vh_flip", 1, 190, al_palette_color[15]);
do {
al_blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
al_draw_sprite_vh_flip(sprite_buffer, running, 0, 0);
animate();
} while (!next);
al_clear_keybuf();
al_draw_rect_fill(al_screen, 0, 190, 320, 200, 0);
al_put_text(al_screen, al_font_8x8, "Now with rotating - al_rotate_sprite", 1, 190, al_palette_color[15]);
do {
/* The last argument to al_rotate_sprite() is a fixed point type,
* so I had to use al_int_to_fix() routine (integer to fixed).
*/
al_blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
al_rotate_sprite(sprite_buffer, running, 0, 0, al_int_to_fix(angle));
animate();
angle += 4;
} while (!next);
al_clear_keybuf();
al_draw_rect_fill(al_screen, 0, 190, 320, 200, 0);
al_put_text(al_screen, al_font_8x8, "Now using al_rotate_sprite_v_flip", 1, 190, al_palette_color[15]);
do {
/* The last argument to al_rotate_sprite_v_flip() is a fixed point type,
* so I had to use al_int_to_fix() routine (integer to fixed).
*/
al_blit(running_data[frame_number].dat, running, 0, 0, 0, 0, 82, 82);
al_rotate_sprite_v_flip(sprite_buffer, running, 0, 0, al_int_to_fix(angle));
animate();
angle += 4;
} while (!next);
al_destroy_datafile(running_data);
al_destroy_bitmap(sprite_buffer);
al_destroy_bitmap(running);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here