SFML community forums

Help => Graphics => Topic started by: Chay Hawk on August 26, 2014, 07:01:40 am

Title: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 26, 2014, 07:01:40 am
Ok so I have a tile editor and I made it so that  you can move the camera with WASD and i noticed that it moves around at a good speed but the more i draw the slower the camera speed gets, it is instantly noticeable the second i start drawing. Im thinking either the tiles im drawing over are either stacking on top of each other or the list that is storing the tiles is getting full really fast and causing problems. I dont feel like spending an hour and a half re formatting code so i'll just put it in google drive. When you draw on the map, just keep drawing, and moving and you'll see it slow down. It's a code blocks project but if you have another IDE you can just take the files out.

https://drive.google.com/file/d/0B2dzHO9C0wPsZHN0S0dkbldVTlU/edit?usp=sharing
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Ixrec on August 26, 2014, 08:09:14 am
If you're having performance problems, use a profiler to find out where your bottlenecks are.  Humans are very bad at guessing the root cause of performance issues.

SFML does not "delete" anything, except in the sense that most of its classes have destructors that follow RAII so they'll clean up after themselves when they go out of scope.  Sprites will certainly not "delete" themselves in any meaningful sense just because you drew over them later.

What I assume you're referring to is "culling," which means not drawing the sprites you know won't be visible in a given frame. SFML does not and cannot do this, as the logic for it will always be application specific.  However, OpenGL typically does some culling internally.  Doing it yourself in the CPU might be better/faster/etc, but that depends heavily on your program, and you'll have to test it.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Nexus on August 26, 2014, 10:06:38 am
Please read this thread (http://en.sfml-dev.org/forums/index.php?topic=5559.0), especially the part about minimal complete examples. It will help you get more answers.

I dont feel like spending an hour and a half re formatting code so i'll just put it in google drive. [...] It's a code blocks project but if you have another IDE you can just take the files out.
Well, most of us don't feel like downloading a whole project and extract files to work in a different IDE -- even less if the problem description is unclear. Since you are asking for help, you have to show some initiative (i.e. spend time) on describing the issue in the most precise way possible, and make it easy for others to test.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 26, 2014, 10:56:49 am
@Ixrec, No I dont mean culling. The sprite im drawing on the grid is  overlapping the other grass sprite and other sprites in general, so I already know what the problem is and I know what needs to be done to solve it but i cant figure out a method to do it as there is no function to delete a sprite or replace it. I'm sure doing that would use up a lot of memory so even just replacing a texture on the current sprite its being drawn to is fine but  I cant figure out how, I've been looking through the documentation and google but I cant seem to find anything. The closest I've come is setTexture but that doesnt seem to work so I don't know what else to do. Replacing the textures when they are drawn over is absolutely necessary for my program, I mean it's obviously causing performance issues so how would i go about doing this?

@Nexus I apologize I will post code and better descriptions from now on.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Stauricus on August 26, 2014, 02:29:37 pm
unless you're constantly creating new sprites, that shouldn't be the cause. simply drawing a sprite on top of another isn't a so expensive process.
and if each one of your tiles is a single sprite, you're probably doing it the wrong way. try a VertexArray (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#example-tile-map).

and i don't understand what you mean by "setTexture doesn't seem to work". it sure does work, i use it all the time  :o
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 26, 2014, 08:02:12 pm
Actually I think i am creating new sprites, here is a snipped of the code thats doing it:

void AddTile(Editor &editor, EditorVars &editorVars, WindowSettings &winSett)
{
    editor.setTile.push_back(editor.tileSprite);
    editor.setTile.push_back(editor.blankGridSprite);
}

void UpdateTileMap(Editor &editor, EditorVars &editorVars, WindowSettings & winSett)
{
    editor.iter = editor.setTile.begin();

    while(editor.iter != editor.setTile.end())
    {
        winSett.window.draw(*editor.iter);
        editor.iter++;
    }
}
 

See it's being pushed back into a list, so what would i do?
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Jesper Juhl on August 26, 2014, 08:04:53 pm
Ehh. Delete sprites from the list?
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 26, 2014, 08:09:20 pm
I actually just was going to say I did that lol, but yeah so I did that and it speeds up as soon as i do, but it's a map editor and the user is going to want to create a map bigger than 10x10 tiles so what do I do? this cant be how tile editors are made, I own RPG Maker VX Ace and you can make upwards of 500 maps each 500x500 tiles wide and tall with no slow down at all, so why am i getting lag when i draw just a few tiles?
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Jesper Juhl on August 26, 2014, 08:14:16 pm
If you shared a complete and minimal example we could run and comment on, that might help.

As for creating your own map editor; why bother? Why not just use Tiled (http://www.mapeditor.org/) or similar?
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 26, 2014, 08:16:25 pm
I would rather make my own tools because then I have complete control over what i'm doing, plus it's a good learning experience. I'll work on getting some code for you to look at.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Jesper Juhl on August 26, 2014, 08:25:13 pm
Some code to read/test would be nice and will make it easier to help you :)
As for tools; sure, I understand the "learning experience" part, but apart from that my life is just too short to do everything myself from first principles - you can, of course, do what you want with your time :)
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 26, 2014, 08:36:49 pm
Yeah, I would rather make my own tools than use someone elses, I actually have used and bought every single 2d game editor/maker out there and they all do not stack up to what I want so I just decided to build my own. But anyways here's some code :) I also attached the tiles i used as well.

