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