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

Author Topic: [Solved] Text Coords and Shape Coords  (Read 12190 times)

0 Members and 2 Guests are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Text Coords and Shape Coords
« Reply #15 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.
Laurent Gomila - SFML developer

DevBug

  • Newbie
  • *
  • Posts: 12
    • View Profile
[Solved] Text Coords and Shape Coords
« Reply #16 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Text Coords and Shape Coords
« Reply #17 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?
Laurent Gomila - SFML developer

DevBug

  • Newbie
  • *
  • Posts: 12
    • View Profile
[Solved] Text Coords and Shape Coords
« Reply #18 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Text Coords and Shape Coords
« Reply #19 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 ;)
Laurent Gomila - SFML developer

DevBug

  • Newbie
  • *
  • Posts: 12
    • View Profile
[Solved] Text Coords and Shape Coords
« Reply #20 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++;
    }    

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Text Coords and Shape Coords
« Reply #21 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?
-Wander

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
[Solved] Text Coords and Shape Coords
« Reply #22 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.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Text Coords and Shape Coords
« Reply #23 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:
-Wander

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Text Coords and Shape Coords
« Reply #24 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.
Laurent Gomila - SFML developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Text Coords and Shape Coords
« Reply #25 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.
-Wander

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
[Solved] Text Coords and Shape Coords
« Reply #26 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++.
I use the latest build of SFML2

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Text Coords and Shape Coords
« Reply #27 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.
-Wander

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] Text Coords and Shape Coords
« Reply #28 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?
Laurent Gomila - SFML developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[Solved] Text Coords and Shape Coords
« Reply #29 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;
}
-Wander

 

anything