Guest User

Untitled

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