Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Gwen/Gwen.h"
- #include "Gwen/BaseRender.h"
- #include "Gwen/Utility.h"
- #include "Gwen/Font.h"
- #include "Gwen/Texture.h"
- #include "Gwen/Renderers/SFML.h"
- #include <GL/gl.h>
- namespace Gwen
- {
- namespace Renderer
- {
- SFML::SFML( sf::RenderTarget& target ) : m_Target(target)
- {
- }
- SFML::~SFML()
- {
- }
- void SFML::SetDrawColor( Gwen::Color color )
- {
- m_Color.r = color.r;
- m_Color.g = color.g;
- m_Color.b = color.b;
- m_Color.a = color.a;
- }
- void SFML::DrawFilledRect( Gwen::Rect rect )
- {
- Translate( rect );
- sf::RectangleShape r( sf::Vector2f( rect.w, rect.h ) );
- r.setPosition( rect.x, rect.y );
- r.setFillColor( m_Color );
- m_Target.draw( r );
- }
- void SFML::LoadFont( Gwen::Font* font )
- {
- font->realsize = font->size * Scale();
- sf::Font* pFont = new sf::Font();
- if ( !pFont->loadFromFile( Utility::UnicodeToString( font->facename ) ) )
- {
- // Ideally here we should be setting the font to a system default font here.
- delete pFont;
- pFont = const_cast<sf::Font*>( &sf::Font::getDefaultFont() );
- }
- font->data = pFont;
- }
- void SFML::FreeFont( Gwen::Font* pFont )
- {
- if ( !pFont->data ) return;
- sf::Font* font = ((sf::Font*)pFont->data);
- // If this is the default font then don't delete it!
- if ( font != &sf::Font::getDefaultFont() )
- {
- delete font;
- }
- pFont->data = NULL;
- }
- void SFML::RenderText( Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString& text )
- {
- Translate( pos.x, pos.y );
- const sf::Font* pSFFont = (sf::Font*)(pFont->data);
- // If the font doesn't exist, or the font size should be changed
- if ( !pSFFont || fabs( pFont->realsize - pFont->size * Scale() ) > 2 )
- {
- FreeFont( pFont );
- LoadFont( pFont );
- }
- if ( !pSFFont )
- {
- pSFFont = &(sf::Font::getDefaultFont());
- }
- m_Target.pushGLStates();
- sf::Text sfStr( text );
- sfStr.setFont( *pSFFont );
- sfStr.move( pos.x, pos.y );
- sfStr.setCharacterSize( pFont->realsize );
- sfStr.setColor( m_Color );
- m_Target.draw( sfStr );
- m_Target.popGLStates();
- }
- Gwen::Point SFML::MeasureText( Gwen::Font* pFont, const Gwen::UnicodeString& text )
- {
- const sf::Font* pSFFont = (sf::Font*)(pFont->data);
- // If the font doesn't exist, or the font size should be changed
- if ( !pSFFont || fabs( pFont->realsize - pFont->size * Scale() ) > 2 )
- {
- FreeFont( pFont );
- LoadFont( pFont );
- }
- if ( !pSFFont )
- {
- pSFFont = &(sf::Font::getDefaultFont());
- }
- sf::Text sfStr( text );
- sfStr.setFont( *pSFFont );
- sfStr.setCharacterSize( pFont->realsize );
- sf::FloatRect sz = sfStr.getGlobalBounds(); /// MAYBE LOCALBOUNDS
- return Gwen::Point( sz.width, sz.height );
- }
- void SFML::StartClip()
- {
- Gwen::Rect rect = ClipRegion();
- // OpenGL's coords are from the bottom left
- // so we need to translate them here.
- {
- GLint view[4];
- glGetIntegerv( GL_VIEWPORT, &view[0] );
- rect.y = view[3] - (rect.y + rect.h);
- }
- glScissor( rect.x * Scale(), rect.y * Scale(), rect.w * Scale(), rect.h * Scale() );
- glEnable( GL_SCISSOR_TEST );
- };
- void SFML::EndClip()
- {
- glDisable( GL_SCISSOR_TEST );
- };
- void SFML::LoadTexture( Gwen::Texture* pTexture )
- {
- if ( !pTexture ) return;
- if ( pTexture->data ) FreeTexture( pTexture );
- sf::Texture* tex = new sf::Texture();
- tex->setSmooth( true );
- if ( !tex->loadFromFile( pTexture->name.Get() ) )
- {
- delete( tex );
- pTexture->failed = true;
- return;
- }
- pTexture->height = tex->getSize().y;
- pTexture->width = tex->getSize().x;
- pTexture->data = tex;
- };
- void SFML::FreeTexture( Gwen::Texture* pTexture )
- {
- sf::Texture* tex = static_cast<sf::Texture*>( pTexture->data );
- if ( tex )
- {
- delete tex;
- }
- pTexture->data = NULL;
- }
- void SFML::DrawTexturedRect( Gwen::Texture* pTexture, Gwen::Rect rect, float u1, float v1, float u2, float v2 )
- {
- const sf::Texture* tex = static_cast<sf::Texture*>( pTexture->data );
- if ( !tex )
- return DrawMissingImage( rect );
- Translate( rect );
- tex->bind();
- glColor4f(1, 1, 1, 1 );
- glBegin( GL_QUADS );
- glTexCoord2f( u1, v1 ); glVertex2f(rect.x, rect.y);
- glTexCoord2f( u1, v2 ); glVertex2f(rect.x, rect.y + rect.h);
- glTexCoord2f( u2, v2 ); glVertex2f(rect.x + rect.w, rect.y + rect.h);
- glTexCoord2f( u2, v1 ); glVertex2f(rect.x + rect.w, rect.y) ;
- glEnd();
- glBindTexture( GL_TEXTURE_2D, 0);
- }
- Gwen::Color SFML::PixelColour( Gwen::Texture* pTexture, unsigned int x, unsigned int y, const Gwen::Color& col_default )
- {
- const sf::Texture* tex = static_cast<sf::Texture*>( pTexture->data );
- if ( !tex ) return col_default;
- sf::Image img = tex->copyToImage();
- sf::Color col = img.getPixel( x, y );
- return Gwen::Color( col.r, col.g, col.b, col.a );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement