Graphics.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. // ATOMIC BEGIN
  50. #include <SDL/include/SDL.h>
  51. #include <SDL/include/SDL_syswm.h>
  52. // ATOMIC END
  53. #include "../DebugNew.h"
  54. namespace Atomic
  55. {
  56. // ATOMIC BEGIN
  57. unsigned Graphics::numPasses_ = 0;
  58. unsigned Graphics::numSinglePassPrimitives_ = 0;
  59. // ATOMIC END
  60. void Graphics::SetExternalWindow(void* window)
  61. {
  62. if (!window_)
  63. externalWindow_ = window;
  64. else
  65. ATOMIC_LOGERROR("Window already opened, can not set external window");
  66. }
  67. void Graphics::SetWindowTitle(const String& windowTitle)
  68. {
  69. windowTitle_ = windowTitle;
  70. if (window_)
  71. SDL_SetWindowTitle(window_, windowTitle_.CString());
  72. }
  73. void Graphics::SetWindowIcon(Image* windowIcon)
  74. {
  75. windowIcon_ = windowIcon;
  76. if (window_)
  77. CreateWindowIcon();
  78. }
  79. void Graphics::SetWindowPosition(const IntVector2& position)
  80. {
  81. if (window_)
  82. SDL_SetWindowPosition(window_, position.x_, position.y_);
  83. else
  84. position_ = position; // Sets as initial position for OpenWindow()
  85. }
  86. void Graphics::SetWindowPosition(int x, int y)
  87. {
  88. SetWindowPosition(IntVector2(x, y));
  89. }
  90. void Graphics::SetOrientations(const String& orientations)
  91. {
  92. orientations_ = orientations.Trimmed();
  93. SDL_SetHint(SDL_HINT_ORIENTATIONS, orientations_.CString());
  94. }
  95. bool Graphics::ToggleFullscreen()
  96. {
  97. return SetMode(width_, height_, !fullscreen_, borderless_, resizable_, highDPI_, vsync_, tripleBuffer_, multiSample_);
  98. }
  99. IntVector2 Graphics::GetWindowPosition() const
  100. {
  101. if (window_)
  102. return position_;
  103. return IntVector2::ZERO;
  104. }
  105. PODVector<IntVector2> Graphics::GetResolutions() const
  106. {
  107. PODVector<IntVector2> ret;
  108. // Emscripten is not able to return a valid list
  109. #ifndef __EMSCRIPTEN__
  110. unsigned numModes = (unsigned)SDL_GetNumDisplayModes(0);
  111. for (unsigned i = 0; i < numModes; ++i)
  112. {
  113. SDL_DisplayMode mode;
  114. SDL_GetDisplayMode(0, i, &mode);
  115. int width = mode.w;
  116. int height = mode.h;
  117. // Store mode if unique
  118. bool unique = true;
  119. for (unsigned j = 0; j < ret.Size(); ++j)
  120. {
  121. if (ret[j].x_ == width && ret[j].y_ == height)
  122. {
  123. unique = false;
  124. break;
  125. }
  126. }
  127. if (unique)
  128. ret.Push(IntVector2(width, height));
  129. }
  130. #endif
  131. return ret;
  132. }
  133. IntVector2 Graphics::GetDesktopResolution() const
  134. {
  135. #if !defined(__ANDROID__) && !defined(IOS)
  136. SDL_DisplayMode mode;
  137. SDL_GetDesktopDisplayMode(0, &mode);
  138. return IntVector2(mode.w, mode.h);
  139. #else
  140. // SDL_GetDesktopDisplayMode() may not work correctly on mobile platforms. Rather return the window size
  141. return IntVector2(width_, height_);
  142. #endif
  143. }
  144. void Graphics::Maximize()
  145. {
  146. if (!window_)
  147. return;
  148. SDL_MaximizeWindow(window_);
  149. }
  150. void Graphics::Minimize()
  151. {
  152. if (!window_)
  153. return;
  154. SDL_MinimizeWindow(window_);
  155. }
  156. void Graphics::AddGPUObject(GPUObject* object)
  157. {
  158. MutexLock lock(gpuObjectMutex_);
  159. gpuObjects_.Push(object);
  160. }
  161. void Graphics::RemoveGPUObject(GPUObject* object)
  162. {
  163. MutexLock lock(gpuObjectMutex_);
  164. gpuObjects_.Remove(object);
  165. }
  166. void* Graphics::ReserveScratchBuffer(unsigned size)
  167. {
  168. if (!size)
  169. return 0;
  170. if (size > maxScratchBufferRequest_)
  171. maxScratchBufferRequest_ = size;
  172. // First check for a free buffer that is large enough
  173. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  174. {
  175. if (!i->reserved_ && i->size_ >= size)
  176. {
  177. i->reserved_ = true;
  178. return i->data_.Get();
  179. }
  180. }
  181. // Then check if a free buffer can be resized
  182. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  183. {
  184. if (!i->reserved_)
  185. {
  186. i->data_ = new unsigned char[size];
  187. i->size_ = size;
  188. i->reserved_ = true;
  189. ATOMIC_LOGDEBUG("Resized scratch buffer to size " + String(size));
  190. return i->data_.Get();
  191. }
  192. }
  193. // Finally allocate a new buffer
  194. ScratchBuffer newBuffer;
  195. newBuffer.data_ = new unsigned char[size];
  196. newBuffer.size_ = size;
  197. newBuffer.reserved_ = true;
  198. scratchBuffers_.Push(newBuffer);
  199. return newBuffer.data_.Get();
  200. ATOMIC_LOGDEBUG("Allocated scratch buffer with size " + String(size));
  201. }
  202. void Graphics::FreeScratchBuffer(void* buffer)
  203. {
  204. if (!buffer)
  205. return;
  206. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  207. {
  208. if (i->reserved_ && i->data_.Get() == buffer)
  209. {
  210. i->reserved_ = false;
  211. return;
  212. }
  213. }
  214. ATOMIC_LOGWARNING("Reserved scratch buffer " + ToStringHex((unsigned)(size_t)buffer) + " not found");
  215. }
  216. void Graphics::CleanupScratchBuffers()
  217. {
  218. for (Vector<ScratchBuffer>::Iterator i = scratchBuffers_.Begin(); i != scratchBuffers_.End(); ++i)
  219. {
  220. if (!i->reserved_ && i->size_ > maxScratchBufferRequest_ * 2 && i->size_ >= 1024 * 1024)
  221. {
  222. i->data_ = maxScratchBufferRequest_ > 0 ? new unsigned char[maxScratchBufferRequest_] : 0;
  223. i->size_ = maxScratchBufferRequest_;
  224. ATOMIC_LOGDEBUG("Resized scratch buffer to size " + String(maxScratchBufferRequest_));
  225. }
  226. }
  227. maxScratchBufferRequest_ = 0;
  228. }
  229. void Graphics::CreateWindowIcon()
  230. {
  231. if (windowIcon_)
  232. {
  233. SDL_Surface* surface = windowIcon_->GetSDLSurface();
  234. if (surface)
  235. {
  236. SDL_SetWindowIcon(window_, surface);
  237. SDL_FreeSurface(surface);
  238. }
  239. }
  240. }
  241. void RegisterGraphicsLibrary(Context* context)
  242. {
  243. Animation::RegisterObject(context);
  244. Material::RegisterObject(context);
  245. Model::RegisterObject(context);
  246. Shader::RegisterObject(context);
  247. Technique::RegisterObject(context);
  248. Texture2D::RegisterObject(context);
  249. Texture2DArray::RegisterObject(context);
  250. Texture3D::RegisterObject(context);
  251. TextureCube::RegisterObject(context);
  252. Camera::RegisterObject(context);
  253. Drawable::RegisterObject(context);
  254. Light::RegisterObject(context);
  255. StaticModel::RegisterObject(context);
  256. StaticModelGroup::RegisterObject(context);
  257. Skybox::RegisterObject(context);
  258. AnimatedModel::RegisterObject(context);
  259. AnimationController::RegisterObject(context);
  260. BillboardSet::RegisterObject(context);
  261. ParticleEffect::RegisterObject(context);
  262. ParticleEmitter::RegisterObject(context);
  263. RibbonTrail::RegisterObject(context);
  264. CustomGeometry::RegisterObject(context);
  265. DecalSet::RegisterObject(context);
  266. Terrain::RegisterObject(context);
  267. TerrainPatch::RegisterObject(context);
  268. DebugRenderer::RegisterObject(context);
  269. Octree::RegisterObject(context);
  270. Zone::RegisterObject(context);
  271. }
  272. // ATOMIC BEGIN
  273. int Graphics::GetCurrentMonitor()
  274. {
  275. return SDL_GetWindowDisplayIndex((SDL_Window*) this->GetSDLWindow());
  276. }
  277. int Graphics::GetNumMonitors()
  278. {
  279. return SDL_GetNumVideoDisplays();
  280. }
  281. bool Graphics::GetMaximized()
  282. {
  283. if (!window_)
  284. return false;
  285. return SDL_GetWindowFlags(window_) & SDL_WINDOW_MAXIMIZED;
  286. }
  287. IntVector2 Graphics::GetMonitorResolution(int monitorId) const
  288. {
  289. SDL_DisplayMode mode;
  290. SDL_GetDesktopDisplayMode(monitorId, &mode);
  291. return IntVector2(mode.w, mode.h);
  292. }
  293. void Graphics::RaiseWindow()
  294. {
  295. if (window_)
  296. SDL_RaiseWindow(window_);
  297. }
  298. // ATOMIC END
  299. }