allegro newbie, need help on my frst program
vlad417

i have just started using the allegro library and i'm already confused why my program won't run properly.
IDE:Code::Blocks
Compiler:GNU GCC

#include <allegro.h>
using namespace std;

int main(){
    allegro_init();
    install_keyboard();
    install_timer();
    do{
        BITMAP *bmp = create_bitmap(800,640);
        clear_bitmap(bmp);
        int blue = makecol(0,0,256);
        putpixel(screen,100,100, blue);
    }while(!key[KEY_ESC]);
    allegro_exit();
}
END_OF_MAIN();

code is free to edit.

all i want this to do is open a window 800,640 and draw a blue pixel at (100,100). the program compiles and links, put when i open the .exe the concole opens and then windows says "pixels.exe has encountered a problem and needs to close. We are sorry for the inconvenience. blah blah blah" how do i get this baby to run the way i want it? thx.

kazzmir

You didnt call set_gfx_mode, the function that creates the graphics window.

int main(){
    allegro_init();
    install_keyboard();
    install_timer();
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 640, 0, 0 ); // add this
    ...
}

Also the lines

BITMAP *bmp = create_bitmap(800,640);
clear_bitmap(bmp);

don't do anything. Maybe you want them to do something later but in this program they do nothing.

vlad417

thx :D

EDIT:
so i continued expanding it and came up with this:

1#include <allegro.h>
2using namespace std;
3 
4int main(){
5 allegro_init();
6 install_keyboard();
7 install_timer();
8 install_mouse();
9 enable_hardware_cursor();
10 
11 int x;
12 int y;
13 
14 set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,640,0,0);
15 clear_to_color(screen,makecol(255,255,255));
16 int blue = makecol(0,0,255);
17 do{
18 x=mouse_x;
19 y=mouse_y;
20 _putpixel32(screen,x,y, blue);
21 }while(!key[KEY_ESC]);
22 
23 allegro_exit();
24}
25END_OF_MAIN();

what happens is it opens a white window and i can draw simply by dragging
problem: the pixels are rectangular, how do i get them to be square.
problem2: when i draw i get 2 streams of pixels at 2 seemingly unrelated places
problem3: about 1/3 of each rectangular pixel is blue, other 2/3s is black

LennyLen

You need to set the color depth before setting the gfx mode. This should fix the last two problems.

kazzmir

Oh yea, the default color depth is 8 but it looks like you want 32. You don't really need nor should use _putpixel32 unless you really know what you are doing.

int main(){
 ...
 set_color_depth( 32 );
 set_gfx_mode( ... );
 ...
 int blue = makecol( 0, 0, 255 );
 ...
 putpixel( screen, x, y, blue );
 ...
}

Jonatan Hedborg

As for the rectangular thing; what resolution is your desktop at? If it's not the same ratio as your monitor (4:3 usually 16:9 for widescreen monitors), or, all pixels will be rectangular.

Though it's probably because you are trying to draw a 32 bit integer to 8 bit surface.

vlad417

thx guys, ya the solution was to set the color depth and not use _putpixel32() it solved all 3 problems

James Stanley

Why 800x640?
800x600 and 640x480 are 'standard' resolutions.
Never seen anyone use 800x640 before...

Thread #591833. Printed from Allegro.cc