CmWin32Context.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef _WIN32_WINNT
  25. #define _WIN32_WINNT 0x0500
  26. #endif
  27. #include "CmWin32Context.h"
  28. #include "OgreException.h"
  29. #include "CmGLRenderSystem.h"
  30. #include "CmRenderSystemManager.h"
  31. namespace CamelotEngine {
  32. Win32Context::Win32Context(HDC HDC,
  33. HGLRC Glrc):
  34. mHDC(HDC),
  35. mGlrc(Glrc)
  36. {
  37. }
  38. Win32Context::~Win32Context()
  39. {
  40. // NB have to do this is subclass to ensure any methods called back
  41. // are on this subclass and not half-destructed superclass
  42. GLRenderSystem *rs = static_cast<GLRenderSystem*>(CamelotEngine::RenderSystemManager::getActive());
  43. rs->_unregisterContext(this);
  44. }
  45. void Win32Context::setCurrent()
  46. {
  47. wglMakeCurrent(mHDC, mGlrc);
  48. }
  49. void Win32Context::endCurrent()
  50. {
  51. wglMakeCurrent(NULL, NULL);
  52. }
  53. GLContext* Win32Context::clone() const
  54. {
  55. // Create new context based on own HDC
  56. HGLRC newCtx = wglCreateContext(mHDC);
  57. if (!newCtx)
  58. {
  59. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  60. "Error calling wglCreateContext", "Win32Context::clone");
  61. }
  62. HGLRC oldrc = wglGetCurrentContext();
  63. HDC oldhdc = wglGetCurrentDC();
  64. wglMakeCurrent(NULL, NULL);
  65. // Share lists with old context
  66. if (!wglShareLists(mGlrc, newCtx))
  67. {
  68. String errorMsg = translateWGLError();
  69. wglDeleteContext(newCtx);
  70. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, String("wglShareLists() failed: ") + errorMsg, "Win32Context::clone");
  71. }
  72. // restore old context
  73. wglMakeCurrent(oldhdc, oldrc);
  74. return new Win32Context(mHDC, newCtx);
  75. }
  76. void Win32Context::releaseContext()
  77. {
  78. if (mGlrc != NULL)
  79. {
  80. wglDeleteContext(mGlrc);
  81. mGlrc = NULL;
  82. mHDC = NULL;
  83. }
  84. }
  85. }
  86. #if CM_THREAD_SUPPORT == 1
  87. // declared in CmGLPrerequisites.h
  88. WGLEWContext * wglewGetContext()
  89. {
  90. using namespace CamelotEngine;
  91. static CM_THREAD_POINTER_VAR(WGLEWContext, WGLEWContextsPtr);
  92. WGLEWContext * currentWGLEWContextsPtr = CM_THREAD_POINTER_GET(WGLEWContextsPtr);
  93. if (currentWGLEWContextsPtr == NULL)
  94. {
  95. currentWGLEWContextsPtr = new WGLEWContext();
  96. CM_THREAD_POINTER_SET(WGLEWContextsPtr, currentWGLEWContextsPtr);
  97. ZeroMemory(currentWGLEWContextsPtr, sizeof(WGLEWContext));
  98. wglewInit();
  99. }
  100. return currentWGLEWContextsPtr;
  101. }
  102. #endif