SFML Vector problem in header file (class)

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef BALL_H
#define BALL_H

#include <SFML/Graphics.hpp>

class Ball
{
public:
	Ball();
protected:
	sf::CircleShape mBallCircle;
	sf::Vector2f directionV = sf::Vector2f(0, 0);
private:
	void directionF();
};

#endif 


on line 12 is the problem. if I comment line 12, I can see my ball standing in the middle of the screen (no movement yet, I'm working on that - trying to make a Pong game). but if I uncomment line 12, I can't see anything anymore. can someone tell me what am I doing wrong? is it wrong coded? is it the compiler's fault? I'm using Visual Studio 2013 and SFML. if you need any more code, ask and I'll post it. thank you in advance.

EDIT: I mean I can see the player (a bar) and I can move it from left to right, so I can see something and I have functionality, but the ball gets "deleted" after I add that vector...
Last edited on
It is basically impossible to know what is going wrong with the code that you posted. We will need to see your actual drawing code to determine what is happening.

Could you please post the following?

- Your main game loop
- The ball's drawing code or better yet the ball's header and source file

With that information we should be able to help guide you on fixing what is going wrong.

Also when you say "I can't see anything anymore" do you mean the ball is no longer on the screen in the middle or the screen?
>Also when you say "I can't see anything anymore" do you mean the ball is no longer on the screen in the middle or the screen?

indeed.

I will post the whole source now:

Main.cpp
1
2
3
4
5
6
7
8
9
#include <Windows.h>

#include "Game.h"

INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
	Game game;
	game.run();
}


Game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef GAME_H
#define GAME_H

#include <SFML/Graphics.hpp>

#include "Player.h"
#include "Ball.h"

class Game : Player, Ball
{
public:
	Game();
	void run();
protected:
	Player mPlayer;
	Ball mBall;
private:
	void processEvents();
	void update();
	void render();
private:
	sf::RenderWindow mWindow;
};

#endif 


Game.cpp (main game loop, events, update and drawing is here)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Game.h"

Game::Game()
: mWindow(sf::VideoMode(800, 600), "Pong", sf::Style::Close)
, mPlayer()
, mBall()
{
	mWindow.setVerticalSyncEnabled(true);
}

void Game::run()
{
	while (mWindow.isOpen())
	{
		processEvents();
		update();
		render();
	}
}

void Game::processEvents()
{
	sf::Event event;
	while (mWindow.pollEvent(event))
	{
		if (event.type == sf::Event::Closed)
			mWindow.close();
	}
}

void Game::update()
{
	// player movement
	sf::Vector2f pMovement(0, 0);

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		pMovement.x -= 5;
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		pMovement.x += 5;

	mPlayerRect.move(pMovement);

	// ball movement
	sf::Vector2f bMovement(0, 0);

	bMovement.x += directionV.x;
	bMovement.y += directionV.y;

	mBallCircle.move(bMovement);
}

void Game::render()
{
	mWindow.clear();
	mWindow.draw(mBallCircle);
	mWindow.draw(mPlayerRect);
	mWindow.display();
}


Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef PLAYER_H
#define PLAYER_H

#include <SFML/Graphics.hpp>

class Player
{
public:
	Player();
protected:
	sf::RectangleShape mPlayerRect;
};

#endif 


Player.cpp
1
2
3
4
5
6
7
8
9
#include "Player.h"

Player::Player()
: mPlayerRect()
{
	mPlayerRect.setFillColor(sf::Color::Blue);
	mPlayerRect.setSize(sf::Vector2f(100, 10));
	mPlayerRect.setPosition(sf::Vector2f(350, 495));
}


Ball.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef BALL_H
#define BALL_H

#include <SFML/Graphics.hpp>

class Ball
{
public:
	Ball();
protected:
	sf::CircleShape mBallCircle;
	sf::Vector2f directionV = sf::Vector2f(0.0f, 0.0f);
private:
	void directionF();
};

#endif 


Ball.cpp (it's obviously not finished... I'm trying to make the logics of the movement of the ball...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Ball.h"

Ball::Ball()
: mBallCircle()
, directionV()
{
	mBallCircle.setFillColor(sf::Color::Red);
	mBallCircle.setRadius(5);
	mBallCircle.setPosition(405, 305);
	
	directionF();
}

void Ball::directionF()
{
	/*srand(time(NULL));
	while (directionV.x == 0)
		directionV.x = rand() % 11 - 5;
	while ((directionV.x * directionV.x / directionV.x + directionV.y * directionV.y / directionV.y) != 5)
		directionV.y = rand() % 11 - 5;*/
	directionV.x = 3;
	directionV.y = 2;
}
if all you do is comment line 12 then you should get a compiler error because directionV is being referenced elsewhere in your code.


To answer your question your ball is not disappearing. The ball is moving so fast of the screen that you cant see it.

here you set directionV.x to be 3 and directionV.y to be 2
1
2
3
4
5
6
7
8
9
10
void Ball::directionF()
{
	/*srand(time(NULL));
	while (directionV.x == 0)
		directionV.x = rand() % 11 - 5;
	while ((directionV.x * directionV.x / directionV.x + directionV.y * directionV.y / directionV.y) != 5)
		directionV.y = rand() % 11 - 5;*/
	directionV.x = 3;
	directionV.y = 2;
}


on every update you move the ball by directionV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Game::update()
{
	// player movement
	sf::Vector2f pMovement(0, 0);

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		pMovement.x -= 5;
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		pMovement.x += 5;

	mPlayerRect.move(pMovement);

	// ball movement
	sf::Vector2f bMovement(0, 0);

	bMovement.x += directionV.x;
	bMovement.y += directionV.y;

	mBallCircle.move(bMovement);
}

Last edited on
Thanks for the additional code :).

First let's go through your code and clean it up a bit and take care of some bad practices in it. Don't worry everyone does some bad practices when they first start out specially when it comes to game development which is a extremely hard subject.


1. This is only a suggestion and isn't need but I would recommend it.

1
2
3
4
5
6
7
8
9
#include <Windows.h>

#include "Game.h"

INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
	Game game;
	game.run();
}


I would recommend you get rid of that very ugly and non cross platform WinMain declaration by linking to sfml-main.lib and sfml-main-d.lib which will allow you to have a simple standard main() like this.

1
2
3
4
5
6
7
#include "Game.h"

int main()
{
	Game game;
	game.run();
}


As stated this looks much cleaner and I don't believe you are using the WINAPI for anything as far as I can see. It also will make your program cross platform which as it sits right now it isn't.

2. This one is a glaring one that you really need to change. In your Game class you are inheiriting from both Ball and Player when there should be absolutly no reason to do so.

class Game : Player, Ball

From just browsing your code it seems like you are inheiriting from those to gain access to mPlayerRect from the Player class and mBallCircle from the Ball class. So you are basically combining all the functionality of the Player, Ball and Game classes all into the Game class which is not how it should be done.

Instead the game class should not inheirit from the Player and Ball classes and instead it should hold a member of each of those classes to represent the player and the ball in the game (Which you already are doing which is the confusing part).

So lets do some changes to your Game class.

The header should look something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// The game class is just mainly used to hold the main game loop
// and manage things that deal with the game as a whole, like managing the window.

class Game // We don't want to inheirit anything.
{
public:
	Game();
	void run();
private:
	void processEvents();
	void update();
	void render();
private:
	sf::RenderWindow mWindow;
        
        // These don't need to be protected since nothing should inheirit from Game I wouldn't think.
        Player mPlayer;
	Ball mBall;
};


Now before I go into the Game.cpp file you are going to need to change somethings about the Player and Ball classes since we aren't inheiriting from then anymore.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Player
{
public:
	Player();

        // We need a way to get access the the below sf::RectangleShape
        // so that we can do it in your drawing loop. So we need a get function
        // for it. There is better ways like inheiriting from sf::Drawable but that
        // is more advanced and beyond this post.
        sf::RectangleShape& getPlayerRect() const { return mPlayerRect; }
protected:
	sf::RectangleShape mPlayerRect;
};


Now the Ball class we need to do the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Ball
{
public:
	Ball();

        // Same as before we need some getters
        sf::CircleShape& getBallCircle() const { return mBallCircle; }
        sf::Vector2f& getDirectionV() const { return directionV; }
protected:
	sf::CircleShape mBallCircle;

        // If you want this to be set to (0.f, 0.f) then I would do it in the constructor
        // instead.
	sf::Vector2f directionV = sf::Vector2f(0.0f, 0.0f);
private:
	void directionF();
};


Now on to your Game.cpp file. It should look like this (Will comment a bit and will go into detail below the code on the changes). I really only changed stuff in the update() and render() methods so that is all I will post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void Game::update()
{
	// player movement
	sf::Vector2f pMovement(0, 0);
        
        // You are not taking into account the delta time but will go into that
        // later.
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		pMovement.x -= 5;
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		pMovement.x += 5;

        // Instead since we are no longer using the inheirited members to
        // store the player data we need to retrieve the playerRect from the
        // player member in our game class to do the movement.
	mPlayer.getPlayerRect().move(pMovement);

	// ball movement
	sf::Vector2f bMovement(0, 0);

        // Same as above we are using the ball member instead so we 
        // call the getter to get access to the vector in the ball class. And then
        // move the ball.
	bMovement.x += mBall.getDirectionV().x;
	bMovement.y += mBall.getDirectionV().y;

	mBall.getBallCircle().move(bMovement);
}

void Game::render()
{
        // The main thing here is the same as above we are using out ball and player
        // members instead now and need to get their visual component to draw
        // instead now.
	mWindow.clear();
	mWindow.draw(mBall.getBallCircle());
	mWindow.draw(mPlayer.getPlayerRect());
	mWindow.display();
}


Now that is all basically for the Game class regarding the inheiritence of Ball and Player. Now it should be more manageable and is more like how it is usually done.

3. - You are going to need to deal with a timestep if you are going to be dealing with movement in your game. This post is already quite long as it is so I won't go into detail about this long subject will post a example of how my main game loops usually look with their timesteps and some articles on the subject.

Here is a simple example of how one of my game loops might look.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f); // 60 frames per second
sf::Clock deltaClock;  
sf::Time timeSinceLastUpdate = sf::Time::Zero;

while (m_Window.isOpen())
{
    sf::Time deltaTime = deltaClock.restart();  
    timeSinceLastUpdate += deltaTime;

    while (timeSinceLastUpdate >= timePerFrame)
    {
        timeSinceLastUpdate -= timePerFrame;

        ProcessInput();
        Update(timePerFrame); 
    }

    Render();
}


By passing timePerFrame to the update() method I can see how much time has pasted since the last update call (Last frame) and move accordingly by doing someEntity.move(moveVector.x * timePerFrame, moveVector.y * timePerFrame);. If I didn't use the timePerFrame and lets say move vector was (5, 0) like in your example the entity would move 5 pixels to the right every frame. So if I was running at 60 FPS I would be moving the entity 300 pixels to the right every second which is really really fast and hard to catch. If the framerate isn't limited to 60FPS you wouldn't even notice it move of screen.

You are actually probably seeing some of this behavior as it is when you press the movement keys in your game. Your player will be moving at a extremely high rate of speed, and so will your Ball like Yanson mentioned.

Anyways here is a nice article on the subject http://gafferongames.com/game-physics/fix-your-timestep/

But with that I am running out of characters for this long ass post ;p. So I will stop here try making those changes and see what it does and if it helps solve your problem or makes it easier for you to solve it. If you still have questions feel free to keep posting and I would be glad to help with another wall of text (Joking joking ;p).
Last edited on
oh, man, Z e r e o, you really took your time, man! ^_^ I appreciate that and it really helped me understand and solve some questions in my head. I'll put it to work later and see if it all works well ^_^ thank you very much, I'll feedback the results; but I'm sure you already compiled the code and saw it worked xD thank you!

EDIT: also the int main() thing... I didn't know that xD hehe thanks again!

EDIT2:hmm... it won't let me keep the refference operator there...
1
2
3
sf::RectangleShape& getPlayerRect() const { return mPlayerRect; }
sf::CircleShape& getBallCircle() const { return mBallCircle; }
sf::Vector2f& getDirectionV() const { return directionV; }

I had to get rid of the "const" so that the player (and ball, of course) could move. aaand I still couldn't see the ball... but I'll keep the code like that, without the "const" and see what can I do with these "timesteps"; maybe, eventually, I'll be able to see the ball xD

EDIT3: oh, forgot to tell you about this: I activated V-Sync. isn't that supposed to fix the FPS? because I guess it is fixed since I can move the player at a normal rate. I guess it runs at ~60 FPS. so, maybe that isn't the problem of the ball not being seen

PS: I love walls of text :P so don't retain.
Last edited on
EDIT2:hmm... it won't let me keep the refference operator there...
1
2
3
sf::RectangleShape& getPlayerRect() const { return mPlayerRect; }
sf::CircleShape& getBallCircle() const { return mBallCircle; }
sf::Vector2f& getDirectionV() const { return directionV; }


I had to get rid of the "const" so that the player (and ball, of course) could move. aaand I still couldn't see the ball... but I'll keep the code like that, without the "const" and see what can I do with these "timesteps"; maybe, eventually, I'll be able to see the ball xD


Opps sorry about that at work and don't have a compiler with me and missed that there (Old habbits with const in getters). You are right in this case you don't want the const because of the reference which is returned.

And yes the ball will still be not visible on the screen since you are moving the ball at a very high rate of speed like Yanson mentioned so it will be far off the screen by the time you see your window. This is where the timestep for you application comes into play.

EDIT3: oh, forgot to tell you about this: I activated V-Sync. isn't that supposed to fix the FPS? because I guess it is fixed since I can move the player at a normal rate. I guess it runs at ~60 FPS. so, maybe that isn't the problem of the ball not being seen


Even if V-Sync is activated you will still be moving at a very high rate of speed because you update calls are being called probably 100's of times per second and each update call moves the ball 3 pixels to the left and 2 pixels downward. So lets say that you are calling update() 400 times per second (which you are probably calling it more then that most likely). This means that every 2.5 milliseconds you are moving the ball (3, 2) pixels.

This is why we need to have a timestep and need to see how much time has past since the last frame and use that in our movement of objects in our games.

So how do we do this? Well it all starts in the main game loop. I will take a snippet of your run() method which is your main game loop and alter it so that you can pass down the delta time (Time since last frame) to use with your ball movement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void Game::run()
{
        // We will use this clock to get the time past since the last frame.
        // When we call restart on this clock it will return the time since the
        // last restart() call was made which we will use as the delta time.
        sf::Clock deltaClock;

        // This variable will be used so we can add up the amount of time
        // since we last called our update() function. We do this because we
        // don't want to call our update() function every time through the main 
        // game loop. Instead we only want to call it 60 times per second at most 
        // and we want the timestep (The time that we pass down to use with movement)
        // to be the same each time.
        sf::Time timeSinceLastUpdate = sf::Time::Zero;

	while (mWindow.isOpen())
	{
                // As mentioned this will give us the time since the last restart
                // was called.
                sf::Time deltaTime = deltaClock.restart();

                // We then add the deltaTime to the timeSinceLastUpdate variable.
                timeSinceLastUpdate += deltaTime;

                // Only if timeSinceLastUpdate is greater equal to mTimePerFrame
                // do we enter the update loop.
                // mTimePerFrame is declared in the header as a member and is 
                // initialized in the constructor like this.
                // mTimePerFrame = sf::seconds(1.0f/60.0f);
                // for 60FPS
                while (timeSinceLastUpdate >= mTimePerFrame)
                {
                        // First thing we do is subtract the mTimePerFrame 
                        // from the timeSinceLastUpdate since we are running a update
                        // We then proceed to process events and update.
                        processEvents();

                        // Here is the key part we pass in to the update function
                        // the timeSinceLastUpdate variable to be used with
                        // the movement and other things that need the timestep.
                        update(timeSinceLastUpdate);
                        
                }

                // Render gets called every time through the game loop.
		render();
	}
}


