File: priklad1.cpp | Size: 4,278 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");
}
 
void pauza(BITMAP *bmp)
{
    clear_keybuf();
 
    for(int y=0;y<bmp->h;y++){
        for(int x=0;x<bmp->w;x++){
            int c=getpixel(bmp,x,y);
            c=(getr(c)+getg(c)+getb(c))/3;
            putpixel(bmp,x,y,makecol(c,c,c));
        }
    }
 
    BITMAP *pauza=load_bitmap("pauza.bmp",NULL);
    if(pauza!=NULL){
        draw_sprite(bmp,pauza,(bmp->w-pauza->w)/2,(bmp->h-pauza->h)/2);
        destroy_bitmap(pauza);
    }
 
    blit(bmp,screen,0,0,0,0,bmp->w,bmp->h);
 
    while(!close_button_pressed)
    {
        if(keypressed()){
            int k=readkey();
            if((k>>8)==KEY_ENTER||(k&255)=='q') break;
        }
        rest(50);
    }
}
 
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;
    }
    if(install_keyboard()!=0){
        fputs("Inicializacia klavesnice 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){
            draw_sprite(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&&!poll_keyboard()&&!key[KEY_ESC]){
 
        if(key[KEY_P]){
            pauza(actual);
        }
 
        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));
 
        if(key[KEY_LEFT]) uhol+=0.1;
        if(key[KEY_RIGHT]) uhol-=0.1;
 
        if(a.x<0||a.x>=actual->w) a.x=(a.x+actual->w)%actual->w;
        if(a.y<0||a.y>=actual->h) a.y=(a.y+actual->h)%actual->h;
 
        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()