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

Author Topic: Sprites inside a Class  (Read 10153 times)

0 Members and 1 Guest are viewing this topic.

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Sprites inside a Class
« on: April 11, 2016, 03:48:55 pm »
hi, i have created a class called "Player"

in its header
"Player.h"
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

using namespace std;

class Player
{
public:
 sf::Sprite sprite(){
         sf::Texture texture;
            texture.loadFromFile("gunship.png");
            texture.setSmooth(true);
        sf::Sprite playersprite;
        playersprite.setTexture(texture);
        return playersprite;        
}
 

its cpp file

Player.cpp
#include "Player.h"
Player::Player()
{

}
 

what i am trying to do is to create an object from this class from another cpp file called

newMap.cpp
#include "NewMap.h"
#include "Player.h"
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
int mapPlayers()
{
    sf::RenderWindow map1(sf::VideoMode(1180, 600), "GUNSHIP", sf::Style::None);
            sf::Font font;
            if (!font.loadFromFile("emulogic.ttf"))
            return EXIT_FAILURE;

    sf::Text gametitle("GUNSHIP", font, 54);
            gametitle.setPosition(300,50);

 Player p1;                //created an object p1 from the class player

while(map1.isOpen())
            {
                map1.draw(gametitle);
                while(map1.pollEvent(event))
                {

                }
            map1.draw(p1.sprite());     //i want to draw the sprite of the object P1 from the class Player
            map1.display();
            }
    return 0;
}
 

in the main.cpp
#include "NewMap.h"
 main()
{
mapPlayers();
return 0;
}
 

my code does not have throw an error but it crashes everytime i execute it.
the first result that i had was a blank white rectangle. please help
« Last Edit: April 11, 2016, 04:20:14 pm by dove96 »
-dove96-

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Sprites inside a Class
« Reply #1 on: April 11, 2016, 04:24:17 pm »
Quote
the first result that i had was a blank white rectangle. please help

In your code :

sf::Sprite sprite(){
         sf::Texture texture;
            texture.loadFromFile("gunship.png");
            texture.setSmooth(true);
        sf::Sprite playersprite;
        playersprite.setTexture(texture);
        return playersprite;        
}

You create the texture in the sprite() function, but once it reaches the end of the function, the texture is free. When you want to display your sprite later, the texture is not in memory anymore, leading to a blank square. You need to keep track of your texture untill the destruction (or the use) of your sprite.

More informations http://www.sfml-dev.org/faq.php#graphics-white-rect


Please, be careful when you create code. You do not define you default construtor, but you implement it in your cpp file. However, you implement your sprite function in the header whereas you should implement it in the cpp. It seems like a lack of basic coding competencies, you should maybe go back to the basics.
« Last Edit: April 11, 2016, 04:27:45 pm by nicox11 »

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Sprites inside a Class
« Reply #2 on: April 11, 2016, 04:35:51 pm »
i saw the link that you said. but i do not really know how to keep the texture "alive".

im sorry im still a beginner that is why.
-dove96-

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Sprites inside a Class
« Reply #3 on: April 11, 2016, 04:39:03 pm »
Quote
you should maybe go back to the basics.

what i was asking is how to make the sprite() function work properly. i am sorry if i lack basic coding competencies like you said, but you see i am a newb. i only use what i see on tutorials and those that i tried and worked as long as it worked.
« Last Edit: April 11, 2016, 05:36:14 pm by dove96 »
-dove96-

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Sprites inside a Class
« Reply #4 on: April 11, 2016, 05:07:55 pm »
mapPlayers() is not a constructor. Sorry, but you seem to not know what you are doing. I recommand you to read a complete C++ tutorial before trying to play with SFML (and not only some parts). SFML need basics knowledge about C++ programming that it appears you don't have. The problem here is not really SFML related, but instead syntaxic C++ programming.

For your problem, there is multiples solutions. Basicly, you can keep an instance of your texture by adding it as private member of your class. Use something like a ressource manager or even other methods.

If you are convienced you have enough skills, I recommand you to seek for SFML book, and check for the online tutorial the official website provides.
« Last Edit: April 11, 2016, 05:09:29 pm by nicox11 »

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Sprites inside a Class
« Reply #5 on: April 11, 2016, 05:32:14 pm »
yeah my mistake on the term..i admit. but if you say that this is a syntactic error, then my program shouldnt have  executed.. the window i created should not have displayed..

nicox11. you are not really providing answers. what you are pointing here is if i have enough skills on c++ and sfml, well i admit i dont, THAT IS WHY I ASK QUESTIONS..
« Last Edit: April 11, 2016, 05:33:52 pm by dove96 »
-dove96-

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Sprites inside a Class
« Reply #6 on: April 11, 2016, 05:35:29 pm »
lets cut the discussion short.

when you say "You need to keep track of your texture untill the destruction (or the use) of your sprite."

how should i implement that in my code??????
-dove96-

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Sprites inside a Class
« Reply #7 on: April 11, 2016, 05:37:33 pm »
No reason to discourage others here.
If I may give some input:

You could store the texture as a member of the class to preserve it throughout the lifetime of the object. You can load the texture/etc in the constructor but store as a member so it doesn't get freed right after the constructor returns.

I would try something such as having a constructor take the name of the texture's file as an argument, or some other function to set that after object creation.  Just keep scope in mind when doing this - once something goes out of scope it will be freed (or a memory leak if not handled properly).  Your texture goes out of scope and is thus freed in the code you gave.  This could give strange behavior, and most certainly can cause a crash.

class Player
{
public:
    Player() = delete; //No default constructor - force the filename to be given
        Player(const std::string &filename);
        ~Player();
       
    //Data members, for later use
        sf::Texture m_texture;
        sf::Sprite m_sprite;
};

Player::Player(const std::string &filename) {
        //do whatever in the constructor
    //Load texture, sprite, etc
}

Player::~Player() {
        //Do whatever you need to in the destructor
}
 

That is incomplete, of course, but it's just an example.  If you decide to make the members private, you'll have to deal with how to you'll render it.  In my projects, for example, I follow the paradigm that anything and everything that needs to be allocated is done so at object creation (with some exceptions, of course). Textures, sprites, shapes, fonts, etc. are kept as members of that class.

I also recommend putting your definitions and declarations in different files (declarations in header files, definitions in cpp files).

But now when you want to render the Player, you can just do

window.draw(p1.m_sprite); //No function call 'm_sprite' is the data member
 

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Sprites inside a Class
« Reply #8 on: April 11, 2016, 05:40:32 pm »
I writed quickly, it's not syntaxic. I did provides some answers :

- Keep your texture as private member, for easy solution (but clearly not the best)
- Create a texture manager : https://www.google.fr/search?q=sfml+resource+manager&ie=utf-8&oe=utf-8&gws_rd=cr&ei=PcQLV5mrOYO6ateigcgN

If you think we will code for you, you are wrong. We can try to help "correct" your code :

In you class player, add a texture as private member instead of creating it in the function. This way, the texture will reamain untill the destruction of the player.

JayhawkZombie add a good base of what to do.