texture.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "texture.h"
  2. #include "windowManager.h"
  3. Texture::Texture(){
  4. mTexture = NULL;
  5. mWidth = 0;
  6. mHeight = 0;
  7. mPitch = 0;
  8. }
  9. Texture::~Texture(){
  10. free();
  11. }
  12. void Texture::free(){
  13. if(mTexture != NULL){
  14. SDL_DestroyTexture (mTexture);
  15. mWidth = 0;
  16. mHeight = 0;
  17. mPitch = 0;
  18. }
  19. }
  20. bool Texture::createBlank(SDL_Renderer *renderer, int width, int height ){
  21. mWidth = width;
  22. mHeight = height;
  23. mPitch = width * sizeof(Uint32);
  24. mTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
  25. SDL_TEXTUREACCESS_STREAMING, mWidth, mHeight);
  26. return mTexture != NULL;
  27. }
  28. void Texture::updateTexture(Uint32 * pixels){
  29. //Lock texture for manipulation
  30. SDL_LockTexture(mTexture, NULL, &mPixels, &mPitch );
  31. //Copy pixels to texture
  32. memcpy(mPixels, pixels, mHeight*mPitch);
  33. //Update texture
  34. SDL_UnlockTexture(mTexture);
  35. mPixels = nullptr;
  36. //SDL_UpdateTexture(mTexture, NULL, pixels, mPitch);
  37. }
  38. void Texture::renderToScreen(SDL_Renderer * renderer){
  39. SDL_SetTextureBlendMode(mTexture, SDL_BLENDMODE_BLEND);
  40. SDL_RenderCopy(renderer, mTexture, NULL , NULL);
  41. }