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

Author Topic: [Help] Multi-file Sprite sheet animation help (Update)  (Read 4430 times)

0 Members and 1 Guest are viewing this topic.

Lyna

  • Newbie
  • *
  • Posts: 3
    • View Profile
[Help] Multi-file Sprite sheet animation help (Update)
« on: October 07, 2015, 09:59:09 pm »
Hello, I am a beginner in using SFML and I am trying to have a sprite class that can be used to draw the same sprite to multiple/ different game-screens. So far the code draws the sprite to the desired game-screen, but I am struggling with having the player sprite move and change image (using a spritesheet, i.e. change sprite to face another direction/ cycle through a short animation).

I have drawn the sprite from it's class, to the desired game-screen, but how can i update the sprite to move and change direction?
Here is the spritesheet:


Here is the code.
//main.cpp
#include <iostream>
#include "ScreenManager.h"
#include "PlayerCharacterSprite.h"
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
        sf::RenderWindow Window(sf::VideoMode(ScreenWidth, ScreenHeight, 32), "Platformer v1");

        ScreenManager::GetInstance().Initialize();
        ScreenManager::GetInstance().LoadContent();

        while (Window.isOpen())
        {
                sf::Event Event;
                while (Window.pollEvent(Event))
                {
                        switch (Event.type)
                        {
                        case sf::Event::Closed:

                                break;
                        case sf::Event::KeyPressed:
                                if (Event.key.code == sf::Keyboard::Escape)
                                {
                                        Window.close();
                                }
                        }
                }

                if (Event.key.code == sf::Keyboard::Up)
                {
                        //make player move and change sprite (up)
                        std::cout << "Up" << std::endl;
                }
                else if (Event.key.code == sf::Keyboard::Down)
                {
                        //make player move and change sprite (up)
                        std::cout << "Down" << std::endl;
                }
                else if (Event.key.code == sf::Keyboard::Right)
                {
                        //make player move and change sprite (up)
                        std::cout << "Right" << std::endl;
                }
                else if (Event.key.code == sf::Keyboard::Left)
                {
                        //make player move and change sprite (up)
                        std::cout << "Left" << std::endl;
                }

                Window.clear();

                ScreenManager::GetInstance().Update();
                ScreenManager::GetInstance().Draw(Window);

                Window.display();
        }

        return 0;
}

//PlayerCharacterSprite.cpp
#include "PlayerCharacterSprite.h"
#include <iostream>

PlayerCharacterSprite::PlayerCharacterSprite()
{
        enum Direction { Down, Left, Right, Up }; //0, 1, 2, 3
        sf::Vector2i source(1, Down); // SF way of doing -> int sourceX = 32, sourceY = Down;

        if (!playerTexture.loadFromFile("charSpriteSheet.png"))
                std::cout << "Error could not load player image" << std::endl;
       
       

        playerSprite.setTexture(playerTexture);
        playerSprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32)); //
}

void PlayerCharacterSprite::AnimSprite()
{
        //source.x++;
        //if (source.x * 32 >= playerTexture.getSize().x) //rotates through the sprite cycle
        //      source.x = 0;

        //return;
}

//PlayerCharacterSprite.h
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include "ScreenManager.h"

class PlayerCharacterSprite
{
public:
        PlayerCharacterSprite();
        void AnimSprite();
       
        sf::Sprite& getSprite() { return playerSprite; }

        sf::Vector2i source; // SF way of doing -> int sourceX = 32, sourceY = Down;
       

protected:
private:
        sf::Texture playerTexture;
        sf::Sprite playerSprite;
        sf::Event Event;
};

 

Trying to update the sprite on this page:
//MgStarterArea.cpp
#include "MgStarterArea.h"


MgStarterArea::MgStarterArea()
{
       
}


MgStarterArea::~MgStarterArea()
{
}

void MgStarterArea::LoadContent()
{
        /*if (!font.loadFromFile("arial_0.ttf"))
        {
                std::cout << "Could not find font" << std::endl;
        }
        sentence = "Starter Area";*/


}

