Unfortunately, it doesn't solve the problem. I used Seconds, but I couldn't even move the paddle with it, that's why I used Microseconds.
Entire code? Here it is.
shapes.h
#ifndef shapes_h
#define shapes_h
#include <SFML/Graphics.hpp>
class Paddle
{
private:
int x, y;
int width, height;
float speed;
sf::RectangleShape rect;
protected:
sf::RectangleShape& get();
public:
Paddle(int nx, int ny, int nwidth, int nheight, float nspeed);
~Paddle();
void init();
void draw(sf::RenderWindow& win);
void move(float);
};
#endif
shapes.cpp
#include "shapes.h"
Paddle::Paddle(int nx, int ny, int nwidth, int nheight, float nspeed) { x=nx; y=ny; width=nwidth; height=nheight, speed=nspeed; }
Paddle::~Paddle() { }
sf::RectangleShape& Paddle::get()
{
return rect;
}
void Paddle::init()
{
rect.setPosition(x, y);
rect.setSize(sf::Vector2f(width,height));
rect.setFillColor(sf::Color(255,255,255));
}
void Paddle::draw(sf::RenderWindow& win)
{
win.draw(rect);
}
void Paddle::move(float elapsed)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) get().move(speed * elapsed * -1, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) get().move(speed * elapsed, 0);
}
main.cpp
#include <SFML\Graphics.hpp>
#include "shapes.h"
using namespace sf;
int main()
{
int w=800, h=600;
RenderWindow window(VideoMode(w,h), "Breakout");
window.setVerticalSyncEnabled(true);
Clock clock;
Time time = clock.getElapsedTime();
float elapsed = time.asMicroseconds();
Paddle pad((w-100)/2,(h-20-5),100,20,5);
pad.init();
while (window.isOpen()) {
Event evt;
while (window.pollEvent(evt)) {
if ((Keyboard::isKeyPressed(Keyboard::Escape)) || (evt.type == Event::Closed)) { window.close(); }
pad.move(elapsed);
clock.restart();
}
window.clear(Color::Black);
pad.draw(window);
window.display();
}
return 0;
}