Advertisement
Felanpro

Animation SFML

Apr 5th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Window.hpp>
  5.  
  6. using namespace std;
  7.  
  8. bool looking_left = false;
  9. bool looking_right = true;
  10. bool walking_right = false;
  11. bool walking_left = false;
  12.  
  13. int main()
  14. {
  15.     sf::RenderWindow window(sf::VideoMode(800, 600), "Game In Development");
  16.  
  17.     sf::Texture character_sheet;
  18.     character_sheet.loadFromFile("hero_spritesheet.png");
  19.     sf::Sprite character;
  20.     character.setTexture(character_sheet);
  21.  
  22.     sf::Time time = sf::seconds(0);
  23.     sf::Time time2 = sf::seconds(0);
  24.  
  25.     sf::Clock clock2; //Start Second clock
  26.     sf::Clock clock; //Start clock
  27.  
  28.     sf::Event event;
  29.     while (window.isOpen())
  30.     {
  31.         while (window.pollEvent(event))
  32.         {
  33.             if (event.type == sf::Event::Closed)
  34.                 window.close();
  35.            
  36.             if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  37.             {
  38.                 walking_right = true;
  39.                 looking_right = false;
  40.                 looking_left = false;
  41.                 walking_left = false;
  42.             }
  43.             else
  44.             {
  45.                 if (walking_right == true)
  46.                 {
  47.                     walking_right = false;
  48.                     looking_right = true;
  49.                 }
  50.                 else
  51.                     walking_right = false;
  52.             }
  53.  
  54.             if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  55.             {
  56.                 walking_left = true;
  57.                 looking_right = false;
  58.             }
  59.         }
  60.  
  61.         //----------------------------PREPERATIONS----------------------------
  62.         time = clock.getElapsedTime();
  63.         time2 = clock2.getElapsedTime();
  64.         if (!walking_right && !walking_left)
  65.             clock2.restart();
  66.         //--------------------------PREPERATIONS END--------------------------
  67.  
  68.         //Check character state and execute animations
  69.         if (looking_right)
  70.         {
  71.             if (time.asSeconds() > .5)
  72.             {
  73.                 character.setTextureRect(sf::IntRect(0, 0, 80, 94));
  74.                 if (time.asSeconds() > 1)
  75.                     clock.restart();
  76.             }
  77.             else
  78.             {
  79.                 character.setTextureRect(sf::IntRect(80, 0, 80, 94));
  80.             }
  81.         }
  82.         else if (walking_right)
  83.         {
  84.             if (time2.asSeconds() > 0 && time2.asSeconds() < .5)
  85.                 character.setTextureRect(sf::IntRect(0, 94, 80, 94));
  86.             else if (time2.asSeconds() > .5 && time2.asSeconds() < 1)
  87.                 character.setTextureRect(sf::IntRect(80, 94, 80, 94));
  88.             else if (time2.asSeconds() > 1 && time2.asSeconds() < 1.5)
  89.                 character.setTextureRect(sf::IntRect(80 * 2, 94, 80, 94));
  90.             else if (time2.asSeconds() > 1.5 && time2.asSeconds() < 2)
  91.                 character.setTextureRect(sf::IntRect(80 * 3, 94, 80, 94));
  92.             else if (time2.asSeconds() > 2 && time2.asSeconds() < 2.5)
  93.                 character.setTextureRect(sf::IntRect(80 * 4, 94, 80, 94));
  94.             else if (time2.asSeconds() > 2.5 && time2.asSeconds() < 3)
  95.                 character.setTextureRect(sf::IntRect(80 * 5, 94, 80, 94));
  96.             else if (time2.asSeconds() > 3)
  97.                 clock2.restart();
  98.  
  99.             character.setPosition(character.getPosition().x + 0.05, character.getPosition().y);
  100.         }
  101.         else if (walking_left)
  102.         {
  103.             if (time2.asSeconds() > 0 && time2.asSeconds() < .5)
  104.             {
  105.                 character.setTextureRect(sf::IntRect(0, 94, 80 * -1, 94));
  106.             }
  107.             else if (time2.asSeconds() > .5 && time2.asSeconds() < 1)
  108.             {
  109.                 character.setTextureRect(sf::IntRect());
  110.             }
  111.             else if (time2.asSeconds() > 1 && time2.asSeconds() < 1.5)
  112.             {
  113.                 character.setTextureRect(sf::IntRect());
  114.             }
  115.             else if (time2.asSeconds() > 1.5 && time2.asSeconds() < 2)
  116.             {
  117.                 character.setTextureRect(sf::IntRect());
  118.             }
  119.             else if (time2.asSeconds() > 2 && time2.asSeconds() < 2.5)
  120.             {
  121.                 character.setTextureRect(sf::IntRect());
  122.             }
  123.             else if (time2.asSeconds() > 2.5 && time2.asSeconds() < 3)
  124.             {
  125.                 character.setTextureRect(sf::IntRect());
  126.             }
  127.             else if (time2.asSeconds() > 3)
  128.                 clock2.restart();
  129.  
  130.             character.setPosition(character.getPosition().x - 0.05, character.getPosition().y);
  131.         }
  132.  
  133.         window.clear(sf::Color::White); //Clear
  134.  
  135.         window.draw(character);
  136.  
  137.         window.display(); //Display
  138.     }
  139.  
  140.     int pause; cin >> pause; //Pause the program
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement