SFML community forums

Help => Graphics => Topic started by: F2CPP on February 11, 2014, 07:46:59 pm

Title: Infinitely repeated tile as background sprite
Post by: F2CPP on February 11, 2014, 07:46:59 pm
What I've got is

        sf::RenderWindow        Window(sf::VideoMode(1536, 768), "", sf::Style::Close);
        sf::View                        View(Window.getDefaultView());
        sf::FloatRect           fBounds(0.f, 0.f, 10.f, 1000.f); // arbitrary > view height
        sf::Texture             Texture;

        Texture.loadFromFile("C:/.../.png");
        sf::IntRect                     iBounds(fBounds);
        Texture.setRepeated(true); // repeat tile over sprite height
        sf::Sprite                      Sprite(Texture, iBounds);
        // move sprite 'up' by its height except the view height for start:
        Sprite.setPosition(fBounds.left, fBounds.top - 1000.f + View.getSize().y);

        while (Window.isOpen()) {

                View.move(0.f, -1.f); // negative y to move 'up' along sprite height

                Window.clear();
                Window.setView(View);
                Window.draw(Sprite);
                Window.display();
        }

Although I got confused with the y-axis pointing downward and the .png upward, this now repeatedly tiles the sprite (vertically), up to sprite height only however.

Is it possible, and how, to make the .png tiling truly infinite, or at least overcome the iBounds restriction?

Thanks
Title: Re: Infinitely repeated tile as background sprite
Post by: AN71 on February 11, 2014, 08:05:58 pm
If I understand correctly, you just need infinite tiling, for which you could emulate by simply having the tiled object larger than the screen, and using a subRect that once the end of the object is found, it loops back to the beginning, kind of like how people emulate toroidal topology, if you understand what I'm saying. Hope this helps a bit :)
Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on February 11, 2014, 08:28:32 pm
You should increase the size of the fBounds width.

EDIT: for infinity, you could move the sprite upwards by the size of the sprite everytime the view moves down by that amount.

EDIT2: adapted your code to use the idea I just suggested.
#include <SFML/Graphics.hpp>
int main()
{
        sf::RenderWindow    Window(sf::VideoMode(1000, 768), "", sf::Style::Close);
        sf::View            View(Window.getDefaultView());
        sf::FloatRect       fBounds(0.f, 0.f, 1000.f, 1000.f); // arbitrary > view height
        sf::Texture     Texture;

        Texture.loadFromFile("image.png");
        sf::IntRect         iBounds(fBounds);
        Texture.setRepeated(true); // repeat tile over sprite height
        sf::Sprite          Sprite(Texture, iBounds);
        // move sprite 'up' by its height except the view height for start:
        Sprite.setPosition(fBounds.left, fBounds.top - 1000.f + View.getSize().y);

        float viewOffsetY = 0;
        float spriteOffsetY = 0;
        unsigned int textureHeight = Texture.getSize().y;

        while (Window.isOpen()) {
                sf::Event event;
                while (Window.pollEvent(event)) {
                        if (event.type == sf::Event::Closed)
                                Window.close();
                }
                View.setCenter(500.f, 500.f - viewOffsetY); // negative y to move 'up' along sprite height
                viewOffsetY += 0.3f; // speed of view movement
                spriteOffsetY = floor(viewOffsetY / textureHeight) * textureHeight;
                Sprite.setPosition(fBounds.left, fBounds.top /* - 1000.f + View.getSize().y */ - spriteOffsetY);

                Window.clear();
                Window.setView(View);
                Window.draw(Sprite);
                Window.display();
        }
}
Title: Re: Infinitely repeated tile as background sprite
Post by: F2CPP on February 11, 2014, 09:28:46 pm
Many thanks to both of you - I was hoping for some kind of ray/open end option but this will do.

A second identical sprite involves moving just as well, so as per your initial suggestion I added a move counter

float   accumulatemoves(View.getSize().y); // simply for speed of 1.f

which increments inside the game loop

    ++accumulatemoves; // must be adjusted for varying speeds

and is checked against the View's travelled distance

 if (Sprite.getTextureRect().height <= accumulatemoves) {

                        Sprite.move(0.f, -Sprite.getTextureRect().height+View.getSize().y);
                        accumulatemoves = View.getSize().y;
                }

(which should be similar to what you show, though only along y.)

Many thanks for your help
Title: Re: Infinitely repeated tile as background sprite
Post by: zsbzsb on February 11, 2014, 09:36:41 pm
Or you could simple set the texture rect of the sprite to whatever size you want (with texture repeat turned on).  ;)

http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php (http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php)
Title: Re: Infinitely repeated tile as background sprite
Post by: F2CPP on February 11, 2014, 11:21:18 pm
Thanks, I'll stretch it to a max (though not leave it open-ended)
Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on February 11, 2014, 11:45:58 pm
Or you could simple set the texture rect of the sprite to whatever size you want (with texture repeat turned on).  ;)

http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php (http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php)
I don't think that tutorial actually shows you how to change your sprite's rectangle to be larger than the texture. I may just be missing it, though. F2CPP was already using a large, repeating texture, but increasing it to a gigantic size would probably cover most applications.
That's just not infinite though :P
Title: Re: Infinitely repeated tile as background sprite
Post by: zsbzsb on February 11, 2014, 11:50:20 pm
Yea the tutorial doesn't show that  :P as for already using a large repeating texture - it was difficult to say either way if the initial code was using a repeating texture  :)
Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on February 11, 2014, 11:52:47 pm
it was difficult to say either way if the initial code was using a repeating texture  :)
I must admit that I didn't see it at first either and I had it open in my IDE.
Weird though; it's quite obvious:
Texture.setRepeated(true); // repeat tile over sprite height
:)
Title: Re: Infinitely repeated tile as background sprite
Post by: zsbzsb on February 11, 2014, 11:55:05 pm
I must admit that I didn't see it at first either and I had it open in my IDE.
Weird though; it's quite obvious:
Texture.setRepeated(true); // repeat tile over sprite height
:)

Yep, I saw that the first time. But this is what I was referring to:

Quote
This works only if your sprite is configured to show a rectangle which is bigger than the texture. Otherwise this property has no effect.
(quote from tutorial)
Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on February 12, 2014, 12:45:49 am
But this is what I was referring to:
Quote
[quote from tutorial]
Ah, I see what you mean now. By "initial code", I thought you meant the one on this thread, not the tutorial   :'(
Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on February 12, 2014, 11:53:26 pm
A second identical sprite involves moving just as well, so as per your initial suggestion I added a move counter

float   accumulatemoves(View.getSize().y); // simply for speed of 1.f

which increments inside the game loop

    ++accumulatemoves; // must be adjusted for varying speeds

and is checked against the View's travelled distance

 if (Sprite.getTextureRect().height <= accumulatemoves) {

                        Sprite.move(0.f, -Sprite.getTextureRect().height+View.getSize().y);
                        accumulatemoves = View.getSize().y;
                }

(which should be similar to what you show, though only along y.)

Many thanks for your help
I changed your usage of "move" to explicit positions. It seems that you prefer to use "move" for the view. Since I still had the code I posted before, I adjusted it to automatically adjust based on where the view is - so you can "move" it wherever and the tiled sprite will follow!
#include <SFML/Graphics.hpp>
int main()
{
        sf::RenderWindow    Window(sf::VideoMode(1000, 768), "", sf::Style::Close);
        sf::View            View(Window.getDefaultView());
        sf::FloatRect       fBounds(0.f, 0.f, 1500.f, 1000.f); // arbitrary > view "SIZE"
        sf::Texture     Texture;

        Texture.loadFromFile("image.png");
        sf::IntRect         iBounds(fBounds);
        Texture.setRepeated(true);
        sf::Sprite          Sprite(Texture, iBounds);

        const sf::Vector2f viewStart(fBounds.left + (fBounds.width / 2), fBounds.top + (fBounds.height / 2));
        const sf::Vector2f spriteStart(fBounds.left, fBounds.top);

        while (Window.isOpen()) {
                sf::Event event;
                while (Window.pollEvent(event)) {
                        if (event.type == sf::Event::Closed)
                                Window.close();
                }
                View.move(-0.2f, -0.3f); // just move the view here in any direction-the tiles will follow automatically
                const sf::Vector2f viewOffset(viewStart - View.getCenter());
                sf::Vector2f spriteOffset;
                spriteOffset.x = floor(viewOffset.x / Texture.getSize().x) * Texture.getSize().x;
                spriteOffset.y = floor(viewOffset.y / Texture.getSize().y) * Texture.getSize().y;
                Sprite.setPosition(spriteStart - spriteOffset);

                Window.clear();
                Window.setView(View);
                Window.draw(Sprite);
                Window.display();
        }
}
Hope you find this useful/interesting, but even if not, I found it relaxing  :D
Title: Re: Infinitely repeated tile as background sprite
Post by: F2CPP on February 13, 2014, 07:21:20 pm
It is useful, much more universal, I'll get back to it when my project evolves - thanks
Title: Re: Infinitely repeated tile as background sprite
Post by: Bogdan on July 08, 2014, 09:40:43 pm
Hi there,
sorry to retrieve this old thread, but I've a question concerning the code that was posted in the last post.
Is it possible to get it working for multiple tiles, that are aligned horizontally to get a earth-like "map" (which means in 2D terms: If you leave it to the left side you will reappear from the right side and vice versa :-) )
Well, I've been trying to implement that for some days now (Visaual c++ 10/SFML 2.1) :-), but it produces strange efffects and doesn't work as intended. The "blue part" (s. Attachment) is somehow "finite". Many thanks in advance for any advice! Here is the code (the main part was taken from the last post where some modifications have been applied).


#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow    Window(sf::VideoMode(1000, 768), "", sf::Style::Close);
    sf::View            View(Window.getDefaultView());
 
        sf::FloatRect       fBounds(0.f, 0.f, 2000.f, 500.f);
        sf::FloatRect       fBounds2(0.f, 0.f, 2000.f, 500.f); // arbitrary > view "SIZE"

    sf::Texture     Texture;
        sf::Texture             Texture2;

    Texture.loadFromFile("mapleft.png");
        Texture2.loadFromFile("mapright.png");

    sf::IntRect         iBounds(fBounds);
        sf::IntRect         iBounds2(fBounds2);

    Texture.setRepeated(true);
        Texture2.setRepeated(true);

    sf::Sprite          Sprite(Texture, iBounds);
        sf::Sprite                      Sprite2(Texture2, iBounds2);

    const sf::Vector2f viewStart(fBounds.left + (fBounds.width / 2), fBounds.top + (fBounds.height / 2));
        const sf::Vector2f viewStart2(fBounds2.left + (fBounds2.width / 2), fBounds2.top + (fBounds2.height / 2));

    const sf::Vector2f spriteStart(fBounds.left, fBounds.top);
        const sf::Vector2f spriteStart2(fBounds2.left, fBounds2.top);

while (Window.isOpen())
        {
                sf::Event event;
                while (Window.pollEvent(event))
                        {
                                if (event.type == sf::Event::Closed)
                                        {
                                        Window.close();
                                        }                                                                                                                                      
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right))
                                        {
                                        View.move(+150, 0);
                                        }
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Left))                                                  
                                        {
                                        View.move(-150, 0);
                                        }
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Up))
                                        {
                                        View.move(0, -150);
                                        }
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Down))                                                  
                                        {
                                        View.move(0, +150);
                                        }      
                }

        const sf::Vector2f viewOffset(viewStart - View.getCenter());

        sf::Vector2f spriteOffset;
        sf::Vector2f spriteOffset2;

        spriteOffset.x = floor(viewOffset.x / Texture.getSize().x) * Texture.getSize().x;
        spriteOffset2.x = floor(viewOffset.x / Texture.getSize().x) * Texture.getSize().x;

        Sprite.setPosition(spriteStart - spriteOffset);
                Sprite2.setPosition(spriteStart2-spriteStart - spriteOffset2+spriteOffset);

        Window.clear();
        Window.setView(View);
        Window.draw(Sprite);
                Window.draw(Sprite2);
        Window.display();
    }
}


 




Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on July 09, 2014, 12:30:40 am
I'm not sure what it is that you are attempting to achieve. Are the two tiles supposed to alternate? i.e. red, blue, red, blue, red, blue. If so, it would be easier to just a single texture that held both. The code I posted it based upon the fact that the texture is repeating - or tiled - and that single texture is tiled over the entire window. If you use a single texture to hold both red and blue, the code I posted should work fine.
However, if that's not what you want and you need red and blue to repeat (and at different rates), you should probably use some form of tile map (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#example-tile-map).

That said, if the blue and red are large enough to only ever show two (or three) in the window at once, the code could be adjusted to work (so they alternate) by stopping the texture from repeating and keeping it its original size.

If you need something different, like overlapping and/or parallax scrolling, it would need more work.
Title: Re: Infinitely repeated tile as background sprite
Post by: Bogdan on July 09, 2014, 06:43:02 am
Yes, I'd like them to alternate and being aligned horizontally like the left and right part of a world map.
It would look like this: <--left right left right left right-->
And with three parts <--left middle right left middle right-->
In the end version I would have 20*20 equally sized parts, whereas the parts would alternate only on the x-axis. If you went  to the north or south on the y-axis on such a map, it would be finite.

A single texture is not possible in my case, because I'm working on a huge world map with 32400*32400 pixels, that is cut into 20*20 = 400--> 1620*1620 pixel sized slices. (or even smaller, if it has to be)

In case of using a tilemap, it would have 400 tiles, where every single tile has a different texture and I don't know if a tilemap makes sence here (it't not going to be a tile based game).

As for the window holding only a certain number of textures, it is supposed to zoom theoretically infinitely in and out, so as a consequence the window should be a able to hold many many "map repeats".

Is it still ok to use a tilemap in such case, even though this map will be stored/used only as a large background image and not for calculations or collisions of any kind or do I need only minimal adjustments of the code I've given as example. It's difficult for me to imagine it visually, where to change the "formula". I've been experimenting with different numbers in many ways for some time now , but to no avail. Do I have to only change the setPosition line of sprite2?
Title: Re: Infinitely repeated tile as background sprite
Post by: Bogdan on July 09, 2014, 10:20:21 am
Tilemap didn't work either, because the exe crashes instantly. I suppose my example map of 16200*16200 (half the size of my real map) is too big for him to handle. Is there no way "to learn him" accepting such big image file? This here is altered tutorial code:

#include <SFML/Graphics.hpp>

class TileMap : public sf::Drawable, public sf::Transformable
{
public:

    bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
    {
        if (!m_tileset.loadFromFile(tileset))
            return false;

        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 10);

        for (unsigned int i = 0; i < width; ++i)
            for (unsigned int j = 0; j < height; ++j)
            {
                int tileNumber = tiles[i + j * width];

                int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                sf::Vertex* quad = &m_vertices[(i + j * width) * 10];

                quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }

        return true;
    }

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // apply the transform
        states.transform *= getTransform();

        // apply the tileset texture
        states.texture = &m_tileset;

        // draw the vertex array
        target.draw(m_vertices, states);
    }

    sf::VertexArray m_vertices;
    sf::Texture m_tileset;
};


