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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Iggy

Pages: [1]
1
Graphics / Re: Drawing to a sf::RenderTexture causing memory leak.
« on: May 23, 2015, 10:42:11 am »
What you should take away from my post is that you are probably creating/destroying too many sf::RenderTextures. How you do it is irrelevant. If you create/destroy the object which contains the sf::RenderTexture every frame, it is not much different to what you are doing now. If you are sure that you aren't creating/destroying a lot of sf::RenderTextures repeatedly and are still experiencing a memory leak, then like zsbzsb said, you will need to post a complete and minimal example for us to test.

Thank you, i thought i had to call sf::RenderTexture::create every frame in case window is resized. Now i just call it once each time the window is resized. Memory is stable once again. But i suspect it will leak small amounts each time the window is resized.

PS. You was also right performance has increased lots. Drawing up to 2025 (1920*1080 screen 32px tiles) sprites to the texture and drawing that to the window now takes 1-4ms when it used to take 30-40ms which is amazing.

Thanks again.

2
Graphics / Re: Drawing to a sf::RenderTexture causing memory leak.
« on: May 23, 2015, 10:16:04 am »
As always you need to provide a complete and minimal example that we can all test.

Can you not test the draw function of the render texture? I posted the other code to show im not using pointers. As i said it's that single line (sf::RenderTexture::Draw) thats causing the problem. Not hard to reproduce - I'm just drawing 32x32 sprites to it.

This is probably due to a driver leak. Creating and destroying an sf::RenderTexture every frame is very expensive both performance and memory-wise, an OpenGL context will be created and destroyed along with it. If you do this often enough, your graphics driver will start to leak.

Instead of declaring your sf::RenderTexture as a local variable, try to reuse the same one as you did in the previous frame. Judging by the code you posted, I'm sure this is doable and will even decrease your frame time.

Thanks, but originally the RenderTexture was a member of the class (none local) when i noticed the mem leak, that's when i switched to local variable thinking that the destructor would clean up any memory.

So should i not be using a RenderTexture to draw an entire scene? Works perfect except for the leak.

3
Graphics / Drawing to a sf::RenderTexture causing memory leak.
« on: May 23, 2015, 12:14:50 am »
Hi there, I'm using a render texture to draw a tilemap everything works perfect except for the fact there is a memory leak caused when i draw a sprite to it.

I left my game engine running overnight and when i checked in the morning i had "program stopped working" messagebox. So i started the engine again and watched the task manager.  I found that memory just keeps building up without being released. I narrowed it down to a single line - drawing a sprite to a local render texture.

Here's the code:

//local variable too
sf::RenderTexture RenderTexture;

RenderTexture.create(RenderTarget.getSize().x + m_TileSize.first * 2, RenderTarget.getSize().y + m_TileSize.second * 2);

for (auto Tile : m_Tiles)//std::vector<sf::Sprite> m_Tiles;
{
        if (ScreenSpace.intersects(Tile.getGlobalBounds()))//check if tile is onscreen
        {
                //get current world co-ordinates
                SpriteWorldPos = Tile.getPosition();

                //set sprites position to converted pixel co-ordinates
                Tile.setPosition(sf::Vector2f{ RenderTarget.mapCoordsToPixel(Tile.getPosition()) });

                //draw tile at pixel position
                RenderTexture.draw(Tile);//this line causes memory to leak, no problem when this single line is commented
                //^^^ problem line ^^^

                //set position back at world co-ordinates
                Tile.setPosition(SpriteWorldPos);
        }
}

EDIT: I was originally using SFML 2.2 when i noticed this, i upgraded to 2.3 but same problem.

4
General / Re: Help drawing only onscreen sprites
« on: April 22, 2015, 11:31:58 pm »
I found my problem i was applying a transform elsewhere in code (very silly thing to do). Thanks for the help! Everything working as expected now. Glad i posted here my code looks cleaner now, and more importantly- works.

5
General / Re: Help drawing only onscreen sprites
« on: April 22, 2015, 06:29:23 pm »
Hi, thanks for your reply. I took another look at the documentation, and adapted my code so that the rectangle is constructed with the topleft co-ordinates of the view (or so i think). But still having the same problems.

sf::View Cam = RenderTarget.getView();

sf::FloatRect StreamArea{
        Cam.getCenter().x - (Cam.getSize().x / 2),//i thought this would be rectLeft (minumum x value of rectangle?)
        Cam.getCenter().y - (Cam.getSize().y / 2),//rectTop?
        Cam.getSize().x,//full size of view...
        Cam.getSize().y
};

//loop through rows
for (auto& Row : m_VecTileRows)//std::vector<std::vector<CTile>> m_VecTileRows
{
        for (auto& ColumnTile : Row)
        {
                if (ColumnTile.graphics_shape().getGlobalBounds().intersects(StreamArea))
                {
                        RenderTarget.draw(ColumnTile);
                }
        }
}
 

Sorry if it's something silly that I'm doing within the sf::FloatRect constructor. If it is could someone tell me exactly what im doing wrong? I think i may be misunderstanding the docs.

Thanks again for the replies.

6
General / Re: Input validation
« on: April 21, 2015, 08:36:01 pm »
Could you check the players velocity and if its more or less than zero the player is moving so don't fire? Might be a cleaner solution.

Or set the player state to 'moving' and dont allow firing while in that state.

EG,
void Game::handleInput(sf::Keyboard::Key keypress, bool keyPressed, sf::Clock temp)
{

        if (keypress == sf::Keyboard::Up || keypress == sf::Keyboard::W)
        {
                pMoveUp = keyPressed;
                PlayerMoving = true;
        }
        else if(...)//... rinse repeat

        if (keypress == FIRE)
        {
                if (!PlayerMoving)
                {
                        //player isnt moving allow firing here
                }
        }
}

Don't include the firing button in your 'if else' if you use this.

7
General / Re: Help drawing only onscreen sprites
« on: April 21, 2015, 08:16:35 pm »
Wow that simple? Thanks a lot, been bouncing off that code for a while now haha.

EDIT: It's still not working correct, do i need to convert any of the co-ords? If so which co-ords? Im really confused.

sf::View Camera = RenderTarget.getView();

sf::FloatRect StreamArea{ Camera.getCenter().x, Camera.getCenter().y, Camera.getSize().x, Camera.getSize().y };//same results as just passing the vectors

for (auto& ColumnTile : Row)
{
        if (ColumnTile.graphics_shape().getGlobalBounds().intersects(StreamArea))
        {
                //draw if it's on the screen
                RenderTarget.draw(ColumnTile);

                //count each draw call
                ++NumDraws;
        }
}

Didn't wana bump.

Thanks again.

8
General / Help drawing only onscreen sprites
« on: April 21, 2015, 07:52:25 pm »
Hi there, im having some trouble streaming sprites that are within screen space. For example if i have 5000 sprites in my level but only 500 are within the screen, i only want to draw those 500 and not blindly draw the full 5000.

Here's some code, note that the windows view is always centered on pEngine->get_player().
for (auto& ColumnTile : Row)
{
    //Trying to check if tile is within the screen? or am i wrong
        if (ColumnTile.graphics_shape().getPosition().x > pEngine->get_player()->getPosition().x - (RenderTarget.getSize().x / 2) &&
                ColumnTile.graphics_shape().getPosition().x < pEngine->get_player()->getPosition().x + (RenderTarget.getSize().x / 2) &&
                ColumnTile.graphics_shape().getPosition().y > pEngine->get_player()->getPosition().y - (RenderTarget.getSize().y / 2) &&
                ColumnTile.graphics_shape().getPosition().y < pEngine->get_player()->getPosition().y + (RenderTarget.getSize().y / 2)
                )
        {
                //draw if it's on the screen
                RenderTarget.draw(ColumnTile);
        }
}

The problem is half of the map dissapears (is not drawn), or to many sprites are drawn when they shouldn't be - it just doesn't work. Anyone have any idea why?

Help is much appreciated, thanks.

EDIT: Also the sprites are definatly initialized to the correct position, as they are all drawn perfect without the attempted bounds check.

Pages: [1]
anything