Advertisement
Guest User

SFML Gravity test bug

a guest
Aug 9th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. //#include <ctime>
  4. //#include <cstdlib>
  5.  
  6. int main()
  7. {
  8.     srand((unsigned)time(NULL));
  9.  
  10.     const int WINDOW_WIDTH = 960;
  11.     const int WINDOW_HEIGHT = 540;
  12.  
  13.     //const double ACCEL_GRAVITY = 9.8;
  14.  
  15.  
  16.     sf::RenderWindow Window;
  17.     Window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML Gravity", sf::Style::Close);
  18.     Window.setVerticalSyncEnabled(true);
  19.  
  20.     sf::Texture brickTexture;
  21.     sf::Sprite brickImage;
  22.     if  (!brickTexture.loadFromFile("brick.png")) // 60x40 image
  23.          { std::cout << "Error: Could not load brick image\n"; return -1; }
  24.     else { std::cout << "Brick image loaded successfully\n"; }
  25.     brickImage.setTexture(brickTexture);
  26.     brickImage.setPosition(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
  27.     brickImage.setOrigin(brickImage.getLocalBounds().width/2.0, brickImage.getLocalBounds().height/2.0);
  28.  
  29.     std::cout << "Local width bounds: " << brickImage.getLocalBounds().width << std::endl;
  30.     std::cout << "Local image origin = (" << brickImage.getOrigin().x << ", " << brickImage.getOrigin().y << ")\n";
  31.  
  32.  
  33.     bool freefall = true;
  34.     int i = 1;
  35.     while (Window.isOpen())
  36.     {
  37.         sf::Event event;
  38.         while(Window.pollEvent(event))
  39.         {
  40.             switch(event.type)
  41.             {
  42.             case sf::Event::MouseButtonPressed:
  43.                 if (event.mouseButton.button == sf::Mouse::Left)
  44.                 {
  45.                     brickImage.setPosition(event.mouseButton.x, event.mouseButton.y);
  46.                     freefall = true;
  47.                     i = 1;
  48.                 }
  49.  
  50.                 break;
  51.                
  52.             case sf::Event::Closed:
  53.                 Window.close();
  54.                 break;
  55.  
  56.             }
  57.         }
  58.         if (brickImage.getPosition().y >= (WINDOW_HEIGHT - WINDOW_HEIGHT/10.0))
  59.         {
  60.             freefall = false;   // The position at which it stops falling is not constant
  61.         }
  62.         else if (freefall == true)
  63.         {
  64.             brickImage.move(0, i);
  65.             i++;
  66.  
  67.         }
  68.  
  69.  
  70.         //Window.clear();
  71.         Window.draw(brickImage);
  72.  
  73.         Window.display();
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement