Hello everyone,
I just started learning SFML with the book, "SFML Game Development".
I tried to build the entire example from the first chapter, but I have to questions:
First, my code doesn't work completely. The only thing I'm seeing is a white square. I compared my code a few times with the example code, but I can't seem to find a difference (next to the fact that I declared two variables globally). I checked the forum, but the only thing I can find is that it occurs when you make it leave his scope due to using an function for example, but that doesn't seem to be the problem in my case. So are you able to find it?
(I put the whole code in 1 file in order to copy it more easlily)
#include <SFML/Graphics.hpp>
static const float PlayerSpeed = 100.f;
const sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
class Game{
private:
sf::RenderWindow mWindow;
sf::Texture mTexture;
sf::Sprite mPlayer;
sf::Time TimePerFrame;
sf::CircleShape shape;
bool mIsMovingUp;
bool mIsMovingDown;
bool mIsMovingLeft;
bool mIsMovingRight;
void processEvents();
void update(sf::Time time);
void render();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
public:
Game();
void run();
};
int main()
{
Game game;
game.run();
}
Game::Game()
:mWindow(sf::VideoMode(640, 480), "SFML Application")
{
if (!mTexture.loadFromFile("Eagle.png")){
//Handle loading error
}
mPlayer.setTexture(mTexture);
mPlayer.setPosition(100.f, 100.f);
}
void Game::run() //Here is the fixed time-step code.
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen()){
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame){
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
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 time)
{
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(time.asSeconds() * movement);
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
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::D)
mIsMovingLeft = isPressed;
else if (key == sf::Keyboard::A)
mIsMovingRight = isPressed;
}
Second, I read the part in the book explaining the fixed time-step. I understand why you do it, but what I can't understand is the given code example (see above). Could you explain it to me?
If you don't understand something, just ask.
Many thanks!