RenderTarget.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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* rt = create(id, texture);
  30. texture->release();
  31. return rt;
  32. }
  33. RenderTarget* RenderTarget::create(const char* id, Texture* texture)
  34. {
  35. RenderTarget* renderTarget = new RenderTarget(id);
  36. renderTarget->_texture = texture;
  37. renderTarget->_texture->addRef();
  38. __renderTargets.push_back(renderTarget);
  39. return renderTarget;
  40. }
  41. RenderTarget* RenderTarget::getRenderTarget(const char* id)
  42. {
  43. GP_ASSERT(id);
  44. // Search the vector for a matching ID.
  45. std::vector<RenderTarget*>::const_iterator it;
  46. for (it = __renderTargets.begin(); it < __renderTargets.end(); ++it)
  47. {
  48. RenderTarget* dst = *it;
  49. GP_ASSERT(dst);
  50. if (strcmp(id, dst->getId()) == 0)
  51. {
  52. return dst;
  53. }
  54. }
  55. return NULL;
  56. }
  57. const char* RenderTarget::getId() const
  58. {
  59. return _id.c_str();
  60. }
  61. Texture* RenderTarget::getTexture() const
  62. {
  63. return _texture;
  64. }
  65. unsigned int RenderTarget::getWidth() const
  66. {
  67. return _texture->getWidth();
  68. }
  69. unsigned int RenderTarget::getHeight() const
  70. {
  71. return _texture->getHeight();
  72. }
  73. }