FS#84 - Rewrite the OpenGL context handling

FS#66 - Free the global rendering context
Added thread-local storage classes

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1116 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
laurentgom 2009-05-31 11:38:54 +00:00
parent 3e23e46a64
commit 23bf546b6a
44 changed files with 1263 additions and 405 deletions

View File

@ -32,11 +32,7 @@
struct sfContext
{
sfContext() : This(sf::Context::New()) {}
~sfContext() {delete This;}
sf::Context* This;
sf::Context This;
};
@ -63,5 +59,5 @@ void sfContext_Destroy(sfContext* Context)
////////////////////////////////////////////////////////////
void sfContext_SetActive(sfContext* Context, sfBool Active)
{
CSFML_CALL_PTR(Context, SetActive(Active == sfTrue))
CSFML_CALL(Context, SetActive(Active == sfTrue))
}

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_workspace_file>
<Workspace title="SFML workspace">
<Project filename="sfml-system.cbp" />
<Project filename="sfml-system.cbp" active="1" />
<Project filename="sfml-window.cbp" />
<Project filename="sfml-network.cbp" />
<Project filename="sfml-graphics.cbp" />
<Project filename="sfml-audio.cbp" active="1" />
<Project filename="sfml-audio.cbp" />
<Project filename="sfml-main.cbp" />
<Project filename="..\..\samples\build\codeblocks\ftp.cbp" />
<Project filename="..\..\samples\build\codeblocks\opengl.cbp" />

View File

@ -138,14 +138,14 @@
<Unit filename="..\..\src\SFML\Graphics\Font.cpp" />
<Unit filename="..\..\src\SFML\Graphics\FontLoader.cpp" />
<Unit filename="..\..\src\SFML\Graphics\FontLoader.hpp" />
<Unit filename="..\..\src\SFML\Graphics\GLCheck.cpp" />
<Unit filename="..\..\src\SFML\Graphics\GLCheck.hpp" />
<Unit filename="..\..\src\SFML\Graphics\GLEW\glew.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="..\..\src\SFML\Graphics\GLEW\glew.h" />
<Unit filename="..\..\src\SFML\Graphics\GLEW\glxew.h" />
<Unit filename="..\..\src\SFML\Graphics\GLEW\wglew.h" />
<Unit filename="..\..\src\SFML\Graphics\GraphicsContext.cpp" />
<Unit filename="..\..\src\SFML\Graphics\GraphicsContext.hpp" />
<Unit filename="..\..\src\SFML\Graphics\Image.cpp" />
<Unit filename="..\..\src\SFML\Graphics\ImageLoader.cpp" />
<Unit filename="..\..\src\SFML\Graphics\ImageLoader.hpp" />

View File

@ -99,6 +99,9 @@
<Unit filename="..\..\include\SFML\System\ResourcePtr.inl" />
<Unit filename="..\..\include\SFML\System\Sleep.hpp" />
<Unit filename="..\..\include\SFML\System\Thread.hpp" />
<Unit filename="..\..\include\SFML\System\ThreadLocal.hpp" />
<Unit filename="..\..\include\SFML\System\ThreadLocalPtr.hpp" />
<Unit filename="..\..\include\SFML\System\ThreadLocalPtr.inl" />
<Unit filename="..\..\include\SFML\System\Unicode.hpp" />
<Unit filename="..\..\include\SFML\System\Unicode.inl" />
<Unit filename="..\..\include\SFML\System\Vector2.hpp" />
@ -112,6 +115,7 @@
<Unit filename="..\..\src\SFML\System\Randomizer.cpp" />
<Unit filename="..\..\src\SFML\System\Sleep.cpp" />
<Unit filename="..\..\src\SFML\System\Thread.cpp" />
<Unit filename="..\..\src\SFML\System\ThreadLocal.cpp" />
<Unit filename="..\..\src\SFML\System\Unicode.cpp" />
<Unit filename="..\..\src\SFML\System\Win32\MutexImpl.cpp" />
<Unit filename="..\..\src\SFML\System\Win32\MutexImpl.hpp" />
@ -119,6 +123,8 @@
<Unit filename="..\..\src\SFML\System\Win32\Platform.hpp" />
<Unit filename="..\..\src\SFML\System\Win32\ThreadImpl.cpp" />
<Unit filename="..\..\src\SFML\System\Win32\ThreadImpl.hpp" />
<Unit filename="..\..\src\SFML\System\Win32\ThreadLocalImpl.cpp" />
<Unit filename="..\..\src\SFML\System\Win32\ThreadLocalImpl.hpp" />
<Extensions>
<code_completion />
<envvars />

View File

@ -119,6 +119,8 @@
<Unit filename="..\..\include\SFML\Window\WindowStyle.hpp" />
<Unit filename="..\..\include\SFML\Window\glew\glew.h" />
<Unit filename="..\..\src\SFML\Window\Context.cpp" />
<Unit filename="..\..\src\SFML\Window\ContextGL.cpp" />
<Unit filename="..\..\src\SFML\Window\ContextGL.hpp" />
<Unit filename="..\..\src\SFML\Window\Input.cpp" />
<Unit filename="..\..\src\SFML\Window\Joystick.hpp" />
<Unit filename="..\..\src\SFML\Window\VideoMode.cpp" />

View File

@ -3314,18 +3314,18 @@
RelativePath="..\..\src\SFML\Graphics\FontLoader.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\GLCheck.cpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\GLCheck.hpp"
>
</File>
<File
RelativePath="..\..\include\SFML\Graphics\Glyph.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\GraphicsContext.cpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\GraphicsContext.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\Image.cpp"
>

View File

@ -365,6 +365,14 @@
RelativePath="..\..\src\SFML\System\Win32\ThreadImpl.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\System\Win32\ThreadLocalImpl.cpp"
>
</File>
<File
RelativePath="..\..\src\SFML\System\Win32\ThreadLocalImpl.hpp"
>
</File>
</Filter>
<File
RelativePath="..\..\src\SFML\System\Clock.cpp"
@ -470,6 +478,22 @@
RelativePath="..\..\include\SFML\System\Thread.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\System\ThreadLocal.cpp"
>
</File>
<File
RelativePath="..\..\include\SFML\System\ThreadLocal.hpp"
>
</File>
<File
RelativePath="..\..\include\SFML\System\ThreadLocalPtr.hpp"
>
</File>
<File
RelativePath="..\..\include\SFML\System\ThreadLocalPtr.inl"
>
</File>
<File
RelativePath="..\..\src\SFML\System\Unicode.cpp"
>

