Using SFML 2.1, Code::Blocks 12.11So far SFML seems really nice. However, I have an issue in this little practice code, it's probably something obvious that I'm not seeing... anyway, here's the code, I'll explain it after:
int main()
{
const int WINDOW_WIDTH = 960;
const int WINDOW_HEIGHT = 540;
// Removed code, Renders "Window", with WIDTH/HEIGHT
Window.setVerticalSyncEnabled(true); // 60 fps
sf::Texture brickTexture;
sf::Sprite brickImage;
// Removed code, just loads the brick.png, a 60x40 image
brickImage.setTexture(brickTexture);
brickImage.setOrigin(brickImage.getLocalBounds().width/2.0, brickImage.getLocalBounds().height/2.0); // Sets sprite origin at (30, 20)
brickImage.setPosition(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
// Important code
bool freefall = true;
int i = 1;
while (Window.isOpen())
{
sf::Event event;
while(Window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
{
brickImage.setPosition(event.mouseButton.x, event.mouseButton.y);
freefall = true;
i = 1;
}
break;
}
}
if (brickImage.getPosition().y >= (WINDOW_HEIGHT - WINDOW_HEIGHT/10.0))
freefall = false; // The position at which it stops falling is not constant...
else if (freefall == true)
{
brickImage.move(0, i);
i++;
}
Window.clear(); // Removed for the screenshot
Window.draw(brickImage);
Window.display();
}
}
If you want compilable code, see:
http://pastebin.com/cjhYfAneNote that you'll need an image named brick.png to go with it (dimensions don't matter)
Problem:Basically, this code is making a rectangle brick accelerate as it falls. I want the brick to stop falling at the same height on the screen each time (in this case, 9/10-ths of the screen height with respect to the center point of the brick), but the problem is that the end position is slightly different depending on how high you drop the brick from.
Here's an example, with Window.clear(); off:
See how the end position at the bottom of the screen of each brick dropped is different? Why does it not stop falling immediately after it reaches the cut-off point? I ask for some help please.
Also, if I'm placing certain parts of code in the wrong part of the loop in general, please tell me.
Unrelated questions:In my
while (Window.isOpen()) loop, if I have
brickImage.move(0, 1), does that make it move 60 pixels per second? (60 if my Vsync/FPS is set to 60)
Also had a question of how things like the standard
rand() function work without including <cstdlib> or how using time as a seed in srand can work without <ctime>.
Thank you for reading!