#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
    sf::Event event;
    sf::Mouse mouse;
    sf::View scrollScreen;

    list<sf::Sprite> setTile;
    list<sf::Sprite>::iterator iter;

    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
    scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }break;
                case sf::Event::Resized:
                {
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                }
            }
        }
    window.clear(sf::Color::White);

    sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
    sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos);


    sf::Texture blankGridTexture;
    sf::Sprite blankGridSprite;
    if(!blankGridTexture.loadFromFile("Resources/Tiles/Sign.png"))
    {

    }
    blankGridSprite.setTexture(blankGridTexture);
    blankGridSprite.setOrigin(16, 16);

    //Load Grass
    sf::Texture tileTexture;
    sf::Sprite tileSprite;
    if(!tileTexture.loadFromFile("Resources/Tiles/Grass.png"))
    {

    }
    tileSprite.setTexture(tileTexture);
    tileSprite.setOrigin(16, 16);

    sf::Vector2f Grid(30, 30);

    Grid.x = 30;
    Grid.y = 30;

    if(sf::Event::KeyPressed)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            scrollScreen.move(-1, 0);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            scrollScreen.move(1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            scrollScreen.move(0, -1);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            scrollScreen.move(0, 1);
        }
    }

    //Draw Grid
    for(int i = 0; i < Grid.x; i++)
    {
        for(int j = 0; j < Grid.y; j++)
        {
            blankGridSprite.setPosition(j * 32, i * 32); // <- this is causing 1 grass tile and 1 sign tile to appear randomly
            window.draw(blankGridSprite);
        }
    }

    //Set tiles on grid - setMapTileSprite
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        //Get Position of the mouse then divide by 32 and round down to integer, then times it by 32 to set grid.
        tileSprite.setPosition(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32);
        blankGridSprite.setTexture(tileTexture);
    }
    setTile.push_back(tileSprite);

    iter = setTile.begin();

    while(iter != setTile.end())
    {
        window.draw(*iter);
        iter++;
    }


    window.setView(scrollScreen);
    window.draw(tileSprite);
    window.display();
    }

    return 0;
}
 
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Hapax on August 26, 2014, 09:26:43 pm
Instead of tens or hundreds of extra sprites for each tile as you change it while 'drawing', why not change the tile within the ones that your are displaying already?
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 26, 2014, 09:34:37 pm
How do I do that? Thats what i'm trying to do but i cant figure out how.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 26, 2014, 10:11:58 pm
Ok so i was reading on vertex arrays and I think i'll try that. It says you need to have a tilesheet though, so how many tilesheets could you reasonably have?
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Ixrec on August 26, 2014, 10:21:39 pm
There really isn't any sort of limit.  The thing with vertex arrays is that they only work because you draw subsets of a single texture (ie, a spritesheet) many many times in a row.  Since the whole point is you put a lot of related images into one texture, you aren't going to have a huge number of these textures at the end.  For instance, you might have a sprite sheet for all the background tiles, one for the player character, one for each enemy, and so on, and there's nothing wrong with that.  You'd probably run out of things to put in spritesheets long before you had "too many" of them by any standard.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Jesper Juhl on August 26, 2014, 10:23:00 pm
GPUs have a max texture size. That usually bounds the size of your sprite-sheet. 1024x1024 is usually a lower bound you can count on. Newer GPUs have larger limits.
In any case, you can always use multiple textures.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 27, 2014, 12:59:24 am
Ah I see, we'll i'm trying to get them to work now, i'm following the tutorial in the documents. I want to draw lines on my grid so the user can see where they're drawing to, I was thinking of drawing a single vertex in the center of each grid but now that I think about it, maybe a single grid texture drawin with a vertexArray would be better, what do you think?
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Hapax on August 27, 2014, 01:06:32 am
For a grid, you could use a single vertex array.
Set its type to sf::Lines (http://sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#primitive-types) and then each pair of vertices form the end points of a line. You can then just draw lines from one side to the other and not need to do each tile individually, all in a single vertex array.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 27, 2014, 01:34:56 am
Ok cool, I was thinking that same thing as well, but wasn't sure whats better performance wise. Also I'm confused on what to do next on the VertexArray Texture; I have this:

    list<sf::VertexArray> setTile;
    list<sf::VertexArray>::iterator iter;

    sf::VertexArray Square(sf::Quads, 4);

    Square[0].texCoords = sf::Vector2f(0, 0);
    Square[0].texCoords = sf::Vector2f(32, 0);
    Square[0].texCoords = sf::Vector2f(32, 32);
    Square[0].texCoords = sf::Vector2f(0, 32);
 

But I don't know what to put after that, I'm following the tutorial in the docs but i'm just confused as to how to apply it to my current program as i'm not using a class and I dont want to right now.

Full code:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>

using namespace std;

sf::Texture spriteSheetTexture;
sf::Sprite spriteSheet;
//if(!spriteSheetTexture.loadFromFile(""))
//{

//}

void LoadSpriteSheet()
{

}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
    sf::Event event;
    sf::Mouse mouse;
    sf::View scrollScreen;

    list<sf::VertexArray> setTile;
    list<sf::VertexArray>::iterator iter;

    sf::VertexArray Square(sf::Quads, 4);

    Square[0].texCoords = sf::Vector2f(0, 0);
    Square[0].texCoords = sf::Vector2f(32, 0);
    Square[0].texCoords = sf::Vector2f(32, 32);
    Square[0].texCoords = sf::Vector2f(0, 32);


    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
    scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }break;
                case sf::Event::Resized:
                {
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                }
            }
        }
    window.clear(sf::Color::White);

    //Convert Pixels to new screen size
    sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
    sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos);


    sf::Texture blankGridTexture;
    sf::Sprite blankGridSprite;
    if(!blankGridTexture.loadFromFile("Resources/Tiles/Sign.png"))
    {

    }
    blankGridSprite.setTexture(blankGridTexture);
    blankGridSprite.setOrigin(16, 16);

    //Load Grass
    sf::Texture tileTexture;
    sf::Sprite tileSprite;
    if(!tileTexture.loadFromFile("Resources/Tiles/Grass.png"))
    {

    }
    tileSprite.setTexture(tileTexture);
    tileSprite.setOrigin(16, 16);

    sf::Vector2f Grid(30, 30);

    Grid.x = 30;
    Grid.y = 30;

    if(sf::Event::KeyPressed)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            scrollScreen.move(-1, 0);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            scrollScreen.move(1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            scrollScreen.move(0, -1);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            scrollScreen.move(0, 1);
        }
    }

    //Draw Grid
    for(int i = 0; i < Grid.x; i++)
    {
        for(int j = 0; j < Grid.y; j++)
        {
            blankGridSprite.setPosition(j * 32, i * 32); // <- this is causing 1 grass tile and 1 sign tile to appear randomly
            window.draw(blankGridSprite);
        }
    }

    //Set tiles on grid - setMapTileSprite
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        //Get Position of the mouse then divide by 32 and round down to integer, then times it by 32 to set grid.
        tileSprite.setPosition(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32);
        blankGridSprite.setTexture(tileTexture);
    }
    setTile.push_back(Square);

    iter = setTile.begin();

    while(iter != setTile.end())
    {
        window.draw(*iter);
        iter++;
    }

    window.setView(scrollScreen);
    window.draw(Square, &tileTexture);
    window.display();
    }

    return 0;
}
 
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 27, 2014, 02:43:19 pm
Alright I thought this would work but i't didnt, I cant figure out how to get the vertex array to replace the tileSprite since its not a sprite I cant draw it to the screen with the mouse which is what I need to do.

