Advertisement
Guest User

main.cpp

a guest
Dec 14th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. /*This source code copyrighted by Lazy Foo' Productions (2004-2015)
  2. and may not be redistributed without written permission.*/
  3.  
  4. //Using SDL, SDL_image, standard IO, and strings
  5. #include <SDL2/SDL.h>
  6. #include <SDL2/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 as texture
  24. SDL_Texture* loadTexture( std::string path );
  25.  
  26. //The window we'll be rendering to
  27. SDL_Window* gWindow = NULL;
  28.  
  29. //The window renderer
  30. SDL_Renderer* gRenderer = NULL;
  31.  
  32. //Current displayed texture
  33. SDL_Texture* gTexture = 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.         //Set texture filtering to linear
  49.         if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
  50.         {
  51.             printf( "Warning: Linear texture filtering not enabled!" );
  52.         }
  53.  
  54.         //Create window
  55.         gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  56.         if( gWindow == NULL )
  57.         {
  58.             printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
  59.             success = false;
  60.         }
  61.         else
  62.         {
  63.             //Create renderer for window
  64.             gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
  65.             if( gRenderer == NULL )
  66.             {
  67.                 printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
  68.                 success = false;
  69.             }
  70.             else
  71.             {
  72.                 //Initialize renderer color
  73.                 SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
  74.  
  75.                 //Initialize PNG loading
  76.                 int imgFlags = IMG_INIT_PNG;
  77.                 if( !( IMG_Init( imgFlags ) & imgFlags ) )
  78.                 {
  79.                     printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
  80.                     success = false;
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     return success;
  87. }
  88.  
  89. bool loadMedia()
  90. {
  91.     //Loading success flag
  92.     bool success = true;
  93.  
  94.     //Load PNG texture
  95.     gTexture = loadTexture( "../res/BMPs/Ghougerry.bmp" );
  96.     if( gTexture == NULL )
  97.     {
  98.         printf( "Failed to load texture image!\n" );
  99.         success = false;
  100.     }
  101.  
  102.     return success;
  103. }
  104.  
  105. void close()
  106. {
  107.     //Free loaded image
  108.     SDL_DestroyTexture( gTexture );
  109.     gTexture = NULL;
  110.  
  111.     //Destroy window   
  112.     SDL_DestroyRenderer( gRenderer );
  113.     SDL_DestroyWindow( gWindow );
  114.     gWindow = NULL;
  115.     gRenderer = NULL;
  116.  
  117.     //Quit SDL subsystems
  118.     IMG_Quit();
  119.     SDL_Quit();
  120. }
  121.  
  122. SDL_Texture* loadTexture( std::string path )
  123. {
  124.     //The final texture
  125.     SDL_Texture* newTexture = NULL;
  126.  
  127.     //Load image at specified path
  128.     SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
  129.     if( loadedSurface == NULL )
  130.     {
  131.         printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
  132.     }
  133.     else
  134.     {
  135.         //Create texture from surface pixels
  136.         newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
  137.         if( newTexture == NULL )
  138.         {
  139.             printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
  140.         }
  141.  
  142.         //Get rid of old loaded surface
  143.         SDL_FreeSurface( loadedSurface );
  144.     }
  145.  
  146.     return newTexture;
  147. }
  148.  
  149. int main( int argc, char* args[] )
  150. {
  151.     //Start up SDL and create window
  152.     if( !init() )
  153.     {
  154.         printf( "Failed to initialize!\n" );
  155.     }
  156.     else
  157.     {
  158.         //Load media
  159.         if( !loadMedia() )
  160.         {
  161.             printf( "Failed to load media!\n" );
  162.         }
  163.         else
  164.         {  
  165.             //Main loop flag
  166.             bool quit = false;
  167.  
  168.             //Event handler
  169.             SDL_Event e;
  170.  
  171.  
  172.  
  173.             SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  174.  
  175.             //Clear screen
  176.                 SDL_RenderClear( gRenderer );
  177.  
  178.                 //Render texture to screen
  179.                 SDL_RenderCopy( gRenderer, gTexture, NULL, NULL );
  180.  
  181.                 //Update screen
  182.                 SDL_RenderPresent( gRenderer );
  183.  
  184.             //While application is running
  185.             while( !quit )
  186.             {
  187.                 //Handle events on queue
  188.                 while( SDL_PollEvent( &e ) != 0 )
  189.                 {
  190.                     //User requests quit
  191.                     if( e.type == SDL_QUIT )
  192.                     {
  193.                         quit = true;
  194.                     }
  195.                     SDL_Delay(200);
  196.                 }
  197.  
  198.                
  199.  
  200.  
  201.             }
  202.         }
  203.     }
  204.  
  205.     //Free resources and close SDL
  206.     close();
  207.  
  208.     return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement