Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "monolith.h"
- using namespace std;
- SDL_Surface *screen;
- //Initial video mode-----------------------------------------------------
- int monoInitVideo( Uint32 flags, int width, int height )
- {
- //Load SDL
- if( SDL_Init( SDL_INIT_VIDEO ) != 0 )
- { //straight forward stuff here
- cout << "Unable to start SDL: " << SDL_GetError() << "\n";
- return false;
- }
- atexit( SDL_Quit ); //And clean up.. apparently
- //get into full screen, specify flags, the usual
- screen = SDL_SetVideoMode( width, height, 16, flags );
- //check that screen has been set
- if( screen == NULL )
- {
- cout << "Unable to set video mode: " << SDL_GetError() << "\n";
- return false;
- }
- SDL_WM_SetCaption("Monolith","Monolith");
- //And finally, set mouse up
- SDL_ShowCursor( 0 );
- SDL_WarpMouse( width/2, height/2 );
- return true;
- }
- //Load image-------------------------------------------------------------
- SDL_Surface *monoLoadImage( string imageName, int &exitState )
- {
- SDL_Surface *temp; //may need to be moved from here
- exitState = false;
- temp = IMG_Load( imageName.c_str() );
- //check if that worked
- if( temp == NULL )
- {
- cout << "Could not load Image \"" << imageName << "\": " << SDL_GetError() << "\n";
- return false;
- }
- //Okay, we're good
- if( SDL_SetColorKey( temp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB
- ( temp -> format, COLOURKEY )) == -1 ) //Or are we?
- {
- cout << "Colour key will not be used: " << SDL_GetError() << "\n";
- }
- exitState = true;
- return temp;
- }
- //Sets extra parameters for object collision detection
- void monoPrepObject( image_coord_t object )
- {
- object.w_col = object.w * 0.80;
- object.h_col = object.h * 0.80;
- object.x_offset = ( object.w - object.w_col )/2;
- object.y_offset = ( object.h - object.h_col )/2;
- object.radius = (( object.w + object.h )/4 );
- }
- //Display Image----------------------------------------------------------
- void monoDrawImage( SDL_Surface *image, image_coord_t surface_param, image_coord_t dest_param )
- {
- SDL_Rect surface, dest;
- //Set image properties (size, etc)
- surface.x = surface_param.x;
- surface.y = surface_param.y;
- surface.w = surface_param.w;
- surface.h = surface_param.h;
- dest.x = dest_param.x;
- dest.y = dest_param.y;
- dest.w = dest_param.w;
- dest.h = dest_param.h;
- SDL_BlitSurface( image, &surface, screen, &dest );
- }
- //Collision Check--------------------------------------------------------
- int monoCheckBoundingBox( image_coord_t obj1, image_coord_t obj2 )
- {
- int left1, left2;
- int right1, right2;
- int top1, top2;
- int bottom1, bottom2;
- //load @params
- left1 = obj1.x + obj1.x_offset;
- left2 = obj2.x + obj2.x_offset;
- right1 = left1 + obj1.w_col;
- right2 = left2 + obj2.w_col;
- top1 = obj1.y + obj1.y_offset;
- top2 = obj2.y + obj2.y_offset;
- bottom1 = top1 + obj1.h_col;
- bottom2 = top2 + obj2.h_col;
- //finally, lets check for collision
- // relative to obj1
- if(( bottom1 <= top2 )&&( right1 >= left2 )) //bottom right
- return true;
- if(( bottom1 <= top2 )&&( left1 <= right2)) //bottom left
- return true;
- if(( top1 >= bottom2 )&&( right1 >= left2 )) //top right
- return true;
- if(( top1 >= bottom2 )&&( left1 <= right2 )) //top left
- return true;
- //no collisions occured
- return false;
- }
- int monoCheckCollision( image_coord_t obj1, image_coord_t obj2 )
- {
- //Basic radius check
- int dx = obj2.x - obj1.x;
- int dy = obj2.y - obj1.y;
- int dradii = obj1.radius + obj2.radius;
- if((( dx*dx )+( dy*dy )) < ( dradii*dradii ))
- {
- //Possible collision, got to bounding box method
- if( monoCheckBoundingBox( obj1, obj2 ))
- {
- //Left return a rough angle of where the collision occured
- //from (relative to obj1)
- int angle = ( int )( atan2(( float )obj2.y, ( float )obj2.x )
- - atan2(( float )obj1.y, ( float )obj1.x ));
- if( angle == 0 )
- return 360;
- else
- return angle;
- }
- }
- return false;
- }
- //Move Image-------------------------------------------------------------
- //AI---------------------------------------------------------------------
- //Main program entry point-----------------------------------------------
- int main( int argc, char* argv[] )
- {
- //Process params
- if( argc != 5 )
- {
- cout << "Program usage: \n \t\tmonolith width height file_name1 file_name2 \n\n";
- return false;
- }
- int result = 0;
- int width = atoi( argv[ 1 ]);
- int height = atoi( argv[ 2 ]);
- //Initialise video
- if( monoInitVideo( SDL_DOUBLEBUF | SDL_SWSURFACE, width, height ) == false )
- return true; //counts as error returning from main
- //Cool, lets make that a string
- //string params = new string( argv[1] );
- //Load image
- SDL_Surface *good_guy = monoLoadImage( string( argv[3]), result );
- if( result == false )
- return true;
- SDL_Surface *bad_guy = monoLoadImage( string( argv[4]), result );
- if( result == false )
- return true;
- //Define display properties
- image_coord_t surface, on_screen;
- SDL_Flip( screen );
- SDL_Delay( 2500 );
- //Cleanup
- SDL_FreeSurface( good_guy );
- SDL_FreeSurface( bad_guy );
- SDL_ShowCursor( 1 );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment