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

Author Topic: sf::Sprite undefined / no default value  (Read 394 times)

0 Members and 1 Guest are viewing this topic.

gwendyn

  • Newbie
  • *
  • Posts: 2
    • View Profile
sf::Sprite undefined / no default value
« on: March 23, 2025, 04:02:49 pm »
Hi,
im pretty new to sfml. i want to get used to classes and i follow the book "LearnByExample". Since its an older book the syntax is flawed.

Trying to follow the examples in the book i want to follow spliting everything into different classes. But how to initiate the sprite? if i anounce sf::sprite sprite in header i get an error for it not having a default value. and if i dont anounce it its not defined for the other functions.

Header:
#pragma once
#include <SFML/Graphics.hpp>
#include "Window.h"

class Game {
public:
        Game();
        ~Game();

        void HandleInput();
        void Update();
        void Render();
        void GetWindow();
       

private:
        sf::Texture m_mushroomTexture;
        //sf::Sprite m_mushroom;
        void MoveMushroom();
        Window m_window;
        sf::Vector2f m_increment;

#include "Game.h"
#include <SFML/Graphics.hpp>

Game::Game() : m_window("Chapter 2", { 800, 600 }) {

   m_mushroomTexture.loadFromFile("resources/smw_mushroom.png");
   sf::Sprite m_mushroom(m_mushroomTexture);
   sf::Vector2u size = m_mushroomTexture.getSize();
   m_mushroom.setOrigin({size.x / 2, size.y / 2});
   m_increment = sf::Vector2f{ 4, 4 };
}

Game::~Game()
{
}

void Game::HandleInput()
{
}

void Game::Update() {
   m_window.Update();
   MoveMushroom();
}

void Game::Render() {
   m_window.BeginDraw();  
   m_window.Draw(m_mushroom);      //undefine
   m_window.EndDraw();      
}

void Game::GetWindow()
{
}

void Game::MoveMushroom() {
   sf::Vector2u l_windSize = m_window.GetWindowSize();
   sf::Vector2u l_textSize = m_mushroomTexture.getSize();

   if ((m_mushroom.getPosition().x > l_windSize.x - l_textSize.x && m_increment.x > 0) ||      //undefined
      (m_mushroom.getPosition().x < 0 && m_increment.x < 0)) {
      m_increment.x = -m_increment.x;
   }
   if (m_mushroom.getPosition().y > l_windSize.y - l_textSize.y && m_increment.y > 0 ||
      m_mushroom.getPosition().y < l_windSize.y && m_increment.y < 0){
      m_increment.y = -m_increment.y;
   }
   m_mushroom.move(m_increment);
}

Can someone help me or lead me to something which explains that? since sfml3 is pretty new i have trouble finding examples to look at.

edit: maybe i can create a vector or an object and just attach the sprite late?
« Last Edit: March 23, 2025, 04:45:33 pm by gwendyn »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11175
    • View Profile
    • development blog
    • Email
Re: sf::Sprite undefined / no default value
« Reply #1 on: March 24, 2025, 04:30:00 pm »
sf::Sprite no longer has a default constructor.

Two migration paths are:
  • Use std::optional<sf::Sprite> if you still need an uninitialized sprite
  • Pass the needed texture to the sprite

For the second option, Ii your class holds a sf::Sprite instance, you'll need to initialize it in the initialization list. I would also then make sure, that you're passing the texture into class as well and to not load the texture in the constructor after the initialization list.

For your code, I'd probably pick the first option.

class Game {
// ...
std::optional<sf::Sprite> m_mushroom;
// ...
};
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gwendyn

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: sf::Sprite undefined / no default value
« Reply #2 on: March 29, 2025, 10:59:41 pm »
hey thanks, passing works great. but i noticed you can also just load the texture in the initialization like that:

Game::Game():
mPlayerTexture("resources/eagle.png"),
mPlayer(mPlayerTexture) {}

I dont get how std::optional is supposed to work tho. if i use it in the header it dosnt really help because it wont allow me to use move() or draw() on that sprite because its not recognized as an sf::Sprite.

And now i built a simple texture manager with the guidance of a sfml book which loads the texture into a std::map with a pointer to retrieve it like that:

textures.Load(Textures::Airplane, "resources/eagle.png");
mPlayer(textures.Get(Textures::Airplane));

So i have now the same problem a third time D:
cant do that in the initialization and i cannot use it in the constructor.


« Last Edit: March 29, 2025, 11:05:26 pm by gwendyn »