Thread: Changing the pallete in allegro?

  1. #1
    Registered User
    Join Date
    Jan 2002

    Question Changing the pallete in allegro?

    Well I've made some code for the menu background to my game, which I will double buffer the menu text above, It generates a random set of blue lines (looks better than it sounds). Well, anyway, I want to cycle through the colors of the background [as an animation], instead of it being blue, by changing the pallete.

    Can anybody help me?

    #include <allegro.h>  // Uses allegro.
    #include <time.h>     // Time.h needed for random numbers.
    #include <stdlib.h>   // Stdlib needed for rand()
    #define SW 640        // Screen width
    #define SH 480        // Screen height
    int main( void )
     int y = 0;
     int color = 0;
     int z;
     srand(time(NULL));                          // Initialise randomness.
     allegro_init();                             // Initialise the
     set_color_depth(32);                        // graphics
     set_gfx_mode(GFX_AUTODETECT, SW, SH, 0, 0); // mode.
     install_keyboard();                         // Get keyboard ready.
     BITMAP *bmp = create_bitmap(SW, SH);        // Make the bitmap.
     clear_bitmap(bmp);                          // Clear it.
     while(y < SH)                               // Loop while y is less
     {                                           // Than screen height.
      hline(bmp, 0, y, SW, color);               // Draw line to bmp.
      y++;                                       // Increase y.
      z = rand() % 30;                           // Generate random number.
      if(z <= 15)
       if(color < 255)
        color+= 3;
       if(color > 0)
        color-= 3;
     blit(bmp, screen, 0, 0, 0, 0, SW, SH);    // Draw BMP to screen.
     while(!key[KEY_ESC])                      // Wait till esc, then quit.
      poll_keyboard();                         // This is needed for some.
     allegro_exit();                           // Bye!
     return 0;     

  2. #2
    Registered User
    Join Date
    Oct 2001
    Unfortunately, the "pallette" does not exist in any color depth higher than 8 bit (256 color). This is a hardware restraint, not software. A pallette in 8 bit modes resides in the video card, storing information for how each 8 bit value is interpreted as a color. For true color modes, the information on how to represent each pixel is stored within the pixel. There are 8 bits used to represent shades of red, 8 to represent shades of green, and 8 to represent shades of blue, giving us a total of 24 bits.

    8 bits is 256 shades of each color, which scientists tell us is the most the human eye can distinguish between. So why 32 bit color, if the human eye can only see 24 bits worth of color? The extra 8 bits are for hardware accelerated "alpha". Think, transparency. If I have a blue pixel that is opaque, and I place a red pixel over it on the display, but it has just half its alpha value set (to 128 in this case), then the video card changes the 32 bit value into a 24 bit value that is a mix of red and blue (purple).


    But I digress.

    In other words, you'll have to make your own palette algorithm and repaint each pixel you want to change for every frame. That can be a bit of a speed killer, especially if you must change every pixel of the screen per frame at a high resolution. (In, say, a fade.) You can either take advantage of that extra 8 bit alpha channel for your 32 bit color depth and/or use this handy little Allegro plugin which takes care of most of that for you and increases the speed dramatically.

    Hope that helps.

    Allegro precompiled Installer for Dev-C++, MSVC, and Borland:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  2. problem in allegro with sprite leaving trail
    By Leeman_s in forum C++ Programming
    Replies: 18
    Last Post: 09-13-2002, 03:04 PM
  3. double buffering for allegro
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 02:45 PM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. Replies: 2
    Last Post: 11-26-2001, 03:05 PM