int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(1200, 900), "Tilemap");

    // define the level with an array of tile indices
    const int level[] =
    {
        0,   2,  3,  4,  5,  6,  7,  8 , 9, 10,
        11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
                31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
                51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
                61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
                71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
                81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
                91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
    };

    TileMap map;
    if (!map.load("world.jpeg", sf::Vector2u(1620, 1620), level, 10, 10))
        return -1;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
                        {
                                if (event.type == sf::Event::Closed)
                                        {
                                        window.close();
                                        }                                                                                                                                      
                        }
        window.clear();
        window.draw(map);
        window.display();
    }

    return 0;
}

 
Title: Re: Infinitely repeated tile as background sprite
Post by: Laurent on July 09, 2014, 12:51:49 pm
A texture of 16200x16200 will be too big for most of today's graphics cards. But your application shouldn't crash, instead it should show an error on the standard output (console) and display nothing.
Title: Re: Infinitely repeated tile as background sprite
Post by: Jesper Juhl on July 09, 2014, 02:22:36 pm
Take a look at Third BigTexture class. Maybe that can help you.
http://www.bromeon.ch/libraries/thor/v2.0/doc/classthor_1_1_big_texture.html
Title: Re: Infinitely repeated tile as background sprite
Post by: Laurent on July 09, 2014, 03:24:10 pm
BigTexture only works with BigSprite. It won't be of any help with a vertex array.
Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on July 09, 2014, 06:02:29 pm
it is supposed to zoom theoretically infinitely in and out
Don't worry about what it should be theoretically be able to do; make it do only what is necessary for the project that it is for. If that project actually requires it to zoom out a large amount, you'll need to dynamically replace the maps with smaller ones as you zoom out (similarly, replace with larger maps as you zoom in). If it actually needs to be able to zoom out forever, it will need to be able to resample them automatically.

The original point of this thread was allow infinite scrolling - not infinite zooming - using a tiling texture. Using multiple textures requires some form of tile map (not necessarily the same code as the tutorial) and zooming requires dynamic replacement.
Title: Re: Infinitely repeated tile as background sprite
Post by: Bogdan on July 10, 2014, 04:21:35 pm
After some days of trial & error, contemplating and analysing, I've found a "sufficient" solution. It's even in some way an infinitely repeating sprite (or more precise: multiple sprites/tiles) and perfect for my prospective game. Here is some exemplaric code (at the moment only covering the right bound) Forgive the "magic" numbers, but I feel better with them than with constants, with which I'm not yet too familiar (only in theory):



#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

int main()
{
        sf::RenderWindow mMainWindow(sf::VideoMode(1200, 900), "Map");
       

        sf::Image image;
        image.loadFromFile("topleft.jpeg");
        sf::Texture texture;
        texture.loadFromImage(image);
        sf::Sprite sprite(texture);
        sprite.setPosition(0, 0);


        sf::Image image2;
        image2.loadFromFile("bottomleft.jpeg");
        sf::Texture texture2;
        texture2.loadFromImage(image2);
        sf::Sprite sprite2(texture2);
        sprite2.setPosition(0, 300);

       
        sf::Image image3;
        image3.loadFromFile("topright.jpeg");
        sf::Texture texture3;
        texture3.loadFromImage(image3);
        sf::Sprite sprite3(texture3);
        sprite3.setPosition(600, 0);


        sf::Image image4;
        image4.loadFromFile("bottomright.jpeg");
        sf::Texture texture4;
        texture4.loadFromImage(image4);
        sf::Sprite sprite4(texture4);
        sprite4.setPosition(600, 300);


        sf::View view(sf::Vector2f(600, 300), sf::Vector2f(1200, 900));
        view.setSize(1200,900);

        sf::RectangleShape rectangle;
        rectangle.setSize(sf::Vector2f(200, 2000));
        rectangle.setFillColor(sf::Color(100,80,100,235));
        rectangle.setPosition(1190, -500);

        sf::FloatRect rightBound = rectangle.getGlobalBounds();


        sf::RectangleShape colliderrectangle;
        colliderrectangle.setSize(sf::Vector2f(20, 20));
        colliderrectangle.setFillColor(sf::Color(255,250,255,235));
        colliderrectangle.setPosition(1160, 450);


        sf::FloatRect collider = colliderrectangle.getGlobalBounds();



while (mMainWindow.isOpen())
        {
                sf::Event event;
                while (mMainWindow.pollEvent(event))
                        {
                        if (event.type == sf::Event::Closed)
                                {
                                mMainWindow.close();
                                }
                            if (event.type == sf::Event::Resized)
                                        {
                                        sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
                                        mMainWindow.setView(sf::View(visibleArea));
                                        }
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right))
                                {
                                        view.move(+150, 0);
                                        colliderrectangle.move(+150,0);
                                }
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right))
                                        if (colliderrectangle.getGlobalBounds().intersects(rightBound))
                                                        {
                                                        cout << "they touch each another" << endl;
                                                        view.setCenter(0,colliderrectangle.getPosition().y-450);
                                                        colliderrectangle.setPosition(100,colliderrectangle.getPosition().y);
                                                        }
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Left))                                                  
                                        {
                                        view.move(-150, 0);
                                        colliderrectangle.move(-150,0);
                                        }
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Up))                                                
                                        {
                                        view.move(0, -150);
                                        colliderrectangle.move(0,-150);
                                        }
                                if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Down))
                                        {
                                        view.move(0, +150);
                                        colliderrectangle.move(0,+150);
                                        }

                }

       

        mMainWindow.setView(view);
        mMainWindow.draw(sprite);
        mMainWindow.draw(sprite2);
        mMainWindow.draw(sprite3);
        mMainWindow.draw(sprite4);
        mMainWindow.draw(rectangle);
        mMainWindow.draw(colliderrectangle);
        mMainWindow.display();
        mMainWindow.clear();

        }

        return 0;
}
Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on July 11, 2014, 01:20:36 am
I don't think I understand your goal.
Your code doesn't repeat anything. The only thing I can see your code doing is resetting the view to the left-side of the "map" when it reaches the right-size of it. If all you wanted is to wrap back to the opposite side when a side is reached, I think it can be done much more simply than this. In fact, the code I posted for a repeating texture should be able to do this if you switch off the...repeatingness. If I remember correctly, the code I posted alters the sprite's position instead of the view's location. That could be easily adapted if that's what is preferred.

