Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - amanuel2

Pages: [1]
1
General / Re: How to do Real Animation in C++ SFML
« on: May 22, 2016, 04:51:32 am »
Ok Erdrick! Thanks for that! But one question.. I dont know how to change the program so that when the user dosent click any controls, it just dosent go on running state.. But as soon as he does it starts to actually show the running motion. Any ideas on how to acomplish that?

2
General / Re: How to do Real Animation in C++ SFML
« on: May 21, 2016, 08:06:25 pm »
For some reason this works:
                source.x++;
                if (source.x * 32 >= _texture.getSize().x)
                        source.x = 0;
 

But i dont understand why! And i want to understand why so i can apply it in my future SFML Programming..

3
General / How to do Real Animation in C++ SFML
« on: May 21, 2016, 07:21:42 pm »
im kinda confused on how the animation works in SFML.. So ima try to explain where i am at and where i am confused. So Right now this is the full sprite:



As you can see above those are the sprites.. Here is the code i currently have:


    #include <SFML/Graphics.hpp>
    #include <time.h>
    #include <string>
    #include <iostream>
   
    #define SPRITE_WIDTH 32
    #define SPRITE_HEIGHT 32
    #define DIRECTION_Y_DOWN_INITAL 0
    #define DIRETION_Y_LEFT_INITAL 1
    #define DIRECTION_Y_RIGHT_INITAL 2
    #define DIRECTION_Y_UP_INITAL 3
   
    #define DIRECTION_X_DOWN_INITAL 0
    #define DIRECTION_X_LEFT_INITAL 0
    #define DIRECTION_X_RIGHT_INITAL 0
    #define DIRECTION_X_UP_INITAL 0
   
   
    using std::cout;
    using std::endl;
   
   
    class Mouvment
    {
    public:
        Mouvment() {};
   
        void goDirection(const int &dir, sf::Sprite &spritesheet)
        {
                switch (dir) {
                case 0:
                        spritesheet.move(sf::Vector2f(0, -3));
                        break;
                case 1:
                        spritesheet.move(sf::Vector2f(0, 3));
                        break;
                case 2:
                        spritesheet.move(sf::Vector2f(3, 0));
                        break;
                case 3:
                        spritesheet.move(sf::Vector2f(-3, 0));
                        break;
                }
        }
    };
   
    int main()
    {
        sf::RenderWindow _window(sf::VideoMode(600, 600), "Hello World");
        sf::Texture _texture;
        sf::Vector2i source(DIRECTION_X_DOWN_INITAL, DIRECTION_Y_DOWN_INITAL);
        Mouvment mouvment;
        if (!(_texture.loadFromFile("Resources/SPRITE.png")))
        {
                cout << "Could Not Load File.." << endl;
        }
   
        sf::Sprite _sprite;
        _sprite.setTexture(_texture);
   
        while (_window.isOpen())
        {
                sf::Event _event;
                while (_window.pollEvent(_event))
                {
                        switch (_event.type)
                        {
                        case sf::Event::Closed:
                                _window.close();
                                exit(1);
                        case sf::Event::KeyPressed:
                                switch (_event.key.code)
                                {
                                case sf::Keyboard::Right:
                                        mouvment.goDirection(2, _sprite);
                                        source.x = DIRECTION_X_RIGHT_INITAL;
                                        source.y = DIRECTION_Y_RIGHT_INITAL;
                                        break;
                                case sf::Keyboard::Left:
                                        mouvment.goDirection(3, _sprite);
                                        source.x = DIRECTION_X_LEFT_INITAL;
                                        source.y = DIRETION_Y_LEFT_INITAL;
                                        break;
                                case sf::Keyboard::Up:
                                        mouvment.goDirection(0, _sprite);
                                        source.x = DIRECTION_X_UP_INITAL;
                                        source.y = DIRECTION_Y_UP_INITAL;
                                        break;
                                case sf::Keyboard::Down:
                                        mouvment.goDirection(1, _sprite);
                                        source.x = DIRECTION_X_DOWN_INITAL;
                                        source.y = DIRECTION_Y_DOWN_INITAL;
                                        break;
                                }
                                break;
                        }
                }
                _sprite.setTextureRect(sf::IntRect(source.x * SPRITE_WIDTH , source.y * SPRITE_HEIGHT , SPRITE_WIDTH, SPRITE_HEIGHT));
                _window.draw(_sprite);
                _window.display();
                _window.clear();
        }
    }
 

