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

Author Topic: Mouse::getPosition(window) always returns 0  (Read 2571 times)

0 Members and 1 Guest are viewing this topic.

Inventor4Life

  • Newbie
  • *
  • Posts: 1
    • View Profile
Mouse::getPosition(window) always returns 0
« on: April 24, 2021, 10:29:59 pm »
Hello! I'm trying to write a mouse trails program, but I keep running into an issue where Mouse::getPosition(Window) returns 0, regardless of mouse position.
#include <SFML\Graphics.hpp>
#include <iostream>
#include <vector>
using namespace sf;
RenderWindow window;

void CircMod(std::vector<CircleShape> &list) {
    for (CircleShape item : list) {
        if (item.getRadius() < 1) {
            item.setRadius(50);
            item.setPosition(Vector2f(Mouse::getPosition(window).x, Mouse::getPosition(window).y));
        }else {
            item.setRadius(item.getRadius() - 1);
        }
     std::cout << Mouse::getPosition(window).x;
    }
}

int main()
{
    int frameCounter = 0;
    std::vector<CircleShape> shape(50);
    for (int i = 0; i < 50; i++) {
        shape[i].setFillColor(Color::Green);
        shape[i].setPointCount(50);
        shape[i].setRadius((-1 * i + 50));
        shape[i].setOrigin(Vector2f(-1 * i + 50, -1 * i + 50));
    }
    ContextSettings settings;
    settings.antialiasingLevel = 8;
    RenderWindow window(sf::VideoMode(500, 500), "Test Window", Style::Default, settings);
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
            if (event.type == Event::MouseMoved) {
            }
        }

        window.clear();
        frameCounter++;
        if (frameCounter / 100 > 1) {
            frameCounter -= 100;
        }
        if (frameCounter % 100 == 0) {
            CircMod(shape);
        }
        for (CircleShape item : shape) {
            window.draw(item);
        }
        window.display();
    }

    return 0;
}
 
The console just logs a long string of 0's. My intent for this is a program that renders 50 circles in gradually decreasing size, and the smallest circle gets moved to the mouse position and has its radius set to 50.

When I run this program, the circles get rendered in the top left corner, and don't follow the mouse.
« Last Edit: April 24, 2021, 10:33:26 pm by Inventor4Life »

kojack

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Mouse::getPosition(window) always returns 0
« Reply #1 on: April 25, 2021, 05:18:36 am »
Your for loop isn't making item a reference, so you are changing the radius of a temporary CircleShape, not the ones in the list.
If the circles start with a 0 radius, then when the code does the setRadius(50) to make them big, that's done to the temporary, so next pass they are all back to 0.

Try this:
for (CircleShape& item : list) {


 

anything