Yes, that's what I need to do, the problem is I don't see any example or explanation of where or how I'm supposed to set it all up? Is it a normal method like my int healthstats? Or is it something a bit more complicated using sf::Time etc? That's what I'm trying to find out, there doesn't seem to be any information in the book, I'll post the source code I'm working from too.
This is the SFML Game Development book code I'm working from, please note, I'm not putting together files at the moment, I'm just trying to get timestep working by itself so ignore the headers.
#include <Book/Game.hpp>
#include <Book/StringHelpers.hpp>
const float Game::PlayerSpeed = 100.f;
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mTexture()
, mPlayer()
, mFont()
, mStatisticsText()
, mStatisticsUpdateTime()
, mStatisticsNumFrames(0)
, mIsMovingUp(false)
, mIsMovingDown(false)
, mIsMovingRight(false)
, mIsMovingLeft(false)
{
if (!mTexture.loadFromFile("Media/Textures/Eagle.png"))
{
// Handle loading error
}
mPlayer.setTexture(mTexture);
mPlayer.setPosition(100.f, 100.f);
mFont.loadFromFile("Media/Sansation.ttf");
mStatisticsText.setFont(mFont);
mStatisticsText.setPosition(5.f, 5.f);
mStatisticsText.setCharacterSize(10);
}
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen())
{
sf::Time elapsedTime = clock.restart();
timeSinceLastUpdate += elapsedTime;
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
updateStatistics(elapsedTime);
render();
}
}
void Game::processEvents()
{
sf::Event event;
while (mWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::update(sf::Time elapsedTime)
{
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= PlayerSpeed;
if (mIsMovingDown)
movement.y += PlayerSpeed;
if (mIsMovingLeft)
movement.x -= PlayerSpeed;
if (mIsMovingRight)
movement.x += PlayerSpeed;
mPlayer.move(movement * elapsedTime.asSeconds());
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.draw(mStatisticsText);
mWindow.display();
}
void Game::updateStatistics(sf::Time elapsedTime)
{
mStatisticsUpdateTime += elapsedTime;
mStatisticsNumFrames += 1;
if (mStatisticsUpdateTime >= sf::seconds(1.0f))
{
mStatisticsText.setString(
"Frames / Second = " + toString(mStatisticsNumFrames) + "\n" +
"Time / Update = " + toString(mStatisticsUpdateTime.asMicroseconds() / mStatisticsNumFrames) + "us");
mStatisticsUpdateTime -= sf::seconds(1.0f);
mStatisticsNumFrames = 0;
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
if (key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S)
mIsMovingDown = isPressed;
else if (key == sf::Keyboard::A)
mIsMovingLeft = isPressed;
else if (key == sf::Keyboard::D)
mIsMovingRight = isPressed;
}
I double checked the headers the code was linked to and there was nothing there that lead me to believe that I missed anything critical to get the code compiling.
Edit: OH LOL! Wait a minute, what's this? It looks like I missed a .hpp file hidden in the folders that's got all the declarations in it, I think this is the right thing to look at, let me know, I didn't think anything of it because it was named the same name but just a different file type.
#ifndef BOOK_GAME_HPP
#define BOOK_GAME_HPP
#include <SFML/Graphics.hpp>
class Game : private sf::NonCopyable
{
public:
Game();
void run();
private:
void processEvents();
void update(sf::Time elapsedTime);
void render();
void updateStatistics(sf::Time elapsedTime);
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
private:
static const float PlayerSpeed;
static const sf::Time TimePerFrame;
sf::RenderWindow mWindow;
sf::Texture mTexture;
sf::Sprite mPlayer;
sf::Font mFont;
sf::Text mStatisticsText;
sf::Time mStatisticsUpdateTime;
std::size_t mStatisticsNumFrames;
bool mIsMovingUp;
bool mIsMovingDown;
bool mIsMovingRight;
bool mIsMovingLeft;
};
#endif // BOOK_GAME_HPP
I should be able to fix it now with this I think.
Edit: Now I'm not so sure looking at it more carefully >_<