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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Inventor4Life

Pages: [1]
1
Window / 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.

Pages: [1]
anything