File: priklad6.cpp | Size: 3,573 bytes | Download file | Back to directory listing | BWPOW's homepage
#include <allegro.h>
#include <cstdio>
#include <cmath>
 
#define RES_W 800
#define RES_H 600
 
#define DLZKA 512
 
struct XY{
    int x,y;
};
 
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!");
        }
    }
 
    BITMAP *actual=create_bitmap(screen->w,screen->h);
    if(actual==NULL) main_koniec(3,"Napodarilo sa naalokovat pamat pre bitmap!");
 
    BITMAP *back=create_bitmap(screen->w,screen->h);
    if(back==NULL) main_koniec(3,"Napodarilo sa naalokovat pamat pre bitmap!");
 
    clear_to_color(back,makecol(255,255,255));
 
    BITMAP *penguin=load_bitmap("penguin.bmp",NULL);
    if(penguin==NULL) main_koniec(3,"Napodarilo sa nacitat penguin.bmp!");
 
    for(int y=0;y<back->h;y+=penguin->h){
        for(int x=0;x<back->w;x+=penguin->w*2){
            int r=rand()&3;
            if(r==0) draw_sprite(back,penguin,x+((y/penguin->h)%2)*penguin->w,y);
            if(r==1) draw_sprite_v_flip(back,penguin,x+((y/penguin->h)%2)*penguin->w,y);
            if(r==2) draw_sprite_h_flip(back,penguin,x+((y/penguin->h)%2)*penguin->w,y);
            if(r==3) draw_sprite_vh_flip(back,penguin,x+((y/penguin->h)%2)*penguin->w,y);
        }
    }
 
    destroy_bitmap(penguin);
 
    BITMAP *hlava=load_bitmap("hlava.bmp",NULL);
    if(hlava==NULL) main_koniec(3,"Napodarilo sa nacitat hlava.bmp!");
 
    int x=screen->w/2;
    int y=screen->h/2;
    XY xy[DLZKA],a;
    float uhol=0;
    for(int i=0;i<DLZKA;i++){
        xy[i].x=x;
        xy[i].y=y;
    }
    int pos=0;
    a.x=-100;
    a.y=-100;
    drawing_mode(DRAW_MODE_TRANS,actual,0,0);
    while(!close_button_pressed){
        blit(back,actual,0,0,0,0,back->w,back->h);
        for(int i=0;i<DLZKA;i++){
            a=xy[(pos+i)%DLZKA];
            if(i+pos>=DLZKA){
                set_trans_blender(255,0,0,(i+1)*255/DLZKA);
                circlefill(actual,a.x,a.y,10,makecol(255,0,0));
            }
        }
        a.x+=(int)(cos(uhol)*8);
        a.y+=(int)(sin(uhol)*8);
        pivot_sprite(actual,hlava,a.x,a.y,hlava->w/2,hlava->h/2,fmul(ftofix(uhol),radtofix_r));
        uhol+=(float)(rand()%101-50)/100;
        if(a.x<0||a.y<0||a.x>=screen->w||a.y>=screen->h) uhol+=1;
        xy[(pos++)%DLZKA]=a;
        blit(actual,screen,0,0,0,0,actual->w,actual->h);
        rest(20);
    }
 
    destroy_bitmap(actual);
    destroy_bitmap(hlava);
 
    main_koniec();
    return 0;
}
END_OF_MAIN()