Examples, tools and demonstration game

Contents

Examples

On the examples subdirectory there are several simple example programs that explains how to use Allegro.pas in specific cases. Here you have a list with all examples ordered by complexity (most simple first) with links to some related types, functions and procedures:

exhello

This is a very simple program showing how to get into graphics mode and draw text onto the screen.

See also: al_init al_set_color_depth al_set_gfx_mode al_screen al_message al_error al_clear_to_color al_textout_ex al_textout_centre_ex al_font al_install_keyboard al_readkey al_set_palette al_desktop_palette al_makecol al_exit

exmem

This program demonstrates the use of memory bitmaps. It creates a small temporary bitmap in memory, draws some circles onto it, and then blits lots of copies of it onto the screen.

See also: AL_BITMAPptr al_acquire_screen al_blit al_circle al_clear_bitmap al_create_bitmap al_destroy_bitmap al_palette_color al_screen

expal

This program demonstrates how to manipulate the palette. It draws a set of concentric circles onto the screen and animates them by cycling the palette.

See also: al_set_palette al_black_palette al_circlefill

expat

This program demonstrates the use of patterned drawing and sub-bitmaps.

See also: exfont AL_BITMAPptr al_create_bitmap al_create_sub_bitmap al_drawing_mode al_makecol

exdbuf

This program demonstrates the use of double buffering. It moves a circle across the screen, first just erasing and redrawing directly to the screen, then with a double buffer.

See also: AL_BITMAPptr al_blit al_install_timer al_retrace_count al_circlefill al_clear_to_color al_create_bitmap al_destroy_bitmap

exflip

This program moves a circle across the screen, first with a double buffer and then using page flips.

See also: exdbuf al_create_video_bitmap al_show_video_bitmap

exfixed

This program demonstrates how to use fixed point numbers, which are signed 32-bit integers storing the integer part in the upper 16 bits and the decimal part in the 16 lower bits. This example also uses the unusual approach of communicating with the user exclusively via the al_message function.

See also: alfixed

exfont

This is a very simple program showing how to load and manipulate fonts.

See also: AL_FONTptr al_font al_load_font al_destroy_font al_extract_font_range al_merge_fonts

exmouse

This program demonstrates how to get mouse input. The first part of the test retrieves the raw mouse input data and displays it on the screen without using any mouse cursor. When you press a key the standard arrow-like mouse cursor appears. You are not restricted to this shape, and a second key press modifies the cursor to be several concentric colored circles. They are not joined together, so you can still see bits of what's behind when you move the cursor over the printed text message.

See also: al_bitmap_mask_color al_clear_keybuf al_desktop_palette al_install_mouse al_set_mouse_sprite al_show_mouse al_get_mouse_mickeys al_mouse_x al_mouse_y al_mouse_z al_poll_mouse al_set_mouse_sprite al_set_mouse_sprite_focus

extimer

This program demonstrates how to use the timer routines. These can be a bit of a pain, because your interrupt handlers are used in "interrupt time". The first part of the example shows a basic use of timing using the blocking procedure al_rest. The second part shows how to use three timers with different frequencies in a non blocking way.

See also: al_install_timer al_install_int al_install_int_ex al_rest

exkeys

This program demonstrates how to access the keyboard. The first part shows the basic use of al_readkey. The second part shows how to extract the ASCII value. Next come the scan codes. The fourth test detects modifier keys like alt or shift. The fifth test requires some focus to be passed. The final step shows how to use the global al_key array to read simultaneous key presses. The last method to detect key presses are keyboard callbacks. This is demonstrated by installing a keyboard callback, which marks all pressed keys by drawing to a grid.

See also: al_install_keyboard al_key_shifts al_keyboard_lowlevel_callback al_scancode_to_name al_ureadkey al_key

exjoy

This program uses the Allegro library to detect and read the value of a joystick. The output of the program is a small target sight on the screen which you can move. At the same time the program will tell you what you are doing with the joystick (moving or firing).

See also: al_install_joystick al_calibrate_joystick al_calibrate_joystick_name al_joy al_num_joysticks al_poll_joystick

exsample

This program demonstrates how to play samples. You have to use this example from the command line to specify as first parameter a WAV or VOC sound file to play. If the file is loaded successfully, the sound will be played in an infinite loop. While it is being played, you can use the left and right arrow keys to modify the panning of the sound. You can also use the up and down arrow keys to modify the pitch.

