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 - person999

Pages: [1]
1
Graphics / Re: How to extract Rotation And Scale from sf::Transform
« on: August 01, 2024, 12:02:49 pm »
Thanks man, it helped me alot. I was very confused with the coordinate system and this transformation matrix because I am new.

2
General / Re: New here, and a simple question
« on: July 25, 2024, 03:33:04 pm »
Actually its just because of framerate limit you set. You should really read the section "Controlling the framerate" of this documentation : https://www.sfml-dev.org/tutorials/2.6/window-window.php

However, I've made some improvements to the code. Maybe you should try it :
#include <SFML/Graphics.hpp>

int main()
{
    sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode(); //Getting desktop mode of the screen (full screen size)

    sf::RenderWindow window(desktopMode, "Screensaver", sf::Style::Fullscreen);

    //setting some constants
    const float WIN_WIDTH = desktopMode.width;
    const float WIN_HEIGHT = desktopMode.height;

    sf::CircleShape shape(50.f);
    shape.setOrigin(50, 50);
    shape.setFillColor(sf::Color::Green);

    shape.setPosition(shape.getRadius(), WIN_HEIGHT / 2.f); //Ball starts from middle left of the screen

    /* To make movement independent of frame rate*/
    sf::Clock clock;
    float dt = 0;
    float multiplier = 60;

    float xVelocity = 5; //Ball will go forward in the start
    float yVelocity = -5; //Ball will go upward in the start

    sf::Event event; //Moved it out of game loop, because it was useless to move it in loop and creating it in each iteration

    while (window.isOpen())
    {  
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
           
        }

        /*Point to remember is that, shapes origin is at its center, so we have to keep this in mind*/
        if (shape.getPosition().x < shape.getOrigin().x || shape.getPosition().x >= (WIN_WIDTH - shape.getOrigin().x)) {
            xVelocity = -xVelocity; //Some basic logic
        }

        if (shape.getPosition().y < shape.getOrigin().x  || shape.getPosition().y >= (WIN_HEIGHT - shape.getOrigin().y)) {
            yVelocity = -yVelocity;
        }

        dt = clock.restart().asSeconds(); //restarting clock on each frame recording time elapsed in the dt variable

        //Using move rather than setPosition
        shape.move(xVelocity * dt * multiplier, yVelocity * dt * multiplier);

        //Drawing stuff
        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

Pages: [1]