Facebook
From Tiny Owl, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 276
  1. #include <SFML/Window.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7. using namespace sf;
  8.  
  9. class AnimatedSprite : public Sprite
  10. {
  11. private:
  12.     Texture texture;
  13.     vector<IntRect>frames_;
  14.     float current_frame_time_ = 0.0;
  15.     int current_frame_index_ = 0;
  16.     float fps_ = 4.0;
  17.  
  18.     float v_x = 0.0;
  19.     float v_y = 0.0;
  20.  
  21.     float acc_x = 0.0;
  22.     float acc_y = 50.0;
  23. public:
  24.     AnimatedSprite(Vector2f position)
  25.     {
  26.         texture.loadFromFile("character.png");
  27.         setTexture(texture);
  28.         setPosition(position);
  29.         setScale(4,4);
  30.     }
  31.     void add_animation_frame(IntRect frame)
  32.     {
  33.         if(frames_.empty())
  34.         {
  35.             setTextureRect(frame);
  36.         }
  37.         frames_.push_back(frame);
  38.     }
  39.     void step(float delta_t)
  40.     {
  41.         v_x += delta_t*acc_x;
  42.         v_y += delta_t*acc_y;
  43.         current_frame_time_ +=delta_t;
  44.         cerr<<current_frame_time_<<endl;
  45.         while (current_frame_time_>= (1.0/fps_))
  46.         {
  47.             current_frame_time_ -= (1.0/fps_);
  48.             current_frame_index_ = (current_frame_index_ +1) % frames_.size();
  49.             setTextureRect(frames_[current_frame_index_]);
  50.         }
  51.         if(Keyboard::isKeyPressed(Keyboard::W))
  52.         {
  53.             move(0,-v_y*delta_t);
  54.             // add_animation_frame(IntRect(0, 0, 32, 32));
  55.         }
  56.         if(Keyboard::isKeyPressed(Keyboard::S))
  57.         {
  58.             move(0,v_y*delta_t);
  59.             //   add_animation_frame(IntRect(0, 0, 32, 32));
  60.         }
  61.         if(Keyboard::isKeyPressed(Keyboard::A))
  62.         {
  63.             move(-v_x*delta_t,0);
  64.             //   add_animation_frame(IntRect(0, 0, 32, 32));
  65.         }
  66.         if(Keyboard::isKeyPressed(Keyboard::D))
  67.         {
  68.             move(v_x*delta_t,0);
  69.             //  add_animation_frame(IntRect(0, 0, 32, 32));
  70.         }
  71.     }
  72. };
  73.  
  74. int main() {
  75.     // create the window
  76.     sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
  77.     AnimatedSprite hero(Vector2f(600,100));
  78.  
  79.     hero.add_animation_frame({0,0,50,37});
  80.     hero.add_animation_frame({50,0,50,37});
  81.     hero.add_animation_frame({100,0,50,37});
  82.     Clock clock;
  83.     // run the program as long as the window is open
  84.     while (window.isOpen()) {
  85.         float delta_t = float (clock.getElapsedTime().asMicroseconds())/1000000.0;
  86.  
  87.         clock.restart();
  88.         // check all the window's events that were triggered since the last iteration of the loop
  89.         sf::Event event;
  90.         while (window.pollEvent(event))
  91.         {
  92.             // "close requested" event: we close the window
  93.             if (event.type == sf::Event::Closed)
  94.                 window.close();
  95.         }
  96.         hero.step(delta_t);
  97.         // clear the window with black color
  98.         window.clear(sf::Color::Black);
  99.         window.draw(hero);
  100.         // draw everything here...
  101.  
  102.  
  103.         // end the current frame
  104.         window.display();
  105.     }
  106.  
  107.     return 0;
  108. }
  109.