See also: al_install_sound al_load_sample al_play_sample al_adjust_sample al_destroy_sample

exmidi

This program demonstrates how to play MIDI files.

See also: al_install_sound al_load_midi al_get_midi_length al_play_midi al_midi_pause al_midi_pos al_midi_resume al_midi_time al_destroy_midi

exgui

This program demonstrates how to use the GUI routines. From the simple dialog controls that display a text or a bitmap to more complex multiple choice selection lists, Allegro provides a framework which can be customised to suit your needs.

See also: algui

excustom

A follow up of the exgui example showing how to customise the default Allegro framework. In this case a dialog procedure animates a graphical clock without disrupting other GUI dialogs. A more simple option shows how to dynamically change the font used by all GUI elements.

See also: algui

exscale

This example demonstrates how to use PCX files, palettes and stretch blits. It loads a PCX file, sets its palette and does some random stretch_blits. Don't worry - it's VERY slowed down using al_vsync.

See also: AL_BITMAPptr al_blit al_stretch_blit

exconfig

This program shows how to use the configuration routines. A first look at the example shows nothing more than a static graphic and the wait for a key press. However, the way this graphic is displayed is configured through a custom exconfig.ini file which is loaded manually. From this file the example obtains parameters like fullscreen/windowed mode, a specific graphic resolution to set up, which graphic to show, how to blit it on the screen, etc.

See also: Configuration routines al_set_config_file

exdata

This program demonstrates how to access the contents of an Allegro datafile (created by the grabber utility). The example loads the file example.dat, then blits a bitmap and shows a font, both from this datafile.

See also: alfile

exsprite

This example demonstrates how to use datafiles, various sprite drawing routines and flicker-free animation.

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 AL_BITMAPptr), 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: al_draw_sprite al_draw_sprite_h_flip al_draw_sprite_v_flip al_draw_sprite_vh_flip

exrotscl

This example demonstrates rotate_scaled_sprite functions.

See also: al_set_color_conversion al_set_alpha_blender al_set_color_depth al_set_trans_blender al_draw_trans_sprite al_rotate_scaled_sprite al_rotate_scaled_sprite_lit al_rotate_scaled_sprite_trans

extrans

Lighting and translucency effects.)

This program demonstrates how to use the lighting and translucency functions. The first part of the example will show a dark screen illuminated by a spotlight you can move with your mouse. After a key press the example shows the full bitmap and the spotlight changes to be a reduced version of the background with 50% of translucency.

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: alvga alblend al_bitmap_color_depth

extrans2

This program demonstrates how to draw trans and lit sprites and flip them at the same time, using al_draw_sprite_ex function. It displays several images moving around using different drawing modes while you can press space key to change the flipping orientation.

See also: al_set_trans_blender

extruec

This program shows how to specify colors in the various different truecolor pixel formats. The example shows the same screen (a few text lines and three coloured gradients) in all the color depth modes supported by your video card. The more color depth you have, the less banding you will see in the gradients.

See also: al_generate_332_palette al_makecol al_set_palette

excolmap

This program demonstrates how to create custom graphic effects with the al_create_color_table function. Allegro drawing routines are affected by any color table you might have set up. In the first part of this example, a greyscale color table is set. The result is that a simple rectfill call, instead of drawing a rectangle with color zero, uses the already drawn pixels to determine the pixel to be drawn (read the comment of ReturnGreyColor for a precise description of the algorithm). In the second part of the test, the color table is changed to be an inverse table, meaning that any pixel drawn will be shown as its color values had been inverted.

See also: AL_BITMAPptr AL_COLOR_MAP al_color_table

exhsvrgb

Translating HSV space -> RGB values.

See also: al_hsv_to_rgb al_rgb_to_hsv

exshade

This program demonstrates how to draw Gouraud shaded (lit) sprites. In an apparently black screen, a planet like sprite is drawn close to the middle of the screen. In a similar way to how the first test of extrans works, you move the cursor on the screen with the mouse. Attached to this mouse you can imagine a virtual spotlight illuminating the scene around. Depending on where the mouse is, the Gouraud shaded sprite will show the direction of the light.

