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

Author Topic: Does SFML delete sprites that are drawn over?  (Read 11839 times)

0 Members and 1 Guest are viewing this topic.

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Does SFML delete sprites that are drawn over?
« 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

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #1 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Does SFML delete sprites that are drawn over?
« Reply #2 on: August 26, 2014, 10:06:38 am »
Please read this thread, 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #3 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.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #4 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.

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
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #5 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?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #6 on: August 26, 2014, 08:04:53 pm »
Ehh. Delete sprites from the list?

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #7 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?
« Last Edit: August 26, 2014, 08:12:25 pm by Chay Hawk »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #8 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 or similar?

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #9 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.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #10 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 :)

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #11 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;
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Does SFML delete sprites that are drawn over?
« Reply #12 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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #13 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.
« Last Edit: August 26, 2014, 09:58:31 pm by Chay Hawk »

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Does SFML delete sprites that are drawn over?
« Reply #14 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?

 

anything