With Allegro comes quite a bunch of examples, which go from the simple introductory `Hello world' to more complicated programs featuring truecolor blending effects. This chapter describes these examples which you can find in the allegro/examples folder. You don't have to go through them in the same order as this documentation, but doing so you will learn the basic functions and avoid missing any important bit of information.
See also: END_OF_MAIN, SCREEN_H, SCREEN_W, acquire_screen, allegro_error, allegro_init, allegro_message, clear_to_color, desktop_palette, font, install_keyboard, makecol, readkey, release_screen, screen, set_gfx_mode, set_palette, textout_centre_ex.
See also: BITMAP, END_OF_MAIN, SCREEN_H, SCREEN_W, acquire_screen, allegro_error, allegro_init, allegro_message, blit, circle, clear_bitmap, create_bitmap, desktop_palette, destroy_bitmap, install_keyboard, palette_color, readkey, release_screen, screen, set_gfx_mode, set_palette.
See also: END_OF_MAIN, PALETTE, RGB, SCREEN_H, SCREEN_W, acquire_screen, allegro_error, allegro_init, allegro_message, black_palette, circlefill, install_keyboard, install_mouse, keypressed, release_screen, screen, set_gfx_mode, set_palette, show_mouse.
See also: BITMAP, END_OF_MAIN, acquire_bitmap, allegro_error, allegro_init, allegro_message, bitmap_mask_color, blit, clear_to_color, create_bitmap, create_sub_bitmap, desktop_palette, destroy_bitmap, drawing_mode, font, install_keyboard, makecol, masked_blit, palette_color, readkey, rectfill, release_bitmap, screen, set_gfx_mode, set_palette, solid_mode, text_height, text_length, textout_ex.
See also: END_OF_MAIN, PALETTE, SCREEN_H, SCREEN_W, acquire_screen, allegro_error, allegro_init, allegro_message, bmp_read_line, bmp_unwrite_line, bmp_write_line, clear_keybuf, font, getpixel, install_keyboard, keypressed, line, makecol, putpixel, release_screen, screen, set_gfx_mode, set_palette, textout_ex.
See also: BITMAP, END_OF_MAIN, SCREEN_H, SCREEN_W, acquire_screen, allegro_error, allegro_init, allegro_message, blit, circlefill, clear_keybuf, clear_to_color, create_bitmap, desktop_palette, destroy_bitmap, font, install_keyboard, install_timer, keypressed, makecol, release_screen, retrace_count, screen, set_gfx_mode, set_palette, textprintf_ex.
See also: BITMAP, END_OF_MAIN, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, circlefill, clear_keybuf, clear_to_color, create_bitmap, create_video_bitmap, desktop_palette, destroy_bitmap, font, install_keyboard, install_timer, keypressed, makecol, retrace_count, screen, set_gfx_mode, set_palette, show_video_bitmap, textprintf_ex.
See also: END_OF_MAIN, allegro_init, allegro_message, fixdiv, fixed, fixmul, fixsqrt, fixtof, ftofix, itofix.
See also: END_OF_MAIN, FONT, SCREEN_H, SCREEN_W, acquire_screen, allegro_error, allegro_init, allegro_message, clear_to_color, desktop_palette, destroy_font, extract_font_range, font, install_keyboard, load_font, makecol, merge_fonts, readkey, release_screen, screen, set_gfx_mode, set_palette, textout_centre_ex.
See also: BITMAP, END_OF_MAIN, SCREEN_H, SCREEN_W, acquire_screen, allegro_error, allegro_init, allegro_message, bitmap_mask_color, circle, clear_keybuf, clear_to_color, create_bitmap, desktop_palette, destroy_bitmap, font, get_mouse_mickeys, install_keyboard, install_mouse, install_timer, key, keypressed, makecol, mouse_b, mouse_x, mouse_y, mouse_z, palette_color, poll_mouse, readkey, release_screen, screen, set_gfx_mode, set_mouse_sprite, set_mouse_sprite_focus, set_palette, show_mouse, textout_centre_ex, textout_ex, textprintf_centre_ex, textprintf_ex, vsync.
See also: END_OF_FUNCTION, END_OF_MAIN, LOCK_FUNCTION, LOCK_VARIABLE, SCREEN_W, allegro_error, allegro_init, allegro_message, clear_to_color, desktop_palette, font, install_int, install_int_ex, install_keyboard, install_timer, key, keypressed, makecol, readkey, rest, screen, set_gfx_mode, set_palette, textprintf_centre_ex.
See also: END_OF_FUNCTION, END_OF_MAIN, LOCK_FUNCTION, SCREEN_H, SCREEN_W, acquire_screen, allegro_error, allegro_init, allegro_message, blit, clear_keybuf, clear_to_color, desktop_palette, font, install_keyboard, install_timer, key, key_shifts, keyboard_lowlevel_callback, keypressed, makecol, readkey, rectfill, release_screen, rest, scancode_to_name, screen, set_gfx_mode, set_palette, textprintf_centre_ex, textprintf_ex, ureadkey, usprintf, ustrzncpy.
See also: BITMAP, END_OF_MAIN, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, calibrate_joystick, calibrate_joystick_name, circle, clear_bitmap, clear_keybuf, create_bitmap, default_palette, destroy_bitmap, drawing_mode, font, install_joystick, install_keyboard, joy, key, keypressed, num_joysticks, palette_color, poll_joystick, putpixel, readkey, screen, set_gfx_mode, set_palette, textout_centre_ex, textprintf_centre_ex, textprintf_ex.
See also: END_OF_MAIN, SAMPLE, SCREEN_H, SCREEN_W, adjust_sample, allegro_error, allegro_init, allegro_message, clear_to_color, desktop_palette, destroy_sample, font, install_keyboard, install_sound, install_timer, key, load_sample, makecol, play_sample, poll_keyboard, rest, screen, set_gfx_mode, set_palette, textprintf_centre_ex.
See also: END_OF_MAIN, MIDI, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, clear_to_color, desktop_palette, destroy_midi, font, get_filename, get_midi_length, install_keyboard, install_sound, install_timer, key, keypressed, load_midi, makecol, midi_pause, midi_pos, midi_resume, midi_time, play_midi, readkey, rectfill, rest, screen, set_display_switch_mode, set_gfx_mode, set_palette, text_height, text_length, textprintf_centre_ex.
See also: DATAFILE, DIALOG, END_OF_MAIN, MENU, active_menu, alert, allegro_error, allegro_init, allegro_message, d_bitmap_proc, d_box_proc, d_button_proc, d_check_proc, d_clear_proc, d_ctext_proc, d_edit_proc, d_icon_proc, d_keyboard_proc, d_list_proc, d_menu_proc, d_radio_proc, d_rtext_proc, d_shadow_box_proc, d_slider_proc, d_text_list_proc, d_text_proc, d_textbox_proc, d_yield_proc, do_dialog, gui_bg_color, gui_fg_color, gui_mg_color, install_keyboard, install_mouse, install_timer, key, load_datafile, makecol, position_dialog, replace_filename, screen, set_dialog_color, set_gfx_mode, set_palette, unload_datafile, ustrtok, ustrzcat, ustrzcpy, uszprintf.
See also: BITMAP, DATAFILE, DIALOG, END_OF_MAIN, FONT, allegro_error, allegro_init, allegro_message, blit, circle, circlefill, clear_to_color, create_bitmap, d_button_proc, d_check_proc, d_clear_proc, d_edit_proc, desktop_palette, destroy_bitmap, do_dialog, fixcos, fixed, fixmul, fixsin, fixtoi, font, install_keyboard, install_mouse, install_timer, itofix, key, line, load_datafile, makecol, object_message, replace_filename, screen, set_gfx_mode, set_palette, unload_datafile.
Note how the Allegro unicode string functions resemble the functions you can find in the standard C library, only these handle Unicode on all platforms.
See also: BITMAP, DATAFILE, END_OF_MAIN, FONT, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, clear_to_color, create_bitmap, font, install_int_ex, install_keyboard, install_timer, keypressed, load_datafile, replace_filename, rest, screen, set_gfx_mode, set_uformat, set_window_title, text_height, text_length, textout_ex, uconvert_ascii, unload_datafile, ustrcat, ustrcpy, ustrsize, ustrsizez.
See also: BITMAP, END_OF_MAIN, PALETTE, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, destroy_bitmap, install_keyboard, load_bitmap, readkey, screen, set_gfx_mode, set_palette.
See also: BITMAP, END_OF_MAIN, PALETTE, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, destroy_bitmap, install_keyboard, keypressed, load_pcx, replace_filename, screen, set_gfx_mode, set_palette, stretch_blit, vsync.
See also: BITMAP, END_OF_MAIN, RGB, SCREEN_H, SCREEN_W, allegro_init, allegro_message, blit, clear_bitmap, font, get_config_argv, get_config_int, get_config_string, install_keyboard, line, load_bitmap, makecol, pop_config_state, push_config_state, readkey, screen, set_color_depth, set_config_file, set_gfx_mode, set_palette, stretch_blit, textout_centre_ex, textprintf_centre_ex, ustrdup, ustricmp.
See also: DATAFILE, END_OF_MAIN, allegro_error, allegro_init, allegro_message, blit, font, install_keyboard, load_datafile, makecol, readkey, replace_filename, screen, set_color_conversion, set_gfx_mode, set_palette, textout_ex, unload_datafile.
Why is the animate() routine coded 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. Drawing sprite on the screen (meaning in VRAM) and then clearing a background for it is not very fast. This example uses a different method which is much faster, but require a bit more memory.
First the buffer is cleared (it's a normal BITMAP), then the sprite is drawn on it, and when the drawing is finished this buffer is copied directly to the screen. So the end result is that there is a single VRAM blit instead of blitting/clearing the background and drawing a sprite on it. It's a good method even when you have to restore the background. And of course, it completely removes any 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.
Note that on modern systems, the above isn't true anymore, and you usually get the best performance by caching all your animations in video ram and doing only VRAM->VRAM blits, so there is no more RAM->VRAM transfer at all anymore. And usually, such transfers can run in parallel on the graphics card's processor as well, costing virtually no main cpu time at all. See the exaccel example for an example of this.
See also: BITMAP, DATAFILE, END_OF_FUNCTION, END_OF_MAIN, LOCK_FUNCTION, LOCK_VARIABLE, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, circle, clear_bitmap, clear_keybuf, create_bitmap, destroy_bitmap, draw_sprite, draw_sprite_h_flip, draw_sprite_v_flip, draw_sprite_vh_flip, fixed, font, hline, install_int_ex, install_keyboard, install_sound, install_timer, itofix, key, keypressed, load_datafile, makecol, palette_color, pivot_sprite, pivot_sprite_v_flip, play_sample, rectfill, replace_filename, rest, screen, set_gfx_mode, set_palette, text_height, textout_centre_ex, unload_datafile, vsync.
You may ask: how do you compile, append and exec your program?
Answer: like this...
1) Compile your program like normal. Use the magic filenames with '#' to load your data where needed.
2) Once you compressed your program, run "exedat foo.exe data.dat"
3) Finally run your program.
Note that appending data to the end of binaries may not be portable accross all platforms supported by Allegro.
See also: DATAFILE, END_OF_MAIN, allegro_error, allegro_init, allegro_message, blit, font, install_keyboard, line, load_datafile, makecol, readkey, screen, set_color_conversion, set_gfx_mode, set_palette, textout_ex, unload_datafile.
The translucency effect is easy to do in all color depths. However, the lighting effect has to be performed in a different way depending on whether the screen is in 8bit mode or another color depth. This is because additive drawing mode uses a different set of routines for truecolor modes.
See also: BITMAP, COLOR_MAP, END_OF_MAIN, PALETTE, RGB, RGB_MAP, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, bitmap_color_depth, blit, circlefill, clear_bitmap, clear_keybuf, color_map, create_bitmap, create_bitmap_ex, create_light_table, create_rgb_table, create_trans_table, destroy_bitmap, draw_trans_sprite, drawing_mode, install_keyboard, install_mouse, install_timer, keypressed, load_bitmap, mouse_x, mouse_y, poll_mouse, rectfill, replace_filename, rest, rgb_map, screen, set_alpha_blender, set_gfx_mode, set_palette, set_trans_blender, set_write_alpha_blender, stretch_blit.
See also: END_OF_MAIN, PALETTE, RGB, SCREEN_H, SCREEN_W, acquire_screen, allegro_init, clear_to_color, font, generate_332_palette, install_keyboard, key, makecol, readkey, release_screen, screen, set_color_depth, set_gfx_mode, set_palette, textout_centre_ex, textout_ex, textprintf_ex, vline.
See also: BITMAP, COLOR_MAP, END_OF_MAIN, PALETTE, RGB, RGB_MAP, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, circlefill, clear_keybuf, color_map, create_bitmap, create_color_table, create_rgb_table, destroy_bitmap, drawing_mode, font, generate_332_palette, install_keyboard, keypressed, makecol, rectfill, rgb_map, screen, set_gfx_mode, set_palette, textout_centre_ex, vsync.
Additionally this example also shows how to "inherit" the behaviour of a GUI object and extend it, here used to create the sliders.
See also: BITMAP, DIALOG, END_OF_MAIN, PALETTE, RGB, RGB_MAP, allegro_error, allegro_init, allegro_message, clear_to_color, create_bitmap_ex, create_rgb_table, d_bitmap_proc, d_box_proc, d_slider_proc, d_text_proc, destroy_bitmap, do_dialog, font, generate_332_palette, get_color_depth, hsv_to_rgb, install_keyboard, install_mouse, install_timer, key, makecol, makecol32, makecol8, object_message, rgb_map, rgb_to_hsv, screen, set_color, set_color_depth, set_gfx_mode, set_palette, textout_ex, vline, vsync.
See also: BITMAP, COLOR_MAP, END_OF_MAIN, PALETTE, RGB, RGB_MAP, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, clear_bitmap, color_map, create_bitmap, create_light_table, create_rgb_table, destroy_bitmap, draw_gouraud_sprite, font, install_keyboard, install_mouse, keypressed, line, load_bitmap, mouse_x, mouse_y, palette_color, poll_mouse, replace_filename, rgb_map, screen, set_gfx_mode, set_palette, set_trans_blender, show_mouse, textout_ex.
See also: BITMAP, END_OF_MAIN, PALETTE, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, circle, clear_bitmap, clear_keybuf, create_bitmap, destroy_bitmap, draw_lit_sprite, draw_trans_sprite, fixcos, fixsin, fixtoi, font, install_keyboard, install_timer, itofix, keypressed, load_bitmap, makecol, replace_filename, retrace_count, screen, set_color_conversion, set_color_depth, set_gfx_mode, set_trans_blender, textprintf_ex, vsync.
See also: BITMAP, END_OF_MAIN, PALETTE, SCREEN_H, SCREEN_W, allegro_error, allegro_exit, allegro_init, allegro_message, blit, create_bitmap, destroy_bitmap, draw_trans_sprite, install_keyboard, keypressed, line, load_bitmap, readkey, screen, set_color_conversion, set_color_depth, set_gfx_mode, set_palette, set_trans_blender, vsync.
See also: BITMAP, END_OF_MAIN, SCREEN_H, SCREEN_W, allegro_error, allegro_init, allegro_message, blit, clear_keybuf, create_bitmap, destroy_bitmap, draw_trans_sprite, drawing_mode, font, getb, getg, getpixel, getr, install_keyboard, install_mouse, install_timer, keypressed, load_bitmap, makecol, mouse_x, mouse_y, putpixel, rectfill, replace_filename, screen, set_alpha_blender, set_color_conversion, set_color_depth, set_gfx_mode, set_multiply_blender, set_write_alpha_blender, solid_mode, stretch_blit, textprintf_ex.
The basic approach is to select a 15 or 16 bit screen mode, but then draw onto 24 bit memory bitmaps. Since we only need the bottom 5 bits of each 8 bit color in order to store 15 bit data within a 24 bit location, we can fit a light level into the top 3 bits. The tricky bit is that these aren't actually 24 bit images at all: they are implemented as 8 bit memory bitmaps, and we just store the red level in one pixel, green in the next, and blue in the next, making the total image be three times wider than we really wanted. This allows us to use all the normal 256 color graphics routines for drawing onto our memory surfaces, most importantly the lookup table translucency, which can be used to combine the low 5 bits of color and the top 3 bits of light in a single drawing operation. Some trickery is needed