See also: AL_BITMAPptr AL_COLOR_MAP AL_PALETTE AL_RGB_MAP al_error al_message al_color_map al_create_bitmap al_create_light_table al_create_rgb_table al_destroy_bitmap al_draw_gouraud_sprite al_load_bitmap al_mouse_x al_mouse_y al_palette_color al_rgb_map al_screen al_set_gfx_mode al_set_palette al_set_trans_blender al_show_mouse

exblend

This program demonstrates how to use the translucency functions in truecolor video modes. Two image files are loaded from disk and displayed moving slowly around the screen. One of the images will be tinted to different colors. The other image will be faded out with a varying alpha strength, and drawn on top of the other image.

See also: alblend

exxfade

This program demonstrates how to load and display bitmap files in truecolor video modes, and how to crossfade between them. You have to use this example from the command line to specify as parameters a number of graphic files. Use at least two files to see the graphical effect. The example will crossfade from one image to another with each key press until you press the ESC key.

See also: al_blit al_draw_trans_sprite al_set_color_conversion al_set_color_depth al_set_gfx_mode al_set_palette al_set_trans_blender

exalpha

This program demonstrates how to use the 32 bit RGBA translucency functions to store an alpha channel along with a bitmap graphic. Two images are loaded from disk. One will be used for the background and the other as a sprite. The example generates an alpha channel for the sprite image, composing the 32 bit RGBA bitmap during runtime, and draws it at the position of the mouse cursor.

See also: al_blit al_draw_trans_sprite al_drawing_mode al_getb al_getg al_getpixel al_getr al_makecol al_putpixel al_set_alpha_blender al_set_color_conversion al_set_color_depth al_set_gfx_mode al_set_multiply_blender al_set_write_alpha_blender al_solid_mode

ex3d

Example for Allegro.pas that displays a 3D cube and rotates it.

Actutally this example has two different files: ex3d, with the main code, and cube.pas, with some useful classes.

See also: al3d

excamera

This program demonstrates how to use the al_get_camera_matrix function to view a 3d world from any position and angle. The example draws a checkered floor through a viewport region on the screen. You can use the keyboard to move around the camera or modify the size of the viewport. The keys that can be used with this example are displayed between brackets at the top of the screen.

See also al3d

exquat

Euler angles are convenient for storing and creating 3D orientations. However, this program demonstrates that they are not good when interpolating between two different orientations. The problem is solved by using Allegro's quaternion operations.

In this program, two cubes are rotated between random orientations. Notice that although they have the same beginning and ending orientations, they do not follow the same path between orientations.

One cube is being rotated by directly incrementing or decrementing the Euler angles from the starting point to the ending point. This is an intuitive notion, but it is incorrect because it does not cause the object to turn around a single unchanging axis of rotation. The axis of rotation wobbles resulting in the object spinning in strange ways. The object will eventually end up in the orientation that the user intended, but it gets there in a way that is unattractive. Imagine if this method was used to update the position of a camera in a game! Sometimes it would swing wildly and disorient the player.

The other cube is animated using quaternions. This results in a much more pleasing animation because the cube turns around a single axis of rotation.

See also al3d

exstars

This program draws a 3D star field (depth-cued) and a polygon starship (controllable with the keyboard cursor keys), using the Allegro math functions.

See also al3d

exscn3d

Another 3D demonstration. This program demonstrates how to use scanline sorting algorithm in Allegro (al_create_scene, al_clear_scene, ... functions). It also provides an example of how to use the 3D clipping function. The example consists of a flyby through a lot of rotating 3d cubes.

See also: al3d al_create_scene

exzbuf

A third 3D demonstration that shows how to use the Z-buffer. It uses the same cube object than the ex3d example.

See also: al3d al_create_zbuffer

exscroll

This program demonstrates how to use hardware scrolling. The scrolling should work on anything that supports virtual screens larger than the physical screen.

See also al_create_sub_bitmap al_desktop_palette al_destroy_bitmap al_scroll_screen al_vline

ex3buf

This program demonstrates the use of triple buffering. Several triangles are displayed rotating and bouncing on the screen until you press a key. Note that on some platforms you can't get real hardware triple buffering. The Allegro code remains the same, but most likely the graphic driver will emulate it. Unfortunately, in these cases you can't expect the animation to be completely smooth and flicker free.

See also: al_create_video_bitmap al_enable_triple_buffer al_gfx_capabilities al_poll_scroll al_request_video_bitmap

exspline