I'm not sure why you have a problem with constants. You should look them up, even if it is to just save you changing every instance of "150" when you want them all to be "100"  ;)

Most of your "magic numbers" could be function calls. e.g. sprite.getSize(), mMainWindow.getSize().

Oh, that remind me. Just out of curiosity, could you tell me what the m prefix means in mMainWindow?

I don't know if your final project requires to modify images after loading them before using them as textures but if they don't, it would be simpler to leave out the sf::Image and do the loadFromFile() directly on the sf::Texture.

Your event conditions look messy (to me). I think it would be better to test for the event type being sf::Event::KeyPressed just once and then nest the tests for which code has been pressed inside it, rather than testing the type for each key.

Also, you test exactly the same conditions twice - one straight after the other - so the code could be contained within the first block. However, the second one, which tests for the collision, should probably not be inside the event loop; it can be tested afterwards.

All that said, if you just want the view to reset position, see if you can adapt my code, or let me know because I might actually be up for trying it myself  :P
Title: Re: Infinitely repeated tile as background sprite
Post by: Bogdan on July 11, 2014, 04:55:17 pm
Neither did I know it before :-). But now I know that all I want, is to reset the view, because I need the static coordinates of the map for unit movments....(I've even found a method for units to leave the map to one bound and appear at the other one). I will try your code out again, after having solved my unit removal problem :-) A good  "finite", but "boundless" map filled with sprites will be the next big step....
Ok, I will look deeper into constants
m Präfix means = main function ?

All right, thats a good idea to leave out the "image" step...

Regarding the event conditions, I'm glad for them to be operational at all :-) But I will see what I can do, if I find some proper examples. For now I don't know how to do it differently.
Title: Re: Infinitely repeated tile as background sprite
Post by: Hapax on July 11, 2014, 05:20:52 pm
So, you want the view to the boundary of one side when you exit the opposite side? Just test for position of the view against the map and if it passes that value, set to to the opposite position
e.g. if (viewCenter.x > map.size.x) { viewCenter.x = 0; } if (viewCenter.x < 0) { viewCenter.x = map.size.x; }
Where map.size would be the visual size of the map i.e. number of tiles * tile size.

Regarding the event conditions, I'm glad for them to be operational at all :-) But I will see what I can do, if I find some proper examples. For now I don't know how to do it differently.
if (event.type == sf::Event::KeyPressed)
{
    if (event.key.code == sf::Keyboard::Right)
    {
        // right is pressed
    }
    else if (event.key.code == sf::Keyboard::Left)
    {
        // left is pressed
    }
    // add "else if" for each key
}

p.s. If you're still having problem with things, I think you should make a new thread so that notifications aren't given to the people involved in this one.