Guest User

toffyrn

a guest
May 13th, 2010
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.72 KB | None | 0 0
  1. #include "monolith.h"
  2.  
  3.  
  4.  
  5. using namespace std;
  6.  
  7. SDL_Surface *screen;
  8.  
  9.  
  10.  
  11. //Initial video mode-----------------------------------------------------
  12.  
  13. int monolith::monoInitVideo( Uint32 flags, int width, int height )
  14.  
  15. {
  16.  
  17.     //Load SDL
  18.  
  19.     if( SDL_Init( SDL_INIT_VIDEO ) != 0 )
  20.  
  21.     { //straight forward stuff here
  22.  
  23.         cout << "Unable to start SDL: " << SDL_GetError() << "\n";
  24.  
  25.         return false;
  26.  
  27.     }
  28.  
  29.     atexit( SDL_Quit ); //And clean up.. apparently
  30.  
  31.    
  32.  
  33.     //get into full screen, specify flags, the usual
  34.  
  35.     screen = SDL_SetVideoMode( width, height, 16, flags );
  36.  
  37.    
  38.  
  39.     //check that screen has been set
  40.  
  41.     if( screen == NULL )
  42.  
  43.     {
  44.  
  45.         cout << "Unable to set video mode: " << SDL_GetError() << "\n";
  46.  
  47.         return false;
  48.  
  49.     }
  50.  
  51.    
  52.  
  53.     SDL_WM_SetCaption("Monolith","Monolith");
  54.  
  55.    
  56.  
  57.     //And finally, set mouse up
  58.  
  59.     SDL_ShowCursor( 0 );
  60.  
  61.     SDL_WarpMouse( width/2, height/2 );    
  62.  
  63.    
  64.  
  65.     return true;    
  66.  
  67. }
  68.  
  69.  
  70.  
  71. //Load image-------------------------------------------------------------
  72.  
  73. SDL_Surface *monolith::monoLoadImage( string imageName, int &exitState )
  74.  
  75. {
  76.  
  77.     SDL_Surface *temp; //EDIT: missing ";"
  78.  
  79.     exitState = 0;
  80.  
  81.  
  82.  
  83.     temp = IMG_Load( imageName.c_str() );
  84.  
  85.     //check if that worked
  86.  
  87.     if( temp == NULL )
  88.  
  89.     {
  90.  
  91.         cout << "Could not load Image \"" << imageName << "\": " << SDL_GetError() << "\n";
  92.  
  93.         return 0;
  94.  
  95.     }
  96.  
  97.    
  98.  
  99.     //Okay, we're good
  100.  
  101.     if( SDL_SetColorKey( temp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB
  102.  
  103.         ( temp -> format, COLOURKEY )) == -1 ) //Or are we?
  104.  
  105.     {
  106.  
  107.         cout << "Colour key will not be used: " << SDL_GetError() << "\n";
  108.  
  109.     }
  110.  
  111.     exitState = 1; //EDIT: missing ";"
  112.  
  113.    
  114.  
  115.     return temp;
  116.  
  117. }
  118.  
  119.  
  120.  
  121. //Sets extra parameters for object collision detection
  122.  
  123. void monolith::monoPrepObject( image_coord_t object )
  124.  
  125. {
  126.  
  127.     object.w_col = object.w * 0.80;
  128.  
  129.     object.h_col = object.h * 0.80;
  130.  
  131.    
  132.  
  133.     object.x_offset = ( object.w - object.w_col )/2;
  134.  
  135.     object.y_offset = ( object.h - object.h_col )/2;
  136.  
  137.    
  138.  
  139.     object.radius = (( object.w + object.h )/4 );
  140.  
  141. }
  142.  
  143.  
  144.  
  145. //Display Image----------------------------------------------------------
  146.  
  147. void monolith::monoDrawImage( SDL_Surface *image, image_coord_t surface_param, image_coord_t dest_param )
  148.  
  149. {
  150.  
  151.     SDL_Rect surface, dest;
  152.  
  153.    
  154.  
  155.     //Set image properties (size, etc)
  156.  
  157.     surface.x = surface_param.x;
  158.  
  159.     surface.y = surface_param.y;
  160.  
  161.     surface.w = surface_param.w;
  162.  
  163.     surface.h = surface_param.h;
  164.  
  165.    
  166.  
  167.     dest.x = dest_param.x;
  168.  
  169.     dest.y = dest_param.y;
  170.  
  171.     dest.w = dest_param.w;
  172.  
  173.     dest.h = dest_param.h;
  174.  
  175.    
  176.  
  177.     SDL_BlitSurface( image, &surface, screen, &dest );    
  178.  
  179. }
  180.  
  181.  
  182.  
  183. //Collision Check--------------------------------------------------------
  184.  
  185. // @ collision, angle (deg) of obj2 relative to obj1 is returned.
  186.  
  187. // 0 means no collision (360 is returned in place of 0deg collision angle)
  188.  
  189. int monolith::monoCheckCollision( image_coord_t obj1, image_coord_t obj2 )
  190.  
  191. {
  192.  
  193.     //Basic radius check
  194.  
  195.     int dx = obj2.x - obj1.x;
  196.  
  197.     int dy = obj2.y - obj1.y;
  198.  
  199.     int dradii = obj1.radius + obj2.radius;
  200.  
  201.    
  202.  
  203.     if((( dx*dx )+( dy*dy )) < ( dradii*dradii ))
  204.  
  205.     {
  206.  
  207.         //Possible collision, got to bounding box method
  208.  
  209.         if( monoCheckBoundingBox( obj1, obj2 ))
  210.  
  211.         {
  212.  
  213.             //Left return a rough angle of where the collision occured
  214.  
  215.             //from (relative to obj1)
  216.  
  217.             int angle = ( int )( atan2(( float )obj2.y, ( float )obj2.x )
  218.  
  219.                                - atan2(( float )obj1.y, ( float )obj1.x ));
  220.  
  221.            
  222.  
  223.             if( angle == 0 )
  224.  
  225.                 return 360;
  226.  
  227.             else
  228.  
  229.                 return angle;
  230.  
  231.         }
  232.  
  233.     }
  234.  
  235.    
  236.  
  237.     return 0;    
  238.  
  239. }
  240.  
  241.  
  242.  
  243. int monolith::monoCheckBoundingBox( image_coord_t obj1, image_coord_t obj2 )
  244.  
  245. {
  246.  
  247.     int left1, left2;
  248.  
  249.     int right1, right2;
  250.  
  251.     int top1, top2;
  252.  
  253.     int bottom1, bottom2;
  254.  
  255.    
  256.  
  257.     //load @params
  258.  
  259.     left1 = obj1.x + obj1.x_offset;
  260.  
  261.     left2 = obj2.x + obj2.x_offset;
  262.  
  263.    
  264.  
  265.     right1 = left1 + obj1.w_col;
  266.  
  267.     right2 = left2 + obj2.w_col;
  268.  
  269.    
  270.  
  271.     top1 = obj1.y + obj1.y_offset;
  272.  
  273.     top2 = obj2.y + obj2.y_offset;
  274.  
  275.    
  276.  
  277.     bottom1 = top1 + obj1.h_col;
  278.  
  279.     bottom2 = top2 + obj2.h_col;
  280.  
  281.    
  282.  
  283.     //finally, lets check for collision
  284.  
  285.     //                                          relative to obj1
  286.  
  287.     if(( bottom1 <= top2 )&&( right1 >= left2 )) //bottom right
  288.  
  289.         return true;
  290.  
  291.    
  292.  
  293.     if(( bottom1 <= top2 )&&( left1 <= right2)) //bottom left
  294.  
  295.         return true;
  296.  
  297.        
  298.  
  299.     if(( top1 >= bottom2 )&&( right1 >= left2 )) //top right
  300.  
  301.         return true;
  302.  
  303.        
  304.  
  305.     if(( top1 >= bottom2 )&&( left1 <= right2 )) //top left
  306.  
  307.         return true;
  308.  
  309.    
  310.  
  311.     //no collisions occured
  312.  
  313.     return false;
  314.  
  315. }
  316.  
  317.  
  318.  
  319. //Move Image-------------------------------------------------------------
  320.  
  321.  
  322.  
  323. //AI---------------------------------------------------------------------
  324.  
  325.  
  326.  
  327. //Main program entry point-----------------------------------------------
  328.  
  329. int main( int argc, char* argv[] )
  330.  
  331. {
  332.  
  333.     //Process params
  334.  
  335.     if( argc != 5 )
  336.  
  337.     {
  338.  
  339.         cout << "Program usage: \n \t\tmonolith width height file_name1 file_name2 \n\n";
  340.  
  341.         return 0;
  342.  
  343.     }
  344.  
  345.    
  346.  
  347.     int result = 0;
  348.  
  349.     int width = atoi( argv[ 1 ]);
  350.  
  351.     int height = atoi( argv[ 2 ]);
  352.  
  353.  
  354.  
  355.     //EDIT: your functions are NOT static, and
  356.  
  357.     ////////and therefore need a object.
  358.  
  359.     ////////Not sure if this is how you want it to be...
  360.  
  361.     monolith my_obj;
  362.  
  363.    
  364.  
  365.     //Initialise video
  366.  
  367.     //EDIT: using my_obj's function
  368.  
  369.     if( my_obj.monoInitVideo( SDL_DOUBLEBUF | SDL_SWSURFACE, width, height ) == false )
  370.  
  371.         return 1; //counts as error returning from main
  372.  
  373.    
  374.  
  375.     //Cool, lets make that a string
  376.  
  377.     //string params = new string( argv[1] );
  378.  
  379.    
  380.  
  381.     //Load image
  382.  
  383.     //EDIT: using my_obj's function
  384.  
  385.     SDL_Surface *good_guy = my_obj.monoLoadImage( string( argv[3]), result );
  386.  
  387.    
  388.  
  389.     if( result == false )
  390.  
  391.         return 1;
  392.  
  393.        
  394.  
  395.     //EDIT: using my_obj's function
  396.  
  397.     SDL_Surface *bad_guy = my_obj.monoLoadImage( string( argv[4]), result );
  398.  
  399.    
  400.  
  401.     if( result == false )
  402.  
  403.         return 1;
  404.  
  405.        
  406.  
  407.    
  408.  
  409.    
  410.  
  411.        
  412.  
  413.        
  414.  
  415.     //Define display properties
  416.  
  417.     image_coord_t surface, on_screen;
  418.  
  419.    
  420.  
  421.     SDL_Flip( screen );
  422.  
  423.     SDL_Delay( 2500 );
  424.  
  425.    
  426.  
  427.     //Cleanup
  428.  
  429.     SDL_FreeSurface( good_guy );
  430.  
  431.     SDL_FreeSurface( bad_guy );
  432.  
  433.     SDL_ShowCursor( 1 );
  434.  
  435.     return 0;    
  436.  
  437. }
Advertisement
Add Comment
Please, Sign In to add comment