File: priklad2.cpp | Size: 1,816 bytes | Download file | Back to directory listing | BWPOW's homepage
#include <allegro.h>
#include <cstdio>
 
#define RES_W 800
#define RES_H 600
 
volatile int close_button_pressed = FALSE;
 
void close_button_handler(void)
{
    close_button_pressed = TRUE;
}
END_OF_FUNCTION(close_button_handler)
 
void main_koniec(int ret,const char *str)
{
    set_gfx_mode(GFX_TEXT,0,0,0,0);
    if(ret>0){
        allegro_message("CHYBA %d: %s\n",ret,str);
    }
    allegro_exit();
    exit(ret);
}
 
void main_koniec(void)
{
    main_koniec(0,"");
}
 
void main_koniec(int ret)
{
    main_koniec(ret,"Neznama chyba");
}
 
int main(void)
{
    if(allegro_init()!=0){
        fputs("Inicializacia allegra zlyhala!\n",stderr);
        return 1;
    }
    if(install_timer()!=0){
        fputs("Inicializacia timeru zlyhala!\n",stderr);
        return 1;
    }
 
    int depth=0;
    if((depth=desktop_color_depth())==0){
        depth=32;
    }
    if(depth<16){
        main_koniec(2,"Je potrebna aspon 16-bitova farebna hlbka!");
    }
 
    LOCK_FUNCTION(close_button_handler);
    set_close_button_callback(close_button_handler);
 
    set_window_title("Packoban");
 
    set_color_depth(depth);
    if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,RES_W,RES_H,0,0)!=0){
        if(set_gfx_mode(GFX_AUTODETECT,RES_W,RES_H,0,0)!=0){
            main_koniec(3,"Nepodarilo sa inicializovat graficky rezim!");
        }
    }
 
    int x=screen->w/2;
    int y=screen->h/2;
    int ax,ay;
    while(!close_button_pressed){
        int farba=makecol(rand()%256,rand()%256,rand()%256);
        line(screen,x,y,ax=rand()%screen->w,ay=rand()%screen->h,farba);
        x=ax;
        y=ay;
        if(rand()&1){
            circle(screen,x,y,5,farba);
        }
        else{
            rect(screen,x-5,y-5,x+5,y+5,farba);
        }
        rest(300);
    }
 
    main_koniec();
    return 0;
}
END_OF_MAIN()