This program demonstrates the use of spline curves to create smooth paths connecting a number of node points. This can be useful for constructing realistic motion and animations.

The technique is to connect the series of guide points p1..p(n) with spline curves from p1-p2, p2-p3, etc. Each spline must pass though both of its guide points, so they must be used as the first and fourth of the spline control points. The fun bit is coming up with sensible values for the second and third spline control points, such that the spline segments will have equal gradients where they meet. I came up with the following solution:

For each guide point p(n), calculate the desired tangent to the curve at that point. I took this to be the vector p(n-1) -> p(n+1), which can easily be calculated with the inverse tangent function, and gives decent looking results. One implication of this is that two dummy guide points are needed at each end of the curve, which are used in the tangent calculations but not connected to the set of splines.

Having got these tangents, it becomes fairly easy to calculate the spline control points. For a spline between guide points p(a) and p(b), the second control point should lie along the positive tangent from p(a), and the third control point should lie along the negative tangent from p(b). How far they are placed along these tangents controls the shape of the curve: I found that applying a 'curviness' scaling factor to the distance between p(a) and p(b) works well.

One thing to note about splines is that the generated points are not all equidistant. Instead they tend to bunch up nearer to the ends of the spline, which means you will need to apply some fudges to get an object to move at a constant speed. On the other hand, in situations where the curve has a noticeable change of direction at each guide point, the effect can be quite nice because it makes the object slow down for the curve.

See also al_calc_spline al_fixatan2 alfixed al_line al_spline al_xor_mode

exsyscur

This program demonstrates the use of hardware accelerated mouse cursors.

See also: al_enable_hardware_cursor al_gfx_capabilities al_select_mouse_cursor

exswitch

This program shows how to control the console switching mode, and let your program run in the background. These functions don't apply to every platform and driver, for example you can't control the switching mode from a DOS program.

See also: al_set_display_switch_mode

Lazarus example

This program shows a way to use Allegro.pas within Lazarus application. It presents a window with a menu that allows to load a bitmap, which is drawn in the window.

Read all comments, including the ones at the example.lpr file for detailed information.

Tools

Grabber

The grabber utility allows to create data files to be loaded by al_load_datafile. It is included in the original Allegro release at http://alleg.sourceforge.net/. You can download it also from http://www.allegro.cc/depot/Grabber/. The user manual is included with the applicacion.

Demonstration game

This game is a demonstration of the Allegro.pas library. Its goal is to create a good piece of code to show how to write your games using Allegro.pas. It includes a map editor. See the sources and read the documentation at the "demo" subdirectory to know how to use this demonstration in your own projects.

Help Alex the Allegator to get the coins and find the exit of the level. Use left and right cursor keys or the joystick to move Alex. Use the space bar or the joystick ''A'' button to jump. Press ''jump'' to play or [Esc] to exit.

Original game concept: Ken Silverman (http://www.advsys.net/ken/)

Programming and graphics: Guillermo Martinez (http://www.burdjia.com/)

Alex the Allegator character courtesy from: Johan Peitz (http://www.freelunchdesign.com/)

Title music (provisional): Garret Thomsom (g@sirsonic.com) (ripped from the original Allegro demo game)

Game music: Partners In Rhyme (http://www.partnersinrhyme.com/)

Sound FX ripped from games by Shawn Hargreaves and Johan Peitz

Authors

Allegro originally created by Shawn Hargreaves (http://talula.demon.co.uk/) and currently mantained by the Allegro Development Team (http://alleg.sourceforge.net/)

Main developer and project administrator: Guillermo Martinez

Additional developers:

Seoane (http://delphi.jmrds.com): Helped with Delphi compatibility in version 4.2.

Martin Kalbfuss: Helped testing and fixing some versions.

sberinde & carlejt (Allegro.pas' forum nicks) helped a lot finding bugs and testing patches for version 4.4.3.

torhu y SiegeLord (Allegro.cc forum nicks) helped a lot telling me how to support 64bit systems.

yamer (from Lazarus forum) Helped to identify and fix a bug in the exalpha example.

Thanks to the Spanish Delphi community (http://www.clubdelphi.net/), the Allegro community (http://www.allegro.cc/) and the Pascal game Development community (http://www.pascalgamedevelopment.com/)


Generated by PasDoc 0.13.0 on 2016-07-20 12:01:36