SFML community forums

Help => General => Topic started by: JeZ-l-Lee on February 04, 2009, 08:34:15 pm

Title: Trying to center horizontally a String on screen...
Post by: JeZ-l-Lee on February 04, 2009, 08:34:15 pm
Trying to center horizontally a String...

Here is the code:
Code: [Select]
void Visuals::DrawTextOnScreenBuffer(char text[256], float size, sf::Color textColor, sf::Color textOutlineColor, float screenX, float screenY, uint16_t xJustification)
{
sf::String textToDisplay(text, Arial, size);

if (xJustification == TextLeft)
{
//do nothing..
}
else if (xJustification == TextCenter)
{
screenX = ( 320 - (textToDisplay.GetCenter / 2) ); //<-ERROR
|

for (float outlineY = screenY - 3; outlineY < screenY + 3; outlineY++)
for (float outlineX = screenX - 3; outlineX < screenX + 3; outlineX++)
{
textToDisplay.SetX(outlineX);
textToDisplay.SetY(outlineY);
textToDisplay.SetColor(textOutlineColor);
App.Draw(textToDisplay);
}

textToDisplay.SetX(screenX);
textToDisplay.SetY(screenY);
textToDisplay.SetColor(textColor);
App.Draw(textToDisplay);
}


Does not compile - see ERROR
Please help, thanks
Title: Trying to center horizontally a String on screen...
Post by: irri on February 04, 2009, 09:13:47 pm
Firstly, GetCenter returns a sf::Vector2f (i think), and therefore you can't divide it by 2.
and secondly, because it's a method you must include the (), otherwise you'll send the adress of the method
Title: Trying to center horizontally a String on screen...
Post by: Avency on February 04, 2009, 09:15:23 pm
How about
Code: [Select]

textToDisplay.GetCenter()

(note the braces)

I don't want to sound rude, but judging from your other threads,  maybe you should try to learn at least some basic C++ first (go read a book or something...).
Title: Trying to center horizontally a String on screen...
Post by: JeZ-l-Lee on February 05, 2009, 04:08:37 am
Hi,

Here is what I got to center a String on a 640x640 window:
Code: [Select]
//----------------------------------------------------------------------------------------------------------
void Visuals::DrawTextOnScreenBuffer(char text[256], float size, sf::Color textColor, sf::Color textOutlineColor, float screenX, float screenY, uint16_t xJustification)
{
sf::String textToDisplay(text, Arial, size);

if (xJustification == TextLeft)
{
//do nothing..
}
else if (xJustification == TextCenter)
// SHOULD WORK, BUT DOES NOT CENTER TEXT ON SCREEN (Screen = 640x640)
{
sf::Vector2<float> vector;
vector = textToDisplay.GetCenter();
screenX = 320 - vector.x;
}
else if (xJustification == TextRight) // NOT DONE YET...
{

}

for (float outlineY = screenY - 3; outlineY < screenY + 3; outlineY++)
for (float outlineX = screenX - 3; outlineX < screenX + 3; outlineX++)
{
textToDisplay.SetX(outlineX);
textToDisplay.SetY(outlineY);
textToDisplay.SetColor(textOutlineColor);
App.Draw(textToDisplay);
}

textToDisplay.SetX(screenX);
textToDisplay.SetY(screenY);
textToDisplay.SetColor(textColor);
App.Draw(textToDisplay);
}
//----------------------------------------------------------------------------------------------------------

It builds with no errors or warnings,
but the text is not centered on the screen:
(http://silentheroproductions.com/images/SFMLStringNotCentered.gif)
Title: Trying to center horizontally a String on screen...
Post by: Tank on February 05, 2009, 09:33:49 am
Code: [Select]
  // SHOULD WORK, BUT DOES NOT CENTER TEXT ON SCREEN (Screen = 640x640)
   {
      sf::Vector2<float> vector;
      vector = textToDisplay.GetCenter();
      screenX = 320 - vector.x;
   }

No, should NOT work. :) It's doing exactly what you typed.

GetCenter() returns the *center point* of a drawable. A center point is notated in object coordinates, not screen coordinates. Per default, the center point for every drawable is at (0, 0). Then you calculate 320 - 0, which is 320. And there your string gets drawn.

The correct way would be to use the string's real width to calculate a horizontal center in screen coordinates. Example:
Code: [Select]

#include <iostream>
#include <SFML/Graphics.hpp>

int main() {
sf::RenderWindow  window;

window.Create( sf::VideoMode( 800, 600 ), "Test" );

/* Relevant code starts here */
sf::String     str( "Test!" );
sf::FloatRect  rect( str.GetRect() );
sf::Vector2f   pos( window.GetWidth() / 2 - rect.GetWidth() / 2, window.GetHeight() / 2 - rect.GetHeight() / 2 );

str.SetPosition( pos );
/* Relevant code ends here */

window.Clear();
window.Draw( str );
window.Display();

sf::Sleep( 2 );

return 0;
}


And voila, in this example you get the following:
(http://www.abload.de/thumb/bildschirmfoto-testyrcz.png) (http://www.abload.de/image.php?img=bildschirmfoto-testyrcz.png)
Title: Trying to center horizontally a String on screen...
Post by: JeZ-l-Lee on February 05, 2009, 06:30:05 pm
Wow, thanks so much, got it working now...
(http://silentheroproductions.com/images/SFML_Logo_Screen.gif)
Moving forward with game engine design!