Advertisement
hugol

Untitled

Mar 24th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <SDL.h>
  2. SDL_Surface * ekran = NULL;
  3. SDL_Surface * obraz = NULL;
  4. SDL_Rect sprst[ 3 ];
  5. SDL_Rect dprst[ 3 ];
  6. SDL_Event zdarzenie;
  7. bool wyjscie = false;
  8.  
  9. void updateworld(int *mapa);
  10.  
  11. void findway(int *mapa);
  12.  
  13. int main( int argc, char * args[] )
  14. {
  15.     SDL_Init( SDL_INIT_EVERYTHING );
  16.     ekran = SDL_SetVideoMode( 320, 320, 32, SDL_SWSURFACE );
  17.     obraz = SDL_LoadBMP( "sprite.bmp" );
  18.     SDL_WM_SetCaption( "Graph", NULL );
  19.    
  20.  
  21.  
  22.  
  23.     int mapa[10*10];
  24.     for (int i =0; i<10*10; i++)
  25.         mapa[i]=2;
  26.  
  27.     mapa[99]= 0;
  28.     mapa[0] = 1;
  29.    
  30.     int myszkaY,myszkaX;
  31.  
  32.     while( !wyjscie )
  33.     {
  34.         while( SDL_PollEvent( & zdarzenie ) )
  35.         {
  36.             if( zdarzenie.type == SDL_QUIT )
  37.             {
  38.                 wyjscie = true;
  39.             }
  40.             else if( zdarzenie.type == SDL_MOUSEBUTTONDOWN )
  41.             {      
  42.                 myszkaX = zdarzenie.motion.x;
  43.                 myszkaY = zdarzenie.motion.y;
  44.                 int x = myszkaX/32;
  45.                 int y = myszkaY/32;
  46.                 switch(zdarzenie.button.button)
  47.                 {
  48.                 case(SDL_BUTTON_LEFT):
  49.                     mapa[y*10 + x]=3;
  50.                     break;
  51.                 case(SDL_BUTTON_RIGHT):
  52.                     mapa[y*10 + x]=2;
  53.                     break;
  54.                 default:
  55.                     break;
  56.                 }  
  57.             }
  58.             else if( zdarzenie.type == SDL_KEYDOWN )
  59.             {
  60.                 switch( zdarzenie.key.keysym.sym )
  61.                 {
  62.                 case SDLK_F1:
  63.                    
  64.                     // search
  65.                     findway(mapa);
  66.  
  67.                     break;
  68.                 }
  69.             }
  70.         }
  71.         updateworld(mapa); 
  72.         SDL_Delay(1);    
  73.     }
  74.      
  75.     SDL_Quit();
  76.     SDL_FreeSurface( obraz );
  77.     return 0;
  78. }
  79.  
  80. void updateworld(int *mapa)
  81. {
  82.     SDL_Rect dest,source;
  83.     dest.h=32; dest.w=32;
  84.     source.h=32; source.w=32; source.y=0;
  85.     for (int i =0; i<10*10; i++)
  86.     {
  87.         dest.y=(i/10) *32;
  88.         dest.x=(i%10) * 32;
  89.         source.x=mapa[i]*32;
  90.         SDL_BlitSurface( obraz, & source, ekran, & dest );
  91.     }
  92.     SDL_Flip( ekran );
  93. }
  94.  
  95. // algorytm
  96.  
  97. struct point
  98. {
  99.     bool odwiedzone;
  100. };
  101.  
  102.  
  103. bool search(int pos, int finish, point *bmapa, int *mapa)
  104. {
  105.    
  106.     // drawing part
  107.     mapa[pos]=4;
  108.     updateworld(mapa);
  109.     SDL_Delay(200);
  110.  
  111.     // alg part
  112.     bmapa[pos].odwiedzone=true;
  113.     if (pos == finish)
  114.         return true;
  115.     else
  116.         for (int x=-1; x<=1; x++)
  117.             for (int y=-1; y<=1; y++)
  118.                 if (!bmapa[pos + y*10 +x].odwiedzone &&  !( x==0 && y==0) &&
  119.                     (pos + y*10 +x)>=0 && (pos + y*10 +x) <100)
  120.                     return search(pos + y*10 +x,finish,bmapa,mapa);
  121. }
  122.  
  123. void findway(int *mapa)
  124. {
  125.     point bmapa[100];
  126.     for (int i =0; i<100; i++)
  127.         if (mapa[i]==3)
  128.             bmapa[i].odwiedzone=true;
  129.         else
  130.             bmapa[i].odwiedzone=false;
  131.  
  132.     search(0,99,bmapa,mapa);
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement