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

Author Topic: Mouse position problem  (Read 4907 times)

0 Members and 1 Guest are viewing this topic.

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Mouse position problem
« on: July 27, 2012, 10:39:11 am »
I have set a cursor sprite to move with the mouse movement. It works fine, except for the fact that it doesn't move with my view. When I move the camera, the cursor sprite stay still. How do I link them togheter?

i514x

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Mouse position problem
« Reply #1 on: July 27, 2012, 11:00:20 am »
Maybe this helps
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Hidden Cursor");
    window.setMouseCursorVisible(false); // Hide cursor
   
    sf::View fixed = window.getView(); // Create a fixed view
   
    // Load image and create sprite
    sf::Texture texture;
    texture.loadFromFile("cursor.png");
    sf::Sprite sprite(texture);
   
    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }
        }
       
        sprite.setPosition(static_cast<sf::Vector2f>(sf::Mouse::getPosition(window))); // Set position
       
        window.clear();
        window.setView(fixed);
        window.draw(sprite);
        window.display();
    }

    return EXIT_SUCCESS;
}
Source: https://github.com/SFML/SFML/wiki/TutorialChangeCursor

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Mouse position problem
« Reply #2 on: July 27, 2012, 11:15:39 am »
My code is exactly the same as you posted, except for the fact that there's some more code that moves the camera when the player moves.

i514x

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Mouse position problem
« Reply #3 on: July 27, 2012, 12:02:33 pm »
Then I have no idea sry, am new to all of this too.

floAr

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Mouse position problem
« Reply #4 on: July 27, 2012, 01:37:36 pm »
The problem could be that you bind the cursor position to the mouse alone.
When you transfer the screen (or the view) you wont move the cursor.
So save a vector for your cursor position and add the mouse AND camera movement to this vector.

So if you do
camera->Move(someVector);
also do
cursor->Move(someVector);

This could solve the problem.

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Mouse position problem
« Reply #5 on: July 27, 2012, 02:27:24 pm »
Yeah, I thought about that too, but I was wondering if there was an easier and cleaner way.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Mouse position problem
« Reply #6 on: July 27, 2012, 07:06:13 pm »
You need to provide some source code, otherwise the only thing we can do is blindly guess which won't help you and steal our time. (Also see this) ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Mouse position problem
« Reply #7 on: July 27, 2012, 11:22:37 pm »
But I'm not asking for a fix, I'm asking how to attach the mouse sprite to the screen view...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Mouse position problem
« Reply #8 on: July 27, 2012, 11:36:48 pm »
But I'm not asking for a fix, I'm asking how to attach the mouse sprite to the screen view...
Okay, but I don't understand what you mean with 'fix'.
Have you read my tutorial about Using sf::View?

If you draw something onto the view and then move the view around, it will be 'fixed' to the view and move with the transformation of the view.
If you don't want it to react to transformations on the view then you should use a sperated view, which doesn't move around.

You need to ellaborate on your question because as obvious it may seem to you it's not from my point of view. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Mouse position problem
« Reply #9 on: July 28, 2012, 12:14:55 am »
Yeah, sorry. So this is my draw routine (it is called every time in the main game loop):

    window.clear(sf::Color(0, 0, 0, 255));
    window.draw(cursor);
    window.setView(camera);
    // start drawing
        terrain.draw();
        window.draw(player);
    // end drawing logic
    window.display();
 

where cursor is the sprite of the cursor
camera is the sf::View() (a very normal sf view, no special settings, it just follows the player around)
and player is our player sprite.

What I basically want to accomplish is replacing the mouse default icon with something else. Right now that code makes the cursor follow the mouse but when the camera moves it's left behind.

Also If I set the cursor sprite to be:

cursor.setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);

I get 150px on top of the actual cursor position.
« Last Edit: July 28, 2012, 12:20:38 am by Jeffrey »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Mouse position problem
« Reply #10 on: July 28, 2012, 12:58:47 am »
Yeah then you just need to use two diffrent views, like (shown in the tutorial, or):
sf::View GUI = window.getDefaultView();
// ...
// main loop ...
window.clear();
window.setView(camera);
window.draw(sprite);
window.setView(GUI);
window.draw(cursor);
window.display();
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Mouse position problem
« Reply #11 on: July 28, 2012, 03:37:04 am »
This worked fine thanks. Now we have just another problem:

The cursor sprite starts 150px on top of the cursor and when moving the cursor it's alway 150 px on the top. Why is that?
This is the code before the main loop:
Code: [Select]
    camera = window.getDefaultView();
    camera.setCenter(terrain.getCenter());
    camera.setSize(screen_width, screen_height);

    int cursor_width = 100;
    int cursor_height = 100;
    cursor_texture.loadFromFile("cursor.png");
    cursor.setTexture(cursor_texture);
    cursor.setTextureRect(sf::IntRect(0, 0, cursor_width, cursor_height));

then here I set the cursor (which is the circular sprite) like this:

Code: [Select]
cursor.setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
« Last Edit: July 28, 2012, 03:44:42 am by Jeffrey »

 

anything