You will also need to alter the update() method in the header to accepts a const sf::Time& like this.

void update(const sf::Time& deltaTime);

Now that we got the main game loop down and we are passing the timestep into the update() function we can move onto our movement code in the update function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Game::update(const sf::Time& deltaTime)
{
	// player movement
	sf::Vector2f pMovement(0, 0);
        
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		pMovement.x -= 5;
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		pMovement.x += 5;
        
        // We need to multiply our pMovement vector by our timestep.
        // I will explain what I am doing here below.
	mPlayer.getPlayerRect().move(pMovement.x * deltaTime.asSeconds(), pMovement.y * deltaTime.asSeconds());

	// ball movement
	sf::Vector2f bMovement(0, 0);

	bMovement.x += mBall.getDirectionV().x;
	bMovement.y += mBall.getDirectionV().y;

        // Same as above
	mBall.getBallCircle().move(bMovement.x * deltaTime.asSeconds(), bMovement.y * deltaTime.asSeconds());
}


Now this was pretty easy all we needed to change was a few lines and that is it. Though I will explain why we need to change those lines. As I stated before we are updating a lot of times per second and we need to take that into account when we are doing our movement.

In this case the deltaTime.asSeconds() will be equal to 0.0166 or in other words about 16ms. So on average the update() function will be called every 16ms or 60 times per second.

So if we want to lets say move 3 pixels to the right every second we would have to move 0.05 pixels every update call (At 60 update calls per second).

This is exactly what multiplying the movement by the mTimePerFrame is doing.

3 * deltaTime.asSeconds() = 0.05
Or in other words
3 * 0.0166 = 0.05

In fact this is what we are doing right here
bMovement.x * deltaTime.asSeconds()

Since bMovement.x = 3.

Again the link above is a good article on timesteps that should help explain this a bit more in depth then I can here (Though it is a bit more advanced).

I would also like to mention that you don't have to have a fixed timestep like I did above you can just pass sf::Time deltaTime directly to the update() function to use as your timestep but I personally would recommend a fixed timestep like above.

Hopefully this helps make sense of this subject a bit more for you, sometimes I am not the greatest at explaining things so if you do have any questions on what I said or anything please feel free to ask.
Thank you for your time and effort to explain this to me. ^_^ I'll try implementing it and see if it works as intended. I'll update the reply later and let you know.

EDIT: can't we put
 
timeSinceLastUpdate += deltaClock.restart();

instead of
1
2
sf::Time deltaTime = deltaClock.restart();
timeSinceLastUpdate += deltaTime;

?
Last edited on
Thank you for your time and effort to explain this to me. ^_^ I'll try implementing it and see if it works as intended. I'll update the reply later and let you know.


No problem at all man :), game dev is something I love and am always willing to chat about it ;p.

EDIT: can't we put

timeSinceLastUpdate += deltaClock.restart();

instead of

1
2
sf::Time deltaTime = deltaClock.restart();
timeSinceLastUpdate += deltaTime;


Yes that way would be better actually.
hmm I changed it a bit (and yes, I understood everything ^_^) but now the game freezes I think, because I can't move the player anymore and after I close the game it remains into debugging mode... strange

added/changed these 2 lines in Game.h
1
2
void update(const sf::Time& deltaTime);
sf::Time mTimePerFrame;

just as you taught me

and in Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Game::run()
{
	sf::Clock deltaClock;
	sf::Time timeSinceLastUpdate = sf::Time::Zero;

	while (mWindow.isOpen())
	{
		timeSinceLastUpdate += deltaClock.restart();

		while (timeSinceLastUpdate >= mTimePerFrame)
		{
			processEvents();
			update(timeSinceLastUpdate);
		}

		render();
	}
}

and
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Game::update(const sf::Time& deltaTime)
{
	// player movement
	sf::Vector2f pMovement(0, 0);

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		pMovement.x -= 5;
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		pMovement.x += 5;

	mPlayer.getPlayerRect().move(pMovement.x * deltaTime.asSeconds(), pMovement.y * deltaTime.asSeconds());

	// ball movement
	sf::Vector2f bMovement(0, 0);

	bMovement.x += mBall.getDirectionV().x;
	bMovement.y += mBall.getDirectionV().y;

	mBall.getBallCircle().move(bMovement.x * deltaTime.asSeconds(), bMovement.y * deltaTime.asSeconds());
}

