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

Author Topic: Mouse problem.  (Read 6009 times)

0 Members and 1 Guest are viewing this topic.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Mouse problem.
« on: January 28, 2012, 05:12:44 am »
So now i want to get mouse into my games. Earlier i was just using the keyboard but now I need to learn how to get mouse input and stuff. Well as a test, i just tried to set the position of a sprite the same as the position of the mouse pointer. So, i used sf::Sprite::SetPosition() and then gave the parameters of the function the mouse x coordinate and y coordinate. The problem was, the sprite wasnt at the point of the mouse. Like when i moved the mouse the sprite moved so it was working, but at the wrong coordinates. So if my mouse pointer was at 0,0 for example, then the sprite would be wayy below to the right or something. Please help and thanks so much!

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Mouse problem.
« Reply #1 on: January 28, 2012, 06:00:56 am »
You might need to set the origin ( or center, depending on whether you're using SFML 1.x or 2.

Code: [Select]

// SFML 1.x
sf::Vector2f size = mySprite.GetSize();
mySprite.SetCenter(size.x / 2, size.y / 2);



Code: [Select]

// SFML 2
sf::FloatRect bounds = mySprite.GetLocalBounds();
mySprite.SetOrigin(bounds.Width / 2, bounds.Height / 2);



Additionally, if you're using SFML 2, make sure to get the mouse position relative to the window, not the desktop:

Code: [Select]

// SFML 2 only
mySprite.SetPosition(sf::Mouse::GetPosition(myWindow));

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Mouse problem.
« Reply #2 on: January 28, 2012, 08:58:54 pm »
I have no idea what 90% of those functions you mentioned do.. Can you please be more clear and thanks!

replicant

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mouse problem.
« Reply #3 on: January 28, 2012, 09:01:52 pm »
Post your code.

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Mouse problem.
« Reply #4 on: January 28, 2012, 10:17:31 pm »
Code: [Select]

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

using namespace std;





int main()
{

    sf::VideoMode vmode(800,600,32);
    sf::RenderWindow Window(vmode, "Pong");

    sf::Texture tex;
    tex.LoadFromFile("Paddle.png");

    sf::Sprite sprite;
    sprite.SetTexture(tex);
    sf::Mouse mouse;





    while(Window.IsOpened())
    {
        sf::Event event;

        while(Window.PollEvent(event))
        {
            if(event.Type == sf::Event::Closed || sf::Keyboard::IsKeyPressed(sf::Keyboard::Escape))
            {
              Window.Close();

            }
        }

        Window.Clear(sf::Color::Cyan);


        sprite.SetPosition(mouse.GetPosition().x,mouse.GetPosition().y);


        Window.Draw(sprite);


        Window.Display();


    }

replicant

  • Newbie
  • *
  • Posts: 18
    • View Profile
Mouse problem.
« Reply #5 on: January 28, 2012, 10:35:35 pm »
You're getting the mouse coordinates relative to the desktop instead of the RenderWindow.

Code: [Select]
sprite.SetPosition(mouse.GetPosition(Window).x, mouse.GetPosition(Window).y);

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Mouse problem.
« Reply #6 on: January 28, 2012, 10:49:00 pm »
And you shouldn't instantiate sf::Mouse. Read the documentation to see how it is used.

@ Laurent: I promised you the name GetPosition() would confuse users :P
(Okay, here the doc was obviously not read, but with a "desktop" in the name, viruses might have found the problem itself... maybe...)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Mouse problem.
« Reply #7 on: January 28, 2012, 11:33:03 pm »
What was unclear about my code? I thought it was pretty minimal and to the point.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Mouse problem.
« Reply #8 on: January 29, 2012, 01:18:35 am »
Quote from: "Viruses"
I have no idea what 90% of those functions you mentioned do..

That's what the documentation is for:
http://www.sfml-dev.org/documentation/

Viruses

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Mouse problem.
« Reply #9 on: January 29, 2012, 02:21:05 am »
Code: [Select]
sprite.SetPosition(mouse.GetPosition(Window).x, mouse.GetPosition(Window).y);

Wow that worked! Umm the only thing is that i dont really know what happened in terms of the actual code. I know what happened in reality but with the code i dont understand. The GetPosition() function takes a parameter? I didn't know that. How does that work?

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Mouse problem.
« Reply #10 on: January 29, 2012, 02:29:13 am »
Quote from: "Viruses"
The GetPosition() function takes a parameter? I didn't know that. How does that work?

You should really read the documentation:
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Mouse.php#ac48d527ca5b712252d80a9a991f5a582