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

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

0 Members and 1 Guest are viewing this topic.

gwendyn

  • Newbie
  • *
  • Posts: 1
    • 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: 11174
    • 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/