SFML community forums

Help => Graphics => Topic started by: prchakal on May 22, 2013, 07:58:33 pm

Title: Bug with texture rendering
Post by: prchakal on May 22, 2013, 07:58:33 pm
Hi,

Im building a game and when i create all in the same class, everything is working fine, and after it works, i separate it into classes and refactored the code.

After refactor, my player class have a texture attribute that store the texture and the engine draw the animated sprite.

So the pseudo-code is:

Quote
Player
  - AnimatedSprite *sprite
  - sf::Texture *texture

Engine
  - player->update(sf::Time delta)
  - player->draw() or window->draw(*player->sprite)   // both code i get the same problem

Can anyone try help me? I think that is a small problem, because it is moving correctly and everything is working, only the rendering problem while player draw.

Screenshot is attached with result.

Thanks.

[attachment deleted by admin]
Title: Re: Bug with texture rendering
Post by: Grimshaw on May 22, 2013, 08:37:20 pm
That seems like GPU memory garbage perhaps.. Are you sure you are instancing the texture properly and setting it correctly to the sprite? That could be the cause.
Title: Re: Bug with texture rendering
Post by: Laurent on May 22, 2013, 08:49:01 pm
Can you show the constructor(s), destructor and assignment operator (if any) of your Player class? And can you show how you handle Player instance(s)?

A good guess is that your destructor does "delete texture", but you have no explicit copy constructor and yet you copy Player instances.
Title: Re: Bug with texture rendering
Post by: prchakal on May 22, 2013, 08:50:23 pm
Im using:

Quote
Player constructor:

width  = 40;
height = 56;

texture = new sf::Texture();
texture->loadFromFile(resourcePath() + "images/player1.png");

walkingAnimation = new Animation();
walkingAnimation->setSpriteSheet(*texture);
walkingAnimation->addFrame(sf::IntRect(0, 2*height, width, height));
walkingAnimation->addFrame(sf::IntRect(1*width, 2*height, width, height));
walkingAnimation->addFrame(sf::IntRect(2*width, 2*height, width, height));
walkingAnimation->addFrame(sf::IntRect(3*width, 2*height, width, height));

stoppedAnimation = new Animation();
stoppedAnimation->setSpriteSheet(*texture);
stoppedAnimation->addFrame(sf::IntRect(0, 2*height, width, height));

jumpingAnimation = new Animation();
jumpingAnimation->setSpriteSheet(*texture);
jumpingAnimation->addFrame(sf::IntRect(0, 2*height, width, height));

sprite = new AnimatedSprite(sf::seconds(0.2));
sprite->setAnimation(*stoppedAnimation);
//sprite->setColor(sf::Color::Red);
sprite->setOrigin(width/2.f,height/2.f);

createEntityPhysics(*GameObjects::world, 50, 50, width, height);


Engine:
$this->player = new Player();
Title: Re: Bug with texture rendering
Post by: prchakal on May 22, 2013, 09:01:11 pm
Based on that code, i think that everything is ok.

I read and re-read it, analyse and i dont find the problem.
Title: Re: Bug with texture rendering
Post by: prchakal on May 22, 2013, 09:03:23 pm
My simple engine code:

Quote
Engine::Engine()
{
    Util::log("Engine::Engine");
    resetRandom();
    setupScreen();
    setupBackgroundColor();

    showSampleContent = true;
}

Engine::~Engine()
{
    Util::log("Engine::~Engine");
}

void Engine::run()
{
    Util::log("Engine::run");

    GameObjects::gravity = new b2Vec2(0.f, 9.8f);
    GameObjects::world   = new b2World(*GameObjects::gravity);

    player = new Player();

    initializeGraphics();

    sf::Clock frameClock;

    while (GameObjects::window->isOpen())
    {
        sf::Time delta = frameClock.restart();

        GameObjects::world->Step(1/60.f, 8, 3);

        checkEvents();

        GameObjects::window->clear(backgroundColor);

        player->update(delta);
        player->draw();

        GameObjects::window->display();

        sf::sleep(sf::milliseconds(1));
    }
}

void Engine::initializeGraphics()
{
    Util::log("Engine::initializeGraphics");

    sf::ContextSettings settings;
    settings.depthBits = 0;
    settings.stencilBits = 0;
    settings.antialiasingLevel = 0;

    GameObjects::window = new sf::RenderWindow(sf::VideoMode(screenWidth, screenHeight, screenBitsPerPixel), screenTitle, sf::Style::Close, settings);
    GameObjects::window->setVerticalSyncEnabled(true);
}

void Engine::checkEvents()
{
    sf::Event Event;

    while (GameObjects::window->pollEvent(Event))
    {
        if (Event.type == sf::Event::Closed)
        {
            GameObjects::window->close();
            return;
        }

        player->updateEvents(Event);
    }

    player->updateEvents(Event);
}

void Engine::resetRandom()
{
    srand(time(NULL));
}

void Engine::setupScreen()
{
    screenWidth        = 800;
    screenHeight       = 600;
    screenBitsPerPixel = 32;
    screenTitle        = "Magic And Zombie";
}