View File

@ -456,6 +456,14 @@
RelativePath="..\..\include\SFML\Window\Context.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Window\ContextGL.cpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Window\ContextGL.hpp"
>
</File>
<File
RelativePath="..\..\include\SFML\Window\ContextSettings.hpp"
>

View File

@ -3308,18 +3308,18 @@
RelativePath="..\..\src\SFML\Graphics\FontLoader.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\GLCheck.cpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\GLCheck.hpp"
>
</File>
<File
RelativePath="..\..\include\SFML\Graphics\Glyph.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\GraphicsContext.cpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\GraphicsContext.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Graphics\Image.cpp"
>

View File

@ -358,6 +358,14 @@
RelativePath="..\..\src\SFML\System\Win32\ThreadImpl.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\System\Win32\ThreadLocalImpl.cpp"
>
</File>
<File
RelativePath="..\..\src\SFML\System\Win32\ThreadLocalImpl.hpp"
>
</File>
</Filter>
<File
RelativePath="..\..\src\SFML\System\Clock.cpp"
@ -427,6 +435,22 @@
RelativePath="..\..\src\SFML\System\Thread.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\System\ThreadLocal.cpp"
>
</File>
<File
RelativePath="..\..\include\SFML\System\ThreadLocal.hpp"
>
</File>
<File
RelativePath="..\..\include\SFML\System\ThreadLocalPtr.hpp"
>
</File>
<File
RelativePath="..\..\include\SFML\System\ThreadLocalPtr.inl"
>
</File>
<File
RelativePath="..\..\src\SFML\System\Unicode.cpp"
>

View File

@ -415,6 +415,14 @@
RelativePath="..\..\include\SFML\Window\Context.hpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Window\ContextGL.cpp"
>
</File>
<File
RelativePath="..\..\src\SFML\Window\ContextGL.hpp"
>
</File>
<File
RelativePath="..\..\include\SFML\Window\ContextSettings.hpp"
>

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -70,7 +70,7 @@ namespace SFML
{
if (This == IntPtr.Zero)
throw new LoadingFailedException("image", filename);
}
}
////////////////////////////////////////////////////////////
/// <summary>

View File

@ -36,6 +36,8 @@
#include <SFML/System/Randomizer.hpp>
#include <SFML/System/Sleep.hpp>
#include <SFML/System/Thread.hpp>
#include <SFML/System/ThreadLocal.hpp>
#include <SFML/System/ThreadLocalPtr.hpp>
#include <SFML/System/Unicode.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/System/Vector3.hpp>

View File

@ -0,0 +1,95 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_THREADLOCAL_HPP
#define SFML_THREADLOCAL_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <cstdlib>
namespace sf
{
namespace priv
{
class ThreadLocalImpl;
}
////////////////////////////////////////////////////////////
/// Wrapper to handle variables that are local to threads.
/// This means that the same ThreadLocalStorage variable will
/// hold a different value for each different thread.
/// For a more strongly-typed interface, use the ThreadLocal
/// template class.
////////////////////////////////////////////////////////////
class SFML_API ThreadLocal : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// Default constructor
///
/// \param Value : Optional value to initalize the variable (NULL by default)
///
////////////////////////////////////////////////////////////
ThreadLocal(void* Value = NULL);
////////////////////////////////////////////////////////////
/// Destructor
///
////////////////////////////////////////////////////////////
~ThreadLocal();
////////////////////////////////////////////////////////////
/// Set the thread-specific value of the variable
///
/// \param Value : Value of the variable for this thread
///
////////////////////////////////////////////////////////////
void SetValue(void* Value);
////////////////////////////////////////////////////////////
/// Retrieve the thread-specific value of the variable
///
/// \return Value of the variable for this thread
///
////////////////////////////////////////////////////////////
void* GetValue() const;
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
priv::ThreadLocalImpl* myImpl; ///< Pointer to the OS specific implementation
};
} // namespace sf
#endif // SFML_THREADLOCAL_HPP

View File

@ -0,0 +1,102 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_THREADLOCALPTR_HPP
#define SFML_THREADLOCALPTR_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/ThreadLocal.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
/// Type-safe wrapper for thread local pointer variables
////////////////////////////////////////////////////////////
template <typename T>
class ThreadLocalPtr : private ThreadLocal
{
public :
////////////////////////////////////////////////////////////
/// Default constructor
///
/// \param Value : Optional value to initalize the variable (NULL by default)
///
////////////////////////////////////////////////////////////
ThreadLocalPtr(T* Value = NULL);
////////////////////////////////////////////////////////////
/// Operator * overload to return a reference to the variable
///
/// \return Reference to the thread-local value of the variable
///
////////////////////////////////////////////////////////////
T& operator *() const;
////////////////////////////////////////////////////////////
/// Operator -> overload to return a pointer to the variable
///
/// \return Pointer to the thread-local value of the variable
///
////////////////////////////////////////////////////////////
T* operator ->() const;
////////////////////////////////////////////////////////////
/// Implicit cast operator to T*
///
/// \return Value of the pointer for this thread
///
////////////////////////////////////////////////////////////
operator T*() const;
////////////////////////////////////////////////////////////
/// Assignment operator
///
/// \param Value : New pointer value to assign for this thread
///
/// \return Reference to this
///
////////////////////////////////////////////////////////////
ThreadLocalPtr<T>& operator =(T* Value);
////////////////////////////////////////////////////////////
/// Assignment operator
///
/// \param Other : Other thread-local pointer value to assign
///
/// \return Reference to this
///
////////////////////////////////////////////////////////////
ThreadLocalPtr<T>& operator =(const ThreadLocalPtr<T>& Other);
};
} // namespace sf
#include <SFML/System/ThreadLocalPtr.inl>
#endif // SFML_THREADLOCALPTR_HPP

