Computing a Picture

Let us quickly summarize the most essential concepts we have mentioned so far. The picture is imagined as being divided into small squares called pixels. In order to calculate the entire picture, the color of each pixel has to be computed and set. All in all, our pictures will be consisting of pixels organized in 600 rows and 800 columns.

In our programs, a color will be a type of value. To create a value of this type, we can use the function makecol, which is made available by the Allegro library. It has 3 parameters, each one specifying the intensity of the red, green and blue component, in that order. The maximum intensity for each component is 255. A grayish white color having 55% of full intensity[*] is created by the expression:

It is not physically possible to create all visible colors by joining three primary color components, regardless of the choice of primary colors. The primary colors that were selected for your monitor and television set can create a reasonably large gamut of colors by mixing light passing through screen's primary color filters. This gamut is larger then the one available by classical paints, but much smaller than the gamut of a photographic film. Monochromatic lasers can produce any visible color.

makecol(200, 200, 200)

A value of white color of full intensity is the result of the expression:

makecol(255, 255, 255)

It is not allowed to set any color component to a value greater than 255, or to a value smaller than 0. The values of color components are integers.

In programmers' parlance, black is a color, too. The value of the black color is produced by the expression:


Without further ado, here we present a table of some commonly used colors:

black makecol(0,0,0) light grey makecol(192,192,192)
blue makecol(0,0,255) grey makecol(128,128,128)
red makecol(255,0,0) dark grey makecol(64,64,64)
magentamakecol(255,0,255) pink makecol(255,192,203)
green makecol(0,255,0) violet makecol(139,0,255)
cyan makecol(0,255,255) brown makecol(150,75,0)
yellow makecol(255,255,0) orange makecol(255,127,0)
white makecol(255,255,255) electric blue makecol(125,249,255)

In our programs, the entire image displayed on the screen will be represented by a variable named surface. We will be using this surface as if it was a surface for painting. The programs will draw various objects on this surface, such as lines, points, rectangles and circles. After the picture on the surface has been completed, we will use the Allegro library to display the surface on the screen.

Let us see how to set a color of a pixel on a surface. If we would like to set a pixel on a 200th row and 300th column to blue color, we would write:

putpixel(surface, 200, 300, makecol(0,0,255) );

Now, how to draw a blue line? Well, if we were to set all the pixels on the 300th row that are located between columns 100 and 400 to the color blue, the result would be an image of a horizontal blue line.[*] We can do just that with the help of a for loop:

for (int i=100;i<400;i++)
    putpixel(surface, i, 300, makecol(0,0,255) );

The entire program that draws a horizontal blue line would go:

#include <allegro.h>

int main()
    if (allegro_init()!=0) return 1;
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0)!=0) 
        return 2;
    BITMAP* surface = create_bitmap(SCREEN_W, SCREEN_H);
    clear_to_color(surface, makecol(255,255,255) );
    for (int i=100;i<400;i++)
        putpixel(surface, i, 300, makecol(0,0,255) );
    blit(surface, screen, 0,0,0,0,SCREEN_W,SCREEN_H);
    while (!keypressed())
    return 0;

The given program produces a horizontal blue line on a white background. What makes the background white? The following command does:

    clear_to_color(surface, makecol(255,255,255) );

It sets every pixel of a given surface to a given color.

For the time being, you do not have to pay too much attention to parts of this program that have not been explained yet, so ignore the line beginning with word install_keyboard and everything above, as well as the line starting with word blit and all below it.