#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>

using namespace std;

sf::Texture spriteSheetTexture;
sf::Sprite spriteSheet;
//if(!spriteSheetTexture.loadFromFile(""))
//{

//}

void LoadSpriteSheet()
{

}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
    sf::Event event;
    sf::Mouse mouse;
    sf::View scrollScreen;

    list<sf::VertexArray> setTile;
    list<sf::VertexArray>::iterator iter;

    sf::VertexArray Square(sf::Quads, 4);
    sf::RenderStates states;

    Square[0].position = sf::Vector2f(0, 0);
    Square[1].position = sf::Vector2f(32, 0);
    Square[2].position = sf::Vector2f(32, 32);
    Square[3].position = sf::Vector2f(0, 32);

    Square[0].texCoords = sf::Vector2f(0, 0);
    Square[0].texCoords = sf::Vector2f(32, 0);
    Square[0].texCoords = sf::Vector2f(32, 32);
    Square[0].texCoords = sf::Vector2f(0, 32);


    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
    scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }break;
                case sf::Event::Resized:
                {
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                }
            }
        }
    window.clear(sf::Color::White);

    //Convert Pixels to new screen size
    sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
    sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos);


    sf::Texture blankGridTexture;
    sf::Sprite blankGridSprite;
    if(!blankGridTexture.loadFromFile("Resources/Tiles/Sign.png"))
    {

    }
    blankGridSprite.setTexture(blankGridTexture);
    blankGridSprite.setOrigin(16, 16);

    //Load Grass
    sf::Texture tileTexture;
    sf::Sprite tileSprite;
    if(!tileTexture.loadFromFile("Resources/Tiles/Grass.png"))
    {

    }
    tileSprite.setTexture(tileTexture);
    tileSprite.setOrigin(16, 16);

    states.texture = &tileTexture;

    sf::Vector2f Grid(30, 30);

    Grid.x = 30;
    Grid.y = 30;

    if(sf::Event::KeyPressed)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            scrollScreen.move(-1, 0);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            scrollScreen.move(1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            scrollScreen.move(0, -1);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            scrollScreen.move(0, 1);
        }
    }

    //Draw Grid
    for(int i = 0; i < Grid.x; i++)
    {
        for(int j = 0; j < Grid.y; j++)
        {
            blankGridSprite.setPosition(j * 32, i * 32); // <- this is causing 1 grass tile and 1 sign tile to appear randomly
            window.draw(blankGridSprite);
        }
    }

    //Set tiles on grid - setMapTileSprite
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        //Get Position of the mouse then divide by 32 and round down to integer, then times it by 32 to set grid.
        tileSprite.setPosition(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32);
        blankGridSprite.setTexture(tileTexture);
    }
    setTile.push_back(Square);

    iter = setTile.begin();

    while(iter != setTile.end())
    {
        window.draw(*iter);
        iter++;
    }

    window.setView(scrollScreen);
    window.draw(Square, states);
    window.display();
    }

    return 0;
}
 
