Graphics.cpp 9.2 KB

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