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

Author Topic: MouseLeft/MouseEntered and Input.GetMouseX() issue  (Read 3688 times)

0 Members and 1 Guest are viewing this topic.

TurboLento

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • http://pjmendes.blogspot.com/
MouseLeft/MouseEntered and Input.GetMouseX() issue
« on: April 11, 2010, 06:11:17 pm »
Hi all,

I've noticed a problem with my program and was wondering if I've detected a bug or if I'm doing anything wrong.

I'm detecting the sf::Event::MouseEntered/MouseLeft events for my window, and only obtaining the mouse coordinates and doing logic if the mouse is inside. However, if I move the mouse close to the left edge (where mouseX = 0), sometimes either the MouseEntered event will fire (if I've moved the mouse out of the window) or no MouseLeft event will fire, but GetMouseX() will return "65535".

(I've hacked a function "isMouseInsideWindow()" returning "(this->mouseIsInsideWindow && (this->getMouseX() < this->renderWindow->GetWidth()));", but it's ugly).

Any ideas? I'm using SFML 1.5, but I don't see any mention to this in the 1.6 change log.
Game development blog: http://pjmendes.blogspot.com/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
MouseLeft/MouseEntered and Input.GetMouseX() issue
« Reply #1 on: April 11, 2010, 06:45:31 pm »
Quote
However, if I move the mouse close to the left edge (where mouseX = 0), sometimes either the MouseEntered event will fire (if I've moved the mouse out of the window) or no MouseLeft event will fire

I'm not sure I understand what happens exactly. Do you have a minimal and complete example that reproduces the problem?

Quote
GetMouseX() will return "65535"

I remember fixing something similar in SFML 1.6 (using signed integers for mouse coordinates).
Laurent Gomila - SFML developer

TurboLento

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • http://pjmendes.blogspot.com/
MouseLeft/MouseEntered and Input.GetMouseX() issue
« Reply #2 on: April 11, 2010, 09:12:14 pm »
Wasn't easy, it seems to happen at very particular window configurations. By example, I got it to occur in windowed mode in 650x480, but not at 640x480 or 649x490... odd. And it only happens if RenderWindow is initialized with the flag "sf::Style::Close". I was testing it in my desktop's resolution at 1280x720 and it also occurred.

By the way, I didn't mention that I'm running SFML in Windows XP.

The code below creates a window, draws a rectangle at 50,50 to 150,150 and moves the camera to the left if the user places the mouse close to the left screen edge (stopping when the view's left side == 0) or to the right if the user places the mouse close to the right edge.

The bug is detected when the user places the mouse very near the left screen edge, where the x mouse coordinate turns from 0 to the large value, while the MouseLeft event is not launched, and the camera pans incorrectly (to the right instead of left or stopping).

Code: [Select]

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

sf::Vector2f getCameraPosition(sf::RenderWindow& rw)
{
    sf::Vector2f pos(rw.GetDefaultView().GetCenter());
    sf::Vector2f halfDims(rw.GetDefaultView().GetHalfSize());
    return sf::Vector2f(pos.x - halfDims.x, pos.y - halfDims.y);
}

void setCameraPosition(sf::RenderWindow& rw, const sf::Vector2f& newPosition)
{
    sf::Vector2f halfDims(rw.GetDefaultView().GetHalfSize());
    rw.GetDefaultView().SetCenter(newPosition.x + halfDims.x, newPosition.y + halfDims.y);
}

int main()
{
    sf::Clock Clock;

    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(650, 480, 32), "SFML Graphics", sf::Style::Close);

    bool mouseIsInside = true;

    sf::Shape rectangle = sf::Shape::Rectangle(50, 50, 150, 150, sf::Color(255,0,0,255));

    // Start game loop
    while (App.IsOpened())
    {
        float timeSinceLastUpdate = Clock.GetElapsedTime();
        Clock.Reset();

        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if(Event.Type == sf::Event::MouseLeft)
                mouseIsInside = false;
            if(Event.Type == sf::Event::MouseEntered)
                mouseIsInside = true;
        }

        sf::Vector2f mousePosition(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());
        sf::Vector2f cameraPosition = getCameraPosition(App);
        float scrollSpeed = 100;
        float coordLeftWindowBorder = cameraPosition.x;

        if(mouseIsInside)
        {
            std::cout << "mouse pos:" << mousePosition.x << ", "<< mousePosition.y << "\n";

            if(mousePosition.x > 590)
                setCameraPosition(App, cameraPosition + sf::Vector2f(scrollSpeed * timeSinceLastUpdate,0));
            else
            if((mousePosition.x < 0 + 50) && (coordLeftWindowBorder > 0))
                setCameraPosition(App, cameraPosition - sf::Vector2f(scrollSpeed * timeSinceLastUpdate,0));
        }
        //else
        //    std::cout << "out ";

        App.Draw(rectangle);

        // Display window contents on screen
        App.Display();

        // Clear the screen (fill it with black color)
        App.Clear();
    }

    return EXIT_SUCCESS;
}
Game development blog: http://pjmendes.blogspot.com/