1
Graphics / sf::Text::draw() renders squares instead of text, regardless of font being used
« on: December 27, 2012, 07:33:20 pm »
Hi, I just compiled SFML 2.0 from source and am dynamically linking the libraries.
Currently, all I've really been doing is playing with the API in hopes to make a basic Asteroids game. The main issue I'm having though is trying to understand what could be causing this issue, as the sf::Text is being rendered squares and not as actual text. I've tried using different fonts, loading the font object itself differently, setting different char sizes; sf::Colors; sf::BlendModes, etc and none of that has worked.
To give you an example, I'll post some code.
At first, there is a utility function which simply performs the following:
In the main game class, the font is loaded like so at the beginning of execution:
And then the code to actually draw the text resides in a simple function ( DrawText() ):
And, as a result, I get the following:
Please note that sf::RenderStates::Default returns a linker error, hence why I'm embedding a sf::RenderStates option within the mWindow.draw().
Also, I should also say that I compiled this to be used with MSVC 2012. I'm not sure if I need to link with FreeType font library as well for this to work, so if any clarification with that could be made, I'd appreciate it.
Thanks.
Currently, all I've really been doing is playing with the API in hopes to make a basic Asteroids game. The main issue I'm having though is trying to understand what could be causing this issue, as the sf::Text is being rendered squares and not as actual text. I've tried using different fonts, loading the font object itself differently, setting different char sizes; sf::Colors; sf::BlendModes, etc and none of that has worked.
To give you an example, I'll post some code.
At first, there is a utility function which simply performs the following:
void LoadFont( const std::string& fontname, sf::Font& out_font )
{
const std::string fullpath = "assets/fonts/" + fontname;
if ( !out_font.loadFromFile( fullpath ) )
ErrorQuit( "loading font \"" + fullpath + "\" failed." );
}
{
const std::string fullpath = "assets/fonts/" + fontname;
if ( !out_font.loadFromFile( fullpath ) )
ErrorQuit( "loading font \"" + fullpath + "\" failed." );
}
In the main game class, the font is loaded like so at the beginning of execution:
LoadFont( "arial.ttf", mMainFont );
And then the code to actually draw the text resides in a simple function ( DrawText() ):
void Game::DrawText( void )
{
// Draw current mouse coordinates:
sf::Vector2i mousepos = sf::Mouse::getPosition( mWindow );
sf::Text out;
out.setFont( mMainFont );
out.setCharacterSize( 30 );
out.setColor( sf::Color( 255, 255, 0, 0 ) );
out.setPosition( 20.0f, 20.0f );
std::ostringstream oss;
oss << "Mouse X => " << mousepos.x << "\n Mouse Y => " << mousepos.y;
out.setString( oss.str() );
mWindow.draw( out, sf::RenderStates( sf::BlendMode::BlendNone ) );
}
{
// Draw current mouse coordinates:
sf::Vector2i mousepos = sf::Mouse::getPosition( mWindow );
sf::Text out;
out.setFont( mMainFont );
out.setCharacterSize( 30 );
out.setColor( sf::Color( 255, 255, 0, 0 ) );
out.setPosition( 20.0f, 20.0f );
std::ostringstream oss;
oss << "Mouse X => " << mousepos.x << "\n Mouse Y => " << mousepos.y;
out.setString( oss.str() );
mWindow.draw( out, sf::RenderStates( sf::BlendMode::BlendNone ) );
}
And, as a result, I get the following:
Please note that sf::RenderStates::Default returns a linker error, hence why I'm embedding a sf::RenderStates option within the mWindow.draw().
Also, I should also say that I compiled this to be used with MSVC 2012. I'm not sure if I need to link with FreeType font library as well for this to work, so if any clarification with that could be made, I'd appreciate it.
Thanks.