void Engine::setupBackgroundColor()
{
    backgroundColor = sf::Color(0, 0, 0);
}
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 04:24:09 am
Today i post all the source in a temp github repository to share the problem. I think that it will help everyone:

https://github.com/prsolucoes/sfml-cmake-temp

Can anyone help me?
Title: Re: Bug with texture rendering
Post by: OniLinkPlus on May 23, 2013, 04:45:19 am
NOBODY is going to read all of that code, and nobody is going to help you unless you post a complete and minimal source code that demonstrates your problem.

However, I did take a brief look at some of your code, and noticed a rather large problem. You are using new and delete for a large number of variables. You should never use new and delete. Ever. And in the very rare case you do need new/delete, you should use smart pointers, NOT raw pointers.
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 04:52:10 am
If you read the other posts, YOU WILL SEE THAT I ALREADY PUT A LITTLE PIECE OF CODE!

I only put the full source to complete if anyone need.

I'm asking for help as a favor, I'm not forcing anyone to do anything.

Ah...and i dont use DELETE.... i dont know where you see it.
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 04:59:09 am
I solve the problem. Thanks.

The problem was solved putting b2BodyDef object as the entity/player attributes.

On header:
b2BodyDef bodyDef;

On impl:
bodyDef = b2BodyDef();
Title: Re: Bug with texture rendering
Post by: OniLinkPlus on May 23, 2013, 05:01:48 am
If you read the other posts, YOU WILL SEE THAT I ALREADY PUT A LITTLE PIECE OF CODE!

I only put the full source to complete if anyone need.
Like I said, we need a COMPLETE and MINIMAL code. This means source code compressed into one single file that can be compiled on its own with every single line irrelevant to your problem removed. This is standard practice on this forum, and you have so much code that it's hard to know where to begin to look.
Quote
I'm asking for help as a favor, I'm not forcing anyone to do anything.
We know this, but we can't help you unless you post complete and minimal code.
Quote
Ah...and i dont use DELETE.... i dont know where you see it.
This is exactly why new and delete are considered no-nos. Whenever you use new, you MUST use delete. Otherwise, your computer will suffer from memory leaks, and if enough memory is leaked, your system will become highly unstable. Not to mention it's damn near impossible to have an instance of delete for every instance of new, especially if the program crashes due to a bug.
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 05:14:42 am
I know about the leaks, i will put delete after in all objects when it works ok.

But thanks. Your help and informations are great.

Problem solved for now.

Thanks for everyone.
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 05:29:26 am
I got the same bug, but sometimes happen and sometimes work nice.

After make some tests, i change other thing:

body->SetUserData(this);
to
body->SetUserData((void*)this);

When create the player physic object.

But the problem still.

Thanks.
Title: Re: Bug with texture rendering
Post by: Nexus on May 23, 2013, 06:16:59 am
I know about the leaks, i will put delete after in all objects when it works ok.
No, don't do that. Go away from manual new and delete and use RAII instead. See here (http://klmr.me/slides/modern-cpp.pdf) for some best practices, and here (http://en.sfml-dev.org/forums/index.php?topic=9359.0) for the reasons why you should use RAII.

I got the same bug, but sometimes happen and sometimes work nice.
As OniLinkPlus stated: If you want help, come up with a minimal and complete example (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368).
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 07:09:02 am
I change almost everything to not pointer and problem happen too.

I only dont change from point the b2Body, because box2d return a pointer here.
I only dont change from my Player *player, because i dont know how to make a constructor like that.
Title: Re: Bug with texture rendering
Post by: FRex on May 23, 2013, 07:17:05 am
Quote
Go away from manual new and delete and use RAII instead. See here for some best practices, and here for the reasons why you should use RAII.
You should have had put that into your signature by now, unless you ran out of characters, like I did. :P
Also you'll have hard time convincing people who go to 'prestigious' unis.


body->SetUserData((void*)this);
 
Why are you doing it, do you need that pointer for something? Also, how does player draw/entire player class look like? Your github link is invalid. etc.
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 07:32:05 am
I think that this engine is with bug.

Sometimes it works nice and after some minutes all the sprites is like that screenshot, bugged. I try open the program more times and everything works ok.
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 07:52:01 am
After make the two versions of project. With and without pointer and no make difference, i put on github the project with pointer, this is not the problem.
Title: Re: Bug with texture rendering
Post by: OniLinkPlus on May 23, 2013, 07:55:39 am
I think that this engine is with bug.

Sometimes it works nice and after some minutes all the sprites is like that screenshot, bugged. I try open the program more times and everything works ok.
You may be suffering from a Schrodinbug. I am so very sorry.

No, I don't think anybody here claimed pointers were causing the bug. They are, however, extremely dangerous, and we wanted to help save you from the headaches they will cause down the road.

Again, we cannot help you further unless you create a complete and minimal code that demonstrates the bug you are experiencing.

On a purely speculative note, have you made sure your video drivers are up to date?
Title: Re: Bug with texture rendering
Post by: prchakal on May 23, 2013, 07:59:19 am
I think everything is ok.

Im using mac osx 10.8 all updated.