Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- //#include <ctime>
- //#include <cstdlib>
- int main()
- {
- srand((unsigned)time(NULL));
- const int WINDOW_WIDTH = 960;
- const int WINDOW_HEIGHT = 540;
- //const double ACCEL_GRAVITY = 9.8;
- sf::RenderWindow Window;
- Window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML Gravity", sf::Style::Close);
- Window.setVerticalSyncEnabled(true);
- sf::Texture brickTexture;
- sf::Sprite brickImage;
- if (!brickTexture.loadFromFile("brick.png")) // 60x40 image
- { std::cout << "Error: Could not load brick image\n"; return -1; }
- else { std::cout << "Brick image loaded successfully\n"; }
- brickImage.setTexture(brickTexture);
- brickImage.setPosition(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
- brickImage.setOrigin(brickImage.getLocalBounds().width/2.0, brickImage.getLocalBounds().height/2.0);
- std::cout << "Local width bounds: " << brickImage.getLocalBounds().width << std::endl;
- std::cout << "Local image origin = (" << brickImage.getOrigin().x << ", " << brickImage.getOrigin().y << ")\n";
- 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;
- case sf::Event::Closed:
- Window.close();
- 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();
- Window.draw(brickImage);
- Window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement