SFML community forums

Help => Graphics => Topic started by: teunissenstefan on August 20, 2014, 09:26:04 pm

Title: Gradient Rectangle
Post by: teunissenstefan on August 20, 2014, 09:26:04 pm
I was searching for how to use gradient, then I found this: http://en.sfml-dev.org/forums/index.php?topic=6750.0
I tried the code that laurent provided but it doesn't work. No errors, just a black screen.
Pieces of code in question:
sf::Vertex backgroundgr[] =
        {
                sf::Vertex(sf::Vector2f(0, 0), sf::Color::Blue),
                sf::Vertex(sf::Vector2f(0, 1280), sf::Color::Blue),
                sf::Vertex(sf::Vector2f(720, 0), sf::Color::Cyan),
                sf::Vertex(sf::Vector2f(720, 1280), sf::Color::Cyan)
        };

m_window.draw(backgroundgr, 4, sf::Quads);
Now, I don't know if I'm using it wrong, or if it just doesn't work.  :-\
Title: Re: Gradient Rectangle
Post by: Jesper Juhl on August 20, 2014, 09:32:40 pm
Just asking the obvious questions here:

Do you clear() before you draw()?
Do you display() after you draw()?
Title: Re: Gradient Rectangle
Post by: teunissenstefan on August 20, 2014, 09:35:09 pm
Yep, both. Just thought those were too obvious to show it to you ;)
Title: Re: Gradient Rectangle
Post by: G. on August 20, 2014, 09:40:21 pm
http://en.sfml-dev.org/forums/index.php?topic=16094.msg115303#msg115303 ?

That's not a rectangle.
And is your window 720, 1280?
Title: Re: Gradient Rectangle
Post by: teunissenstefan on August 20, 2014, 09:47:04 pm
Uhm 1280 x 720, but even with 720, 1280 it should at least be drawn for a bit.
Title: Re: Gradient Rectangle
Post by: G. on August 20, 2014, 10:10:13 pm
Yep, but are you going to post an SSCCE or is this thread not worth coming back?

>Good luck then. :D
Title: Re: Gradient Rectangle
Post by: teunissenstefan on August 20, 2014, 10:35:37 pm
Well I guess it's not worth for you to come back then.
Title: Re: Gradient Rectangle
Post by: zsbzsb on August 21, 2014, 01:09:44 am
All primitive types must be defined in either clockwise or counter clockwise direction. Your code should be changed to the following.

Note: I changed the vertex order.
sf::Vertex backgroundgr[] =
        {
                sf::Vertex(sf::Vector2f(0, 0), sf::Color::Blue),
                sf::Vertex(sf::Vector2f(0, 1280), sf::Color::Blue),
                sf::Vertex(sf::Vector2f(720, 1280), sf::Color::Cyan)
                sf::Vertex(sf::Vector2f(720, 0), sf::Color::Cyan),
        };
Title: Re: Gradient Rectangle
Post by: teunissenstefan on August 21, 2014, 05:14:24 pm
Kay thanks, but it didn't fix the problem.
Title: Re: Gradient Rectangle
Post by: zsbzsb on August 21, 2014, 05:31:19 pm
Lets keep guessing here..... Or will you follow G.'s advice?
Title: Re: Gradient Rectangle
Post by: teunissenstefan on August 21, 2014, 05:37:17 pm
FINE YA DINGUS, for you, because you're nice. Not for G...
#include <memory>
#include <iostream>

#include "StateMachine.hpp"
#include "Level1State.hpp"
#include "PlayState.hpp"
#include "GameState.hpp"
#include "StateMachine.hpp"
#include "MenuState.hpp"
#include "Application.hpp"

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>

int main() {
    sf::RenderWindow window( sf::VideoMode(1280, 720, 32), "Vieze kut" );
        sf::Vertex backgroundgr[] =
        {
                sf::Vertex(sf::Vector2f(0, 0), sf::Color::Blue),
                sf::Vertex(sf::Vector2f(0, 1280), sf::Color::Blue),
                sf::Vertex(sf::Vector2f(720, 1280), sf::Color::Cyan),
                sf::Vertex(sf::Vector2f(720, 0), sf::Color::Cyan),
        };
       
    while( window.isOpen() ) {
        sf::Event event;
        while( window.pollEvent( event ) ) {
            if( event.type == sf::Event::Closed ) {
                window.close();
            }
        }

                window.clear();
                window.draw(backgroundgr, 4, sf::Quads);
        window.display();
    }

    return EXIT_SUCCESS;
}
Title: Re: Gradient Rectangle
Post by: teunissenstefan on August 21, 2014, 06:02:12 pm
Hmm okay so that alone works, but not if
sf::Vertex backgroundgr[4];
is set in header?

And if I just put
sf::Vertex backgroundgr[4] =
        {
                sf::Vertex(sf::Vector2f(0, 0), sf::Color::Blue),
                sf::Vertex(sf::Vector2f(0, 1280), sf::Color::Blue),
                sf::Vertex(sf::Vector2f(720, 1280), sf::Color::Cyan),
                sf::Vertex(sf::Vector2f(720, 0), sf::Color::Cyan),
        };
in the .cpp it says: backgroundgr : undeclared identifier
Title: Re: Gradient Rectangle
Post by: Nexus on August 23, 2014, 09:09:44 pm
You can't just declare variables anywhere. Either declare the array of vertices as a local variable, or use a function... Then I recommend sf::VertexArray or at least std::array<sf::Vertex, 4> for value semantics. Plain C arrays should be avoided where possible.

By the way, this is not a SFML-related question... It might be worth having a look at a good C++ book :)