Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. /*This source code copyrighted by Lazy Foo' Productions (2004-2020)
  2. and may not be redistributed without written permission.*/
  3.  
  4. //Using SDL, SDL_image, standard IO, and strings
  5. #include <SDL.h>
  6. #include <SDL_image.h>
  7. #include <stdio.h>
  8. #include <string>
  9.  
  10. //Screen dimension constants
  11. const int SCREEN_WIDTH = 640;
  12. const int SCREEN_HEIGHT = 480;
  13.  
  14. //Starts up SDL and creates window
  15. bool init();
  16.  
  17. //Loads media
  18. bool loadMedia();
  19.  
  20. //Frees media and shuts down SDL
  21. void close();
  22.  
  23. //Loads individual image
  24. SDL_Surface* loadSurface( std::string path );
  25.  
  26. //The window we'll be rendering to
  27. SDL_Window* gWindow = NULL;
  28.    
  29. //The surface contained by the window
  30. SDL_Surface* gScreenSurface = NULL;
  31.  
  32. //Current displayed PNG image
  33. SDL_Surface* gPNGSurface = NULL;
  34.  
  35. bool init()
  36. {
  37.     //Initialization flag
  38.     bool success = true;
  39.  
  40.     //Initialize SDL
  41.     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  42.     {
  43.         printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
  44.         success = false;
  45.     }
  46.     else
  47.     {
  48.         //Create window
  49.         gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  50.         if( gWindow == NULL )
  51.         {
  52.             printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
  53.             success = false;
  54.         }
  55.         else
  56.         {
  57.             //Initialize PNG loading
  58.             int imgFlags = IMG_INIT_PNG;
  59.             if( !( IMG_Init( imgFlags ) & imgFlags ) )
  60.             {
  61.                 printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
  62.                 success = false;
  63.             }
  64.             else
  65.             {
  66.                 //Get window surface
  67.                 gScreenSurface = SDL_GetWindowSurface( gWindow );
  68.             }
  69.         }
  70.     }
  71.  
  72.     return success;
  73. }
  74.  
  75. bool loadMedia()
  76. {
  77.     //Loading success flag
  78.     bool success = true;
  79.  
  80.     //Load PNG surface
  81.     gPNGSurface = loadSurface( "06_extension_libraries_and_loading_other_image_formats/loaded.png" );
  82.     if( gPNGSurface == NULL )
  83.     {
  84.         printf( "Failed to load PNG image!\n" );
  85.         success = false;
  86.     }
  87.  
  88.     return success;
  89. }
  90.  
  91. void close()
  92. {
  93.     //Free loaded image
  94.     SDL_FreeSurface( gPNGSurface );
  95.     gPNGSurface = NULL;
  96.  
  97.     //Destroy window
  98.     SDL_DestroyWindow( gWindow );
  99.     gWindow = NULL;
  100.  
  101.     //Quit SDL subsystems
  102.     IMG_Quit();
  103.     SDL_Quit();
  104. }
  105.  
  106. SDL_Surface* loadSurface( std::string path )
  107. {
  108.     //The final optimized image
  109.     SDL_Surface* optimizedSurface = NULL;
  110.  
  111.     //Load image at specified path
  112.     SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
  113.     if( loadedSurface == NULL )
  114.     {
  115.         printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
  116.     }
  117.     else
  118.     {
  119.         //Convert surface to screen format
  120.         optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, 0 );
  121.         if( optimizedSurface == NULL )
  122.         {
  123.             printf( "Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
  124.         }
  125.  
  126.         //Get rid of old loaded surface
  127.         SDL_FreeSurface( loadedSurface );
  128.     }
  129.  
  130.     return optimizedSurface;
  131. }
  132.  
  133. int main( int argc, char* args[] )
  134. {
  135.     //Start up SDL and create window
  136.     if( !init() )
  137.     {
  138.         printf( "Failed to initialize!\n" );
  139.     }
  140.     else
  141.     {
  142.         //Load media
  143.         if( !loadMedia() )
  144.         {
  145.             printf( "Failed to load media!\n" );
  146.         }
  147.         else
  148.         {  
  149.             //Main loop flag
  150.             bool quit = false;
  151.  
  152.             //Event handler
  153.             SDL_Event e;
  154.  
  155.             //While application is running
  156.             while( !quit )
  157.             {
  158.                 //Handle events on queue
  159.                 while( SDL_PollEvent( &e ) != 0 )
  160.                 {
  161.                     //User requests quit
  162.                     if( e.type == SDL_QUIT )
  163.                     {
  164.                         quit = true;
  165.                     }
  166.                 }
  167.  
  168.                 //Apply the PNG image
  169.                 SDL_BlitSurface( gPNGSurface, NULL, gScreenSurface, NULL );
  170.            
  171.                 //Update the surface
  172.                 SDL_UpdateWindowSurface( gWindow );
  173.             }
  174.         }
  175.     }
  176.  
  177.     //Free resources and close SDL
  178.     close();
  179.  
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement