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

Author Topic: GetGlobalBounds not updating?  (Read 6215 times)

0 Members and 2 Guests are viewing this topic.

Quentin

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
GetGlobalBounds not updating?
« on: January 09, 2013, 07:11:07 pm »
Edit: I've posted this in graphics because I think it's not .net specific.

Hello,

I have just started working with SFML (2.0 RC)  in C# with the .Net Binding. To get a feel for the whole thing I wrote a simple test application with the help of the API documentation. I'm drawing a sprite with a 250*250 test texture, which I'm moving ten pixels at a time by modifying its position via a KeyPressed Event Handler. Additionally, I've registered a MouseButtonPressed Event Handler for clicking my sprite. To do that, I'm comparing the mouse position with the FloatRect of the sprite.

public void OnMousePressed(object sender, MouseButtonEventArgs e)
    {
        Vector2f mousePos = new Vector2f(Mouse.GetPosition(window).X, Mouse.GetPosition(window).Y);
        switch (e.Button)
        {
            case Mouse.Button.Left:
                if(sprite.GetGlobalBounds().Contains(mousePos.X, mousePos.Y)) {
                    window.Close();
                }
                break;
        }
    }

That works like a charm. But, if I have moved the sprite previously, then nothing happens when clicking on the new position. Instead, the window closes when I click the old position of the sprite.
This is my OnKeyPressed method:

public void OnKeyPressed(object sender, KeyEventArgs e)
    {
        float x = sprite.Position.X;
        float y = sprite.Position.Y;
        switch(e.Code) {
            case Keyboard.Key.Up:
                y -= 10f;
                break;
            case Keyboard.Key.Down:
                y += 10f;
                break;
            case Keyboard.Key.Left:
                x -= 10f;
                break;
            case Keyboard.Key.Right:
                x += 10f;
                break;
        }
        Vector2f newPosition = new Vector2f(x, y);
        sprite.Position = newPosition;
        Console.WriteLine(sprite.GetGlobalBounds().ToString());
    }
It always displays the same debug info: [FloatRect] Left(0) Top(0) Width(250) Height(250).

So, have I made a mistake? How is GetGlobalBounds() calculated and why isn't it using the new position of the sprite? Does it not use the Position member at all? Thanks for any help!
« Last Edit: January 09, 2013, 07:15:25 pm by Quentin »

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: GetGlobalBounds not updating?
« Reply #1 on: January 09, 2013, 07:27:38 pm »
Can show a minimal example using your code?
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

Quentin

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: GetGlobalBounds not updating?
« Reply #2 on: January 09, 2013, 07:34:22 pm »
Of course.

Class Program with Main:
class Program
    {      
        static void Main(string[] args)
        {
            SFMLProgram program = new SFMLProgram();
            program.StartSFMLProgram();
        }
    }

Class SFMLProgram:
    class SFMLProgram
    {
        RenderWindow window;
        Sprite sprite;

        public void OnClose(object sender, EventArgs e)
        {
            window.Close();
        }
        public void OnKeyPressed(object sender, KeyEventArgs e) {
            float x = sprite.Position.X;
            float y = sprite.Position.Y;
            switch(e.Code) {
                case Keyboard.Key.Up:
                    y -= 10f;
                    break;
                case Keyboard.Key.Down:
                    y += 10f;
                    break;
                case Keyboard.Key.Left:
                    x -= 10f;
                    break;
                case Keyboard.Key.Right:
                    x += 10f;
                    break;
            }
            Vector2f newPosition = new Vector2f(x, y);
            sprite.Position = newPosition;
            Console.WriteLine(sprite.GetGlobalBounds().ToString());
        }
        public void OnMousePressed(object sender, MouseButtonEventArgs e)
        {
            Vector2f mousePos = new Vector2f(Mouse.GetPosition(window).X, Mouse.GetPosition(window).Y);
            switch (e.Button)
            {
                case Mouse.Button.Left:
                    Console.WriteLine(mousePos);
                    if(sprite.GetGlobalBounds().Contains(mousePos.X, mousePos.Y)) {
                        window.Close();
                    }
                    break;
                case Mouse.Button.Right:
                    break;
            }
        }
        public void StartSFMLProgram()
        {
            window = new RenderWindow(new VideoMode(800, 600), "Testwindow");
            window.SetVisible(true);
            window.Closed += new EventHandler(OnClose);
            window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
            window.MouseButtonPressed += new EventHandler<MouseButtonEventArgs>(OnMousePressed);
            Texture texture = new Texture("placeholder.gif");
            sprite = new Sprite(texture);
            // Mainloop
            while (window.IsOpen())
            {
                window.DispatchEvents();
                window.Clear(Color.Black);
                window.Draw(sprite);
                window.Display();
            }
        }
    }

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: GetGlobalBounds not updating?
« Reply #3 on: January 09, 2013, 08:24:52 pm »
Hmm... I don't know programming in C# but, I think, to get new values you need to put in the main loop so every frame to get the new value.
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

