Texture.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "Texture.h"
  2. #include "util/AllocDebug.h"
  3. #include "img/ImageData.h"
  4. USING_NS_BF;
  5. Texture::Texture()
  6. {
  7. mRefCount = 0;
  8. }
  9. void Texture::AddRef()
  10. {
  11. mRefCount++;
  12. }
  13. void Texture::Release()
  14. {
  15. mRefCount--;
  16. if (mRefCount == 0)
  17. delete this;
  18. }
  19. void TextureSegment::InitFromTexture(Texture* texture)
  20. {
  21. mTexture = texture;
  22. mU1 = 0;
  23. mV1 = 0;
  24. mU2 = 1.0f;
  25. mV2 = 1.0f;
  26. mScaleX = (float) mTexture->mWidth;
  27. mScaleY = (float) mTexture->mHeight;
  28. }
  29. void TextureSegment::SetBits(int destX, int destY, int destWidth, int destHeight, int srcPitch, uint32* bits)
  30. {
  31. int x1 = (int)(mU1 * mTexture->mWidth + 0.5f);
  32. int y1 = (int)(mV1 * mTexture->mHeight + 0.5f);
  33. mTexture->SetBits(destX + x1, destY + y1, destWidth, destHeight, srcPitch, bits);
  34. }
  35. void TextureSegment::GetBits(int srcX, int srcY, int srcWidth, int srcHeight, int destPitch, uint32* bits)
  36. {
  37. int x1 = (int)(mU1 * mTexture->mWidth + 0.5f);
  38. int y1 = (int)(mV1 * mTexture->mHeight + 0.5f);
  39. mTexture->GetBits(srcX + x1, srcY + y1, srcWidth, srcHeight, destPitch, bits);
  40. }
  41. void TextureSegment::GetImageData(ImageData& imageData)
  42. {
  43. int x1 = (int)(mU1 * mTexture->mWidth + 0.5f);
  44. int x2 = (int)(mU2 * mTexture->mWidth + 0.5f);
  45. int y1 = (int)(mV1 * mTexture->mHeight + 0.5f);
  46. int y2 = (int)(mV2 * mTexture->mHeight + 0.5f);
  47. imageData.CreateNew(x2 - x1, y2 - y1);
  48. mTexture->GetBits(x1, y1, x2 - x1, y2 - y1, x2 - x1, imageData.mBits);
  49. }
  50. void TextureSegment::GetImageData(ImageData& imageData, int destX, int destY)
  51. {
  52. int x1 = (int)(mU1 * mTexture->mWidth + 0.5f);
  53. int x2 = (int)(mU2 * mTexture->mWidth + 0.5f);
  54. int y1 = (int)(mV1 * mTexture->mHeight + 0.5f);
  55. int y2 = (int)(mV2 * mTexture->mHeight + 0.5f);
  56. mTexture->GetBits(x1, y1, x2 - x1, y2 - y1, imageData.mWidth, imageData.mBits + destX + destY * imageData.mWidth);
  57. }
  58. void TextureSegment::SetImageData(ImageData& imageData)
  59. {
  60. SetBits(0, 0, imageData.mWidth, imageData.mHeight, imageData.mStride, imageData.mBits);
  61. }
  62. RectF TextureSegment::GetRect()
  63. {
  64. float x1 = mU1 * mTexture->mWidth;
  65. float x2 = mU2 * mTexture->mWidth;
  66. float y1 = mV1 * mTexture->mHeight;
  67. float y2 = mV2 * mTexture->mHeight;
  68. return RectF(x1, y1, x2 - x1, y2 - y1);
  69. }