00001 00002 // 00003 // SFML - Simple and Fast Multimedia Library 00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) 00005 // 00006 // This software is provided 'as-is', without any express or implied warranty. 00007 // In no event will the authors be held liable for any damages arising from the use of this software. 00008 // 00009 // Permission is granted to anyone to use this software for any purpose, 00010 // including commercial applications, and to alter it and redistribute it freely, 00011 // subject to the following restrictions: 00012 // 00013 // 1. The origin of this software must not be misrepresented; 00014 // you must not claim that you wrote the original software. 00015 // If you use this software in a product, an acknowledgment 00016 // in the product documentation would be appreciated but is not required. 00017 // 00018 // 2. Altered source versions must be plainly marked as such, 00019 // and must not be misrepresented as being the original software. 00020 // 00021 // 3. This notice may not be removed or altered from any source distribution. 00022 // 00024 00026 // Headers 00028 #include <SFML/Window/ContextGL.hpp> 00029 #include <SFML/System/ThreadLocalPtr.hpp> 00030 #include <SFML/OpenGL.hpp> 00031 #include <SFML/Window/glext/glext.h> 00032 #include <stdlib.h> 00033 00034 00035 #if defined(SFML_SYSTEM_WINDOWS) 00036 00037 #include <SFML/Window/Win32/ContextWGL.hpp> 00038 typedef sf::priv::ContextWGL ContextType; 00039 00040 #elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD) 00041 00042 #include <SFML/Window/Linux/ContextGLX.hpp> 00043 typedef sf::priv::ContextGLX ContextType; 00044 00045 #elif defined(SFML_SYSTEM_MACOS) 00046 00047 #include <SFML/Window/Cocoa/ContextAGL.hpp> 00048 typedef sf::priv::ContextAGL ContextType; 00049 00050 #endif 00051 00052 00054 // Private data 00056 namespace 00057 { 00058 // This thread-local variable will hold the "global" context for each thread 00059 sf::ThreadLocalPtr<sf::priv::ContextGL> threadContext(NULL); 00060 00061 // Now we create two global contexts. 00062 // The first one is the reference context: it will be shared with every other 00063 // context, and it can't be activated if we want the sharing operation to always succeed. 00064 // That's why we need the second context: this one will be activated and used 00065 // in the main thread whenever there's no other context (window) active. 00066 ContextType referenceContext(NULL); 00067 ContextType defaultContext(&referenceContext); 00068 } 00069 00070 00071 namespace sf 00072 { 00073 namespace priv 00074 { 00076 ContextGL* ContextGL::New() 00077 { 00078 return new ContextType(&referenceContext); 00079 } 00080 00081 00083 ContextGL* ContextGL::New(const WindowImpl* owner, unsigned int bitsPerPixel, const ContextSettings& settings) 00084 { 00085 ContextType* context = new ContextType(&referenceContext, owner, bitsPerPixel, settings); 00086 00087 // Enable antialiasing if needed 00088 if (context->GetSettings().AntialiasingLevel > 0) 00089 glEnable(GL_MULTISAMPLE_ARB); 00090 00091 return context; 00092 } 00093 00094 00096 ContextGL::~ContextGL() 00097 { 00098 if (threadContext == this) 00099 { 00100 threadContext = NULL; 00101 } 00102 else if (threadContext) 00103 { 00104 // Don't call this->SetActive(false), it would lead to a pure virtual function call 00105 threadContext->SetActive(true); 00106 } 00107 } 00108 00109 00111 const ContextSettings& ContextGL::GetSettings() const 00112 { 00113 return mySettings; 00114 } 00115 00116 00118 bool ContextGL::SetActive(bool active) 00119 { 00120 if (active) 00121 { 00122 // Activate the context 00123 if (MakeCurrent()) 00124 { 00125 // If this is the first context to be activated on this thread, make 00126 // it the reference context for the whole thread 00127 if (!threadContext) 00128 threadContext = this; 00129 00130 return true; 00131 } 00132 } 00133 else 00134 { 00135 // Deactivate the context 00136 if (threadContext && (threadContext != this)) 00137 { 00138 // To deactivate the context, we actually activate another one 00139 // so that we make sure that there is always an active context 00140 // for subsequent graphics operations 00141 return threadContext->SetActive(true); 00142 } 00143 } 00144 00145 // If we got there then something failed 00146 return false; 00147 } 00148 00149 00151 bool ContextGL::SetReferenceActive() 00152 { 00153 if (threadContext) 00154 return threadContext->SetActive(true); 00155 else 00156 return false; 00157 } 00158 00159 00161 ContextGL::ContextGL() 00162 { 00163 // Nothing to do 00164 } 00165 00166 00168 int ContextGL::EvaluateFormat(unsigned int bitsPerPixel, const ContextSettings& settings, int colorBits, int depthBits, int stencilBits, int antialiasing) 00169 { 00170 return abs(static_cast<int>(bitsPerPixel - colorBits)) + 00171 abs(static_cast<int>(settings.DepthBits - depthBits)) + 00172 abs(static_cast<int>(settings.StencilBits - stencilBits)) + 00173 abs(static_cast<int>(settings.AntialiasingLevel - antialiasing)); 00174 } 00175 00176 } // namespace priv 00177 00178 } // namespace sf
:: Copyright © 2007-2008 Laurent Gomila, all rights reserved :: Documentation generated by doxygen 1.5.2 ::