Title: Re: Does SFML delete sprites that are drawn over?
Post by: smguyk on August 27, 2014, 03:14:46 pm
off-topic but here are a few things I'd like to say

- your sf::Event::Resized case doesn't have a break. also the default statement is missing (it's optional but should always be there and if it's left out on purpose there should be a comment about it)

- is there a reason you are creating the textures/sprites and loading the textures in every loop iteration? put it outside. also there's no error handling

- your input checking is weird. also I recommend you don't mix events and real-time input the way you do in that code

- you should arrange that code better. that includes avoiding global sf variables, using classes / member variables & functions, organising code like input & draw calls instead of scattering it throughout the entire program

- in connection with the above point, in general try to code more object oriented especially in a project like yours

- it's always nice to be consistent in variable naming and code indentation, it will make everything much better. also I personally avoid things like using namespace std, just use std::

if anyone wants to object to any of the above feel free to!
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 27, 2014, 03:52:04 pm
"- your sf::Event::Resized case doesn't have a break. also the default statement is missing (it's optional but should always be there and if it's left out on purpose there should be a comment about it)"

Ah, yes thank you for catching that, I always add break and default, but I just threw this together so that i could have an example for them to help me, my actual code looks neat and organized and is in classes.

"- it's always nice to be consistent in variable naming and code indentation, it will make everything much better. also I personally avoid things like using namespace std, just use std:: "

I usually just do using std::cout; etc etc. My code here is pretty sloppy I agree, but like I said I just slapped it together, which is not a good idea but i just did it so I could have a minimal code example to recieve help and once i get what i need working i break it up and neatly integrate it into my programs code, but thank you for your suggestions :)
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 27, 2014, 04:47:53 pm
Ok so how do i push this vertex array back into the list? I'm sorry I keep asking for help every single step of the way, usually I can figure stuff out on my own but i'm not working in console anymore so it's a lot harder to do, but the only way to learn, at least for me is to just do it. Having complete working examples helps me to no end as well, because then i can play around with the code and see how it works but the example in the docs section doesnt push anything back into a vector or list so thats why i'm stuck here.

#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>

using namespace std;

sf::Texture spriteSheetTexture;
sf::Sprite spriteSheet;
//if(!spriteSheetTexture.loadFromFile(""))
//{

//}

void LoadSpriteSheet()
{

}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
    sf::Event event;
    sf::Mouse mouse;
    sf::View scrollScreen;

    list<sf::VertexArray> setTile;
    list<sf::VertexArray>::iterator iter;


    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
    scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }break;
                case sf::Event::Resized:
                {
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                }
            }
        }
    window.clear(sf::Color::White);

    //Convert Pixels to new screen size
    sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
    sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos);

    sf::VertexArray Square(sf::Quads, 4);
    sf::RenderStates states;

    Square[0].position = sf::Vector2f(0, 0);
    Square[1].position = sf::Vector2f(32, 0);
    Square[2].position = sf::Vector2f(32, 32);
    Square[3].position = sf::Vector2f(0, 32);

    Square[0].texCoords = sf::Vector2f(0, 0);
    Square[0].texCoords = sf::Vector2f(32, 0);
    Square[0].texCoords = sf::Vector2f(32, 32);
    Square[0].texCoords = sf::Vector2f(0, 32);


    sf::Texture blankGridTexture;
    sf::Sprite blankGridSprite;
    if(!blankGridTexture.loadFromFile("Resources/Tiles/Sign.png"))
    {

    }
    blankGridSprite.setTexture(blankGridTexture);
    blankGridSprite.setOrigin(0, 0);

    //Load Grass
    sf::Texture tileTexture;
    sf::Sprite tileSprite;
    if(!tileTexture.loadFromFile("Resources/Tiles/Grass.png"))
    {

    }
    tileSprite.setTexture(tileTexture);
    tileSprite.setOrigin(16, 16);

    states.texture = &tileTexture;

    sf::Vector2f Grid(30, 30);

    Grid.x = 30;
    Grid.y = 30;

    if(sf::Event::KeyPressed)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            scrollScreen.move(-1, 0);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            scrollScreen.move(1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            scrollScreen.move(0, -1);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            scrollScreen.move(0, 1);
        }
    }

    //Draw Grid
    for(int i = 0; i < Grid.x; i++)
    {
        for(int j = 0; j < Grid.y; j++)
        {
            blankGridSprite.setPosition(j * 32, i * 32); // <- this is causing 1 grass tile and 1 sign tile to appear randomly
            window.draw(blankGridSprite);
        }
    }

    //Set tiles on grid - setMapTileSprite
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        //Get Position of the mouse then divide by 32 and round down to integer, then times it by 32 to set grid.
        states.transform.translate(sf::Vector2f(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32));
        //tileSprite.setPosition(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32);
        blankGridSprite.setTexture(tileTexture);
    }
    setTile.push_back(Square);

    iter = setTile.begin();
    while(iter != setTile.end())
    {
        window.draw(*iter);
        iter++;
    }

    window.setView(scrollScreen);
    window.draw(Square, states);
    window.display();
    }

    return 0;
}
 
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Hapax on August 27, 2014, 06:27:41 pm
What is the list for? The vertex array can display all the tiles, whatever they are (just make sure that all the different tile types are in the same texture/tilesheet), so you just need one vertex array. Have a look at the TileMap example (http://sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#example-tile-map) in the tutorial. You can see that every group of four vertices in a vertex array will display a textured quad (assuming you've still set the vertex array's primitive type to sf::Quads).
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 27, 2014, 09:40:06 pm
Ok I looked at the example, I copied the code into CB and slowly broke it down to try to make it work for me and I couldnt get it working still, the vertex is not drawing to the grid.

#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>

using namespace std;

sf::Texture spriteSheetTexture;
sf::Sprite spriteSheet;
//if(!spriteSheetTexture.loadFromFile(""))
//{

//}

void LoadSpriteSheet()
{

}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
    sf::Event event;
    sf::Mouse mouse;
    sf::View scrollScreen;

    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
    scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));


    sf::Texture blankGridTexture;
    sf::Sprite blankGridSprite;
    if(!blankGridTexture.loadFromFile("Sign.png"))
    {

    }
    blankGridSprite.setTexture(blankGridTexture);
    blankGridSprite.setOrigin(0, 0);

    //Load Grass
    sf::Texture tileTexture;
    sf::Sprite tileSprite;
    if(!tileTexture.loadFromFile("Outside_A5.png"))
    {

    }
    tileSprite.setTexture(tileTexture);
    tileSprite.setOrigin(16, 16);

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }break;
                case sf::Event::Resized:
                {
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                }
            }
        }
    window.clear(sf::Color::White);

    sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
    sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos);

    sf::VertexArray Square(sf::Quads, 4);
    sf::RenderStates states;

    Square[0].position = sf::Vector2f(0, 0);
    Square[1].position = sf::Vector2f(32, 0);
    Square[2].position = sf::Vector2f(32, 32);
    Square[3].position = sf::Vector2f(0, 32);

    Square[0].texCoords = sf::Vector2f(0, 0);
    Square[0].texCoords = sf::Vector2f(32, 0);
    Square[0].texCoords = sf::Vector2f(32, 32);
    Square[0].texCoords = sf::Vector2f(0, 32);


    states.texture = &tileTexture;

    sf::Vector2f Grid(30, 30);

    if(sf::Event::KeyPressed)
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            scrollScreen.move(-1, 0);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            scrollScreen.move(1, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            scrollScreen.move(0, -1);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            scrollScreen.move(0, 1);
        }
    }

    //Draw Grid
    for(int i = 0; i < Grid.x; i++)
    {
        for(int j = 0; j < Grid.y; j++)
        {
            blankGridSprite.setPosition(j * 32, i * 32); // <- this is causing 1 grass tile and 1 sign tile to appear randomly
            window.draw(blankGridSprite);
        }
    }

    //Set tiles on grid - setMapTileSprite
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        //Get Position of the mouse then divide by 32 and round down to integer, then times it by 32 to set grid.
        states.transform.translate(sf::Vector2f(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32));
    }

    window.setView(scrollScreen);
    window.draw(Square, states);
    window.display();
    }

    return 0;
}
 
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Hapax on August 27, 2014, 11:55:48 pm
    Square[0].position = sf::Vector2f(0, 0);
    Square[1].position = sf::Vector2f(32, 0);
    Square[2].position = sf::Vector2f(32, 32);
    Square[3].position = sf::Vector2f(0, 32);

    Square[0].texCoords = sf::Vector2f(0, 0);
    Square[0].texCoords = sf::Vector2f(32, 0);
    Square[0].texCoords = sf::Vector2f(32, 32);
    Square[0].texCoords = sf::Vector2f(0, 32);
 
I assume the second set of Square[] assignments should also be 0, 1, 2, and 3.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 27, 2014, 11:57:07 pm
Ok i dont think i'm going to use vertex arrays they are just too hard for me right now, also i found out why my program was going slow, it was because i was drawing so many sprites at once. With one click i created 35 sprites and holding it down for 7 seconds i created over 5000 sprites.


EDIT: Yeah I noticed that but changing it did nothing, but thats alright i found a solution to my sprite based tile editor.
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Hapax on August 28, 2014, 12:02:14 am
Vertex arrays are good because they allow you to display multiple things with just one draw() call. They're obviously useful for other reasons too  :P

thats alright i found a solution to my sprite based tile editor.
Are you going to share this solution?
Title: Re: Does SFML delete sprites that are drawn over?
Post by: Chay Hawk on August 28, 2014, 12:11:28 am
Oh i'm sorry i didnt mean solution i meant problem. I'll upgrade to vertex arrays later, for now sprites are good.