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: