2

I'm just trying out the allegro library, and here is the code which I've got so far:

#include <allegro.h>

int main(int argc, char *argv[]) {
    allegro_init();  // initialize the allegro libraries
    install_keyboard(); // initialize keyboard functions
    
    set_color_depth(16); // set the color depth
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); // set up 640*480px window
    
    BITMAP *pic = NULL;
    pic = load_bitmap("C:/picture.bmp", NULL); // load the picture
    blit(pic, screen, 0, 0, 0, 0, 1000, 1000);

    readkey();
    destroy_bitmap(pic);
    return 0;
} 
END_OF_MAIN()

It works fine, but when I run it, while the program's window is open, Windows 7 changes the theme from Aero to Aero basic. If you aren't sure what I mean, this pops up (I got this from Google, which is why it says Vista rather than Windows 7):

http://www.suitedcowboys.com/wp-content/uploads/2007/01/010607_0906_HelloVistai28.png
(source: suitedcowboys.com)

  1. Why?
  2. How can I ensure that this doesn't happen?
Lundin
  • 195,001
  • 40
  • 254
  • 396
Matthew H
  • 5,831
  • 8
  • 47
  • 82
  • 1
    When you write "production" code, be sure to check return values, especially things that have good reason to fail, like `load_bitmap`. – Matthew Mar 28 '10 at 03:58
  • Yeah, I know. I'd never release rubbish code like this. xD Thanks though. – Matthew H Mar 28 '10 at 10:44

2 Answers2

5

Aero needs color set to 32 bit, but you're setting it to 16:

set_color_depth(16);

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • I love simple solutions. :) Thanks for the speedy answer. Will this impact the programs cross-platform capabilities though? According to the tutorial I'm following it does, but it does look pretty old. – Matthew H Mar 28 '10 at 00:58
  • What platforms use 16-bit color depth today? Most have 24 or 32. – jpyllman Mar 28 '10 at 02:32
  • jpyllman: Have you ever used RDP (Terminal Services) over a remote connection? I regularly use it at 8-bit color. It's also common for people to run VMs at lower bit depths because the hardware is easier to emulate. – Gabe Mar 28 '10 at 05:06
  • konforce's answer shows how to use the current color depth. Not much I can add. – Brendan Long Mar 28 '10 at 05:22
2

Unless you have good reason to use a specific color depth, do this:

int cd = desktop_color_depth();
if (cd < 15) cd = 32;
set_color_depth(cd);

While generally not a problem today, many older video cards only support one of 15/16 bit and one of 24/32 bit.

If you need to use 8-bit color depth because you use a palette, then just use the GFX_GDI driver for maximum compatibility.

Matthew
  • 47,584
  • 11
  • 86
  • 98