This is a piece of code I copied from a tutorial of allegro.
When I changed the window width and height to 800, the program just crash, what happened?

set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 800, 0, 0);

buffer = create_bitmap( 800, 800);

#include <allegro.h>
#include <cstdlib>
#include <time.h>


int x = 100;
int y = 100;

int tempX = 100;
int tempY = 100;

int dir = 1; //This will keep track of the circles direction
            //1= up and left, 2 = down and left, 3 = up and right, 4 = down and right

BITMAP *buffer; //This will be our temporary bitmap for double buffering

void setupboard(){

    tempX = x;
    tempY = y;

    if (dir == 1 && x != 20 && y != 20){
     
        --x;
        --y;
              
    } else if (dir == 2 && x != 20 && y != 460){

        --x;
        ++y;

    } else if (dir == 3 && x != 620 && y != 20){

        ++x;
        --y;

    } else if (dir == 4 && x != 620 && y != 460){

        ++x;
        ++y;

    } else { 

        dir = rand() % 4 + 1;

    }    
    
    acquire_screen();
    circlefill ( buffer, tempX, tempY, 20, makecol( 0, 0, 0));
    circlefill ( buffer, x, y, 20, makecol( 128, 255, 0));
    draw_sprite( screen, buffer, 0, 0);
    release_screen();
    
    rest(10);

}    

int main(){

    allegro_init();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    
    buffer = create_bitmap( 640, 480); 
    
    while( !key[KEY_ESC]){
     
        setupboard();
   
    }    
    
    return 0;

}
END_OF_MAIN();

Dani AI

Generated

Quick diagnostic checklist and fixes for (and building on 's suggestion):

  • Always check Allegro calls for errors. If a mode or allocation fails the program can continue with uninitialized video structures and crash. Right after attempting the video mode, print the error string and abort if nonzero. Also check the bitmap allocation and bail out if it returns NULL.
int err = set_gfx_mode(...);
if (err) {
    allegro_message("set_gfx_mode failed: %s", allegro_error);
    return -1;
}

if (!buffer) {
    allegro_message("create_bitmap failed (out of memory)");
    return -1;
}
  • Use the actual screen size to compute movement limits instead of hard-coded numbers. Replace magic constants with values derived from the screen or buffer size so the circle never tries to move outside the bitmap:
int r = 20;
int minx = r, miny = r;
int maxx = SCREEN_W - r, maxy = SCREEN_H - r;
  • Prefer copying the whole buffer to the screen with blit(buffer, screen, 0,0,0,0, SCREEN_W, SCREEN_H) inside acquire_screen()/release_screen() for a clean full-frame transfer. Match your color depth to the desktop (try 32bpp on modern systems) or test with common resolutions like 800x600 to rule out driver/driver-mode restrictions.

A note on the "multiple of 4" rumor: that comes from scanline alignment issues on some 24bpp drivers. Allegro's software bitmaps handle scanline padding internally, so you usually don't need to force widths to multiples of 4. The important steps are: check the return values, use SCREEN_W/SCREEN_H for bounds, and report any allegro_error text if problems persist — that exact message will point to the real cause.

Recommended Answers

All 2 Replies

I'm not really sure why it would do this. But i'd suggest using standard resolutions like 800x600, instead of custom resolutions like what you just did there with 800x800. Also check to make sure that your program's color depth is equal to your desktop's color depth.

From my expereience with allegro, the way you write the program is what affect's it's ability to run in either full screen or windowed mode. Your program is almost guaranteed to run in full screen mode, but might now work in windowed mode.

I heard ppl said the dimension got to be a mutiiple of 4,
but i am never able to test it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.