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

Author Topic: Moving Spites Help! (Speed Problem)  (Read 2186 times)

0 Members and 1 Guest are viewing this topic.

guitarmatt99

  • Newbie
  • *
  • Posts: 19
    • View Profile
Moving Spites Help! (Speed Problem)
« on: September 28, 2013, 08:27:56 pm »
Hello there! I have recently played around with moving characters (with animation) from a single sprite sheet. I want to know whether I have done this correctly or in the most efficient way? (it does work!)

My sprite sheet contained 4 different angles of my character with 3 different positions for each. ( 96 x 128 )

EDIT: I am also having a problem with the speed at which is goes through the animation (not the speed it moves across the screen), is there anyway to control how fast it cycles through the sprite sheet?

Here's my code!

#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>

int main()
{
    enum Direction{ Down, Left, Right, Up };
    sf::Vector2i source(1, Down);

    sf::RenderWindow window(sf::VideoMode(800, 450), "Testing", sf::Style::Fullscreen);

    sf::Texture pTexture;
    sf::Sprite pChar;

    //load sprite sheet
    if(!pTexture.loadFromFile("image.png"))
        std::cout << "Error" << std::endl;

    pChar.setTexture(pTexture);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            source.y = Up;
            pChar.move(0, -0.1);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            source.y = Down;
            pChar.move(0, 0.1);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            source.y = Right;
            pChar.move(0.1, 0);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            source.y = Left;
            pChar.move(-0.1, 0);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
        {
            window.create(sf::VideoMode(800, 450), "Testing", sf::Style::Default);
        }

        pChar.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
        window.draw(pChar);
        window.display();

    }

    return 0;
}
 

ALSO, when I change the size of my window so does the sprite, how do I give it a fixed size?
« Last Edit: September 29, 2013, 08:30:07 pm by guitarmatt99 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Moving Spites Help!
« Reply #1 on: September 28, 2013, 09:23:55 pm »
1) This should be in the help section.

2) For a simple test program this honestly looks fine to me.  The only part that jumps out as potentially wrong is that you used real-time input instead of an event for resizing the window, but since that's just for testing purposes that's little more than a nitpick.  In the long term, you'll probably want to move a lot of this logic up into a Player class so you can keep your input handling organized well as it gets more complicated, but that's about it.

3) It's strange that you'd ask just about the player character and not the rest of the world, so depending on what you want you either need to:
  a) Experiment with views.  See the official tutorial: http://sfml-dev.org/tutorials/2.1/graphics-view.php
  b) Simply change the size of the player sprite using methods like setScale().

guitarmatt99

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Moving Spites Help!
« Reply #2 on: September 28, 2013, 10:32:34 pm »
Very sorry for posting in the wrong section, I've actually just realised, is there a way to change it? I'm new to these forums.

Thanks for replying, I was going to ask about setting a background but I'll post it in a new post in the correct section!

Would I create a function that checks for keyboard input inside a class?

Also, the reason I used keyboard input to resize was just so I could get out of the program easily because I wanted it fullscreen but thanks for noticing!  :)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Moving Spites Help! (wrong section sorry)
« Reply #3 on: September 28, 2013, 10:46:06 pm »
You might be able to "move" your original post, or we might have to wait for an admin.  Not sure how that works on this forum.

"Setting a background" isn't really any different from drawing any other sprite.  It's just a much bigger one.

You could check for it inside a class, but it's generally cleaner and easier to do all of your event and input handling directly in the main loop.  In general, all main() should really be doing is event handling, input handling, and the clear/draw/display cycle.   What I mean is doing something like:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
   Player.myMove(Up);
}
Where Player is a class you defined, myMove() is a method that does exactly what your current code does, and Up is the same enum value you have right now.  This gets most of the movement logic out of the main() loop and into your Player class, without any change in behavior.

And as I said, this is just a long-term suggestion.  You definitely don't need to do it right now.  Just be aware that you can do it, because eventually your program will get more complicated and you'll want to clean it up a bit by doing stuff like this.  I brought it up because you asked if you did things the best way possible, and eventually a change like this will probably be part of the "best way."

Keyboard input is fine.  My nitpick was specifically real-time keyboard input instead of event-based keyboard input.  And that's still just a nitpick.
« Last Edit: September 28, 2013, 10:51:07 pm by Ixrec »

guitarmatt99

  • Newbie
  • *
  • Posts: 19
    • View Profile
Moving Spites Help!
« Reply #4 on: September 28, 2013, 11:27:21 pm »
Thanks for helping, people on these forums are great, great community!

I'll have to brush up on classes as it's been a while since I've programmed anything, I'll post an update with my progress in the future, I'd rather do everything clean and functional rather than sloppy and functional, I hate having messy code which is why I thought something could be improved!

I'll also get to work on adding a background, I have no clue where this is going to end up, it's more of an experiment at the moment!

Oh, and is "myMove();" built into SFML?

Thanks.
« Last Edit: September 28, 2013, 11:30:27 pm by guitarmatt99 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Moving Spites Help!
« Reply #5 on: September 28, 2013, 11:53:49 pm »
lol no, myMove was just a suggestion for what to name the class method that implements the movement logic.  The whole point of writing your own class is that you write the functions used to manipulate it.  I chose "myMove" instead of "move" because having the same name as the SFML function might be confusing.  And that's just a suggestion anyway, there's lots of ways you can do this (which is part of the beauty of programming).

guitarmatt99

  • Newbie
  • *
  • Posts: 19
    • View Profile
Moving Spites Help!
« Reply #6 on: September 29, 2013, 09:56:17 am »
Okay thanks :) I was guessing it wasn't but I'm glad I asked haha, I'll get there eventually. I have an idea in my head now of how it works.