SFML community forums

Help => Graphics => Topic started by: chessguy on June 29, 2013, 04:20:11 pm

Title: [Solved] View only moves once
Post by: chessguy on June 29, 2013, 04:20:11 pm
Greetings. Well, I basically have something that draws a 2D array of colored squares. Easy enough, and that part works. I wanted it to scroll, however, as I plan for the random generation to be much wider than a single screen.

        view.move(100, 0);

        std::cout << view.getCenter().x << "\n";

        window.setView(view);

        drawWorld(world);

        std::cout << window.getView().getCenter().x << "\n";

Simple enough. Now, according to the debug lines, the window does have the same view as what it was set to. The view's center does move.

(http://snag.gy/XfhnG.jpg)

That is what it looks like. It started with the gradient in full view. The black space shows that the view moved. Changing the value (From 100) leaves different amounts of black space. However, it always looks like in the picture. It doesn't scroll.

What am I missing?

Also, the view (and the window) are members of a class, and the view is initialized like so, in the constructor:

view.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
               
view.setViewport(sf::FloatRect(0, 0, 1, 1));
[code]
Title: Re: View only moves once
Post by: The Hatchet on June 29, 2013, 04:43:14 pm
I've reread your post a few times and I'm still unsure what you are asking.  Can you redefine or extrapolate more on your problem?  Do you want the view to move more?  Or to have a transition that looks like the background is 'scrolling' along?  Or are you wondering why your arrays of squares are only drawing so far and leaving black where they stop?

From the miniscule amount of code you've shown it looks like everything is behaving as I would think it should.
Title: Re: View only moves once
Post by: Groogy on June 29, 2013, 04:46:21 pm
I think it would help if you provide some kind of minimal example for us. Also use the [ CODE ] tags to paste in code. Makes it much more readable.
Title: Re: View only moves once
Post by: chessguy on June 29, 2013, 04:55:07 pm
Ok. What I expected to happen is that the gradient would keep moving left (or rather, the view moving right and the gradient "falls out" the left), leaving only black. But... it doesn't change visually at all.

Here is that class:

#include "Drawer.h"
#include "BlockSizeCalculator.h"
#include <iostream>

Drawer::Drawer()
{
        window.create(sf::VideoMode(500, 500),  "");

        view.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
               
        view.setViewport(sf::FloatRect(0, 0, 1, 1));
}

void Drawer::drawWorld(World world)
{
        int blockSize = BlockSizeCalculator::getBlockSize();

        sf::RectangleShape rectangle;

        rectangle.setSize(sf::Vector2f(blockSize, blockSize));

        for (int x = 0; x < world.getWidth() ; ++x)
                for (int y = 0; y < world.getHeight() ; ++y)
                {
                        rectangle.setFillColor(world.getColor(x, y));

                        rectangle.setPosition(x*blockSize, window.getSize().y - (y*blockSize + blockSize));

                        window.draw(rectangle);
                }

                window.display();
}

bool Drawer::draw(World world)
{
        view.move(100, 0);

        std::cout << view.getCenter().x << "\n";

        window.setView(view);

        drawWorld(world);

        std::cout << window.getView().getCenter().x << "\n";

        return false;
}

const sf::RenderWindow & Drawer::getWindow() {return window;}

 

And called like so:

while (true)
{
        drawer.draw(world);

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
 
Title: Re: View only moves once
Post by: The Hatchet on June 29, 2013, 05:02:47 pm
window.clear();
needs to be called once each time before you draw.  I do not see you clearing the screen in your code.

So basically:
window.clear();
//draw all the things
window.display();
Title: Re: View only moves once
Post by: chessguy on June 29, 2013, 05:11:50 pm
Yup, that was it.

You can probably tell I haven't used SFML for a while :D

Thanks.