Quentin

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: GetGlobalBounds not updating?
« Reply #4 on: January 09, 2013, 08:32:31 pm »
But the sprite moves just fine this way, when redrawing the window it uses the correct new position. Why shouldn't it use that new position the next time an event is handled? I don't know what values GetGlobalBounds is using, could be it's not sprite.Position?

Edit: To clarify, onMousePressed and onKeyPressed are called by window.DispatchEvents(), so in that way they are in the main loop...
« Last Edit: January 09, 2013, 08:38:14 pm by Quentin »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: GetGlobalBounds not updating?
« Reply #5 on: January 09, 2013, 08:42:50 pm »
Works perfectly for me using the latest sources.

By the way, in the mouse button event handler, you can use e.X and e.Y instead of calling Mouse.GetPosition.
Laurent Gomila - SFML developer

Quentin

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: GetGlobalBounds not updating?
« Reply #6 on: January 09, 2013, 08:51:15 pm »
Hm... so what you're saying is that my code is not the problem? When you move the sprite you can click it and it will close the window?

Perhaps I should check if pulling the newest sources works, then.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: GetGlobalBounds not updating?
« Reply #7 on: January 09, 2013, 08:54:52 pm »
Quote
Hm... so what you're saying is that my code is not the problem? When you move the sprite you can click it and it will close the window?
This is exactly what I'm saying ;)

Quote
Perhaps I should check if pulling the newest sources works, then.
Yup. This is probably it, I remember that there were problems with transformations that were solved recently.
Laurent Gomila - SFML developer

Quentin

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: GetGlobalBounds not updating?
« Reply #8 on: January 09, 2013, 09:37:39 pm »
I checked out and compiled the latest release with VS 2012, and, like you said, everything works as expected. Thank you very much, was beginning to doubt myself there.
I had used the RC version from the website because I didn't know about the repository before reading on the forum, that was obviously a mistake. Perhaps it would have been better to wait for the finished 2.0 version before using SFML for the first time in my life, but I was anxious to get started ;)

Just one final thing, when building SFML.net it says it expects Tao, but Tao is superseded by OpenTk, should you use the former or the latter? At the moment I just took the errors in stride because I have neither on my machine.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: GetGlobalBounds not updating?
« Reply #9 on: January 09, 2013, 10:11:24 pm »
I know that Tao is deprecated, I should upgrade the examples :-\
Laurent Gomila - SFML developer

ParadoxGery

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: GetGlobalBounds not updating?
« Reply #10 on: January 27, 2013, 08:03:36 pm »
hi i got the same proble but i fixes it like Quentin....
but now i can't use sprite.Transform.Translate() anymore why ?! you going to remove this


nice work i love sfml

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: GetGlobalBounds not updating?
« Reply #11 on: January 27, 2013, 08:36:19 pm »
Can you please be more precise about your problem?
Laurent Gomila - SFML developer

ParadoxGery

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: GetGlobalBounds not updating?
« Reply #12 on: January 27, 2013, 11:09:13 pm »
i want to move things around and it was simple with the Translate method
but after compiling the newest sfml the sprite is not moving
public void StartTest()
{
    //set the window and event handlers
    renWin = new RenderWindow(new VideoMode(800, 600), "DraggTest");
    renWin.Closed += new EventHandler(OnClose);
    renWin.MouseButtonPressed += new EventHandler<MouseButtonEventArgs>(OnClick);
    renWin.MouseButtonReleased += new EventHandler<MouseButtonEventArgs>(OnClickRelease);
    renWin.MouseMoved += new EventHandler<MouseMoveEventArgs>(OnMousMove);
    renWin.KeyPressed += new EventHandler<KeyEventArgs>(OnKey);
    renWin.MouseLeft += new EventHandler(OnMouseLeft);

    //set the sprite
    Texture tex = new Texture("imgs\\ace.gif");
    sprite = new Sprite(tex);
    //move the sprite from the start 0,0 position to 20,20
    sprite.Position = new Vector2f(20,20); //works
    //sprite.Transform.Translate(20,20); //doesn't work

    //the loop
    while (renWin.IsOpen())
    {
        //events
        renWin.DispatchEvents();

        //clear
        renWin.Clear(Color.Black);

        //move sprite
        if (dragging)
        {
            sprite.Position = mPos-dPos;
        }

        //draw sprite
        renWin.Draw(sprite);

        //update window
        renWin.Display();
    }
}

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: GetGlobalBounds not updating?
« Reply #13 on: January 27, 2013, 11:34:02 pm »
It doesn't work, because in original SFML you can't set transform: https://github.com/SFML/SFML/blob/master/include/SFML/Graphics/Transformable.hpp#L302

It should be GetTransform() here IMO, less misleading.
I just noticed it's a struct, so it's ok.
« Last Edit: January 28, 2013, 12:41:25 am by krzat »
SFML.Utils - useful extensions for SFML.Net

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: GetGlobalBounds not updating?
« Reply #14 on: January 28, 2013, 07:55:08 am »
C# doesn't support const/readonly return types, therefore, the fact that you can modify a returned instance doesn't mean that it is supposed to work.

I hate C# for this, it causes so much confusion.
Laurent Gomila - SFML developer

 

anything