Guest User

toffyrn

a guest
May 13th, 2010
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | None | 0 0
  1. #include "monolith.h"
  2.  
  3. using namespace std;
  4. SDL_Surface *screen;
  5.  
  6. //Initial video mode-----------------------------------------------------
  7. int monoInitVideo( Uint32 flags, int width, int height )
  8. {
  9.     //Load SDL
  10.     if( SDL_Init( SDL_INIT_VIDEO ) != 0 )
  11.     { //straight forward stuff here
  12.         cout << "Unable to start SDL: " << SDL_GetError() << "\n";
  13.         return false;
  14.     }
  15.     atexit( SDL_Quit ); //And clean up.. apparently
  16.    
  17.     //get into full screen, specify flags, the usual
  18.     screen = SDL_SetVideoMode( width, height, 16, flags );
  19.    
  20.     //check that screen has been set
  21.     if( screen == NULL )
  22.     {
  23.         cout << "Unable to set video mode: " << SDL_GetError() << "\n";
  24.         return false;
  25.     }
  26.    
  27.     SDL_WM_SetCaption("Monolith","Monolith");
  28.    
  29.     //And finally, set mouse up
  30.     SDL_ShowCursor( 0 );
  31.     SDL_WarpMouse( width/2, height/2 );    
  32.    
  33.     return true;    
  34. }
  35.  
  36. //Load image-------------------------------------------------------------
  37. SDL_Surface *monoLoadImage( string imageName, int &exitState )
  38. {
  39.     SDL_Surface *temp; //may need to be moved from here
  40.     exitState = false;
  41.  
  42.     temp = IMG_Load( imageName.c_str() );
  43.     //check if that worked
  44.     if( temp == NULL )
  45.     {
  46.         cout << "Could not load Image \"" << imageName << "\": " << SDL_GetError() << "\n";
  47.         return false;
  48.     }
  49.    
  50.     //Okay, we're good
  51.     if( SDL_SetColorKey( temp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB
  52.         ( temp -> format, COLOURKEY )) == -1 ) //Or are we?
  53.     {
  54.         cout << "Colour key will not be used: " << SDL_GetError() << "\n";
  55.     }
  56.     exitState = true;
  57.    
  58.     return temp;
  59. }
  60.  
  61. //Sets extra parameters for object collision detection
  62. void monoPrepObject( image_coord_t object )
  63. {
  64.     object.w_col = object.w * 0.80;
  65.     object.h_col = object.h * 0.80;
  66.    
  67.     object.x_offset = ( object.w - object.w_col )/2;
  68.     object.y_offset = ( object.h - object.h_col )/2;
  69.    
  70.     object.radius = (( object.w + object.h )/4 );
  71. }
  72.  
  73. //Display Image----------------------------------------------------------
  74. void monoDrawImage( SDL_Surface *image, image_coord_t surface_param, image_coord_t dest_param )
  75. {
  76.     SDL_Rect surface, dest;
  77.    
  78.     //Set image properties (size, etc)
  79.     surface.x = surface_param.x;
  80.     surface.y = surface_param.y;
  81.     surface.w = surface_param.w;
  82.     surface.h = surface_param.h;
  83.    
  84.     dest.x = dest_param.x;
  85.     dest.y = dest_param.y;
  86.     dest.w = dest_param.w;
  87.     dest.h = dest_param.h;
  88.    
  89.     SDL_BlitSurface( image, &surface, screen, &dest );    
  90. }
  91.  
  92. //Collision Check--------------------------------------------------------
  93. int monoCheckBoundingBox( image_coord_t obj1, image_coord_t obj2 )
  94. {
  95.     int left1, left2;
  96.     int right1, right2;
  97.     int top1, top2;
  98.     int bottom1, bottom2;
  99.    
  100.     //load @params
  101.     left1 = obj1.x + obj1.x_offset;
  102.     left2 = obj2.x + obj2.x_offset;
  103.    
  104.     right1 = left1 + obj1.w_col;
  105.     right2 = left2 + obj2.w_col;
  106.    
  107.     top1 = obj1.y + obj1.y_offset;
  108.     top2 = obj2.y + obj2.y_offset;
  109.    
  110.     bottom1 = top1 + obj1.h_col;
  111.     bottom2 = top2 + obj2.h_col;
  112.    
  113.     //finally, lets check for collision
  114.     //                                          relative to obj1
  115.     if(( bottom1 <= top2 )&&( right1 >= left2 )) //bottom right
  116.         return true;
  117.    
  118.     if(( bottom1 <= top2 )&&( left1 <= right2)) //bottom left
  119.         return true;
  120.        
  121.     if(( top1 >= bottom2 )&&( right1 >= left2 )) //top right
  122.         return true;
  123.        
  124.     if(( top1 >= bottom2 )&&( left1 <= right2 )) //top left
  125.         return true;
  126.    
  127.     //no collisions occured
  128.     return false;
  129. }
  130.  
  131. int monoCheckCollision( image_coord_t obj1, image_coord_t obj2 )
  132. {
  133.     //Basic radius check
  134.     int dx = obj2.x - obj1.x;
  135.     int dy = obj2.y - obj1.y;
  136.     int dradii = obj1.radius + obj2.radius;
  137.    
  138.     if((( dx*dx )+( dy*dy )) < ( dradii*dradii ))
  139.     {
  140.         //Possible collision, got to bounding box method
  141.         if( monoCheckBoundingBox( obj1, obj2 ))
  142.         {
  143.             //Left return a rough angle of where the collision occured
  144.             //from (relative to obj1)
  145.             int angle = ( int )( atan2(( float )obj2.y, ( float )obj2.x )
  146.                                - atan2(( float )obj1.y, ( float )obj1.x ));
  147.            
  148.             if( angle == 0 )
  149.                 return 360;
  150.             else
  151.                 return angle;
  152.         }
  153.     }
  154.    
  155.     return false;    
  156. }
  157.  
  158. //Move Image-------------------------------------------------------------
  159.  
  160. //AI---------------------------------------------------------------------
  161.  
  162. //Main program entry point-----------------------------------------------
  163. int main( int argc, char* argv[] )
  164. {
  165.     //Process params
  166.     if( argc != 5 )
  167.     {
  168.         cout << "Program usage: \n \t\tmonolith width height file_name1 file_name2 \n\n";
  169.         return false;
  170.     }
  171.    
  172.     int result = 0;
  173.     int width = atoi( argv[ 1 ]);
  174.     int height = atoi( argv[ 2 ]);
  175.    
  176.     //Initialise video
  177.     if( monoInitVideo( SDL_DOUBLEBUF | SDL_SWSURFACE, width, height ) == false )
  178.         return true; //counts as error returning from main
  179.    
  180.     //Cool, lets make that a string
  181.     //string params = new string( argv[1] );
  182.    
  183.     //Load image
  184.     SDL_Surface *good_guy = monoLoadImage( string( argv[3]), result );
  185.    
  186.     if( result == false )
  187.         return true;
  188.        
  189.     SDL_Surface *bad_guy = monoLoadImage( string( argv[4]), result );
  190.    
  191.     if( result == false )
  192.         return true;
  193.        
  194.    
  195.    
  196.        
  197.        
  198.     //Define display properties
  199.     image_coord_t surface, on_screen;
  200.    
  201.     SDL_Flip( screen );
  202.     SDL_Delay( 2500 );
  203.    
  204.     //Cleanup
  205.     SDL_FreeSurface( good_guy );
  206.     SDL_FreeSurface( bad_guy );
  207.     SDL_ShowCursor( 1 );
  208.     return 0;    
  209. }
Advertisement
Add Comment
Please, Sign In to add comment