RenderTarget.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "Base.h"
  2. #include "RenderTarget.h"
  3. namespace gameplay
  4. {
  5. static std::vector<RenderTarget*> __renderTargets;
  6. RenderTarget::RenderTarget(const char* id)
  7. : _id(id), _texture(NULL)
  8. {
  9. }
  10. RenderTarget::~RenderTarget()
  11. {
  12. SAFE_RELEASE(_texture);
  13. // Remove ourself from the cache
  14. std::vector<RenderTarget*>::iterator it = std::find(__renderTargets.begin(), __renderTargets.end(), this);
  15. if (it != __renderTargets.end())
  16. {
  17. __renderTargets.erase(it);
  18. }
  19. }
  20. RenderTarget* RenderTarget::create(const char* id, unsigned int width, unsigned int height)
  21. {
  22. // Create a new texture with the given width
  23. Texture* texture = Texture::create(Texture::RGBA8888, width, height, NULL, false);
  24. if (texture == NULL)
  25. {
  26. return NULL;
  27. }
  28. RenderTarget* renderTarget = new RenderTarget(id);
  29. renderTarget->_texture = texture;
  30. __renderTargets.push_back(renderTarget);
  31. return renderTarget;
  32. }
  33. RenderTarget* RenderTarget::getRenderTarget(const char* id)
  34. {
  35. // Search the vector for a matching ID.
  36. std::vector<RenderTarget*>::const_iterator it;
  37. for (it = __renderTargets.begin(); it < __renderTargets.end(); it++)
  38. {
  39. RenderTarget* dst = *it;
  40. if (strcmp(id, dst->getID()) == 0)
  41. {
  42. return dst;
  43. }
  44. }
  45. return NULL;
  46. }
  47. const char* RenderTarget::getID() const
  48. {
  49. return _id.c_str();
  50. }
  51. Texture* RenderTarget::getTexture() const
  52. {
  53. return _texture;
  54. }
  55. }