Advertisement
s1ay3r44

SFML Example with Comments

Aug 28th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #inlcude <SFML/System.hpp> // For sf::Clock
  2. #include <SFML/graphics.hpp> //All the other stuff we need for now is in here
  3.  
  4. using namespace std; // I like to use this, personally
  5.  
  6. int main(){
  7.     sf::RenderWindow app(sf::VideoMode(800, 600), "Graphics, woo!"); // Sets up screen with resolution of 800x600 and with a title
  8.     sf::View view(sf::FloatRect(0, 0, 800, 600)); // View is like a camera into our window. useful if you are in need of different views or move the camera
  9.  
  10.     sf::Clock clock;
  11.  
  12.     sf::CircleShape c(2); // Creates a circle object with a radius of 2
  13.  
  14.     c.setPosition(800/2, 600/2); //Sets c to the center of the window
  15.     c.setOutlineColor(sf::Color::Red);
  16.     c.setFillColor(sf::Color::Blue);
  17.  
  18.     while(app.isOpen()){
  19.     float dt = (float)(clock.reset().asSeconds())
  20.     //Start of our main loop
  21.         pollEvents(app);
  22.  
  23.     //Poll Inputs
  24.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
  25.         view.move(0, dt * -10);
  26.     }
  27.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
  28.         view.move(dt * -10, 0);
  29.     }
  30.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
  31.         view.move(0, dt * 10);
  32.     }
  33.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
  34.         view.move(dt * 10, 0);
  35.     }
  36.  
  37.     if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
  38.         app.close();
  39.     }
  40.  
  41.     //This is actually a very bad way of handling inputs, use a message pump instead
  42.  
  43.     //Drawing
  44.         app.clear();
  45.         app.setView(view); // Good idea, if you are using multiple views
  46.         app.draw(c); // Draw our circle
  47.         app.display(); // Update our window
  48.  
  49.     }
  50.  
  51.     return 0;
  52.  
  53. }
  54.  
  55. void pollInputs(sf::RenderWindow& app){
  56.     sf::Events e;
  57.  
  58.     while(app -> pollEvent(e)){
  59.         if(e.type == sf::Event::Closed){
  60.             app -> close(); // Close the window if X is clicked
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement