SFML community forums

Help => Graphics => Topic started by: Wander on December 21, 2010, 05:11:55 am

Title: [Solved] Text Coords and Shape Coords
Post by: Wander on December 21, 2010, 05:11:55 am
I am making a class that creates buttons in SFML. I have been having a problem for about a month now. The problem is that the text won't position inside of the outlining box.

After much trial and error I have figured out why it won't match up.
It seems to me that the sf::Shape class and the sf::String class are positioned on separate coordinate planes or move at different interval factor per one coordinate (e.g. 1 sf::Shape coordinate = 0.5 sf::String coordinates).

If anyone knows how to fix this I would love some help. I'm about ready to pull my hair out at this. Thank you. :)[/b]
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 05:40:51 am
Ah, it seems that you and me have the same problem: My thread (http://www.sfml-dev.org/forum/viewtopic.php?p=24762).
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on December 21, 2010, 05:48:22 am
Okay, but how do I fix it?
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 06:00:30 am
What version of SFML are you using? Maybe a new revision changed something. I take that you're using 1.6, I'm using 2.0 and it has the same problem.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on December 21, 2010, 06:01:14 am
1.6
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 06:08:04 am
I saw no obvious errors in a run through.  However, I've got suspicions of Glyphs though.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on December 21, 2010, 06:15:36 am
whats that?
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 06:19:23 am
A single character from a font.  I've found something intriguing: It seems like the text is rendered at 0; I can't find any references to the current position of the text.

Edit:

It seems that everything is translated with the matrix (makes sense, SFML uses OpenGL).

Edit:

There is no derived GetMatrix so I don't know.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on December 21, 2010, 06:22:42 am
so do you have any ideas on how to fix this? :/
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 06:25:01 am
Wait for Laurent?
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on December 21, 2010, 06:28:23 am
hahaha im confused
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on December 21, 2010, 07:38:28 am
I need a minimal and complete code that I can test please.

Be careful with SFML 1.6, if you use scales things might be offseted.
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 09:10:32 am
This is quite odd, we are both getting the same problem. Yet when I try to produce the error with minimal code it doesn't show up.  Compiler error maybe? It might have to do with the way I'm drawing text: I'm creating my sf::Text inside a loop (which is called through multiple functions).

Here's the minimal code:
Code: [Select]

#include <string>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow Window(sf::VideoMode(800, 600), "SFML");
   
    sf::Event Event;
    while(Window.IsOpened()) {
        while(Window.GetEvent(Event)) {
            if(Event.Type == sf::Event::Closed) {
                Window.Close();
                break;
            }
        }
       
        Window.Clear();
       
        sf::Shape rTest = sf::Shape::Rectangle(10, 235, 32, 32, sf::Color(255, 255, 255, 255));
        sf::Text tTest = sf::Text("My First Button", sf::Font::GetDefaultFont(), 10);
        tTest.SetColor(sf::Color(172, 172, 172, 255));
        tTest.SetPosition(sf::Vector2f(10, 235));

        Window.Draw(rTest);
        Window.Draw(tTest);
       
        Window.Display();
    }
   
    return EXIT_SUCCESS;
}


Here is what I'm doing to draw text and debug, etc.

Code: [Select]

void SFMLRenderer::DrawRectangle(Rect& rect) {
    sf::Shape sfRect = sf::Shape::Rectangle(rect.Left, rect.Top, rect.Width, rect.Height, m_Color);
    m_pTarget->Draw(sfRect);
}

void SFMLRenderer::DrawText(std::string& text, Point& position) {
    Gum::Base::Size size = MeasureText(text);
    sf::Text sfText = sf::Text(text, sf::Font::GetDefaultFont(), 10);
    sfText.SetPosition(sf::Vector2f((int)position.x, (int)position.y));

    DrawRectangle(Gum::Base::Rect(position.x, position.y, size.w, size.h));
    m_pTarget->Draw(sfText);
}


Edit I've added this in my DrawText:

Code: [Select]

if(sfText.GetPosition() != sf::Vector2f((int)position.x, (int)position.y)) {
        printf("!=\n");
}


Nothing in the console.  This is really weird.
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on December 21, 2010, 09:17:42 am
Quote
Yet when I try to produce the error with minimal code it doesn't show up
[...]
Here's the minimal code

So to be clear, does this minimal code produce the bug or not?
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 09:18:55 am
Sadly no, but I don't understand how my code is producing the error (if the rectangle is drawing in the correct position).

Edit:

Here's my project that I'm working on (it has the error).

https://github.com/mtwilliams/World-of-Mana
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on December 21, 2010, 09:49:31 am
So what's the point of showing me a working piece of code? :P

At least now you have a code that works, that you can compare to yours and see what's different.
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 09:59:06 am
Quote from: "Laurent"
So what's the point of showing me a working piece of code? :P

At least now you have a code that works, that you can compare to yours and see what's different.


Here's the problem: sfText.GetPosition() != sf::Vector2f((int)position.x, (int)position.y) always returns false.  I'm also using the same position for the rectangle.
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on December 21, 2010, 10:31:50 am
Quote
Here's the problem: sfText.GetPosition() != sf::Vector2f((int)position.x, (int)position.y) always returns false. I'm also using the same position for the rectangle.

:?:
Why is it a problem that the position of the text is equal to what you assigned it?
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 10:37:12 am
Quote from: "Laurent"
Quote
Here's the problem: sfText.GetPosition() != sf::Vector2f((int)position.x, (int)position.y) always returns false. I'm also using the same position for the rectangle.

:?:
Why is it a problem that the position of the text is equal to what you assigned it?


Because it's drawing in a different position.
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on December 21, 2010, 10:59:48 am
Yes... but you need to find the code that doesn't produce the expected results. It doesn't help to know that SetPosition and GetPosition work, I already knew that ;)

What you should do is to replace the implementation of your DrawText and DrawRectangle with something as basic as the minimal example that you showed me (use hard-coded constants, etc.). Make sure it works as expected. Then gradually put back your initial code (use the function's arguments, etc.) until the bug shows up again.

This is a debugging strategy that always works when you don't know exactly the origin of the bug: find a working minimal code, and gradually fill the gap between the working code and the original (buggy) one -- you cannot miss the bug with this technique.

I don't have the time to do this for every bug which is posted on this forum, so it is really a huge help when people do it seriously by theirselves. And of course it highly increases the probability that the bug is fixed quickly ;)
Title: [Solved] Text Coords and Shape Coords
Post by: DevBug on December 21, 2010, 11:43:00 am
Quote from: "Laurent"
Yes... but you need to find the code that doesn't produce the expected results. It doesn't help to know that SetPosition and GetPosition work, I already knew that ;)

...


Explain how this works:

Code: [Select]

void SFMLRenderer::DrawText(std::string& text, Point& position) {
    Gum::Base::Size size = MeasureText(text);
    Gum::Base::Rect rect = Gum::Base::Rect(position.x, position.y, 0, 0);
   
    sf::Text sfText = sf::Text(text, sf::Font::GetDefaultFont(), 10);
    sfText.SetColor(m_Color);
    sfText.SetPosition(sf::Vector2f(rect.Left, rect.Top));
    m_pTarget->Draw(sfText);
}


Edit:

I'm starting to think my compiler is messing things up because, this draws perfectly centered text.

Code: [Select]

    Gum::Base::Size size = GetRenderer()->MeasureText(GetCaption());
   
    Gum::Base::Point position;
    position.x = GetGlobalPos().x;
    position.y = GetGlobalPos().y;
   
    position.y += (GetSize().w - size.w) / 2;
    position.x += (GetSize().h - size.h) / 2;
   
    if(m_Pressed) {
        position.x++;
        position.y++;
    }    
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on December 23, 2010, 12:20:11 am
So whats the verdict here? That either out compilers are both messing up, or that we both made the same coding error and some where in the code is something that is screwing it up?
Title: [Solved] Text Coords and Shape Coords
Post by: Terrydil on December 23, 2010, 03:18:23 pm
Laurent basically gave you the best answer. :) If you are able to create an example doing the same thing that works, then compare it to the code that doesn't work and see whats different..  Or better yet, just stick the working code into your program and if you still have the wrong effects then you know the problem lies elsewhere in your code.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on December 25, 2010, 02:52:42 am
Here is my small code with hardcoded values. I don't know how to make it any simpler. It still doesn't work though.

