Advertisement
Guest User

Untitled

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