View File

@ -0,0 +1,89 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
namespace sf
{
////////////////////////////////////////////////////////////
/// Default constructor
////////////////////////////////////////////////////////////
template <typename T>
ThreadLocalPtr<T>::ThreadLocalPtr(T* Value) :
ThreadLocal(Value)
{
}
////////////////////////////////////////////////////////////
/// Operator * overload to return a reference to the variable
////////////////////////////////////////////////////////////
template <typename T>
T& ThreadLocalPtr<T>::operator *() const
{
return *static_cast<T*>(GetValue());
}
////////////////////////////////////////////////////////////
/// Operator -> overload to return a pointer to the variable
////////////////////////////////////////////////////////////
template <typename T>
T* ThreadLocalPtr<T>::operator ->() const
{
return static_cast<T*>(GetValue());
}
////////////////////////////////////////////////////////////
/// Implicit cast operator to T*
////////////////////////////////////////////////////////////
template <typename T>
ThreadLocalPtr<T>::operator T*() const
{
return static_cast<T*>(GetValue());
}
////////////////////////////////////////////////////////////
/// Assignment operator
////////////////////////////////////////////////////////////
template <typename T>
ThreadLocalPtr<T>& ThreadLocalPtr<T>::operator =(T* Value)
{
SetValue(Value);
return *this;
}
////////////////////////////////////////////////////////////
/// Assignment operator
////////////////////////////////////////////////////////////
template <typename T>
ThreadLocalPtr<T>& ThreadLocalPtr<T>::operator =(const ThreadLocalPtr<T>& Other)
{
SetValue(Other.GetValue());
return *this;
}
} // namespace sf

View File

@ -29,7 +29,6 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/Window/ContextSettings.hpp>
#include <SFML/System/NonCopyable.hpp>
@ -37,132 +36,70 @@ namespace sf
{
namespace priv
{
class WindowImpl;
class ContextGL;
}
////////////////////////////////////////////////////////////
/// Abstract class representing an OpenGL context
/// Class holding a valid drawing context
///
/// If you need to make OpenGL / graphics calls without
/// having an active window (like in a thread), you can use
/// an instance of this class to get a valid context.
///
/// Having a valid context is necessary for *every* OpenGL call,
/// and for most of the classes from the Graphics package.
///
/// Note that a context is only active in its current thread,
/// if you create a new thread it will have no valid context
/// by default.
///
/// \code
/// void ThreadedFunc(void*)
/// {
/// sf::Context Ctx;
/// // from now on, you have a valid context
///
/// // you can make OpenGL calls
/// glClear(GL_DEPTH_BUFFER_BIT);
///
/// // as well as using objects from the graphics package
/// sf::Image Img;
/// Img.LoadFromFile("image.png");
/// }
/// // the context is automatically deactivated and destroyed
/// // by the sf::Context destructor
/// \endcode
////////////////////////////////////////////////////////////
class SFML_API Context : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// Create a new context, not associated to a window
///
/// \return Pointer to the created context
///
////////////////////////////////////////////////////////////
static Context* New();
////////////////////////////////////////////////////////////
/// Create a new context attached to a window
///
/// \param Owner : Pointer to the owner window
/// \param BitsPerPixel : Pixel depth (in bits per pixel)
/// \param Settings : Creation parameters
///
/// \return Pointer to the created context
///
////////////////////////////////////////////////////////////
static Context* New(const priv::WindowImpl* Owner, unsigned int BitsPerPixel, const ContextSettings& Settings);
////////////////////////////////////////////////////////////
/// Check if a context is active on the current thread
///
/// \return True if there's an active context, false otherwise
///
////////////////////////////////////////////////////////////
static bool IsContextActive();
////////////////////////////////////////////////////////////
/// Return the default context
///
/// \return Reference to the default context
///
////////////////////////////////////////////////////////////
static Context& GetDefault();
public :
////////////////////////////////////////////////////////////
/// Virtual destructor
///
////////////////////////////////////////////////////////////
virtual ~Context();
////////////////////////////////////////////////////////////
/// Get the settings of the context
///
/// \return Structure containing the settings
///
////////////////////////////////////////////////////////////
const ContextSettings& GetSettings() const;
////////////////////////////////////////////////////////////
/// Activate or deactivate the context as the current target
/// for rendering
///
/// \param Active : True to activate, false to deactivate
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
bool SetActive(bool Active);
////////////////////////////////////////////////////////////
/// Display the contents of the context
///
////////////////////////////////////////////////////////////
virtual void Display() = 0;
////////////////////////////////////////////////////////////
/// Enable / disable vertical synchronization
///
/// \param Enabled : True to enable v-sync, false to deactivate
///
////////////////////////////////////////////////////////////
virtual void UseVerticalSync(bool Enabled) = 0;
protected :
////////////////////////////////////////////////////////////
/// Default constructor
/// Default constructor -- creates and activates the context
///
////////////////////////////////////////////////////////////
Context();
////////////////////////////////////////////////////////////
/// Make this context the current one
///
/// \param Active : True to activate, false to deactivate
///
/// \return True on success, false if any error happened
/// Destructor -- deactivates and destroys the context
///
////////////////////////////////////////////////////////////
virtual bool MakeCurrent(bool Active) = 0;
~Context();
////////////////////////////////////////////////////////////
/// Evaluate a pixel format configuration.
/// This functions can be used by implementations that have
/// several valid formats and want to get the best one
/// Activate or deactivate explicitely the context
///
/// \param BitsPerPixel : Requested pixel depth (bits per pixel)
/// \param Settings : Requested additionnal settings
/// \param ColorBits : Color bits of the configuration to evaluate
/// \param DepthBits : Depth bits of the configuration to evaluate
/// \param StencilBits : Stencil bits of the configuration to evaluate
/// \param Antialiasing : Antialiasing level of the configuration to evaluate
///
/// \return Score of the configuration : the lower the better
/// \param Active True to activate, false to deactivate
///
////////////////////////////////////////////////////////////
static int EvaluateFormat(unsigned int BitsPerPixel, const ContextSettings& Settings, int ColorBits, int DepthBits, int StencilBits, int Antialiasing);
void SetActive(bool Active);
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
ContextSettings mySettings; ///< Creation settings of the context
priv::ContextGL* myContext; ///< Internal OpenGL context
};
} // namespace sf

View File

@ -46,10 +46,9 @@ namespace sf
namespace priv
{
class WindowImpl;
class ContextGL;
}
class Context;
////////////////////////////////////////////////////////////
/// Window is a rendering window ; it can create a new window
/// or connect to an existing one
@ -309,7 +308,7 @@ private :
// Member data
////////////////////////////////////////////////////////////
priv::WindowImpl* myWindow; ///< Platform-specific implementation of the window
Context* myContext; ///< Platform-specific implementation of the OpenGL context
priv::ContextGL* myContext; ///< Platform-specific implementation of the OpenGL context
std::queue<Event> myEvents; ///< Queue of received events
Input myInput; ///< Input manager connected to window
Clock myClock; ///< Clock for measuring the elapsed time between frames

View File

@ -26,7 +26,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include <SFML/Window/Window.hpp>
#include <math.h>

View File

@ -29,7 +29,7 @@
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include FT_GLYPH_H
#include <iostream>
#include <map>
@ -163,9 +163,6 @@ bool FontLoader::LoadFontFromMemory(const char* Data, std::size_t SizeInBytes, u
////////////////////////////////////////////////////////////
FT_Error FontLoader::CreateBitmapFont(FT_Face FontFace, unsigned int CharSize, const Unicode::UTF32String& Charset, Font& LoadedFont)
{
// Make sure we have a valid context
priv::GraphicsContext Ctx;
// Let's find how many characters to put in each row to make them fit into a squared texture
GLint MaxSize;
GLCheck(glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxSize));

View File

@ -22,84 +22,19 @@
//
////////////////////////////////////////////////////////////
#ifndef SFML_GRAPHICSCONTEXT_HPP
#define SFML_GRAPHICSCONTEXT_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <SFML/Graphics/GLEW/glew.h>
#include <SFML/Graphics/GLCheck.hpp>
#include <iostream>
#include <string>
namespace sf
{
class Context;
namespace priv
{
////////////////////////////////////////////////////////////
/// RAII class ensuring a valid shared OpenGL context is bound
/// to the current thread for all the lifetime of the instance.
/// It also ensures all third party libraries (like GLEW)
/// are properly initialized
/// This class is for internal use only, it is required
/// to solve tricky problems involving multi-threading
////////////////////////////////////////////////////////////
class GraphicsContext : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// Default constructor, activate the global context
/// if no other context is bound to the current thread
///
////////////////////////////////////////////////////////////
GraphicsContext();
////////////////////////////////////////////////////////////
/// Destructor, deactivate the global context
/// if no other context was previously bound to the current thread
///
////////////////////////////////////////////////////////////
~GraphicsContext();
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
bool myActivated; ///< Keep track of the activation state of the global context
};
} // namespace priv
////////////////////////////////////////////////////////////
/// Let's define a macro to quickly check every OpenGL
/// API calls
////////////////////////////////////////////////////////////
#ifdef SFML_DEBUG
// In debug mode, perform a test on every OpenGL call
#define GLCheck(Func) ((Func), GLCheckError(__FILE__, __LINE__))
#else
// Else, we don't add any overhead
#define GLCheck(Func) (Func)
#endif
////////////////////////////////////////////////////////////
/// Check the last OpenGL error
///
////////////////////////////////////////////////////////////
inline void GLCheckError(const std::string& File, unsigned int Line)
void GLCheckError(const std::string& File, unsigned int Line)
{
// Get the last error
GLenum ErrorCode = glGetError();
@ -170,7 +105,18 @@ inline void GLCheckError(const std::string& File, unsigned int Line)
}
}
////////////////////////////////////////////////////////////
/// Make sure that GLEW is initialized
////////////////////////////////////////////////////////////
void EnsureGlewInit()
{
static bool Initialized = false;
if (!Initialized)
{
glewInit();
Initialized = true;
}
}
} // namespace sf
#endif // SFML_GRAPHICSCONTEXT_HPP

View File

@ -0,0 +1,73 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_GLCHECK_HPP
#define SFML_GLCHECK_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/Graphics/GLEW/glew.h>
#include <string>
namespace sf
{
////////////////////////////////////////////////////////////
/// Let's define a macro to quickly check every OpenGL
/// API calls
////////////////////////////////////////////////////////////
#ifdef SFML_DEBUG
// In debug mode, perform a test on every OpenGL call
#define GLCheck(Func) ((Func), GLCheckError(__FILE__, __LINE__))
#else
// Else, we don't add any overhead
#define GLCheck(Func) (Func)
#endif
////////////////////////////////////////////////////////////
/// Check the last OpenGL error
///
/// \param File Source file where the call is located
/// \param Line Line number of the source file where the call is located
///
////////////////////////////////////////////////////////////
void GLCheckError(const std::string& File, unsigned int Line);
////////////////////////////////////////////////////////////
/// Make sure that GLEW is initialized
///
////////////////////////////////////////////////////////////
void EnsureGlewInit();
} // namespace sf
#endif // SFML_GLCHECK_HPP

View File

@ -28,7 +28,7 @@
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/ImageLoader.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
@ -495,9 +495,6 @@ void Image::SetSmooth(bool Smooth)
if (myTexture)
{
// Make sure we have a valid context
priv::GraphicsContext Ctx;
GLint PreviousTexture;
GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &PreviousTexture));
@ -569,8 +566,8 @@ FloatRect Image::GetTexCoords(const IntRect& Rect, bool Adjust) const
////////////////////////////////////////////////////////////
unsigned int Image::GetValidTextureSize(unsigned int Size)
{
// Make sure we have a valid context
priv::GraphicsContext Ctx;
// Make sure that GLEW is initialized
EnsureGlewInit();
if (glewIsSupported("GL_ARB_texture_non_power_of_two") != 0)
{
@ -619,9 +616,6 @@ bool Image::CreateTexture()
if (!myWidth || !myHeight)
return false;
// Make sure we have a valid context
priv::GraphicsContext Ctx;
// Adjust internal texture dimensions depending on NPOT textures support
unsigned int TextureWidth = GetValidTextureSize(myWidth);
unsigned int TextureHeight = GetValidTextureSize(myHeight);
@ -772,9 +766,6 @@ void Image::DestroyTexture()
// Destroy the internal texture
if (myTexture)
{
// Make sure we have a valid context
priv::GraphicsContext Ctx;
GLuint Texture = static_cast<GLuint>(myTexture);
GLCheck(glDeleteTextures(1, &Texture));
myTexture = 0;

View File

@ -28,7 +28,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics/PostFX.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include <fstream>
#include <iostream>
#include <set>
@ -43,6 +43,9 @@ namespace sf
PostFX::PostFX() :
myShaderProgram(0)
{
// Make sure that GLEW is initialized
EnsureGlewInit();
// No filtering on frame buffer
myFrameBuffer.SetSmooth(false);
}
@ -74,12 +77,7 @@ PostFX::~PostFX()
{
// Destroy effect program
if (myShaderProgram)
{
// Make sure we have a valid context
priv::GraphicsContext Ctx;
GLCheck(glDeleteObjectARB(myShaderProgram));
}
}
@ -264,8 +262,8 @@ PostFX& PostFX::operator =(const PostFX& Other)
////////////////////////////////////////////////////////////
bool PostFX::CanUsePostFX()
{
// Make sure we have a valid context
priv::GraphicsContext Ctx;
// Make sure that GLEW is initialized
EnsureGlewInit();
return glewIsSupported("GL_ARB_shading_language_100") != 0 &&
glewIsSupported("GL_ARB_shader_objects") != 0 &&
@ -411,9 +409,6 @@ std::string PostFX::PreprocessEffect(std::istream& File)
////////////////////////////////////////////////////////////
void PostFX::CreateProgram()
{
// Make sure we have a valid context
priv::GraphicsContext Ctx;
// Check that we can use post-FX !
if (!CanUsePostFX())
{

View File

@ -27,7 +27,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include <iostream>

View File

@ -28,7 +28,7 @@
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include <iostream>
@ -113,9 +113,6 @@ Image RenderWindow::Capture() const
// Set our window as the current target for rendering
if (SetActive())
{
// Make sure we have a valid context
priv::GraphicsContext Ctx;
// Get pixels from the backbuffer
std::vector<Uint8> Pixels(Width * Height * 4);
Uint8* PixelsPtr = &Pixels[0];

View File

@ -26,7 +26,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Shape.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include <math.h>

View File

@ -27,7 +27,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
namespace sf

View File

@ -27,7 +27,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Graphics/String.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Graphics/GLCheck.hpp>
#include <locale>

View File

@ -0,0 +1,80 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/ThreadLocal.hpp>
#if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/System/Win32/ThreadLocalImpl.hpp>
#else
#include <SFML/System/Unix/ThreadLocalImpl.hpp>
#endif
namespace sf
{
////////////////////////////////////////////////////////////
/// Default constructor
////////////////////////////////////////////////////////////
ThreadLocal::ThreadLocal(void* Value)
{
myImpl = new priv::ThreadLocalImpl;
SetValue(Value);
}
////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
ThreadLocal::~ThreadLocal()
{
delete myImpl;
}
////////////////////////////////////////////////////////////
/// Set the thread-specific value of the variable
////////////////////////////////////////////////////////////
void ThreadLocal::SetValue(void* Value)
{
myImpl->SetValue(Value);
}
////////////////////////////////////////////////////////////
/// Retrieve the thread-specific value of the variable
////////////////////////////////////////////////////////////
void* ThreadLocal::GetValue() const
{
return myImpl->GetValue();
}
} // namespace sf

View File

@ -25,31 +25,7 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/GraphicsContext.hpp>
#include <SFML/Window/Context.hpp>
namespace
{
////////////////////////////////////////////////////////////
// One time initialization of 3rd party libraries.
// We use a global function with a static boolean rather
// than directly a global boolean, to avoid the randomness
// of global variables initializations across compile units.
////////////////////////////////////////////////////////////
void InitGraphicsLibs()
{
static bool InitDone = false;
if (!InitDone)
{
// Initialize GLEW
glewInit();
InitDone = true;
}
}
}
#include <SFML/System/Win32/ThreadLocalImpl.hpp>
namespace sf
@ -57,36 +33,38 @@ namespace sf
namespace priv
{
////////////////////////////////////////////////////////////
/// Default constructor, activate the global context
/// if no other context is bound to the current thread
/// Default constructor -- allocate the storage
////////////////////////////////////////////////////////////
GraphicsContext::GraphicsContext()
ThreadLocalImpl::ThreadLocalImpl()
{
// Activate the global context
if (!Context::IsContextActive())
{
Context::GetDefault().SetActive(true);
myActivated = true;
}
else
{
myActivated = false;
}
// Make sure third party libraries are initialized
InitGraphicsLibs();
pthread_key_create(&myKey, NULL);
}
////////////////////////////////////////////////////////////
/// Destructor, deactivate the global context
/// if no other context was previously bound to the current thread
/// Destructor -- free the storage
////////////////////////////////////////////////////////////
GraphicsContext::~GraphicsContext()
ThreadLocalImpl::~ThreadLocalImpl()
{
// Deactivate the global context
if (myActivated)
Context::GetDefault().SetActive(false);
pthread_key_delete(myKey);
}
////////////////////////////////////////////////////////////
/// Set the thread-specific value of the variable
////////////////////////////////////////////////////////////
void ThreadLocalImpl::SetValue(void* Value)
{
pthread_setspecific(myKey, Value);
}
////////////////////////////////////////////////////////////
/// Retrieve the thread-specific value of the variable
////////////////////////////////////////////////////////////
void* ThreadLocalImpl::GetValue() const
{
return pthread_getspecific(myKey);
}
} // namespace priv

View File

@ -0,0 +1,87 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_THREADLOCALIMPL_HPP
#define SFML_THREADLOCALIMPL_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/NonCopyable.hpp>
#include <pthread.h>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// Unix implementation of thread-local storage
////////////////////////////////////////////////////////////
class ThreadLocalImpl : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// Default constructor -- allocate the storage
///
////////////////////////////////////////////////////////////
ThreadLocalImpl();
////////////////////////////////////////////////////////////
/// Destructor -- free the storage
///
////////////////////////////////////////////////////////////
~ThreadLocalImpl();
////////////////////////////////////////////////////////////
/// Set the thread-specific value of the variable
///
/// \param Value : Value of the variable for this thread
///
////////////////////////////////////////////////////////////
void SetValue(void* Value);
////////////////////////////////////////////////////////////
/// Retrieve the thread-specific value of the variable
///
/// \return Value of the variable for this thread
///
////////////////////////////////////////////////////////////
void* GetValue() const;
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
pthread_key_t myKey; ///< Index of our thread-local storage slot
};
} // namespace priv
} // namespace sf
#endif // SFML_THREADLOCALIMPL_HPP

View File

@ -0,0 +1,72 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Win32/ThreadLocalImpl.hpp>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// Default constructor -- allocate the storage
////////////////////////////////////////////////////////////
ThreadLocalImpl::ThreadLocalImpl()
{
myIndex = TlsAlloc();
}
////////////////////////////////////////////////////////////
/// Destructor -- free the storage
////////////////////////////////////////////////////////////
ThreadLocalImpl::~ThreadLocalImpl()
{
TlsFree(myIndex);
}
////////////////////////////////////////////////////////////
/// Set the thread-specific value of the variable
////////////////////////////////////////////////////////////
void ThreadLocalImpl::SetValue(void* Value)
{
TlsSetValue(myIndex, Value);
}
////////////////////////////////////////////////////////////
/// Retrieve the thread-specific value of the variable
////////////////////////////////////////////////////////////
void* ThreadLocalImpl::GetValue() const
{
return TlsGetValue(myIndex);
}
} // namespace priv
} // namespace sf

View File

@ -0,0 +1,87 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_THREADLOCALIMPL_HPP
#define SFML_THREADLOCALIMPL_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/NonCopyable.hpp>
#include <windows.h>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// Windows implementation of thread-local storage
////////////////////////////////////////////////////////////
class ThreadLocalImpl : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// Default constructor -- allocate the storage
///
////////////////////////////////////////////////////////////
ThreadLocalImpl();
////////////////////////////////////////////////////////////
/// Destructor -- free the storage
///
////////////////////////////////////////////////////////////
~ThreadLocalImpl();
////////////////////////////////////////////////////////////
/// Set the thread-specific value of the variable
///
/// \param Value : Value of the variable for this thread
///
////////////////////////////////////////////////////////////
void SetValue(void* Value);
////////////////////////////////////////////////////////////
/// Retrieve the thread-specific value of the variable
///
/// \return Value of the variable for this thread
///
////////////////////////////////////////////////////////////
void* GetValue() const;
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
DWORD myIndex; ///< Index of our thread-local storage slot
};
} // namespace priv
} // namespace sf
#endif // SFML_THREADLOCALIMPL_HPP

View File

@ -26,125 +26,38 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Context.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Window/glext/glext.h>
#include <stdlib.h>
#if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/Window/Win32/ContextWGL.hpp>
typedef sf::priv::ContextWGL ContextType;
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD)
#include <SFML/Window/Linux/ContextGLX.hpp>
typedef sf::priv::ContextGLX ContextType;
#elif defined(SFML_SYSTEM_MACOS)
#include <SFML/Window/Cocoa/ContextAGL.hpp>
typedef sf::priv::ContextAGL ContextType;
#endif
#include <SFML/Window/ContextGL.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
/// Create a new context, not associated to a window
////////////////////////////////////////////////////////////
Context* Context::New()
{
ContextType* Shared = static_cast<ContextType*>(&GetDefault());
return new ContextType(Shared);
}
////////////////////////////////////////////////////////////
/// Create a new context attached to a window
////////////////////////////////////////////////////////////
Context* Context::New(const priv::WindowImpl* Owner, unsigned int BitsPerPixel, const ContextSettings& Settings)
{
ContextType* Shared = static_cast<ContextType*>(&GetDefault());
ContextType* NewContext = new ContextType(Shared, Owner, BitsPerPixel, Settings);
// Enable antialiasing if needed
if (NewContext->GetSettings().AntialiasingLevel > 0)
glEnable(GL_MULTISAMPLE_ARB);
return NewContext;
}
////////////////////////////////////////////////////////////
/// Check if a context is active on the current thread
////////////////////////////////////////////////////////////
bool Context::IsContextActive()
{
return ContextType::IsContextActive();
}
////////////////////////////////////////////////////////////
/// Return the default context
////////////////////////////////////////////////////////////
Context& Context::GetDefault()
{
static ContextType DefaultContext(NULL);
return DefaultContext;
}
////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
Context::~Context()
{
// Nothing to do
}
////////////////////////////////////////////////////////////
/// Get the settings of the context
////////////////////////////////////////////////////////////
const ContextSettings& Context::GetSettings() const
{
return mySettings;
}
////////////////////////////////////////////////////////////
/// Activate or deactivate the context as the current target
/// for rendering
////////////////////////////////////////////////////////////
bool Context::SetActive(bool Active)
{
return MakeCurrent(Active);
}
////////////////////////////////////////////////////////////
/// Default constructor
/// Default constructor -- creates and activates the context
////////////////////////////////////////////////////////////
Context::Context()
{
myContext = priv::ContextGL::New();
SetActive(true);
}
////////////////////////////////////////////////////////////
/// Evaluate a pixel format configuration.
/// This functions can be used by implementations that have
/// several valid formats and want to get the best one
/// Destructor -- deactivates and destroys the context
////////////////////////////////////////////////////////////
int Context::EvaluateFormat(unsigned int BitsPerPixel, const ContextSettings& Settings, int ColorBits, int DepthBits, int StencilBits, int Antialiasing)
Context::~Context()
{
return abs(static_cast<int>(BitsPerPixel - ColorBits)) +
abs(static_cast<int>(Settings.DepthBits - DepthBits)) +
abs(static_cast<int>(Settings.StencilBits - StencilBits)) +
abs(static_cast<int>(Settings.AntialiasingLevel - Antialiasing));
SetActive(false);
delete myContext;
}
////////////////////////////////////////////////////////////
/// Activate or deactivate explicitely the context
////////////////////////////////////////////////////////////
void Context::SetActive(bool Active)
{
myContext->SetActive(Active);
}
} // namespace sf

View File

@ -0,0 +1,178 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/ContextGL.hpp>
#include <SFML/System/ThreadLocalPtr.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Window/glext/glext.h>
#include <stdlib.h>
#if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/Window/Win32/ContextWGL.hpp>
typedef sf::priv::ContextWGL ContextType;
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD)
#include <SFML/Window/Linux/ContextGLX.hpp>
typedef sf::priv::ContextGLX ContextType;
#elif defined(SFML_SYSTEM_MACOS)
#include <SFML/Window/Cocoa/ContextAGL.hpp>
typedef sf::priv::ContextAGL ContextType;
#endif
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// This thread-local variable will hold the "gloabl" context for each thread
sf::ThreadLocalPtr<sf::priv::ContextGL> ThreadContext(NULL);
// Now we create two global contexts.
// The first one is the reference context: it will be shared with every other
// context, and he can't be activated if we want the sharing operation to always succeed.
// That why we need the second context: this one will be activated and used
// in the main thread whenever there's no other context (window) active.
ContextType ReferenceContext(NULL);
ContextType DefaultContext(&ReferenceContext);
}
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// Create a new context, not associated to a window
////////////////////////////////////////////////////////////
ContextGL* ContextGL::New()
{
return new ContextType(&ReferenceContext);
}
////////////////////////////////////////////////////////////
/// Create a new context attached to a window
////////////////////////////////////////////////////////////
ContextGL* ContextGL::New(const WindowImpl* Owner, unsigned int BitsPerPixel, const ContextSettings& Settings)
{
ContextType* Context = new ContextType(&ReferenceContext, Owner, BitsPerPixel, Settings);
// Enable antialiasing if needed
if (Context->GetSettings().AntialiasingLevel > 0)
glEnable(GL_MULTISAMPLE_ARB);
return Context;
}
////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
ContextGL::~ContextGL()
{
if (ThreadContext == this)
{
ThreadContext = NULL;
}
else if (ThreadContext != NULL)
{
ThreadContext->SetActive(true);
}
}
////////////////////////////////////////////////////////////
/// Get the settings of the context
////////////////////////////////////////////////////////////
const ContextSettings& ContextGL::GetSettings() const
{
return mySettings;
}
////////////////////////////////////////////////////////////
/// Activate or deactivate the context as the current target
/// for rendering
////////////////////////////////////////////////////////////
bool ContextGL::SetActive(bool Active)
{
if (MakeCurrent(Active))
{
if (Active && (ThreadContext == NULL))
{
// If this is the first context to be activated on this thread, make
// it the reference context for the whole thread
ThreadContext = this;
}
else if (!Active && (ThreadContext != NULL) && (ThreadContext != this))
{
// Activate the reference context for this thread to ensure
// that there is always an active context for subsequent graphics operations
ThreadContext->SetActive(true);
}
return true;
}
else
{
return false;
}
}
////////////////////////////////////////////////////////////
/// Default constructor
////////////////////////////////////////////////////////////
ContextGL::ContextGL()
{
// Nothing to do
}
////////////////////////////////////////////////////////////
/// Evaluate a pixel format configuration.
/// This functions can be used by implementations that have
/// several valid formats and want to get the best one
////////////////////////////////////////////////////////////
int ContextGL::EvaluateFormat(unsigned int BitsPerPixel, const ContextSettings& Settings, int ColorBits, int DepthBits, int StencilBits, int Antialiasing)
{
return abs(static_cast<int>(BitsPerPixel - ColorBits)) +
abs(static_cast<int>(Settings.DepthBits - DepthBits)) +
abs(static_cast<int>(Settings.StencilBits - StencilBits)) +
abs(static_cast<int>(Settings.AntialiasingLevel - Antialiasing));
}
} // namespace priv
} // namespace sf

View File

@ -0,0 +1,156 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_CONTEXTGL_HPP
#define SFML_CONTEXTGL_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/Window/ContextSettings.hpp>
#include <SFML/System/NonCopyable.hpp>
namespace sf
{
namespace priv
{
class WindowImpl;
////////////////////////////////////////////////////////////
/// Abstract class representing an OpenGL context
////////////////////////////////////////////////////////////
class ContextGL : NonCopyable
{
public :
////////////////////////////////////////////////////////////
/// Create a new context, not associated to a window
///
/// \return Pointer to the created context
///
////////////////////////////////////////////////////////////
static ContextGL* New();
////////////////////////////////////////////////////////////
/// Create a new context attached to a window
///
/// \param Owner : Pointer to the owner window
/// \param BitsPerPixel : Pixel depth (in bits per pixel)
/// \param Settings : Creation parameters
///
/// \return Pointer to the created context
///
////////////////////////////////////////////////////////////
static ContextGL* New(const WindowImpl* Owner, unsigned int BitsPerPixel, const ContextSettings& Settings);
public :
////////////////////////////////////////////////////////////
/// Virtual destructor
///
////////////////////////////////////////////////////////////
virtual ~ContextGL();
////////////////////////////////////////////////////////////
/// Get the settings of the context
///
/// \return Structure containing the settings
///
////////////////////////////////////////////////////////////
const ContextSettings& GetSettings() const;
////////////////////////////////////////////////////////////
/// Activate or deactivate the context as the current target
/// for rendering
///
/// \param Active : True to activate, false to deactivate
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
bool SetActive(bool Active);
////////////////////////////////////////////////////////////
/// Display the contents of the context
///
////////////////////////////////////////////////////////////
virtual void Display() = 0;
////////////////////////////////////////////////////////////
/// Enable / disable vertical synchronization
///
/// \param Enabled : True to enable v-sync, false to deactivate
///
////////////////////////////////////////////////////////////
virtual void UseVerticalSync(bool Enabled) = 0;
protected :
////////////////////////////////////////////////////////////
/// Default constructor
///
////////////////////////////////////////////////////////////
ContextGL();
////////////////////////////////////////////////////////////
/// Make this context the current one
///
/// \param Active : True to activate, false to deactivate
///
/// \return True on success, false if any error happened
///
////////////////////////////////////////////////////////////
virtual bool MakeCurrent(bool Active) = 0;
////////////////////////////////////////////////////////////
/// Evaluate a pixel format configuration.
/// This functions can be used by implementations that have
/// several valid formats and want to get the best one
///
/// \param BitsPerPixel : Requested pixel depth (bits per pixel)
/// \param Settings : Requested additionnal settings
/// \param ColorBits : Color bits of the configuration to evaluate
/// \param DepthBits : Depth bits of the configuration to evaluate
/// \param StencilBits : Stencil bits of the configuration to evaluate
/// \param Antialiasing : Antialiasing level of the configuration to evaluate
///
/// \return Score of the configuration : the lower the better
///
////////////////////////////////////////////////////////////
static int EvaluateFormat(unsigned int BitsPerPixel, const ContextSettings& Settings, int ColorBits, int DepthBits, int StencilBits, int Antialiasing);
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
ContextSettings mySettings; ///< Creation settings of the context
};
} // namespace priv
} // namespace sf
#endif // SFML_CONTEXTGL_HPP

View File

@ -27,9 +27,8 @@
////////////////////////////////////////////////////////////
#include <SFML/Window/Linux/ContextGLX.hpp>
#include <SFML/Window/WindowImpl.hpp>
#include <GL/gl.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Window/glext/glxext.h>
#include <SFML/Window/glext/glext.h>
#include <iostream>
@ -59,6 +58,10 @@ myOwnsWindow(true)
// Create the context
CreateContext(Shared, VideoMode::GetDesktopMode().BitsPerPixel, ContextSettings(0, 0, 0));
// Activate the context
if (Shared)
SetActive(true);
}
@ -76,6 +79,10 @@ myOwnsWindow(false)
// Create the context
if (myWindow)
CreateContext(Shared, BitsPerPixel, Settings);
// Activate the context
if (Shared)
SetActive(true);
}

View File

@ -27,7 +27,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Window/Win32/ContextWGL.hpp>
#include <SFML/Window/WindowImpl.hpp>
#include <GL/gl.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Window/glext/wglext.h>
#include <iostream>
@ -45,7 +45,8 @@ myDC (NULL),
myContext (NULL),
myOwnsWindow(true)
{
// ------------ TEMP ------------
// TODO : try to create a bitmap in memory instead of a dummy window
// Create a dummy window (disabled and hidden)
myWindow = CreateWindowA("STATIC", "", WS_POPUP | WS_DISABLED, 0, 0, 1, 1, NULL, NULL, GetModuleHandle(NULL), NULL);
ShowWindow(myWindow, SW_HIDE);
@ -54,7 +55,10 @@ myOwnsWindow(true)
// Create the context
if (myDC)
CreateContext(Shared, VideoMode::GetDesktopMode().BitsPerPixel, ContextSettings(0, 0, 0));
// ------------ TEMP ------------
// Activate the context
if (Shared)
SetActive(true);
}
@ -74,6 +78,10 @@ myOwnsWindow(false)
// Create the context
if (myDC)
CreateContext(Shared, BitsPerPixel, Settings);
// Activate the context
if (Shared)
SetActive(true);
}
@ -304,7 +312,10 @@ void ContextWGL::CreateContext(ContextWGL* Shared, unsigned int BitsPerPixel, co
// Share this context with others
if (SharedContext)
wglShareLists(SharedContext, myContext);
{
if (!wglShareLists(SharedContext, myContext))
std::cerr << "Failed to share the OpenGL context" << std::endl;
}
}
}

View File

@ -28,7 +28,7 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Context.hpp>
#include <SFML/Window/ContextGL.hpp>
#include <windows.h>
@ -39,7 +39,7 @@ namespace priv
////////////////////////////////////////////////////////////
/// Windows (WGL) implementation of OpenGL contexts
////////////////////////////////////////////////////////////
class ContextWGL : public Context
class ContextWGL : public ContextGL
{
public :

View File

@ -26,6 +26,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Window.hpp>
#include <SFML/Window/ContextGL.hpp>
#include <SFML/Window/Context.hpp>
#include <SFML/Window/WindowImpl.hpp>
#include <SFML/System/Sleep.hpp>
@ -142,11 +143,11 @@ void Window::Create(VideoMode Mode, const std::string& Title, unsigned long Wind
// Make sure another context is bound, so that:
// - the context creation can request OpenGL extensions if necessary
// - myContext can safely be destroyed (it's no longer bound)
Context::GetDefault().SetActive(true);
Context Ctx;
// Recreate the context
delete myContext;
myContext = Context::New(myWindow, Mode.BitsPerPixel, Settings);
myContext = priv::ContextGL::New(myWindow, Mode.BitsPerPixel, Settings);
Initialize();
}
@ -164,11 +165,11 @@ void Window::Create(WindowHandle Handle, const ContextSettings& Settings)
// Make sure another context is bound, so that:
// - the context creation can request OpenGL extensions if necessary
// - myContext can safely be destroyed (it's no longer bound)
Context::GetDefault().SetActive(true);
Context Ctx;
// Recreate the context
delete myContext;
myContext = Context::New(myWindow, VideoMode::GetDesktopMode().BitsPerPixel, Settings);
myContext = priv::ContextGL::New(myWindow, VideoMode::GetDesktopMode().BitsPerPixel, Settings);
Initialize();
}
@ -183,9 +184,6 @@ void Window::Close()
{
if (myContext)
{
// Make sure that our context is no longer bound
Context::GetDefault().SetActive(true);
// Delete the context
delete myContext;
myContext = NULL;