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

Author Topic: Bug with texture rendering  (Read 10120 times)

0 Members and 2 Guests are viewing this topic.

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Bug with texture rendering
« 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]

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: Bug with texture rendering
« Reply #1 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Bug with texture rendering
« Reply #2 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.
Laurent Gomila - SFML developer

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #3 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();

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #4 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.

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #5 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);
}

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #6 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?

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Bug with texture rendering
« Reply #7 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.
« Last Edit: May 23, 2013, 04:47:31 am by OniLinkPlus »
I use the latest build of SFML2

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #8 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.

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #9 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();
« Last Edit: May 23, 2013, 05:09:04 am by prchakal »

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Bug with texture rendering
« Reply #10 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.
« Last Edit: May 23, 2013, 05:03:19 am by OniLinkPlus »
I use the latest build of SFML2

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #11 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.

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #12 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bug with texture rendering
« Reply #13 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 for some best practices, and here 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Bug with texture rendering
« Reply #14 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.

 

anything