Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trying to center horizontally a String on screen...  (Read 4911 times)

0 Members and 1 Guest are viewing this topic.

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
Trying to center horizontally a String on screen...
« 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
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

irri

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • http://www.irri.se
Trying to center horizontally a String on screen...
« Reply #1 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
2D RPG Game (School project): http://PA.irri.se/

Avency

  • Full Member
  • ***
  • Posts: 113
    • View Profile
Trying to center horizontally a String on screen...
« Reply #2 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...).

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
Trying to center horizontally a String on screen...
« Reply #3 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:
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Trying to center horizontally a String on screen...
« Reply #4 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:

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
Trying to center horizontally a String on screen...
« Reply #5 on: February 05, 2009, 06:30:05 pm »
Wow, thanks so much, got it working now...

Moving forward with game engine design!
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

 

anything