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

Author Topic: Is this the right way to use sf::VertexBuffer ?  (Read 5902 times)

0 Members and 1 Guest are viewing this topic.

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Is this the right way to use sf::VertexBuffer ?
« on: May 12, 2018, 05:19:35 am »
Static :
#include <array>
#include <SFML/Graphics.hpp>

int main()
{

    const float start = 100;
    const float width = 200;

    sf::RenderWindow win{ sf::VideoMode{ 600, 600 }, "using VertexBuffer" };
    win.setFramerateLimit( 60 );

    sf::VertexBuffer v_buffer{ sf::Quads, sf::VertexBuffer::Static };
    v_buffer.create( 4 );

    std::array< sf::Vertex, 4 > v_arr;
    v_arr.at( 0 ).position = sf::Vector2f{ start, start + width };
    v_arr.at( 1 ).position = sf::Vector2f{ start, start };
    v_arr.at( 2 ).position = sf::Vector2f{ start + width, start };
    v_arr.at( 3 ).position = sf::Vector2f{ start + width, start + width };

    v_arr.at( 0 ).color = sf::Color::Blue;
    v_arr.at( 1 ).color = sf::Color::Red;
    v_arr.at( 2 ).color = sf::Color::Green;
    v_arr.at( 3 ).color = sf::Color::White;

    v_buffer.update( v_arr.data() );

    while( win.isOpen() )
    {
        sf::Event event;
        while( win.pollEvent( event ) )
        {
            if ( event.type == sf::Event::Closed ) win.close();
            if ( ( event.type == sf::Event::KeyPressed ) && ( event.key.code == sf::Keyboard::Escape ) ) win.close();
        }

        win.clear();
        win.draw( v_buffer );
        win.display();
    }
}
 

#include <array>
#include <SFML/Graphics.hpp>

int main()
{

    const float start = 0;
    const float width = 200;

    sf::RenderWindow win{ sf::VideoMode{ 600, 600 }, "using VertexBuffer" };
    win.setFramerateLimit( 60 );

    sf::VertexBuffer v_buffer{ sf::Quads, sf::VertexBuffer::Stream };
    v_buffer.create( 4 );

    std::array< sf::Vertex, 4 > v_arr;
    v_arr.at( 0 ).position = sf::Vector2f{ start, start + width };
    v_arr.at( 1 ).position = sf::Vector2f{ start, start };
    v_arr.at( 2 ).position = sf::Vector2f{ start + width, start };
    v_arr.at( 3 ).position = sf::Vector2f{ start + width, start + width };

    v_arr.at( 0 ).color = sf::Color::Blue;
    v_arr.at( 1 ).color = sf::Color::Red;
    v_arr.at( 2 ).color = sf::Color::Green;
    v_arr.at( 3 ).color = sf::Color::White;

    v_buffer.update( v_arr.data() );

    sf::Vector2f init_position;
    init_position.x = ( v_arr.at( 2 ).position.x + v_arr.at( 1 ).position.x ) / 2;
    init_position.y = ( v_arr.at( 0 ).position.y + v_arr.at( 1 ).position.y ) / 2;

    sf::Vector2f position{ init_position };

    while( win.isOpen() )
    {
        sf::Event event;
        while( win.pollEvent( event ) )
        {
            if ( event.type == sf::Event::Closed ) win.close();
            if ( ( event.type == sf::Event::KeyPressed ) && ( event.key.code == sf::Keyboard::Escape ) ) win.close();
        }

        if ( position.x == static_cast<float>( win.getSize().x  ) )
        {
            position = init_position;

            v_arr.at( 0 ).position.x = init_position.x - ( width / 2 );
            v_arr.at( 1 ).position.x = init_position.x - ( width / 2 );
            v_arr.at( 2 ).position.x = init_position.x + ( width / 2 );
            v_arr.at( 3 ).position.x = init_position.x + ( width / 2 );
        }

        v_arr.at( 0 ).position.x += 1;
        v_arr.at( 1 ).position.x += 1;
        v_arr.at( 2 ).position.x += 1;
        v_arr.at( 3 ).position.x += 1;

        position.x += 1;

        v_buffer.update( v_arr.data() );

        win.clear();
        win.draw( v_buffer );
        win.display();
    }
}
 

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: Is this the right way to use sf::VertexBuffer ?
« Reply #1 on: May 22, 2018, 05:15:27 am »
hi

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Is this the right way to use sf::VertexBuffer ?
« Reply #2 on: May 22, 2018, 08:02:39 am »
Does it work?
What does the documentation say?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: Is this the right way to use sf::VertexBuffer ?
« Reply #3 on: May 25, 2018, 12:28:19 pm »
yes, it does work...and yay, a reply...
ok thank you