BsSkybox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsSkybox.h"
  4. #include "BsSkyboxRTTI.h"
  5. #include "BsSceneObject.h"
  6. #include "BsFrameAlloc.h"
  7. #include "BsTexture.h"
  8. #include "BsRenderer.h"
  9. #include "BsUUID.h"
  10. namespace bs
  11. {
  12. SkyboxBase::SkyboxBase()
  13. : mIsActive(true), mBrightness(1.0f)
  14. { }
  15. template <bool Core>
  16. TSkybox<Core>::TSkybox()
  17. : SkyboxBase()
  18. { }
  19. template class TSkybox<true>;
  20. template class TSkybox<false>;
  21. Skybox::Skybox()
  22. { }
  23. SPtr<ct::Skybox> Skybox::getCore() const
  24. {
  25. return std::static_pointer_cast<ct::Skybox>(mCoreSpecific);
  26. }
  27. SPtr<Skybox> Skybox::create()
  28. {
  29. Skybox* skybox = new (bs_alloc<Skybox>()) Skybox();
  30. SPtr<Skybox> skyboxPtr = bs_core_ptr<Skybox>(skybox);
  31. skyboxPtr->_setThisPtr(skyboxPtr);
  32. skyboxPtr->mUUID = UUIDGenerator::generateRandom();
  33. skyboxPtr->initialize();
  34. return skyboxPtr;
  35. }
  36. SPtr<ct::CoreObject> Skybox::createCore() const
  37. {
  38. ct::Skybox* skybox = new (bs_alloc<ct::Skybox>()) ct::Skybox();
  39. SPtr<ct::Skybox> skyboxPtr = bs_shared_ptr<ct::Skybox>(skybox);
  40. skyboxPtr->mUUID = mUUID;
  41. skyboxPtr->_setThisPtr(skyboxPtr);
  42. return skyboxPtr;
  43. }
  44. CoreSyncData Skybox::syncToCore(FrameAlloc* allocator)
  45. {
  46. UINT32 size = 0;
  47. size += rttiGetElemSize(mIsActive);
  48. size += rttiGetElemSize(mBrightness);
  49. size += sizeof(SPtr<ct::Texture>);
  50. size += rttiGetElemSize(mUUID);
  51. size += rttiGetElemSize(getCoreDirtyFlags());
  52. UINT8* buffer = allocator->alloc(size);
  53. char* dataPtr = (char*)buffer;
  54. dataPtr = rttiWriteElem(mIsActive, dataPtr);
  55. dataPtr = rttiWriteElem(mBrightness, dataPtr);
  56. dataPtr = rttiWriteElem(mUUID, dataPtr);
  57. dataPtr = rttiWriteElem(getCoreDirtyFlags(), dataPtr);
  58. SPtr<ct::Texture>* texture = new (dataPtr) SPtr<ct::Texture>();
  59. if (mTexture.isLoaded(false))
  60. *texture = mTexture->getCore();
  61. else
  62. *texture = nullptr;
  63. dataPtr += sizeof(SPtr<ct::Texture>);
  64. return CoreSyncData(buffer, size);
  65. }
  66. void Skybox::_markCoreDirty(SkyboxDirtyFlag flags)
  67. {
  68. markCoreDirty((UINT32)flags);
  69. }
  70. RTTITypeBase* Skybox::getRTTIStatic()
  71. {
  72. return SkyboxRTTI::instance();
  73. }
  74. RTTITypeBase* Skybox::getRTTI() const
  75. {
  76. return Skybox::getRTTIStatic();
  77. }
  78. namespace ct
  79. {
  80. Skybox::Skybox()
  81. { }
  82. Skybox::~Skybox()
  83. {
  84. gRenderer()->notifySkyboxRemoved(this);
  85. }
  86. void Skybox::initialize()
  87. {
  88. gRenderer()->notifySkyboxAdded(this);
  89. CoreObject::initialize();
  90. }
  91. void Skybox::syncToCore(const CoreSyncData& data)
  92. {
  93. char* dataPtr = (char*)data.getBuffer();
  94. SkyboxDirtyFlag dirtyFlags;
  95. bool oldIsActive = mIsActive;
  96. dataPtr = rttiReadElem(mIsActive, dataPtr);
  97. dataPtr = rttiReadElem(mBrightness, dataPtr);
  98. dataPtr = rttiReadElem(mUUID, dataPtr);
  99. dataPtr = rttiReadElem(dirtyFlags, dataPtr);
  100. SPtr<Texture>* texture = (SPtr<Texture>*)dataPtr;
  101. mTexture = *texture;
  102. texture->~SPtr<Texture>();
  103. dataPtr += sizeof(SPtr<Texture>);
  104. if (oldIsActive != mIsActive)
  105. {
  106. if (mIsActive)
  107. gRenderer()->notifySkyboxAdded(this);
  108. else
  109. gRenderer()->notifySkyboxRemoved(this);
  110. }
  111. else
  112. {
  113. if (dirtyFlags == SkyboxDirtyFlag::Texture)
  114. gRenderer()->notifySkyboxTextureChanged(this);
  115. else
  116. {
  117. gRenderer()->notifySkyboxRemoved(this);
  118. gRenderer()->notifySkyboxAdded(this);
  119. }
  120. }
  121. }
  122. }
  123. }