Added missing setActive virtual method to sf::RenderTarget, added setActive calls to OpenGL example to demonstrate proper explicit context management.

This commit is contained in:
binary1248 2016-10-06 23:44:56 +02:00 committed by Lukas Dürrenberger
parent 39208efb55
commit 2df9abf341
7 changed files with 69 additions and 55 deletions

View File

@ -63,6 +63,9 @@ int main()
// mipmapping is purely optional in this example // mipmapping is purely optional in this example
texture.generateMipmap(); texture.generateMipmap();
// Make the window the active window for OpenGL calls
window.setActive(true);
// Enable Z-buffer read and write // Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
@ -141,6 +144,9 @@ int main()
glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
// Make the window no longer the active window for OpenGL calls
window.setActive(false);
// Create a clock for measuring the time elapsed // Create a clock for measuring the time elapsed
sf::Clock clock; sf::Clock clock;
@ -196,7 +202,15 @@ int main()
// Adjust the viewport when the window is resized // Adjust the viewport when the window is resized
if (event.type == sf::Event::Resized) if (event.type == sf::Event::Resized)
{
// Make the window the active window for OpenGL calls
window.setActive(true);
glViewport(0, 0, event.size.width, event.size.height); glViewport(0, 0, event.size.width, event.size.height);
// Make the window no longer the active window for OpenGL calls
window.setActive(false);
}
} }
// Draw the background // Draw the background
@ -204,6 +218,9 @@ int main()
window.draw(background); window.draw(background);
window.popGLStates(); window.popGLStates();
// Make the window the active window for OpenGL calls
window.setActive(true);
// Clear the depth buffer // Clear the depth buffer
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
@ -222,6 +239,9 @@ int main()
// Draw the cube // Draw the cube
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
// Make the window no longer the active window for OpenGL calls
window.setActive(false);
// Draw some text on top of our OpenGL object // Draw some text on top of our OpenGL object
window.pushGLStates(); window.pushGLStates();
window.draw(text); window.draw(text);

View File

@ -255,6 +255,28 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
virtual Vector2u getSize() const = 0; virtual Vector2u getSize() const = 0;
////////////////////////////////////////////////////////////
/// \brief Activate or deactivate the render target for rendering
///
/// This function makes the render target's context current for
/// future OpenGL rendering operations (so you shouldn't care
/// about it if you're not doing direct OpenGL stuff).
/// A render target's context is active only on the current thread,
/// if you want to make it active on another thread you have
/// to deactivate it on the previous thread first if it was active.
/// Only one context can be current in a thread, so if you
/// want to draw OpenGL geometry to another render target
/// don't forget to activate it again. Activating a render
/// target will automatically deactivate the previously active
/// context (if any).
///
/// \param active True to activate, false to deactivate
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool setActive(bool active = true) = 0;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Save the current OpenGL render states and matrices /// \brief Save the current OpenGL render states and matrices
/// ///
@ -380,20 +402,6 @@ private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void applyShader(const Shader* shader); void applyShader(const Shader* shader);
////////////////////////////////////////////////////////////
/// \brief Activate the target for rendering
///
/// This function must be implemented by derived classes to make
/// their OpenGL context current; it is called by the base class
/// everytime it's going to use OpenGL calls.
///
/// \param active True to make the target active, false to deactivate it
///
/// \return True if the function succeeded
///
////////////////////////////////////////////////////////////
virtual bool activate(bool active) = 0;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Render states cache /// \brief Render states cache
/// ///

View File

@ -204,19 +204,6 @@ public:
private: private:
////////////////////////////////////////////////////////////
/// \brief Activate the target for rendering
///
/// This function is called by the base class
/// everytime it's going to use OpenGL calls.
///
/// \param active True to make the target active, false to deactivate it
///
/// \return True if the function succeeded
///
////////////////////////////////////////////////////////////
virtual bool activate(bool active);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -112,6 +112,24 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
virtual Vector2u getSize() const; virtual Vector2u getSize() const;
////////////////////////////////////////////////////////////
/// \brief Activate or deactivate the window as the current target
/// for OpenGL rendering
///
/// A window is active only on the current thread, if you want to
/// make it active on another thread you have to deactivate it
/// on the previous thread first if it was active.
/// Only one window can be active on a thread at a time, thus
/// the window previously active (if any) automatically gets deactivated.
/// This is not to be confused with requestFocus().
///
/// \param active True to activate, false to deactivate
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
bool setActive(bool active = true);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Copy the current contents of the window to an image /// \brief Copy the current contents of the window to an image
/// ///
@ -159,18 +177,6 @@ protected:
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
virtual void onResize(); virtual void onResize();
private:
////////////////////////////////////////////////////////////
/// \brief Activate the target for rendering
///
/// \param active True to make the target active, false to deactivate it
///
/// \return True if the function succeeded
///
////////////////////////////////////////////////////////////
virtual bool activate(bool active);
}; };
} // namespace sf } // namespace sf

View File

@ -98,7 +98,7 @@ RenderTarget::~RenderTarget()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void RenderTarget::clear(const Color& color) void RenderTarget::clear(const Color& color)
{ {
if (activate(true)) if (setActive(true))
{ {
// Unbind texture to fix RenderTexture preventing clear // Unbind texture to fix RenderTexture preventing clear
applyTexture(NULL); applyTexture(NULL);
@ -214,7 +214,7 @@ void RenderTarget::draw(const Vertex* vertices, std::size_t vertexCount,
#define GL_QUADS 0 #define GL_QUADS 0
#endif #endif
if (activate(true)) if (setActive(true))
{ {
// First set the persistent OpenGL states if it's the very first call // First set the persistent OpenGL states if it's the very first call
if (!m_cache.glStatesSet) if (!m_cache.glStatesSet)
@ -304,7 +304,7 @@ void RenderTarget::draw(const Vertex* vertices, std::size_t vertexCount,
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void RenderTarget::pushGLStates() void RenderTarget::pushGLStates()
{ {
if (activate(true)) if (setActive(true))
{ {
#ifdef SFML_DEBUG #ifdef SFML_DEBUG
// make sure that the user didn't leave an unchecked OpenGL error // make sure that the user didn't leave an unchecked OpenGL error
@ -336,7 +336,7 @@ void RenderTarget::pushGLStates()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void RenderTarget::popGLStates() void RenderTarget::popGLStates()
{ {
if (activate(true)) if (setActive(true))
{ {
glCheck(glMatrixMode(GL_PROJECTION)); glCheck(glMatrixMode(GL_PROJECTION));
glCheck(glPopMatrix()); glCheck(glPopMatrix());
@ -358,7 +358,7 @@ void RenderTarget::resetGLStates()
// Check here to make sure a context change does not happen after activate(true) // Check here to make sure a context change does not happen after activate(true)
bool shaderAvailable = Shader::isAvailable(); bool shaderAvailable = Shader::isAvailable();
if (activate(true)) if (setActive(true))
{ {
// Make sure that extensions are initialized // Make sure that extensions are initialized
priv::ensureExtensionsInit(); priv::ensureExtensionsInit();

View File

@ -156,11 +156,4 @@ const Texture& RenderTexture::getTexture() const
return m_texture; return m_texture;
} }
////////////////////////////////////////////////////////////
bool RenderTexture::activate(bool active)
{
return setActive(active);
}
} // namespace sf } // namespace sf

View File

@ -62,16 +62,16 @@ RenderWindow::~RenderWindow()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool RenderWindow::activate(bool active) Vector2u RenderWindow::getSize() const
{ {
return setActive(active); return Window::getSize();
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Vector2u RenderWindow::getSize() const bool RenderWindow::setActive(bool active)
{ {
return Window::getSize(); return Window::setActive(active);
} }