Main Function:
Code: [Select]
#include <iostream>
#include "tGUI.h"

using namespace std;

int main()
{
    sf::RenderWindow App(sf::VideoMode::GetMode(0), " ");
    tGUI::Button button;
    button.SetText("Text String");

    while (App.IsOpened())
    {
        button.Render(App);

        App.Display();
    }

    return 0;
}


Constructor:
Code: [Select]
Button()
{

    canvas = sf::Shape::Rectangle(100,100,400,400, Color::White, 5, Color::White);

    canvas.EnableFill(false);
    canvas.EnableOutline(true);

    canvas.SetPosition(100,100);
    sf_text.SetPosition(100,100);
}


tGUI::Button::SetText( std::string ):
Code: [Select]
void SetText(string c = "")
{
     text = c;
     sf_text.SetText(text);
}


tGUI::Button::Render( sf::RenderTarget& ):
Code: [Select]
virtual void Render(sf::RenderTarget& Window) const
{
    Window.Clear();
    Window.SetView(Window.GetDefaultView());
    Window.Draw(canvas);
    Window.Draw(sf_text);
}



Results:
(http://i52.tinypic.com/2sbuxs7.png)
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on December 25, 2010, 10:24:36 am
It's simple but not complete ;)
And you could put everything in a single file with main(), so that we can easily test it.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on January 01, 2011, 02:45:58 am
This should be good. :D
What do you mean by incomplete?

Code: [Select]
#include <iostream>

using namespace std;

namespace tGUI
{
class Button
{
string text;
string sf_text;
sf::Shape canvas;
Button()
{

    canvas = sf::Shape::Rectangle(100,100,400,400, Color::White, 5, Color::White);

    canvas.EnableFill(false);
    canvas.EnableOutline(true);

    canvas.SetPosition(100,100);
    sf_text.SetPosition(100,100);
}

void SetText(string c = "")
{
     text = c;
     sf_text.SetText(text);
}

virtual void Render(sf::RenderTarget& Window) const
{
    Window.Clear();
    Window.SetView(Window.GetDefaultView());
    Window.Draw(canvas);
    Window.Draw(sf_text);
}
}
}

int main()
{
    sf::RenderWindow App(sf::VideoMode::GetMode(0), " ");
    tGUI::Button button;
    button.SetText("Text String");

    while (App.IsOpened())
    {
        button.Render(App);

        App.Display();
    }

    return 0;
}


EDIT: Added variable declaration.
Title: [Solved] Text Coords and Shape Coords
Post by: OniLinkPlus on January 01, 2011, 07:50:52 am
Quote from: "Wander"
This should be good. :D
What do you mean by incomplete?

Code: [Select]
#include <iostream>

using namespace std;

class tGUI
{
Button()
{

    canvas = sf::Shape::Rectangle(100,100,400,400, Color::White, 5, Color::White);

    canvas.EnableFill(false);
    canvas.EnableOutline(true);

    canvas.SetPosition(100,100);
    sf_text.SetPosition(100,100);
}

void SetText(string c = "")
{
     text = c;
     sf_text.SetText(text);
}

virtual void Render(sf::RenderTarget& Window) const
{
    Window.Clear();
    Window.SetView(Window.GetDefaultView());
    Window.Draw(canvas);
    Window.Draw(sf_text);
}
}

int main()
{
    sf::RenderWindow App(sf::VideoMode::GetMode(0), " ");
    tGUI::Button button;
    button.SetText("Text String");

    while (App.IsOpened())
    {
        button.Render(App);

        App.Display();
    }

    return 0;
}
By incomplete he means you haven't given him all the code needed to compile. In fact, the code you put in this post is completely invalid C++.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on January 01, 2011, 09:19:38 am
I updated the code for missing two variable declarations, but I don't understand what else I'm missing.
Either I'm stupid or I'm missing something completely obvious. I severely hope I'm not stupid.
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on January 01, 2011, 10:08:44 am
Quote
What do you mean by incomplete?

By incomplete I meant that I couldn't compile it (line 2: #include "tGUI.h", I don't have that file).

