Graphics.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Graphics/AnimatedModel.h"
  24. #include "../Graphics/Animation.h"
  25. #include "../Graphics/AnimationController.h"
  26. #include "../Graphics/Camera.h"
  27. #include "../Graphics/CustomGeometry.h"
  28. #include "../Graphics/DebugRenderer.h"
  29. #include "../Graphics/DecalSet.h"
  30. #include "../Graphics/Graphics.h"
  31. #include "../Graphics/GraphicsImpl.h"
  32. #include "../Graphics/Material.h"
  33. #include "../Graphics/Octree.h"
  34. #include "../Graphics/ParticleEffect.h"
  35. #include "../Graphics/ParticleEmitter.h"
  36. #include "../Graphics/RibbonTrail.h"
  37. #include "../Graphics/Shader.h"
  38. #include "../Graphics/Skybox.h"
  39. #include "../Graphics/StaticModelGroup.h"
  40. #include "../Graphics/Technique.h"
  41. #include "../Graphics/Terrain.h"
  42. #include "../Graphics/TerrainPatch.h"
  43. #include "../Graphics/Texture2D.h"
  44. #include "../Graphics/Texture2DArray.h"
  45. #include "../Graphics/Texture3D.h"
  46. #include "../Graphics/TextureCube.h"
  47. #include "../Graphics/Zone.h"
  48. #include "../IO/Log.h"
  49. #include <SDL/SDL.h>
  50. #include <SDL/SDL_syswm.h>
  51. #include "../DebugNew.h"
  52. namespace Urho3D
  53. {
  54. void Graphics::SetExternalWindow(void* window)
  55. {
  56. if (!impl_->window_)
  57. externalWindow_ = window;
  58. else
  59. URHO3D_LOGERROR("Window already opened, can not set external window");
  60. }
  61. void Graphics::SetWindowTitle(const String& windowTitle)
  62. {
  63. windowTitle_ = windowTitle;
  64. if (impl_->window_)
  65. SDL_SetWindowTitle(impl_->window_, windowTitle_.CString());
  66. }
  67. void Graphics::SetWindowIcon(Image* windowIcon)
  68. {
  69. windowIcon_ = windowIcon;
  70. if (impl_->window_)
  71. CreateWindowIcon();
  72. }
  73. void Graphics::SetWindowPosition(const IntVector2& position)
  74. {
  75. if (impl_->window_)
  76. SDL_SetWindowPosition(impl_->window_, position.x_, position.y_);
  77. else
  78. position_ = position; // Sets as initial position for OpenWindow()
  79. }
  80. void Graphics::SetWindowPosition(int x, int y)
  81. {
  82. SetWindowPosition(IntVector2(x, y));
  83. }
  84. void Graphics::SetOrientations(const String& orientations)
  85. {
  86. orientations_ = orientations.Trimmed();
  87. SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
  88. }
  89. bool Graphics::ToggleFullscreen()
  90. {
  91. return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, highDPI_, vsync_, tripleBuffer_, multiSample_);
  92. }
  93. IntVector2 Graphics::GetWindowPosition() const
  94. {
  95. if (impl_->window_)
  96. return position_;
  97. return IntVector2::ZERO;
  98. }
  99. PODVector<IntVector2> Graphics::GetResolutions() const
  100. {
  101. PODVector<IntVector2> ret;
  102. // Emscripten is not able to return a valid list
  103. #ifndef __EMSCRIPTEN__
  104. unsigned numModes = (unsigned)SDL_GetNumDisplayModes(0);
  105. for (unsigned i = 0; i < numModes; ++i)
  106. {
  107. SDL_DisplayMode mode;
  108. SDL_GetDisplayMode(0, i, &mode);
  109. int width = mode.w;
  110. int height = mode.h;
  111. // Store mode if unique
  112. bool unique = true;
  113. for (unsigned j = 0; j < ret.Size(); ++j)
  114. {
  115. if (ret[j].x_ == width && ret[j].y_ == height)
  116. {
  117. unique = false;
  118. break;
  119. }
  120. }
  121. if (unique)
  122. ret.Push(IntVector2(width, height));
  123. }
  124. #endif
  125. return ret;
  126. }
  127. IntVector2 Graphics::GetDesktopResolution() const
  128. {
  129. #if !defined(__ANDROID__) && !defined(IOS)
  130. SDL_DisplayMode mode;
  131. SDL_GetDesktopDisplayMode(0, &mode);
  132. return IntVector2(mode.w, mode.h);
  133. #else
  134. // SDL_GetDesktopDisplayMode() may not work correctly on mobile platforms. Rather return the window size
  135. return IntVector2(width_, height_);
  136. #endif
  137. }
  138. void Graphics::Maximize()
  139. {
  140. if (!impl_->window_)
  141. return;
  142. SDL_MaximizeWindow(impl_->window_);
  143. }
  144. void Graphics::Minimize()
  145. {
  146. if (!impl_->window_)
  147. return;
  148. SDL_MinimizeWindow(impl_->window_);
  149. }
  150. void Graphics::AddGPUObject(GPUObject* object)
  151. {
  152. MutexLock lock(gpuObjectMutex_);
  153. gpuObjects_.Push(object);
  154. }
  155. void Graphics::RemoveGPUObject(GPUObject* object)
  156. {
  157. MutexLock lock(gpuObjectMutex_);
  158. gpuObjects_.Remove(object);
  159. }
  160. void* Graphics::ReserveScratchBuffer(unsigned size)
  161. {
  162. if (!size)
  163. return 0;
  164. if (size > maxScratchBufferRequest_)
  165. maxScratchBufferRequest_ = size;
  166. // First check for a free buffer that is large enough
  167. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  168. {
  169. if (!i->reserved_ && i->size_ >= size)
  170. {
  171. i->reserved_ = true;
  172. return i->data_.Get();
  173. }
  174. }
  175. // Then check if a free buffer can be resized
  176. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  177. {
  178. if (!i->reserved_)
  179. {
  180. i->data_ = new unsigned char[size];
  181. i->size_ = size;
  182. i->reserved_ = true;
  183. URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(size));
  184. return i->data_.Get();
  185. }
  186. }
  187. // Finally allocate a new buffer
  188. ScratchBuffer newBuffer;
  189. newBuffer.data_ = new unsigned char[size];
  190. newBuffer.size_ = size;
  191. newBuffer.reserved_ = true;
  192. scratchBuffers_.Push(newBuffer);
  193. return newBuffer.data_.Get();
  194. URHO3D_LOGDEBUG("Allocated scratch buffer with size " + String(size));
  195. }
  196. void Graphics::FreeScratchBuffer(void* buffer)
  197. {
  198. if (!buffer)
  199. return;
  200. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  201. {
  202. if (i->reserved_ && i->data_.Get() == buffer)
  203. {
  204. i->reserved_ = false;
  205. return;
  206. }
  207. }
  208. URHO3D_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
  209. }
  210. void Graphics::CleanupScratchBuffers()
  211. {
  212. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  213. {
  214. if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
  215. {
  216. i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
  217. i->size_ = maxScratchBufferRequest_;
  218. URHO3D_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
  219. }
  220. }
  221. maxScratchBufferRequest_ = 0;
  222. }
  223. void Graphics::CreateWindowIcon()
  224. {
  225. if (windowIcon_)
  226. {
  227. SDL_Surface* surface = windowIcon_->GetSDLSurface();
  228. if (surface)
  229. {
  230. SDL_SetWindowIcon(impl_->window_, surface);
  231. SDL_FreeSurface(surface);
  232. }
  233. }
  234. }
  235. void RegisterGraphicsLibrary(Context* context)
  236. {
  237. Animation::RegisterObject(context);
  238. Material::RegisterObject(context);
  239. Model::RegisterObject(context);
  240. Shader::RegisterObject(context);
  241. Technique::RegisterObject(context);
  242. Texture2D::RegisterObject(context);
  243. Texture2DArray::RegisterObject(context);
  244. Texture3D::RegisterObject(context);
  245. TextureCube::RegisterObject(context);
  246. Camera::RegisterObject(context);
  247. Drawable::RegisterObject(context);
  248. Light::RegisterObject(context);
  249. StaticModel::RegisterObject(context);
  250. StaticModelGroup::RegisterObject(context);
  251. Skybox::RegisterObject(context);
  252. AnimatedModel::RegisterObject(context);
  253. AnimationController::RegisterObject(context);
  254. BillboardSet::RegisterObject(context);
  255. ParticleEffect::RegisterObject(context);
  256. ParticleEmitter::RegisterObject(context);
  257. RibbonTrail::RegisterObject(context);
  258. CustomGeometry::RegisterObject(context);
  259. DecalSet::RegisterObject(context);
  260. Terrain::RegisterObject(context);
  261. TerrainPatch::RegisterObject(context);
  262. DebugRenderer::RegisterObject(context);
  263. Octree::RegisterObject(context);
  264. Zone::RegisterObject(context);
  265. }
  266. }