#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(640,480,32), "SFML works!");
const float gravity = 0.0002f;
int groundheight=440;
const float moveSpeed = 0.1f,jumpSpeed = 0.2f;
sf::Vector2f velocity(sf::Vector2f(0,0));
sf::RectangleShape rect(sf::Vector2f(20,20));
rect.setPosition(0,0);
rect.setFillColor(sf::Color::Green);
sf::Texture texture;
sf::Texture Block;
bool onGround = false;
if(!texture.loadFromFile("map.png"))
{
}
if(!Block.loadFromFile("block.png"))
{
}
sf::Sprite backround(texture);
backround.setPosition(0,460);
sf::Sprite block(Block);
block.setPosition(300,200);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
velocity.x = moveSpeed;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
velocity.x = -moveSpeed;
}
else{
velocity.x=0;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
if(onGround==true)
{
velocity.y = -jumpSpeed;
}
}
if(rect.getPosition().y + rect.getSize().y < groundheight || velocity.y < 0)
{
velocity.y+=gravity;
onGround=false;
}
if(rect.getGlobalBounds().intersects(backround.getGlobalBounds()))
{
velocity.y=0;
onGround = true;
rect.move(0,-0.01);
}
rect.move(velocity.x,velocity.y);
window.clear();
window.draw(backround);
window.draw(rect);
window.display();
}
return 0;
}
I dont know why but after about 8-10 seconds my game will just slow down to about half the original framerate.Any help would be appreciated. Thanks