| 426 | } |
| 427 | |
| 428 | void Canvas::Bind() { |
| 429 | auto allocation = GetAllocation(); |
| 430 | |
| 431 | if( !m_render_texture ) { |
| 432 | // Make sure there is a non-internal/shared context active. |
| 433 | // After 8 hours of debugging, I found out that if you deactivate the |
| 434 | // currently active context and construct a sf::RenderTexture, it leads |
| 435 | // to the sf::RenderTexture's internal OpenGL texture being created |
| 436 | // in a "corrupted" context leading to OpenGL possibly generating the |
| 437 | // same texture IDs that it already previously generated and are still in |
| 438 | // use. Since 2 textures share the same ID, when you draw to the |
| 439 | // sf::RenderTexture it actually overwrites whatever was in the other |
| 440 | // normally created texture. This leads to very exotic looking sf::Sprites, |
| 441 | // not for the end-user. SFML context management strikes again. |
| 442 | sf::Context context; |
| 443 | |
| 444 | m_render_texture = std::make_shared<sf::RenderTexture>(); |
| 445 | |
| 446 | if( !m_render_texture->resize( { static_cast<unsigned int>( std::floor( allocation.size.x + .5f ) ), static_cast<unsigned int>( std::floor( allocation.size.y + .5f ) ) }, sf::ContextSettings{ m_depth } ) ) { |
| 447 | #if defined( SFGUI_DEBUG ) |
| 448 | std::cerr << "SFGUI warning: Canvas failed to create internal SFML RenderTexture.\n"; |
| 449 | #endif |
| 450 | } |
| 451 | } |
| 452 | else if( m_resize ) { |
| 453 | if( !m_render_texture->resize( { static_cast<unsigned int>( std::floor( allocation.size.x + .5f ) ), static_cast<unsigned int>( std::floor( allocation.size.y + .5f ) ) }, sf::ContextSettings{ m_depth } ) ) { |
| 454 | #if defined( SFGUI_DEBUG ) |
| 455 | std::cerr << "SFGUI warning: Canvas failed to create internal SFML RenderTexture.\n"; |
| 456 | #endif |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | m_resize = false; |
| 461 | |
| 462 | (void)m_render_texture->setActive( true ); |
| 463 | } |
| 464 | |
| 465 | void Canvas::Unbind() { |
| 466 | if( !m_render_texture ) { |