Hi
I'm at the beginning of learning SFML , i wrote a program to show the character moving around but it moves too fast
i want to control its speed and calculate frame rates per second but i have no idea how to do that
here is my code
#ifdef SFML_STATIC
#pragma comment(lib, "glew.lib")
#pragma comment(lib, "freetype.lib")
#pragma comment(lib, "jpeg.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")
#endif // SFML_STATIC
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
const int Window_Width = 800;
const int Window_Height = 600;
const std::string Window_Name = "My First SFML Game";
int main()
{
sf::RenderWindow window(sf::VideoMode(Window_Width, Window_Height), Window_Name, sf::Style::Close);
window.setPosition(sf::Vector2i(250, 50)); // change position of the window
window.setKeyRepeatEnabled(false);
enum Dirction{ Down, Left, Right, Up };
sf::Vector2i source(1, 0);
sf::Texture pTexture;
sf::Sprite sPlayer;
if (!pTexture.loadFromFile("Player.png")){
std::cout << "Error loading the Texture" << std::endl;
return -1;
}
sPlayer.setTexture(pTexture); //Sprite
sf::Event MyEvent;
while (window.isOpen())
{
while (window.pollEvent(MyEvent))
{
switch (MyEvent.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
source.y = Down;
sPlayer.move(0,1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
source.y = Left;
sPlayer.move(-1, 0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
source.y = Right;
sPlayer.move(1, 0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
source.y = Up;
sPlayer.move(0, -1);
}
source.x++;
if (source.x * 32 >= pTexture.getSize().x)
source.x = 0;
window.clear();
sPlayer.setTextureRect(sf::IntRect(source.x * 32, source.y*32, 32, 32));
window.draw(sPlayer);
window.display();
}
return EXIT_SUCCESS;
}
Thanks