Topic : Allegro Vivace
Author : Filipe Medeiros
Page : << Previous 4  Next >>
Go to page :


   void vline(BITMAP *bmp, int x, int y1, int y2, int color);
     void hline(BITMAP *bmp, int x1, int y, int x2, int color);
     void line(BITMAP *bmp, int x1, int y1, int x2, int y2, int color);
     void triangle(BITMAP *bmp, int x1, y1, x2, y2, x3, y3, int color);
     void rect(BITMAP *bmp, int x1, int y1, int x2, int y2, int color);
     void rectfill(BITMAP *bmp, int x1, int y1, int x2, int y2, int color);
     void circle(BITMAP *bmp, int x, int y, int radius, int color);
     void circlefill(BITMAP *bmp, int x, int y, int radius, int color);


Try modifying the example program `src/4.2/ii' to use some of these.  For information about them, read the file `allegro.txt' in the `docs' subdirectory of your Allegro directory.
If you have trouble with this, look at the example `src/4.2/iii' which draws one of each at various places on the screen.

4.2.4 Writing text

To write text onto the screen, you use the following functions:

     void textout(BITMAP *bmp, FONT *f, unsigned char *s, int x, y, int c);
     void textout_center(BITMAP *bmp, FONT *f, unsigned char *s, int x, y, int c);

    `bmp' is, of course, the bitmap onto which you want to write.
    The FONT struct is defined in `allegro.h' and is used to refer to different fonts in memory.  You use it in much the same way that you use the BITMAP struct -- most of the time you just pass pointers to them to various functions, and don't need to know anything about what is actually in the struct.
    You can create your own fonts, use the ones from the distribution of GRX (another graphics library), which originally come from XFree86 (a windowing system for Unix), and convert fonts from Windows's TTF format.  For all of these you need to know how to use Allegro's Grabber utility and datafiles.  You can see `grabber.txt' for more information, but datafiles aren't discussed until much later in this tutorial. For now, though, you can just pass `font' to these routines; it's a FONT * declared in `allegro.h' and refers to the 8x8 BIOS font.
    `s' is the string you want to print, `x' and `y' are the coordinates, and `c' is the colour.  If `c' is -1 and the font is proportional, Allegro will use the colour information stored in the font, allowing multicolour fonts.
    `textout_center' is almost identical; the only difference is that it centres text about the x-coordinate.
The background colour for text output can be set with:

     void text_mode(int mode);

where `mode' is the new background colour.  If mode is negative the background will not be drawn, and what was there before will show through.
    Play around with these for yourself, or look at `src/4.2/iv' which demonstrates them.

4.3 Palette manipulation

Note
For most purposes, palettes only have meaning in 8-bit colour
(256 colurs).  In Allegro 3.0 you have the option of using
higher colour depths; if you do, the rest of this section won't
have much effect.  See the Pot of Gold, linked from the
Documentation part of the Allegro web pages, for information on
higher colour depths.

4.3.1 Palette explanation

When you draw to the screen, you specify a colour number from 0 to 255.   Conventionally the lower 16 colours are black, blue, green, cyan, red, magenta, brown, light grey, dark grey, bright blue, bright green, ..., bright magenta, yellow, white.
    However, the remaining 240 colours are not always the same. Before using them you should tell the computer what colour you want to appear on the screen when you set pixels to the given colour number.  It's safest to explicitly set all colours you use; that way you can be sure that they'll appear how you want them to appear.
    I will call the colour numbers you send to the graphics functions "logical" colours and the actual colour which appears on the screen for each will be the associated "physical" colour.  So we need functions to assign physical colours to logical colours.
    Logical colours are expressed as numbers from 0 to 255.  Each possible physical colour is expressed as three numbers, each of which ranges from 0 to 63.   These numbers represent the strengths of red, green and blue in the colour.   63 is full strength of course.  To store physical colours, use the RGB struct which is defined in `allegro.h':

     typedef struct RGB
     {
         unsigned char r, g, b;
     } RGB;


Needless to say, `r', `g' and `b' are the red, green and blue intensities of the colour, as described above.

4.3.2 Changing a single logical colour's physical appearance


To change the appearance of a logical colour, use the function:

     void set_color (int index, RGB *p);

`index' is the logical colour number and `p' points to an RGB record as above.
    The example program `src/4.3/ii' demonstrates the use of this function by setting colour 0 (the background colour) to be different colours as you press keys.   Press ESC to quit.
    Note that there is a slight complication here, in that if you intend to change colours repeatedly over a period of time, you should call the Allegro function `vsync' between changes (which will also add a certain delay). The main reason for this is that some video cards don't like you fiddling with the palette when they are trying to display things, and they display the wrong colours from time to time, resulting in "snow" appearing. `vsync' waits for the monitor to reach the end of the frame, which removes the problem. We'll see more of `vsync' later on.

4.3.3 Changing the entire palette

    To change the whole palette at once we could call `set_color' repeatedly, but this would be slow, awkward and inefficient. Instead, `allegro.h' defines the type `PALETTE' to be an array of 256 RGB records, one for each logical colour.   You can create your entire palette in this array and then set it with a single function call:

     void set_palette (PALETTE p);

    The example `src/4.3/iii' demonstrates this function by drawing a rainbow of colours and then switching the palette to a greyscale one.

4.3.4 Fading in and out

    In low colour-depth modes (e.g. 256 colours) this is simple: to fade out you keep making the palette darker and darker until it's all black, and to fade in you do it the other way around.

Allegro has several functions to do this; the most basic are:

     void fade_in (PALETTE p, int speed);
     void fade_out (int speed);


    Note that `fade_in' requires you to say what palette you are fading to; `fade_out' doesn't need a palette because it assumes you mean to fade from the current palette.
    `speed' is the speed of the fade, ranging from 1 to 64 with 64 being an instantaneous change. `src/4.3/iv' demonstrates these functions.

4.4 Simple animation

4.4.1 What is animation?


    Animation is making things appear to move.  This could mean making a pixel move around the screen, but it is usually taken to mean repeatedly changing something's appearance so that it seems to propel itself.
    For now we will just look at how to make simple things move around the screen; the second interpretation will be dealt with later on when we look at sprites.

4.4.2 Making things appear to move

    The simplest way to make something appear to move is to erase it from where it is and put it on the screen again somewhere else.  It's a philosophical matter whether it has in fact moved; since it doesn't exist anyway there's not a lot of point worrying about it.  So we'll just say that it has moved.
    To make a solid circle move across the screen you could do something like this:

     int x;
     circlefill (screen, 0, 100, 5, 15);         /* draw first time */
     for (x = 1; x < 320; x++) {
         delay (10);                          


Page : << Previous 4  Next >>