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

Author Topic: Mouse Position not working  (Read 319 times)

0 Members and 1 Guest are viewing this topic.

KokosNaPalmie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Mouse Position not working
« on: March 08, 2020, 09:13:07 pm »
I'm working on a game and have a problem with mouse position relative to screen, window and view
void Things::UpdateMousePos() // parent class to objects like buttons, texts etc.
{
    this->mPosScreen = sf::Mouse::getPosition(); // working on fullscreen
    this->mPosWindow = sf::Mouse::getPosition(); // not working
    this->mPosView = this->window->mapPixelToCoords(sf::Mouse::getPosition(*this->window)); // crashing the game
}

I've searched quite a lot, but even the view tutorial on sfml site hasn't resolved it

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9626
    • View Profile
    • development blog
    • Email
Re: Mouse Position not working
« Reply #1 on: March 09, 2020, 10:48:40 am »
Use a debugger to get a call stack of the crash.

Also your window should probably not be a pointer.
Personally, I would recommend to not use this-> to access member variables, especially if you also prefix those with m. It adds more "noise" and thus harder to read.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

KokosNaPalmie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mouse Position not working
« Reply #2 on: March 09, 2020, 06:35:31 pm »
I used the debugger, and I got
#0 0x6e185ab0   sf::Window::getSystemHandle(this=0xbaadf00d) (D:\Programming\C++\Releases\_Sources\SFML\src\SFML\Window\Window.cpp:390)
error (segment fault)

Also deleted all pointers to window, but it still doesn't want to work

KokosNaPalmie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mouse Position not working
« Reply #3 on: March 09, 2020, 10:09:21 pm »
OK, I solved this problem by getting my mouse position from game state, not from the button's parent class.
Now my code looks like this:
   if mouse is on a button:
bool SomeText::isMouseInvaded(sf::Vector2i mouse_position)
{
    if      ( mouse_position.x >= this->text.getPosition().x - textRect.width / 2  &&
              mouse_position.x <= this->text.getPosition().x + textRect.width / 2  &&
              mouse_position.y >= this->text.getPosition().y - textRect.height / 2 &&
              mouse_position.y <= this->text.getPosition().y + textRect.height / 2 &&
              this->if_halfed )                                                                  return true; //if_halfed means if origin is set in the middle or in the top-left corner
    else if ( mouse_position.x >= this->text.getPosition().x                                   &&
              mouse_position.x <= this->text.getPosition().x + textRect.width                  &&
              mouse_position.y >= this->text.getPosition().y + textRect.top                    &&
              mouse_position.y <= this->text.getPosition().y + textRect.height + textRect.top  &&
              !this->if_halfed)                                                                  return true;
    else                                                                                         return false;
}
   and a fragment from function checking that event:
if (this->things["QUIT_BUTTON"]->isClicked_Left(mPosWindow))    // quitting the game
        this->quit = true;
« Last Edit: March 09, 2020, 10:10:54 pm by KokosNaPalmie »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32453
    • View Profile
    • SFML's website
    • Email
Re: Mouse Position not working
« Reply #4 on: March 09, 2020, 10:14:50 pm »
Quote
this=0xbaadf00d
This is a special value to help debugging. This particular one means uninitialized memory allocated on the heap (to be verified, I just searched quickly).

So most likely you were allocating a new Things instance without initializing its window member to a valid sf::Window pointer.
« Last Edit: March 09, 2020, 10:16:36 pm by Laurent »
Laurent Gomila - SFML developer

KokosNaPalmie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mouse Position not working
« Reply #5 on: March 09, 2020, 10:27:20 pm »
Could this be the reason for using the same names for the window?

Things::Things(sf::RenderWindow* window)
{
    this->window = window;
    this->arial.loadFromFile("Fonts/arial.ttf"); // loading fonts
    this->comicsans.loadFromFile("Fonts/comic.ttf");
    this->ethnocen.loadFromFile("Fonts/ethnocen.ttf");
}

 

anything