Listing 6. Source code for PutPixel02.
/*Project PutPixel02 This is an upgrade of the project named PutPixel01. The purpose of this project is to illustrate a relatively advanced approach to writing code to control the color of each pixel on the screen. An off-screen bitmap buffer with dimensions of 256x256 is created. Nested for loops are used to set the value of each pixel in the buffer. The color ranges from mid- intensity blue in the upper left corner to yellow in the lower right corner. An onscreen window with dimensions of 256x256 is also created. The color of each pixel in the buffer is set. Then the blit function is called to copy the contents of the buffer to the onscreen window. As a result, the entire program runs very rapidly, even with a color depth of 32. The color produced by the code in the nested for loops is displayed in the onscreen window as soon as that window appears on the screen. There is no visible delay waiting for the onscreen window to be painted. Pressing any key causes the program to terminate. */ #include <allegro.h> int main(){ allegro_init();//Allegro initialization install_keyboard();//Set up for keyboard input //Need to set the color depth before setting the // graphics mode. set_color_depth(32); //Set the graphics mode to a 256x256-pixel window. set_gfx_mode(GFX_AUTODETECT_WINDOWED,256,256,0,0); //Declare a pointer variable named buffer that can be // used to point to a BITMAP. BITMAP *buffer = NULL; //Create an empty bitmap and store its address in buffer buffer = create_bitmap(256,256); //Cycle through the buffer bitmap setting the color of // each pixel individually. for(int row = 0;row < 255;row++){ for(int column = 0;column < 255;column++){ putpixel(buffer,column,row,makecol(column,row,128)); }//end loop row }//end loop on column //Call the blit function to copy the off-screen buffer // contents to the screen. blit(buffer, screen, 0,0,0,0,256,256); //Block and wait until the user presses a key. readkey(); //Destroy bitmap to avoid memory leaks. destroy_bitmap(buffer); return 0;//Return 0 to indicate a successful run. }//end main function END_OF_MAIN() |