I hope I'm not doing anything wrong, that's why I pasted it here...

also
1
2
3
4
5
6
7
8
9
Game::Game()
: mWindow(sf::VideoMode(800, 600), "Pong", sf::Style::Close)
, mTimePerFrame()
, mPlayer()
, mBall()
{
	//mWindow.setVerticalSyncEnabled(true);
	mTimePerFrame = sf::seconds(1.0f / 60.0f);
}


EDIT:
I think I can get rid of the "mPlayer()" and "mBall()", right? because I'm not using them in the constructor anyway.
Last edited on
EDIT:
I think I can get rid of the "mPlayer()" and "mBall()", right? because I'm not using them in the constructor anyway.


Yup you don't need them there. Their default constructors will be called automatically. The only time you would need to have them in the initializer list or constructor is if you wished to use something other then the default constructor or if it was a primitive type like a integer, or if it doesn't have a default constructor.

hmm I changed it a bit (and yes, I understood everything ^_^) but now the game freezes I think, because I can't move the player anymore and after I close the game it remains into debugging mode... strange


Hmm I don't see anything wrong that really sticks out at me (Though I could be missing something obvious) and sadly am not by a compiler/debugger at the moment so can't dig into it.

Though if the player square isn't responding to the movement keys it sounds like it might be a input processing problem. Is the ball moving or visible on the screen?

Also when you say it stays in debug mode do you mean your IDE continues debugging after you hit the X in the top right corner and the window closes?

EDIT: Also the movement is quite low so it might not look like it is not moving unless you hold down the movement a long time (We are only moving 5 pixels a second for the player) so you might want to try bumping that up to maybe 100-400 a see how that works depending how fast you want it. To put 5 pixels a second in perspective it would take over 2 minutes to move from 1 side of the screen to the other ;p.
Last edited on
indeed, after I close the game (X button) the compiler continues debugging and I have to stop it manually.

also I set the player speed to 50 instead of 5 and still no changes. the player is stuck xD

and the ball is still invisible...
Last edited on
Whoop knew I would miss something

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Game::run()
{
	sf::Clock deltaClock;
	sf::Time timeSinceLastUpdate = sf::Time::Zero;

	while (mWindow.isOpen())
	{
		timeSinceLastUpdate += deltaClock.restart();

		while (timeSinceLastUpdate >= mTimePerFrame)
		{
                        // You forgot to subtract mTimePerFrame from timeSinceLastUpdate here

                        // So for example timeSinceLastUpdate -= mTimePerFrame

			processEvents();
			update(timeSinceLastUpdate);
		}

		render();
	}
}


The above will cause it to be stuck in a infinite loop in the update loop and nothing new will be rendered after the first frame.

EDIT: Also just noticed I didn't include that in my above example so that would be why. Sorry about that :(. Should work fine now with the player responding to input and drawing. Also with luck the ball should be moving on the screen also.
Last edited on
oh, now it works! :D I also had to turn V-Sync back on and raise the player speed to 500 to be ok. but still... the ball simply isn't there :-s

EDIT: hmm and from time to time the movement skips a couple of pixels... it's choppy at times. I'll try to figure it out
Last edited on
Nice! Glad you got it working :). Now just need to figure out the problem with the ball.

Great work with the code btw. To me personally it is much easier to follow now and looks quite good.

Though just getting off work in a couple minutes then hopefully will be home in a couple of hours and will have access to my compilers and debuggers. So if you don't get it working by then I will try setting up the project on my end to see if I can't help debug it a bit.

Sorry I couldn't be more help with the ball not showing up issue :(.
Last edited on
hehe not a problem! ^_^ you helped me a ton until now and I'm really glad and appreciate it! I rarely meet someone that dedicated to solve someone else's problems. it's nice of you. thank you again
Topic archived. No new replies allowed.