Quote
I updated the code for missing two variable declarations, but I don't understand what else I'm missing.

Looks good (didn't try to compile though -- but you should before posting it).
I don't see a declaration for canvas but I think it's a sf::Shape ;)

I'll try this code as soon as possible. Do you confirm that it produces the same bug as shown in the screenshot above?
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on January 01, 2011, 10:59:51 pm
This is the same code, but I fixed the compiling errors that appeared. Sorry for causing you so much trouble over this code posting thing. :/
And I do confirm that this produces the same error.

Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

using namespace std;

namespace tGUI
{
    class Button
    {
        string text;
        sf::String sf_text;
        sf::Shape canvas;
        public:
        Button()
        {

            canvas = sf::Shape::Rectangle(100,100,400,400, sf::Color::White, 5, sf::Color::White);

            canvas.EnableFill(false);
            canvas.EnableOutline(true);

            canvas.SetPosition(100,100);
            sf_text.SetPosition(100,100);
        }

        void SetText(string c = "")
        {
             text = c;
             sf_text.SetText(text);
        }

        virtual void Render(sf::RenderTarget& Window) const
        {
            Window.Clear();
            Window.SetView(Window.GetDefaultView());
            Window.Draw(canvas);
            Window.Draw(sf_text);
        }
    };
}

int main()
{
    sf::RenderWindow App(sf::VideoMode::GetMode(0), " ");
    tGUI::Button button;
    button.SetText("Text String");

    while (App.IsOpened())
    {
        button.Render(App);

        App.Display();
    }

    return 0;
}
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on January 01, 2011, 11:30:13 pm
I don't know why people want to keep their code structure, namespaces etc. in minimal codes ;)

Here is what I ended up with after 30 sec:
Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "test");

    sf::Shape canvas = sf::Shape::Rectangle(100, 100, 400, 400, sf::Color::White, 5, sf::Color::White);
    canvas.EnableFill(false);
    canvas.EnableOutline(true);
    canvas.SetPosition(100, 100);

    sf::String text;
    text.SetPosition(100, 100);
    text.SetText("Text String");

    while (window.IsOpened())
    {
        window.Clear();
        window.Draw(canvas);
        window.Draw(text);
        window.Display();
    }

    return 0;
}

But I'm just quibbling, your code was good enough, thank you ;)

Now the error: you put your rectangle's geometry at 100,100, and then you move this geometry to 100,100: the geometry ends up at 200,200. The position of the shape's points is not the global position that you set with SetPosition. They are combined.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on January 01, 2011, 11:38:12 pm
Wow! That's it? I thought the .Move() member was the one that combined. So What's the difference between .Move() and .SetPosition()?
Also, how would I go about just setting a new position for it without combining it.
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on January 02, 2011, 12:02:24 am
No you misunderstand me. Move and SetPosition modify the same variable, the first one is relative to the current position and the second one absolute.

But the coordinates that you pass to sf::Shape::Rectangle define the local position of points. This is a totally different thing. If you want to play with the position of your rectangle, create it at 0,0 and only use SetPosition.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on January 02, 2011, 12:07:18 am
OH!! I get it now! So, if I set the Rectangle originally at 100, 100 then that would act like a 0, 0?
Title: [Solved] Text Coords and Shape Coords
Post by: Laurent on January 02, 2011, 12:18:34 pm
Quote
OH!! I get it now! So, if I set the Rectangle originally at 100, 100 then that would act like a 0, 0?

Yes, when the rectangle is at position 0,0, its top-left corner is already at 100,100.
Title: [Solved] Text Coords and Shape Coords
Post by: Wander on January 02, 2011, 08:18:30 pm
Okay. Thanks. :)