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

Author Topic: Window Boundaries  (Read 4173 times)

0 Members and 1 Guest are viewing this topic.

SerNick

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Window Boundaries
« on: June 06, 2016, 07:21:46 pm »
So I'm new to SFML and I want to start learning to code to eventually build myself up to creating my own games. But for now, I want to create a version of Space Invaders. So far, it's fine, however, I can't find a way around one problem: Window Boundaries. This is my code so far (Quite messy I know. Not quite got to using classes yet):

#include <SFML/Graphics.hpp>

int main()
{
        int INV_HORIZONTAL_POSITION = 330;
        int INV_VERTICAL_POSITION = 440;
        //Renders the Window
        sf::RenderWindow window(sf::VideoMode(720, 480), "Space Invaders!");
        //Initializes the texture for the sprite - so the image can be loaded.
        sf::Texture texture;
        //Loads the Image
        texture.loadFromFile("sprites.PNG");
        //Sets the location to load from on the sprite sheet
        sf::IntRect invader;
        invader.left = 23;
        invader.top = 110;
        invader.width = 106;
        invader.height = 73;
        sf::Sprite sprite(texture, invader);

        //Sets the position of the sprite on the screen
        sprite.setPosition(INV_HORIZONTAL_POSITION, INV_VERTICAL_POSITION);
        //Sets the Scale of the image. (1,1) Native Size.
        sprite.setScale(.5, .5);

        while (window.isOpen())
        {
               

                sf::Event event;
                while (window.pollEvent(event))
                {
                       
                                if (event.type == sf::Event::Closed)
                                        window.close();
                }

                window.clear();
                window.draw(sprite);
                window.display();


                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        sprite.move(-0.1, 0);

                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        sprite.move(0.1, 0);
                }


        }

        return 0;
}


Where in this code do I set any window boundaries to stop sprites straying outside them, and how do I add them?


Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Window Boundaries
« Reply #1 on: June 07, 2016, 12:42:57 am »
i guess you need to create window boundary as sf::FloatRect.
sf::FloatRect windowBounds(sf::Vector2f(0.f, 0.f), window.getDefaultView().getSize());

then at end of update adapt the shape postilion based on whither or not it is within window boundary. it can be done by using std::min and std::max instead of using bock of if-statements

here simple example by using sf::RectangleShape. to show the idea.
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window({720, 480}, "test");
    sf::FloatRect windowBounds({0.f, 0.f}, window.getDefaultView().getSize());
    sf::RectangleShape shape({100.f, 100.f});
    // set position in the middle of window
    shape.setPosition((window.getView().getSize() - shape.getSize())/2.f);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                    window.close();
        }
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
                shape.move(-0.1, 0);
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
                shape.move(0.1, 0);
        }
       
        // adapt shape position tobe within window boundary
        sf::Vector2f position = shape.getPosition();
        position.x = std::max(position.x, windowBounds.left);
        position.x = std::min(position.x, windowBounds.left + windowBounds.width - shape.getSize().x);
        shape.setPosition(position);
       
        window.clear();
        window.draw(shape);
        window.display();

    }
}

 

anything