RenderTarget.cpp 1.5 KB

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