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
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();
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);
}
If you read the other posts, YOU WILL SEE THAT I ALREADY PUT A LITTLE PIECE OF CODE!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.
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.We know this, but we can't help you unless you post complete and minimal code.
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.
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).
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
I think that this engine is with bug.You may be suffering from a Schrodinbug. I am so very sorry.
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.