4
« on: August 17, 2022, 01:11:44 pm »
I removed the vector<Sprite> from this one & implemented them as individual objects and yet still it jitters, so it is not the vector. I cannot even get 1 image to move across without stuttering/jitttering at one point or another at 60FPS.
I tried the method from the book & I also tried the method where you just multiply by good old dt.asSeconds() & still the same. I even tried setting a FloatRectangle to the getGlobalBounds() & then setPosition() and still no go.
#include <iostream>
#include<vector>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace sf;
using namespace std;
float m_SpeedMoving = 600.0f;
int main()
{
//****************** WINDOW ******************
Vector2f resolution;
resolution.x = VideoMode::getDesktopMode().width;
resolution.y = VideoMode::getDesktopMode().height;
VideoMode vm(resolution.x, resolution.y);
RenderWindow window(vm, "TEST", Style::Default);
window.setFramerateLimit(60);
//****************** ******************
Texture texture;
texture.loadFromFile("graphics/Image1.png");
Sprite sprite1;
sprite1.setTexture(texture);
sprite1.setScale(.5, .5);
sprite1.setPosition(0, 400);
Sprite sprite2;
sprite2.setTexture(texture);
sprite2.setScale(.5, .5);
sprite2.setPosition(0, 480);
Sprite sprite3;
sprite3.setTexture(texture);
sprite3.setScale(.5, .5);
sprite3.setPosition(0, 560);
Sprite sprite4;
sprite4.setTexture(texture);
sprite4.setScale(.5, .5);
sprite4.setPosition(0, 640);
Sprite sprite5;
sprite5.setTexture(texture);
sprite5.setScale(.5, .5);
sprite5.setPosition(0, 720);
//FloatRect RectFloatSprite1 = sprite1.getGlobalBounds();
//FloatRect RectFloatSprite2 = sprite2.getGlobalBounds();
//FloatRect RectFloatSprite3 = sprite3.getGlobalBounds();
//FloatRect RectFloatSprite4 = sprite4.getGlobalBounds();
//FloatRect RectFloatSprite5 = sprite5.getGlobalBounds();
//FloatRect RectFloatSprite = sprite.getGlobalBounds();
//****************** TEXT ******************
//****************** CONFIG ******************
Clock clock;
Time timeSinceLastUpdate = Time::Zero;
Time TIME_PER_FRAME = seconds(1.0f / 60.0f);
bool isMovingLeft = false;
bool isMovingRight = false;
while (window.isOpen())
{
//**********************************************************
//******************* KEYBOARD/MOUSE *******************
//**********************************************************
Event event;
//NEEDED, to prevent intial screen blue circle
while (window.pollEvent(event))
{
}
//ESC to exit game
if (Keyboard::isKeyPressed(Keyboard::Escape))
window.close();
////******************* KEYS *******************
//Time dt = clock.restart();
if (Keyboard::isKeyPressed(Keyboard::Left))
{
isMovingLeft = true;
//sprite1.setPosition(sprite1.getPosition().x - m_SpeedMoving * dt.asSeconds(), sprite1.getPosition().y);
//sprite2.setPosition(sprite2.getPosition().x - m_SpeedMoving * dt.asSeconds(), sprite2.getPosition().y);
//sprite3.setPosition(sprite3.getPosition().x - m_SpeedMoving * dt.asSeconds(), sprite3.getPosition().y);
//sprite4.setPosition(sprite4.getPosition().x - m_SpeedMoving * dt.asSeconds(), sprite4.getPosition().y);
//sprite5.setPosition(sprite5.getPosition().x - m_SpeedMoving * dt.asSeconds(), sprite5.getPosition().y);
//RectFloatSprite1.left = RectFloatSprite1.left - m_SpeedMoving * dt.asSeconds();
//RectFloatSprite2.left = RectFloatSprite2.left - m_SpeedMoving * dt.asSeconds();
//RectFloatSprite3.left = RectFloatSprite3.left - m_SpeedMoving * dt.asSeconds();
//RectFloatSprite4.left = RectFloatSprite4.left - m_SpeedMoving * dt.asSeconds();
//RectFloatSprite5.left = RectFloatSprite5.left - m_SpeedMoving * dt.asSeconds();
////RectFloatSprite.left = RectFloatSprite.left - m_SpeedMoving * dt.asSeconds();
//sprite1.setPosition(RectFloatSprite1.left, RectFloatSprite1.top);
//sprite2.setPosition(RectFloatSprite2.left, RectFloatSprite2.top);
//sprite3.setPosition(RectFloatSprite3.left, RectFloatSprite3.top);
//sprite4.setPosition(RectFloatSprite4.left, RectFloatSprite4.top);
//sprite5.setPosition(RectFloatSprite5.left, RectFloatSprite5.top);
//sprite.setPosition(RectFloatSprite.left, RectFloatSprite.top);
}
else
{
isMovingLeft = false;
}
if (Keyboard::isKeyPressed(Keyboard::Right))
{
isMovingRight = true;
//sprite1.setPosition(sprite1.getPosition().x + m_SpeedMoving * dt.asSeconds(), sprite1.getPosition().y);
//sprite2.setPosition(sprite2.getPosition().x + m_SpeedMoving * dt.asSeconds(), sprite2.getPosition().y);
//sprite3.setPosition(sprite3.getPosition().x + m_SpeedMoving * dt.asSeconds(), sprite3.getPosition().y);
//sprite4.setPosition(sprite4.getPosition().x + m_SpeedMoving * dt.asSeconds(), sprite4.getPosition().y);
//sprite5.setPosition(sprite5.getPosition().x + m_SpeedMoving * dt.asSeconds(), sprite5.getPosition().y);
//RectFloatSprite1.left = RectFloatSprite1.left + m_SpeedMoving * dt.asSeconds();
//RectFloatSprite2.left = RectFloatSprite2.left + m_SpeedMoving * dt.asSeconds();
//RectFloatSprite3.left = RectFloatSprite3.left + m_SpeedMoving * dt.asSeconds();
//RectFloatSprite4.left = RectFloatSprite4.left + m_SpeedMoving * dt.asSeconds();
//RectFloatSprite5.left = RectFloatSprite5.left + m_SpeedMoving * dt.asSeconds();
////RectFloatSprite.left = RectFloatSprite.left + m_SpeedMoving * dt.asSeconds();
//sprite1.setPosition(RectFloatSprite1.left, RectFloatSprite1.top);
//sprite2.setPosition(RectFloatSprite2.left, RectFloatSprite2.top);
//sprite3.setPosition(RectFloatSprite3.left, RectFloatSprite3.top);
//sprite4.setPosition(RectFloatSprite4.left, RectFloatSprite4.top);
//sprite5.setPosition(RectFloatSprite5.left, RectFloatSprite5.top);
//sprite.setPosition(RectFloatSprite.left, RectFloatSprite.top);
}
else
{
isMovingRight = false;
}
//**********************************************************
//******************* UPDATE *******************
//**********************************************************
timeSinceLastUpdate += clock.restart();
//Time dt = clock.restart();
//cout << TIME_PER_FRAME.asSeconds() << endl;
while (timeSinceLastUpdate > TIME_PER_FRAME)
{
timeSinceLastUpdate -= TIME_PER_FRAME;
if (isMovingLeft)
{
sprite1.setPosition(sprite1.getPosition().x - m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite1.getPosition().y);
//sprite2.setPosition(sprite2.getPosition().x - m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite2.getPosition().y);
//sprite3.setPosition(sprite3.getPosition().x - m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite3.getPosition().y);
//sprite4.setPosition(sprite4.getPosition().x - m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite4.getPosition().y);
//sprite5.setPosition(sprite5.getPosition().x - m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite5.getPosition().y);
//RectFloatSprite1.left = RectFloatSprite1.left - m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//RectFloatSprite2.left = RectFloatSprite2.left - m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//RectFloatSprite3.left = RectFloatSprite3.left - m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//RectFloatSprite4.left = RectFloatSprite4.left - m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//RectFloatSprite5.left = RectFloatSprite5.left - m_SpeedMoving * TIME_PER_FRAME.asSeconds();
////RectFloatSprite.left = RectFloatSprite.left - m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//sprite1.setPosition(RectFloatSprite1.left, RectFloatSprite1.top);
//sprite2.setPosition(RectFloatSprite2.left, RectFloatSprite2.top);
//sprite3.setPosition(RectFloatSprite3.left, RectFloatSprite3.top);
//sprite4.setPosition(RectFloatSprite4.left, RectFloatSprite4.top);
//sprite5.setPosition(RectFloatSprite5.left, RectFloatSprite5.top);
//sprite.setPosition(RectFloatSprite.left, RectFloatSprite.top);
}
if (isMovingRight)
{
sprite1.setPosition(sprite1.getPosition().x + m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite1.getPosition().y);
//sprite2.setPosition(sprite2.getPosition().x + m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite2.getPosition().y);
//sprite3.setPosition(sprite3.getPosition().x + m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite3.getPosition().y);
//sprite4.setPosition(sprite4.getPosition().x + m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite4.getPosition().y);
//sprite5.setPosition(sprite5.getPosition().x + m_SpeedMoving * TIME_PER_FRAME.asSeconds(), sprite5.getPosition().y);
//RectFloatSprite1.left = RectFloatSprite1.left + m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//RectFloatSprite2.left = RectFloatSprite2.left + m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//RectFloatSprite3.left = RectFloatSprite3.left + m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//RectFloatSprite4.left = RectFloatSprite4.left + m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//RectFloatSprite5.left = RectFloatSprite5.left + m_SpeedMoving * TIME_PER_FRAME.asSeconds();
////RectFloatSprite.left = RectFloatSprite.left + m_SpeedMoving * TIME_PER_FRAME.asSeconds();
//sprite1.setPosition(RectFloatSprite1.left, RectFloatSprite1.top);
//sprite2.setPosition(RectFloatSprite2.left, RectFloatSprite2.top);
//sprite3.setPosition(RectFloatSprite3.left, RectFloatSprite3.top);
//sprite4.setPosition(RectFloatSprite4.left, RectFloatSprite4.top);
//sprite5.setPosition(RectFloatSprite5.left, RectFloatSprite5.top);
//sprite.setPosition(RectFloatSprite.left, RectFloatSprite.top);
}
}
//**********************************************************
//******************* DRAW *******************
//**********************************************************
window.clear();
window.draw(sprite1);
//window.draw(sprite2);
//window.draw(sprite3);
//window.draw(sprite4);
//window.draw(sprite5);
//window.draw(sprite);
window.display();
}
return 0;
}