Advertisement
Al99

TextureManager.cpp

Nov 11th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include "TextureManager.h"
  2.  
  3. TextureManager* TextureManager::s_pInstance= 0;
  4.  
  5. bool TextureManager::load(std::string fileName, std::string id,
  6. SDL_Renderer* pRenderer)
  7. {
  8.     SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
  9.    
  10.     if(pTempSurface == 0)
  11.     {
  12.         return false;
  13.     }
  14.    
  15.     SDL_Texture* pTexture =
  16.     SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
  17.    
  18.     //everything went ok, add the texture to our list
  19.     if(pTexture !=0)
  20.     {
  21.         m_textureMap[id] = pTexture;
  22.         return true;
  23.     }
  24.    
  25.     //reaching here means something went wrong
  26.     return false;
  27. }
  28.  
  29. void TextureManager::draw(std::string id, int x, int y, int width, int height,
  30. SDL_Renderer* pRenderer, SDL_RendererFlip flip)
  31. {
  32.     SDL_Rect srcRect;
  33.     SDL_Rect destRect;
  34.    
  35.     srcRect.x = 0;
  36.     srcRect.y = 0;
  37.     srcRect.w = destRect.w = width;
  38.     srcRect.h = destRect.h = height;
  39.     destRect.x = x;
  40.     destRect.y = y;
  41.    
  42.     SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect,
  43.     0, 0, flip);
  44. }
  45.  
  46. void TextureManager::drawFrame(std::string id, int x, int y, int width, int height,
  47. int currentRow, int currentFrame, SDL_Renderer* pRenderer,
  48. SDL_RendererFlip flip)
  49. {
  50.     SDL_Rect srcRect;
  51.     SDL_Rect destRect;
  52.     srcRect.x = width * currentFrame;
  53.     srcRect.y = height * (currentRow -1);
  54.     srcRect.w = destRect.w = width;
  55.     srcRect.h = destRect.h = height;
  56.     destRect.x = x;
  57.     destRect.y = y;
  58.    
  59.     SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect,
  60.     0,0, flip);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement