Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Romee258

Pages: [1]
1
Graphics / Re: White square problem and fixed time step struggle
« on: May 03, 2016, 08:39:52 pm »
Okay thanks,

I understand why you pass a constant delta time to your update function.

What I only can't grasp yet, is why you count the time the past frame took and subtract this
with the constant time you would like your frame to be?

2
Graphics / Re: White square problem and fixed time step struggle
« on: May 02, 2016, 11:40:56 pm »
Hello again,

I indeed found the problem, it was caused by the global variables. I rewrote a small part of the program (private part of the Game class and the constructor), so that the variables were declared locally.

About the fixed time-step problem: I can't put my finger exactly on the spot of what I don't understand. I basically don't know what happens in that part of the update function at all in order to have a constant deltatime at all. For example, I don't know why you have to perform all those steps in the function and why your not able to simply pass a constant variable for a fixed deltatime without performing all those steps.

Could you maybe answer this question and explain to me what you do and why you do it line by line, in order for me to get the whole picture.

If you are not able, let me know as well, then I will still try to make a more clear question.

Thanks.

3
Graphics / Re: White square problem and fixed time step struggle
« on: May 02, 2016, 12:33:01 am »
I'm experiencing the following:

When the window shows up (840, 480), its white. When I enlarge the window (just using the top right corner button, the white spot stays on the same place with the same measurements and another one shows up in the top left corner). This happens both when using a texture and just making a shape.

But I think I might have found the solution, I'm testing it right now and will let you know if that caused the trouble (it has to do with the global variables).

Thanks!

4
Graphics / Re: White square problem and fixed time step struggle
« on: May 01, 2016, 09:34:55 pm »
Thanks for the answer,

I completely forgot about the article, I will read it immidiately when I'm home.
If I still find myself not grasping it, I will ask a more specific question.

About the white square error, I initially thought that I might be caused due to
not loading the file properly, so I tried exactly the same thing with an circle, made with
sf::CircleShape, but I got exactly the same problem.

So, maybe do you see something else?

5
Graphics / White square problem and fixed time step struggle
« on: May 01, 2016, 07:39:14 pm »
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!

Pages: [1]