Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not
  2. be redestributed without written permission.*/
  3.  
  4. //The headers
  5. #include "SDL/SDL.h"
  6. #include "SDL/SDL_image.h"
  7. #include <string>
  8.  
  9. //Screen attributes
  10. const int SCREEN_WIDTH = 640;
  11. const int SCREEN_HEIGHT = 480;
  12. const int SCREEN_BPP = 32;
  13.  
  14. //The surfaces
  15. SDL_Surface *image = NULL;
  16. SDL_Surface *screen = NULL;
  17.  
  18. //The event structure that will be used
  19. SDL_Event event;
  20.  
  21. SDL_Surface *load_image( std::string filename )
  22. {
  23.     //The image that's loaded
  24.     SDL_Surface* loadedImage = NULL;
  25.  
  26.     //The optimized image that will be used
  27.     SDL_Surface* optimizedImage = NULL;
  28.  
  29.     //Load the image
  30.     loadedImage = IMG_Load( filename.c_str() );
  31.  
  32.     //If the image loaded
  33.     if( loadedImage != NULL )
  34.     {
  35.         //Create an optimized image
  36.         optimizedImage = SDL_DisplayFormat( loadedImage );
  37.  
  38.         //Free the old image
  39.         SDL_FreeSurface( loadedImage );
  40.     }
  41.  
  42.     //Return the optimized image
  43.     return optimizedImage;
  44. }
  45.  
  46. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
  47. {
  48.     //Temporary rectangle to hold the offsets
  49.     SDL_Rect offset;
  50.  
  51.     //Get the offsets
  52.     offset.x = x;
  53.     offset.y = y;
  54.  
  55.     //Blit the surface
  56.     SDL_BlitSurface( source, NULL, destination, &offset );
  57. }
  58.  
  59. bool init()
  60. {
  61.     //Initialize all SDL subsystems
  62.     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  63.     {
  64.         return false;
  65.     }
  66.  
  67.     //Set up the screen
  68.     screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  69.  
  70.     //If there was an error in setting up the screen
  71.     if( screen == NULL )
  72.     {
  73.         return false;
  74.     }
  75.  
  76.     //Set the window caption
  77.     SDL_WM_SetCaption( "Event test", NULL );
  78.  
  79.     //If everything initialized fine
  80.     return true;
  81. }
  82.  
  83. bool load_files()
  84. {
  85.     //Load the image
  86.     image = load_image( "x.png" );
  87.  
  88.     //If there was an error in loading the image
  89.     if( image == NULL )
  90.     {
  91.         return false;
  92.     }
  93.  
  94.     //If everything loaded fine
  95.     return true;
  96. }
  97.  
  98. void clean_up()
  99. {
  100.     //Free the surface
  101.     SDL_FreeSurface( image );
  102.  
  103.     //Quit SDL
  104.     SDL_Quit();
  105. }
  106.  
  107. int main( int argc, char* args[] )
  108. {
  109.     //Make sure the program waits for a quit
  110.     bool quit = false;
  111.  
  112.     //Initialize
  113.     if( init() == false )
  114.     {
  115.         return 1;
  116.     }
  117.  
  118.     //Load the files
  119.     if( load_files() == false )
  120.     {
  121.         return 1;
  122.     }
  123.  
  124.     //Apply the surface to the screen
  125.     apply_surface( 0, 0, image, screen );
  126.  
  127.     //Update the screen
  128.     if( SDL_Flip( screen ) == -1 )
  129.     {
  130.         return 1;
  131.     }
  132.  
  133.     //While the user hasn't quit
  134.     while( quit == false )
  135.     {
  136.         //While there's an event to handle
  137.         while( SDL_PollEvent( &event ) )
  138.         {
  139.             //If the user has Xed out the window
  140.             if( event.type == SDL_QUIT )
  141.             {
  142.                 //Quit the program
  143.                 quit = true;
  144.             }
  145.         }
  146.     }
  147.  
  148.     //Free the surface and quit SDL
  149.     clean_up();
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement