RenderTarget.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ? 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::RGBA, width, height, NULL, false);
  24. if (texture == NULL)
  25. {
  26. GP_ERROR("Failed to create texture for render target.");
  27. return NULL;
  28. }
  29. RenderTarget* renderTarget = new RenderTarget(id);
  30. renderTarget->_texture = texture;
  31. __renderTargets.push_back(renderTarget);
  32. return renderTarget;
  33. }
  34. RenderTarget* RenderTarget::getRenderTarget(const char* id)
  35. {
  36. GP_ASSERT(id);
  37. // Search the vector for a matching ID.
  38. std::vector<RenderTarget*>::const_iterator it;
  39. for (it = __renderTargets.begin(); it < __renderTargets.end(); it++)
  40. {
  41. RenderTarget* dst = *it;
  42. GP_ASSERT(dst);
  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. }