BsRenderWindow.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "BsRenderWindow.h"
  2. #include "BsCoreThread.h"
  3. #include "BsRenderWindowManager.h"
  4. #include "BsViewport.h"
  5. #include "BsPlatform.h"
  6. #include "BsFrameAlloc.h"
  7. namespace BansheeEngine
  8. {
  9. RenderWindowProperties::RenderWindowProperties(const RENDER_WINDOW_DESC& desc)
  10. {
  11. mWidth = desc.videoMode.getWidth();
  12. mHeight = desc.videoMode.getHeight();
  13. mHwGamma = desc.gamma;
  14. mVSync = desc.vsync;
  15. mVSyncInterval = desc.vsyncInterval;
  16. mMultisampleCount = desc.multisampleCount;
  17. mLeft = desc.left;
  18. mTop = desc.top;
  19. mIsFullScreen = desc.fullscreen;
  20. mHidden = desc.hidden;
  21. mIsModal = desc.modal;
  22. mIsWindow = true;
  23. mRequiresTextureFlipping = false;
  24. }
  25. RenderWindowCore::RenderWindowCore(const RENDER_WINDOW_DESC& desc)
  26. :mDesc(desc)
  27. {
  28. }
  29. RenderWindowCore::~RenderWindowCore()
  30. {
  31. Platform::resetNonClientAreas(*this);
  32. RenderWindowCoreManager::instance().windowDestroyed(this);
  33. }
  34. void RenderWindowCore::setHidden(bool hidden)
  35. {
  36. THROW_IF_NOT_CORE_THREAD;
  37. }
  38. void RenderWindowCore::setActive(bool state)
  39. {
  40. THROW_IF_NOT_CORE_THREAD;
  41. RenderWindowProperties& props = const_cast<RenderWindowProperties&>(getProperties());
  42. props.mActive = state;
  43. RenderWindowManager::instance().notifyPropertiesDirty(this);
  44. }
  45. void RenderWindowCore::syncToCore(const CoreSyncData& data)
  46. {
  47. RenderWindowProperties& props = const_cast<RenderWindowProperties&>(getProperties());
  48. props = data.getData<RenderWindowProperties>();
  49. }
  50. void RenderWindowCore::_windowMovedOrResized()
  51. {
  52. THROW_IF_NOT_CORE_THREAD;
  53. RenderWindowManager::instance().notifyMovedOrResized(this);
  54. }
  55. void RenderWindowCore::_windowFocusReceived()
  56. {
  57. THROW_IF_NOT_CORE_THREAD;
  58. RenderWindowProperties& properties = const_cast<RenderWindowProperties&>(getProperties());
  59. properties.mHasFocus = true;
  60. RenderWindowManager::instance().notifyFocusReceived(this);
  61. }
  62. void RenderWindowCore::_windowFocusLost()
  63. {
  64. THROW_IF_NOT_CORE_THREAD;
  65. RenderWindowProperties& properties = const_cast<RenderWindowProperties&>(getProperties());
  66. properties.mHasFocus = false;
  67. RenderWindowManager::instance().notifyFocusLost(this);
  68. }
  69. void RenderWindowCore::_notifyMaximized()
  70. {
  71. THROW_IF_NOT_CORE_THREAD;
  72. RenderWindowProperties& props = const_cast<RenderWindowProperties&>(getProperties());
  73. props.mIsMaximized = true;
  74. RenderWindowManager::instance().notifyPropertiesDirty(this);
  75. }
  76. void RenderWindowCore::_notifyMinimized()
  77. {
  78. THROW_IF_NOT_CORE_THREAD;
  79. RenderWindowProperties& props = const_cast<RenderWindowProperties&>(getProperties());
  80. props.mIsMaximized = false;
  81. RenderWindowManager::instance().notifyPropertiesDirty(this);
  82. }
  83. void RenderWindowCore::_notifyRestored()
  84. {
  85. THROW_IF_NOT_CORE_THREAD;
  86. RenderWindowProperties& props = const_cast<RenderWindowProperties&>(getProperties());
  87. props.mIsMaximized = false;
  88. RenderWindowManager::instance().notifyPropertiesDirty(this);
  89. }
  90. const RenderWindowProperties& RenderWindowCore::getProperties() const
  91. {
  92. return static_cast<const RenderWindowProperties&>(getPropertiesInternal());
  93. }
  94. void RenderWindow::destroy()
  95. {
  96. RenderWindowManager::instance().notifyWindowDestroyed(this);
  97. RenderTarget::destroy();
  98. }
  99. RenderWindow::RenderWindow(const RENDER_WINDOW_DESC& desc)
  100. :mDesc(desc)
  101. {
  102. }
  103. void RenderWindow::resize(CoreAccessor& accessor, UINT32 width, UINT32 height)
  104. {
  105. std::function<void(SPtr<RenderWindowCore>, UINT32, UINT32)> resizeFunc =
  106. [](SPtr<RenderWindowCore> renderWindow, UINT32 width, UINT32 height)
  107. {
  108. renderWindow->resize(width, height);
  109. };
  110. getMutableProperties().mWidth = width;
  111. getMutableProperties().mHeight = height;
  112. accessor.queueCommand(std::bind(resizeFunc, getCore(), width, height));
  113. }
  114. void RenderWindow::move(CoreAccessor& accessor, INT32 left, INT32 top)
  115. {
  116. std::function<void(SPtr<RenderWindowCore>, INT32, INT32)> moveFunc =
  117. [](SPtr<RenderWindowCore> renderWindow, INT32 left, INT32 top)
  118. {
  119. renderWindow->move(left, top);
  120. };
  121. getMutableProperties().mLeft = left;
  122. getMutableProperties().mTop = top;
  123. accessor.queueCommand(std::bind(moveFunc, getCore(), left, top));
  124. }
  125. void RenderWindow::hide(CoreAccessor& accessor)
  126. {
  127. std::function<void(SPtr<RenderWindowCore>)> hideFunc =
  128. [](SPtr<RenderWindowCore> renderWindow)
  129. {
  130. renderWindow->setHidden(true);
  131. };
  132. getMutableProperties().mHidden = true;
  133. accessor.queueCommand(std::bind(hideFunc, getCore()));
  134. }
  135. void RenderWindow::show(CoreAccessor& accessor)
  136. {
  137. std::function<void(SPtr<RenderWindowCore>)> showFunc =
  138. [](SPtr<RenderWindowCore> renderWindow)
  139. {
  140. renderWindow->setHidden(false);
  141. };
  142. getMutableProperties().mHidden = false;
  143. accessor.queueCommand(std::bind(showFunc, getCore()));
  144. }
  145. void RenderWindow::minimize(CoreAccessor& accessor)
  146. {
  147. std::function<void(SPtr<RenderWindowCore>)> minimizeFunc =
  148. [](SPtr<RenderWindowCore> renderWindow)
  149. {
  150. renderWindow->minimize();
  151. };
  152. accessor.queueCommand(std::bind(minimizeFunc, getCore()));
  153. }
  154. void RenderWindow::maximize(CoreAccessor& accessor)
  155. {
  156. std::function<void(SPtr<RenderWindowCore>)> maximizeFunc =
  157. [](SPtr<RenderWindowCore> renderWindow)
  158. {
  159. renderWindow->maximize();
  160. };
  161. accessor.queueCommand(std::bind(maximizeFunc, getCore()));
  162. }
  163. void RenderWindow::restore(CoreAccessor& accessor)
  164. {
  165. std::function<void(SPtr<RenderWindowCore>)> restoreFunc =
  166. [](SPtr<RenderWindowCore> renderWindow)
  167. {
  168. renderWindow->restore();
  169. };
  170. accessor.queueCommand(std::bind(restoreFunc, getCore()));
  171. }
  172. void RenderWindow::setFullscreen(CoreAccessor& accessor, UINT32 width, UINT32 height,
  173. float refreshRate, UINT32 monitorIdx)
  174. {
  175. std::function<void(SPtr<RenderWindowCore>, UINT32, UINT32, float, UINT32)> fullscreenFunc =
  176. [](SPtr<RenderWindowCore> renderWindow, UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  177. {
  178. renderWindow->setFullscreen(width, height, refreshRate, monitorIdx);
  179. };
  180. accessor.queueCommand(std::bind(fullscreenFunc, getCore(), width, height, refreshRate, monitorIdx));
  181. }
  182. void RenderWindow::setFullscreen(CoreAccessor& accessor, const VideoMode& mode)
  183. {
  184. std::function<void(SPtr<RenderWindowCore>, const VideoMode&)> fullscreenFunc =
  185. [](SPtr<RenderWindowCore> renderWindow, const VideoMode& mode)
  186. {
  187. renderWindow->setFullscreen(mode);
  188. };
  189. accessor.queueCommand(std::bind(fullscreenFunc, getCore(), std::cref(mode)));
  190. }
  191. void RenderWindow::setWindowed(CoreAccessor& accessor, UINT32 width, UINT32 height)
  192. {
  193. std::function<void(SPtr<RenderWindowCore>, UINT32, UINT32)> windowedFunc =
  194. [](SPtr<RenderWindowCore> renderWindow, UINT32 width, UINT32 height)
  195. {
  196. renderWindow->setWindowed(width, height);
  197. };
  198. accessor.queueCommand(std::bind(windowedFunc, getCore(), width, height));
  199. }
  200. SPtr<RenderWindowCore> RenderWindow::getCore() const
  201. {
  202. return std::static_pointer_cast<RenderWindowCore>(mCoreSpecific);
  203. }
  204. SPtr<CoreObjectCore> RenderWindow::createCore() const
  205. {
  206. RENDER_WINDOW_DESC desc = mDesc;
  207. return RenderWindowCoreManager::instance().createInternal(desc);
  208. }
  209. RenderWindowPtr RenderWindow::create(RENDER_WINDOW_DESC& desc, RenderWindowPtr parentWindow)
  210. {
  211. return RenderWindowManager::instance().create(desc, parentWindow);
  212. }
  213. RenderWindowProperties& RenderWindow::getMutableProperties()
  214. {
  215. return const_cast<RenderWindowProperties&>(getProperties());
  216. }
  217. CoreSyncData RenderWindow::syncToCore(FrameAlloc* allocator)
  218. {
  219. UINT32 size = sizeof(RenderWindowProperties);
  220. UINT8* buffer = allocator->alloc(size);
  221. RenderWindowProperties& props = const_cast<RenderWindowProperties&>(getProperties());
  222. memcpy(buffer, &props, size);
  223. return CoreSyncData(buffer, size);
  224. }
  225. const RenderWindowProperties& RenderWindow::getProperties() const
  226. {
  227. return static_cast<const RenderWindowProperties&>(getPropertiesInternal());
  228. }
  229. }