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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - gwendyn

Pages: [1]
1
General / 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?

Pages: [1]