void MgStarterArea::UnloadContent()
{

}

void MgStarterArea::Update()
{

}

void MgStarterArea::Draw(sf::RenderWindow &Window)
{
       
        /*sf::Text text(sentence, font, 40);
        text.setColor(sf::Color(255, 255, 255));*/


        PlayerCharacterSprite player;
        Window.draw(player.getSprite());
        Window.draw(text);
}
 

Result:
« Last Edit: October 09, 2015, 06:20:20 pm by Lyna »

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: [bug] Multi-file Sprite sheet animation help
« Reply #1 on: October 08, 2015, 02:58:36 am »
Edit:// Found out that the program isn't even reading the input code, that may be the error

You have your input code in the constructor of "PlayerCharacterSprite". Is this intended?

Lyna

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [bug] Multi-file Sprite sheet animation help
« Reply #2 on: October 08, 2015, 12:27:21 pm »
Edit:// Found out that the program isn't even reading the input code, that may be the error

You have your input code in the constructor of "PlayerCharacterSprite". Is this intended?

What would be the best way to handle input for this project? with an input Manager class?

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: [bug] Multi-file Sprite sheet animation help
« Reply #3 on: October 08, 2015, 01:38:49 pm »
What would be the best way to handle input for this project? with an input Manager class?

An input manager class is certainly the better way to deal with input (Both realtime and event based), but as far as a single "correct" answer? I guess its really up to you.

The event stuff should be part of your game loop so it gets checked every tick. At the moment, your input code is run once but only when your constructor gets called.

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: [bug] Multi-file Sprite sheet animation help
« Reply #4 on: October 08, 2015, 01:53:39 pm »
Having the event-handling inside the constructor makes no sense at all, this case in particular.

At best you get a sprite which faces downwards, because event is initialised with 0(debug mode).
At worst you just found a way into the wonderful world of undefined behaviour, because your event isn't properly initialised and you are trying to access it.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [bug] Multi-file Sprite sheet animation help
« Reply #5 on: October 08, 2015, 02:48:35 pm »
An input manager class is certainly the better way to deal with input (Both realtime and event based), but as far as a single "correct" answer? I guess its really up to you.
That depends on what you mean with "manager". If it's one of these global singleton god-classes, then no, that's one of the worst ways to deal with input ;)

At best you get a sprite which faces downwards, because event is initialised with 0(debug mode).
Do debuggers really initialize things with 0? I keep hearing this, but decent ones I work with use specific memory patterns. In contrast to 0, this still causes segfaults/assertions at runtime and doesn't just swallow the errors. Which makes sense, as you start a debugger to find bugs, not to hide them...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: [bug] Multi-file Sprite sheet animation help
« Reply #6 on: October 08, 2015, 05:47:47 pm »
That depends on what you mean with "manager". If it's one of these global singleton god-classes, then no, that's one of the worst ways to deal with input ;)

I dont. But Thor has ways of dealing with input.

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: [bug] Multi-file Sprite sheet animation help
« Reply #7 on: October 08, 2015, 07:05:50 pm »
At best you get a sprite which faces downwards, because event is initialised with 0(debug mode).
Do debuggers really initialize things with 0? I keep hearing this, but decent ones I work with use specific memory patterns. In contrast to 0, this still causes segfaults/assertions at runtime and doesn't just swallow the errors. Which makes sense, as you start a debugger to find bugs, not to hide them...

You should be right, can't say for sure. But the important thing is that he gets some garbage value and his debugger does swallow the error and leads him into the wonderful world of undefined behaviour;)

Satus

  • Guest
Re: [bug] Multi-file Sprite sheet animation help
« Reply #8 on: October 08, 2015, 07:37:31 pm »
Quote
Do debuggers really initialize things with 0?

I used to work with debugger that initialized deleted pointers to null, but it was long time ago.

Lyna

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [bug] Multi-file Sprite sheet animation help
« Reply #9 on: October 08, 2015, 11:09:27 pm »
Updated the query