Advertisement
Guest User

GWEN'S SFML Renderer

a guest
Nov 26th, 2012
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. #include "Gwen/Gwen.h"
  2. #include "Gwen/BaseRender.h"
  3. #include "Gwen/Utility.h"
  4. #include "Gwen/Font.h"
  5. #include "Gwen/Texture.h"
  6. #include "Gwen/Renderers/SFML.h"
  7. #include <GL/gl.h>
  8.  
  9. namespace Gwen
  10. {
  11.     namespace Renderer
  12.     {
  13.  
  14.         SFML::SFML( sf::RenderTarget& target ) : m_Target(target)
  15.         {
  16.         }
  17.  
  18.         SFML::~SFML()
  19.         {
  20.         }
  21.  
  22.         void SFML::SetDrawColor( Gwen::Color color )
  23.         {
  24.             m_Color.r = color.r;
  25.             m_Color.g = color.g;
  26.             m_Color.b = color.b;
  27.             m_Color.a = color.a;
  28.         }
  29.  
  30.         void SFML::DrawFilledRect( Gwen::Rect rect )
  31.         {
  32.             Translate( rect );
  33.  
  34.             sf::RectangleShape r( sf::Vector2f( rect.w, rect.h ) );
  35.             r.setPosition( rect.x, rect.y );
  36.             r.setFillColor( m_Color );
  37.             m_Target.draw( r );
  38.         }
  39.  
  40.         void SFML::LoadFont( Gwen::Font* font )
  41.         {
  42.             font->realsize = font->size * Scale();
  43.  
  44.             sf::Font* pFont = new sf::Font();
  45.  
  46.             if ( !pFont->loadFromFile( Utility::UnicodeToString( font->facename ) ) )
  47.             {
  48.                 // Ideally here we should be setting the font to a system default font here.
  49.                 delete pFont;
  50.  
  51.                 pFont = const_cast<sf::Font*>( &sf::Font::getDefaultFont() );
  52.             }
  53.            
  54.             font->data = pFont;
  55.         }
  56.  
  57.         void SFML::FreeFont( Gwen::Font* pFont )
  58.         {
  59.             if ( !pFont->data ) return;
  60.  
  61.             sf::Font* font = ((sf::Font*)pFont->data);
  62.  
  63.             // If this is the default font then don't delete it!
  64.             if ( font != &sf::Font::getDefaultFont() )
  65.             {
  66.                 delete font;
  67.             }
  68.  
  69.             pFont->data = NULL;
  70.         }
  71.  
  72.         void SFML::RenderText( Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString& text )
  73.         {
  74.             Translate( pos.x, pos.y );
  75.  
  76.             const sf::Font* pSFFont = (sf::Font*)(pFont->data);
  77.  
  78.             // If the font doesn't exist, or the font size should be changed
  79.             if ( !pSFFont || fabs( pFont->realsize - pFont->size * Scale() ) > 2 )
  80.             {
  81.                 FreeFont( pFont );
  82.                 LoadFont( pFont );
  83.             }
  84.  
  85.             if  ( !pSFFont )
  86.             {
  87.                 pSFFont = &(sf::Font::getDefaultFont());
  88.             }
  89.  
  90.             m_Target.pushGLStates();
  91.                 sf::Text sfStr( text );
  92.                 sfStr.setFont( *pSFFont );
  93.                 sfStr.move( pos.x, pos.y );
  94.                 sfStr.setCharacterSize( pFont->realsize );
  95.                 sfStr.setColor( m_Color );
  96.                 m_Target.draw( sfStr );
  97.             m_Target.popGLStates();
  98.         }
  99.  
  100.         Gwen::Point SFML::MeasureText( Gwen::Font* pFont, const Gwen::UnicodeString& text )
  101.         {
  102.             const sf::Font* pSFFont = (sf::Font*)(pFont->data);
  103.  
  104.             // If the font doesn't exist, or the font size should be changed
  105.             if ( !pSFFont || fabs( pFont->realsize - pFont->size * Scale() ) > 2 )
  106.             {
  107.                 FreeFont( pFont );
  108.                 LoadFont( pFont );
  109.             }
  110.  
  111.             if  ( !pSFFont )
  112.             {
  113.                 pSFFont = &(sf::Font::getDefaultFont());
  114.             }
  115.  
  116.             sf::Text sfStr( text );
  117.             sfStr.setFont( *pSFFont );
  118.             sfStr.setCharacterSize( pFont->realsize );
  119.             sf::FloatRect sz = sfStr.getGlobalBounds(); /// MAYBE LOCALBOUNDS
  120.  
  121.             return Gwen::Point( sz.width, sz.height ); 
  122.         }
  123.  
  124.         void SFML::StartClip()
  125.         {
  126.             Gwen::Rect rect = ClipRegion();
  127.  
  128.             // OpenGL's coords are from the bottom left
  129.             // so we need to translate them here.
  130.             {
  131.                 GLint view[4];
  132.                 glGetIntegerv( GL_VIEWPORT, &view[0] );
  133.                 rect.y = view[3] - (rect.y + rect.h);
  134.             }
  135.  
  136.             glScissor( rect.x * Scale(), rect.y * Scale(), rect.w * Scale(), rect.h * Scale() );
  137.             glEnable( GL_SCISSOR_TEST );
  138.         };
  139.  
  140.  
  141.         void SFML::EndClip()
  142.         {
  143.             glDisable( GL_SCISSOR_TEST );
  144.         };
  145.  
  146.         void SFML::LoadTexture( Gwen::Texture* pTexture )
  147.         {
  148.             if ( !pTexture ) return;
  149.             if ( pTexture->data ) FreeTexture( pTexture );
  150.  
  151.             sf::Texture* tex = new sf::Texture();
  152.  
  153.             tex->setSmooth( true );
  154.  
  155.             if ( !tex->loadFromFile( pTexture->name.Get() ) )
  156.             {
  157.                 delete( tex );
  158.                 pTexture->failed = true;
  159.                 return;
  160.             }
  161.  
  162.             pTexture->height = tex->getSize().y;
  163.             pTexture->width = tex->getSize().x;
  164.             pTexture->data = tex;
  165.  
  166.         };
  167.  
  168.         void SFML::FreeTexture( Gwen::Texture* pTexture )
  169.         {
  170.             sf::Texture* tex = static_cast<sf::Texture*>( pTexture->data );
  171.  
  172.             if ( tex )
  173.             {
  174.                 delete tex;
  175.             }
  176.  
  177.             pTexture->data = NULL;
  178.         }
  179.  
  180.         void SFML::DrawTexturedRect( Gwen::Texture* pTexture, Gwen::Rect rect, float u1, float v1, float u2, float v2 )
  181.         {
  182.             const sf::Texture* tex = static_cast<sf::Texture*>( pTexture->data );
  183.  
  184.             if ( !tex )
  185.                 return DrawMissingImage( rect );
  186.  
  187.             Translate( rect );
  188.        
  189.             tex->bind();
  190.  
  191.             glColor4f(1, 1, 1, 1 );
  192.  
  193.             glBegin( GL_QUADS );
  194.                 glTexCoord2f( u1, v1 );     glVertex2f(rect.x,     rect.y);
  195.                 glTexCoord2f( u1, v2 );     glVertex2f(rect.x,     rect.y + rect.h);
  196.                 glTexCoord2f( u2, v2 );     glVertex2f(rect.x + rect.w, rect.y + rect.h);
  197.                 glTexCoord2f( u2, v1 );     glVertex2f(rect.x + rect.w, rect.y) ;
  198.             glEnd();
  199.  
  200.             glBindTexture( GL_TEXTURE_2D, 0);
  201.         }
  202.  
  203.         Gwen::Color SFML::PixelColour( Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color& col_default )
  204.         {
  205.                 const sf::Texture* tex = static_cast<sf::Texture*>( pTexture->data );
  206.                 if ( !tex ) return col_default;
  207.  
  208.                 sf::Image img = tex->copyToImage();
  209.                 sf::Color col = img.getPixel( x, y );
  210.                 return Gwen::Color( col.r, col.g, col.b, col.a );
  211.         }
  212.  
  213.    
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement