Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #inlcude <SFML/System.hpp> // For sf::Clock
- #include <SFML/graphics.hpp> //All the other stuff we need for now is in here
- using namespace std; // I like to use this, personally
- int main(){
- sf::RenderWindow app(sf::VideoMode(800, 600), "Graphics, woo!"); // Sets up screen with resolution of 800x600 and with a title
- 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
- sf::Clock clock;
- sf::CircleShape c(2); // Creates a circle object with a radius of 2
- c.setPosition(800/2, 600/2); //Sets c to the center of the window
- c.setOutlineColor(sf::Color::Red);
- c.setFillColor(sf::Color::Blue);
- while(app.isOpen()){
- float dt = (float)(clock.reset().asSeconds())
- //Start of our main loop
- pollEvents(app);
- //Poll Inputs
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
- view.move(0, dt * -10);
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
- view.move(dt * -10, 0);
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
- view.move(0, dt * 10);
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
- view.move(dt * 10, 0);
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
- app.close();
- }
- //This is actually a very bad way of handling inputs, use a message pump instead
- //Drawing
- app.clear();
- app.setView(view); // Good idea, if you are using multiple views
- app.draw(c); // Draw our circle
- app.display(); // Update our window
- }
- return 0;
- }
- void pollInputs(sf::RenderWindow& app){
- sf::Events e;
- while(app -> pollEvent(e)){
- if(e.type == sf::Event::Closed){
- app -> close(); // Close the window if X is clicked
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement