#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); show_mouse(screen); while(!close_button_pressed) { if(keypressed()){ int k=readkey(); if((k>>8)==KEY_ENTER||(k&255)=='q') break; } rest(50); } show_mouse(NULL); } 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; } if(install_mouse()<0){ fputs("Inicializacia mysi 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!"); BITMAP *jahoda=load_bitmap("jahoda.bmp",NULL); if(jahoda==NULL) main_koniec(3,"Napodarilo sa nacitat jahoda.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; int jx=-1,jy=-1; a.x=-100; a.y=-100; drawing_mode(DRAW_MODE_TRANS,actual,0,0); show_mouse(screen); 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); if(abs(a.x-jx)<32&&abs(a.y-jy)<32) jx=jy=-1; if(jx>=0&&jy>=0){ draw_sprite(actual,jahoda,jx-jahoda->w/2,jy-jahoda->h/2); } pivot_sprite(actual,hlava,a.x,a.y,hlava->w/2,hlava->h/2,fmul(ftofix(uhol),radtofix_r)); if(jx>=0&&jy>=0){ float ju=atan2f(jy-a.y,jx-a.x); if(ju<uhol) uhol-=0.1; else uhol+=0.1; } else{ uhol+=(float)(rand()%101-50)/100; } 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; if(mouse_b&1||key[KEY_SPACE]){ jx=mouse_x; jy=mouse_y; } show_mouse(NULL); blit(actual,screen,0,0,0,0,actual->w,actual->h); show_mouse(screen); rest(20); } show_mouse(NULL); destroy_bitmap(actual); destroy_bitmap(hlava); destroy_bitmap(jahoda); main_koniec(); return 0; } END_OF_MAIN()