Advertisement
Guest User

11: Clip Rendering and Sprite Sheets (lazyfoo)

a guest
Apr 6th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1. //Using SDL, SDL_image, standard math, and strings
  2. #include <SDL.h>
  3. #include <SDL_image.h>
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. //Screen dimension constants
  10. const int SCREEN_WIDTH = 640;
  11. const int SCREEN_HEIGHT = 480;
  12.  
  13. //Texture wrapper class
  14. class LTexture
  15. {
  16.     public:
  17.         //Initializes variables
  18.         LTexture();
  19.  
  20.         //Deallocates memory
  21.         ~LTexture();
  22.  
  23.         //Loads image at specified path
  24.         bool loadFromFile( string path );
  25.  
  26.         //Deallocates texture
  27.         void free();
  28.  
  29.         //Renders texture at given point
  30.         void render( int x, int y, SDL_Rect* clip = NULL );
  31.  
  32.         //Gets image dimensions
  33.         int getWidth();
  34.         int getHeight();
  35.  
  36.     private:
  37.         //The actual hardware texture
  38.         SDL_Texture* mTexture;
  39.  
  40.         //Image dimensions
  41.         int mWidth;
  42.         int mHeight;
  43. };
  44.  
  45. //Starts up SDL and creates window
  46. bool init();
  47.  
  48. //Loads media
  49. bool loadMedia();
  50.  
  51. //Frees media and shuts down SDL
  52. void close();
  53.  
  54. //The window we'll be rendering to
  55. SDL_Window* gWindow = NULL;
  56.  
  57. //The window renderer
  58. SDL_Renderer* gRenderer = NULL;
  59.  
  60. //Scene sprites
  61. SDL_Rect gSpriteClips[ 4 ];
  62. LTexture gSpriteSheetTexture;
  63.  
  64.  
  65. LTexture::LTexture()
  66. {
  67.     //Initialize
  68.     mTexture = NULL;
  69.     mWidth = 0;
  70.     mHeight = 0;
  71. }
  72.  
  73. LTexture::~LTexture()
  74. {
  75.     //Deallocate
  76.     free();
  77. }
  78.  
  79. bool LTexture::loadFromFile( string path )
  80. {
  81.     //Get rid of preexisting texture
  82.     free();
  83.  
  84.     //The final texture
  85.     SDL_Texture* newTexture = NULL;
  86.  
  87.     //Load image at specified path
  88.     SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
  89.     if( loadedSurface == NULL )
  90.     {
  91.         cout << "Unable to load image " << path.c_str() << "! SDL_image Error: " << IMG_GetError() << endl;
  92.     }
  93.     else
  94.     {
  95.         //Color key image
  96.         SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0xFF, 0xFF ) );
  97.  
  98.         //Create texture from surface pixels
  99.         newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
  100.         if( newTexture == NULL )
  101.         {
  102.             cout << "Unable to create texture from " << path.c_str() << "! SDL Error: " << SDL_GetError() << endl;
  103.         }
  104.         else
  105.         {
  106.             //Get image dimensions
  107.             mWidth = loadedSurface->w;
  108.             mHeight = loadedSurface->h;
  109.         }
  110.  
  111.         //Get rid of old loaded surface
  112.         SDL_FreeSurface( loadedSurface );
  113.     }
  114.  
  115.     //Return success
  116.     mTexture = newTexture;
  117.     return mTexture != NULL;
  118. }
  119.  
  120. void LTexture::free()
  121. {
  122.     //Free texture if it exists
  123.     if( mTexture != NULL )
  124.     {
  125.         SDL_DestroyTexture( mTexture );
  126.         mTexture = NULL;
  127.         mWidth = 0;
  128.         mHeight = 0;
  129.     }
  130. }
  131.  
  132. void LTexture::render( int x, int y, SDL_Rect* clip )
  133. {
  134.     //Set rendering space and render to screen
  135.     SDL_Rect renderQuad = { x, y, mWidth, mHeight };
  136.  
  137.     //Set clip rendering dimensions
  138.     if( clip != NULL )
  139.     {
  140.         renderQuad.w = clip->w;
  141.         renderQuad.h = clip->h;
  142.     }
  143.  
  144.     //Render to screen
  145.     SDL_RenderCopy( gRenderer, mTexture, clip, &renderQuad );
  146. }
  147.  
  148. int LTexture::getWidth()
  149. {
  150.     return mWidth;
  151. }
  152.  
  153. int LTexture::getHeight()
  154. {
  155.     return mHeight;
  156. }
  157.  
  158. bool init()
  159. {
  160.     //Initialization flag
  161.     bool success = true;
  162.  
  163.     //Initialize SDL
  164.     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  165.     {
  166.         cout << "SDL could not initialize! SDL Error: " << SDL_GetError() << endl;
  167.         success = false;
  168.     }
  169.     else
  170.     {
  171.         //Set texture filtering to linear
  172.         if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
  173.         {
  174.             cout << "Warning: Linear texture filtering not enabled!";
  175.         }
  176.  
  177.         //Create window
  178.         gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  179.         if( gWindow == NULL )
  180.         {
  181.             cout << "Window could not be created! SDL Error: " << SDL_GetError() << endl;
  182.             success = false;
  183.         }
  184.         else
  185.         {
  186.             //Create renderer for window
  187.             gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
  188.             if( gRenderer == NULL )
  189.             {
  190.                 cout << "Renderer could not be created! SDL Error: " << SDL_GetError() << endl;
  191.                 success = false;
  192.             }
  193.             else
  194.             {
  195.                 //Initialize renderer color
  196.                 SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
  197.  
  198.                 //Initialize PNG loading
  199.                 int imgFlags = IMG_INIT_PNG;
  200.                 if( !( IMG_Init( imgFlags ) & imgFlags ) )
  201.                 {
  202.                     cout << "SDL_image could not initialize! SDL_mage Error: " << IMG_GetError() << endl;
  203.                     success = false;
  204.                 }
  205.             }
  206.         }
  207.     }
  208.  
  209.     return success;
  210. }
  211.  
  212. bool loadMedia()
  213. {
  214.     //Loading success flag
  215.     bool success = true;
  216.  
  217.     //Load sprite sheet texture
  218.     if( !gSpriteSheetTexture.loadFromFile( "11_clip_rendering_and_sprite_sheets/dots.png" ) )
  219.     {
  220.         cout << "Failed to load sprite sheet texture!\n";
  221.         success = false;
  222.     }
  223.     else
  224.     {
  225.         //Set top left sprite
  226.         gSpriteClips[ 0 ].x =   0;
  227.         gSpriteClips[ 0 ].y =   0;
  228.         gSpriteClips[ 0 ].w = 100;
  229.         gSpriteClips[ 0 ].h = 100;
  230.  
  231.         //Set top right sprite
  232.         gSpriteClips[ 1 ].x = 100;
  233.         gSpriteClips[ 1 ].y =   0;
  234.         gSpriteClips[ 1 ].w = 100;
  235.         gSpriteClips[ 1 ].h = 100;
  236.  
  237.         //Set bottom left sprite
  238.         gSpriteClips[ 2 ].x =   0;
  239.         gSpriteClips[ 2 ].y = 100;
  240.         gSpriteClips[ 2 ].w = 100;
  241.         gSpriteClips[ 2 ].h = 100;
  242.  
  243.         //Set bottom right sprite
  244.         gSpriteClips[ 3 ].x = 100;
  245.         gSpriteClips[ 3 ].y = 100;
  246.         gSpriteClips[ 3 ].w = 100;
  247.         gSpriteClips[ 3 ].h = 100;
  248.     }
  249.  
  250.     return success;
  251. }
  252.  
  253. void close()
  254. {
  255.     //Free loaded images
  256.     gSpriteSheetTexture.free();
  257.  
  258.     //Destroy window
  259.     SDL_DestroyRenderer( gRenderer );
  260.     SDL_DestroyWindow( gWindow );
  261.     gWindow = NULL;
  262.     gRenderer = NULL;
  263.  
  264.     //Quit SDL subsystems
  265.     IMG_Quit();
  266.     SDL_Quit();
  267. }
  268.  
  269. int main( int argc, char* args[] )
  270. {
  271.     //Start up SDL and create window
  272.     if( !init() )
  273.     {
  274.         cout << "Failed to initialize!\n";
  275.     }
  276.     else
  277.     {
  278.         //Load media
  279.         if( !loadMedia() )
  280.         {
  281.             cout << "Failed to load media!\n";
  282.         }
  283.         else
  284.         {
  285.             //Main loop flag
  286.             bool quit = false;
  287.  
  288.             //Event handler
  289.             SDL_Event e;
  290.  
  291.             //While application is running
  292.             while( !quit )
  293.             {
  294.                 //Handle events on queue
  295.                 while( SDL_PollEvent( &e ) != 0 )
  296.                 {
  297.                     //User requests quit
  298.                     if( e.type == SDL_QUIT )
  299.                     {
  300.                         quit = true;
  301.                     }
  302.                 }
  303.  
  304.                 //Clear screen
  305.                 SDL_SetRenderDrawColor( gRenderer, 0, 0xFF, 0xFF, 0xFF );
  306.                 SDL_RenderClear( gRenderer );
  307.  
  308.                 SDL_Delay(2000);        // put this in to check if it is really repeating.
  309.                
  310.                 //Render top left sprite
  311.                 gSpriteSheetTexture.render( 0, 0, &gSpriteClips[ 0 ] );
  312.  
  313.                 //Render top right sprite
  314.                 gSpriteSheetTexture.render( SCREEN_WIDTH - gSpriteClips[ 1 ].w, 0, &gSpriteClips[ 1 ] );
  315.  
  316.                 //Render bottom left sprite
  317.                 gSpriteSheetTexture.render( 0, SCREEN_HEIGHT - gSpriteClips[ 2 ].h, &gSpriteClips[ 2 ] );
  318.  
  319.                 //Render bottom right sprite
  320.                 gSpriteSheetTexture.render( SCREEN_WIDTH - gSpriteClips[ 3 ].w, SCREEN_HEIGHT - gSpriteClips[ 3 ].h, &gSpriteClips[ 3 ] );
  321.  
  322.                 //Update screen
  323.                 SDL_RenderPresent( gRenderer );
  324.             }
  325.         }
  326.     }
  327.  
  328.     //Free resources and close SDL
  329.     close();
  330.  
  331.     return 0;
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement