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

Author Topic: Textures and Sprites inside a class(beginner)  (Read 1900 times)

0 Members and 2 Guests are viewing this topic.

Innkeeper

  • Newbie
  • *
  • Posts: 3
    • View Profile
Textures and Sprites inside a class(beginner)
« on: December 17, 2020, 08:14:07 pm »
Hello Im trying to use a texture and sprite inside a class I created but it didnt work with the way I tried and I need help
This is my Hero.h file

#include<SFML/Graphics.hpp>

#include<iostream>

class Hero
{

         Texture wizard;
        Sprite s_wizard;

        Hero();
        void drawHero();
};
 

This is my Hero.cpp file

#include<SFML/Graphics.hpp>


using namespace sf;

Hero::Hero()
{
        wizard.loadFromFile("files/magician.png");  // I want to load the texture inside contstructor
        s_wizard(wizard);
       
       
}

void Hero::drawHero()
{
       
}

 

in the drawHero function I want to draw this sprite using a window I created in main file but I dont know how to that.Any help is appreciated.Thank you


Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Textures and Sprites inside a class(beginner)
« Reply #1 on: December 17, 2020, 10:00:22 pm »
hi. this isn't really related to SFML, but to C++. i'm going to help you, but I suggest you to have a look on the topic around in google later.
you need to pass arguments to your function drawHero(). these arguments are references OR copies of other variables. in this case, you want to pass a reference of your original window, because if you pass a copy you'll end up having one game window for each Hero (what would be weird in a game). to pass by reference you use the '&' signal. so, basically it becomes like this:

class Hero
{

         Texture wizard;
        Sprite s_wizard;

        Hero();
public: //notice that the drawHero function needs to be public to be accessed from outside; in this case, it is called from main()
        void drawHero(sf::RenderWindow&);
};

and this:
Hero::Hero()
{
        wizard.loadFromFile("files/magician.png");  // I want to load the texture inside contstructor
        s_wizard(wizard);
       
       
}

void Hero::drawHero(sf::RenderWindow& window_ref)
{
        window_ref.draw(s_wizard);
}

and in your main code:
main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML"));
    Hero hero;
    //[your code]
    hero.drawHero(window); //here we pass the window to be accessed from inside the Hero
    //[more code]
    return 0;
}
 
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Innkeeper

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Textures and Sprites inside a class(beginner)
« Reply #2 on: December 17, 2020, 10:55:23 pm »
Thank you for the reply. I corrected the drawHero function but Im getting this error from my constructor.at the s_wizard(wizard) part   ;  call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type

Hero::Hero()
{
        wizard.loadFromFile("files/magician.png");
        s_wizard(wizard); // error
       

       
}

 

it seems like Im doing something wrong while creating sprite.What should I do?
« Last Edit: December 17, 2020, 10:57:35 pm by Innkeeper »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Textures and Sprites inside a class(beginner)
« Reply #3 on: December 18, 2020, 12:09:47 am »
for setting a sprite's texture, use
s_wizard.setTexture(wizard);
these specific functions can be found in the API documentation: https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Sprite.php

ps: I'd advise you to give better names to your variables. I usually start with 'm_' when the variable is a member of a class. just 'wizard' also doesn't mean much. so maybe 'm_wizard_spr' and 'm_wizard_tex'?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Innkeeper

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Textures and Sprites inside a class(beginner)
« Reply #4 on: December 18, 2020, 11:35:10 am »
Thank you for the advice and reply. I didnt know the setTexture function

 

anything