So right now my code currently operates in the first row of the Spritesheet. It can do Down , Up, Left, and Right direction BUT ONLY for the first row.. Is there a way to do it so if i press right key, it cycles through three of the rows in the first column so it actually looks like the character is Walking? Thankyou, btw im watching this [tutorial][2].


  [2]:

4
General / Wierd Mouvment C++
« on: May 21, 2016, 05:47:59 am »
For some reason when i run the code below... The character only moves diagonaly down.. Lets say line im drawing is character:

    \
     \
      \
       \
        \

Thats the only direction it moves if i press up , right, down , or left key! Please help, i want to go right if right, up if up, down if down, and left if left.

My Code is:

    #include<iostream>
    #include<string>
    #include<SFML\Graphics.hpp>
   
    using std::cout;
    using std::endl;
   
    enum Direction
    {
       DOWN,
       LEFT,
       RIGHT,
       UP
    };
   
    int main()
    {
       sf::RenderWindow _Win(sf::VideoMode(600, 600), "Hello World");
       sf::Texture _texture;
       if (!(_texture.loadFromFile("Resources/SPRITE.png")))
       {
          cout << "Could not load iamge" << endl;
       }
   
       //Source, tell us our starting position.
       //Vector2i = Vector of 2 in SFML
       sf::Vector2i source(1, DOWN/*or 0*/);
   
       sf::Sprite _sprite(_texture);
   
       float x = _sprite.getPosition().x;
       float y = _sprite.getPosition().y;
       while (true)
       {
          sf::Event _event;
          while (_Win.pollEvent(_event))
          {
             switch (_event.type)
             {
             case sf::Event::Closed:
                _Win.close();
                exit(1);
                break;
             case sf::Event::KeyPressed:
                switch (_event.key.code)
                {
                case sf::Keyboard::Up:
                   source.y = UP;
                   _sprite.move(sf::Vector2f(x,y--));
                   y = 3, x=3;
                   
                   break;
                case sf::Keyboard::Down:
                   source.y = DOWN;
                   _sprite.move(sf::Vector2f(x, y++));
                   y = 3, x = 3;
                   break;
                case sf::Keyboard::Right:
                   source.y = RIGHT;
                   _sprite.move(sf::Vector2f(x++, y));
                   y = 3, x = 3;
                   break;
                   case sf::Keyboard::Left:
                   source.y = LEFT;
                   _sprite.move(sf::Vector2f(x--, y));
                   y = 3, x = 3;
                   break;
                }
                break;
             }
          }
   
          //Cropping Out Image
          //Please Look at sprite in resources/Sprite.png
          //When we run this :
          //_sprite.setTextureRect(sf::IntRect( source.x*32 , source.y*32 , 32 , 32 ));
          //Its going to give us the top left corner sprite image. Thats so because
          //we are cropping source.x*32 , which of 32 is the width of the sprite.. So it
          //starts from 1 * 32. 32 is the width of one sprite so it goes to the end of it.
          //Same Applies to the y. source.y * 32. It just goes to the end of the down sprite.
          //As you go down the y increases, 1 * 32 = 32. And 32 is the width of one sprite
          //so it shows body of one full sprite.
          _sprite.setTextureRect(sf::IntRect( source.x*32 , source.y*32 , 32 , 32 ));
          //Clears Window(Flickering..)
          _Win.clear();
          //Draw Sprite
          _Win.draw(_sprite);
          //And Finally Display the Window.
          _Win.display();
       }
    }


Pages: [1]
anything