CmViewport.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "CmViewport.h"
  2. #include "CmException.h"
  3. #include "CmRenderTarget.h"
  4. #include "CmMath.h"
  5. #include "CmRenderSystem.h"
  6. namespace BansheeEngine
  7. {
  8. const Color Viewport::DEFAULT_CLEAR_COLOR = Color(143.0f / 255.0f, 111.0f / 255.0f, 0);
  9. Viewport::Viewport()
  10. :mTarget(nullptr), mClearColor(DEFAULT_CLEAR_COLOR), mRequiresColorClear(true), mRequiresDepthClear(true),
  11. mRequiresStencilClear(false), mStencilClearValue(0), mDepthClearValue(1.0f)
  12. {
  13. updateArea();
  14. }
  15. Viewport::Viewport(const RenderTargetPtr& target, float x, float y, float width, float height)
  16. :mTarget(target), mNormArea(x, y, width, height), mClearColor(DEFAULT_CLEAR_COLOR), mRequiresColorClear(true),
  17. mRequiresDepthClear(true), mRequiresStencilClear(false), mStencilClearValue(0), mDepthClearValue(1.0f)
  18. {
  19. if(target != nullptr)
  20. {
  21. // Note: RenderTarget resize will only get triggered for RenderWindows, RenderTextures are immutable
  22. mTargetResizedConn = target->onResized.connect(std::bind(&Viewport::targetResized, this));
  23. }
  24. updateArea();
  25. }
  26. Viewport::~Viewport()
  27. {
  28. mTargetResizedConn.disconnect();
  29. }
  30. void Viewport::targetResized()
  31. {
  32. updateArea();
  33. if(!onResized.empty())
  34. onResized();
  35. }
  36. void Viewport::updateArea()
  37. {
  38. if(mTarget != nullptr)
  39. {
  40. float height = (float) mTarget->getHeight();
  41. float width = (float) mTarget->getWidth();
  42. mArea.x = (int) (mNormArea.x * width);
  43. mArea.y = (int) (mNormArea.y * height);
  44. mArea.width = (int) (mNormArea.width * width);
  45. mArea.height = (int) (mNormArea.height * height);
  46. }
  47. }
  48. void Viewport::setArea(float x, float y, float width, float height)
  49. {
  50. mNormArea.x = x;
  51. mNormArea.y = y;
  52. mNormArea.width = width;
  53. mNormArea.height = height;
  54. updateArea();
  55. }
  56. }