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

Author Topic: Image Class Question  (Read 3523 times)

0 Members and 1 Guest are viewing this topic.

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Image Class Question
« on: January 07, 2013, 04:30:42 am »
class Graphics
{

    public:

        Graphics();

        ~Graphics();


        sf::Sprite GetSprite(){return spriteBJtable;}
        sf::Image GetImage(){return BJtable;}


                void loadBJtable(std::string file)
                {
                        BJtable.LoadFromFile(file);
                }

                void sprite_loadBJtable(sf::Image& pic)
                {
                        spriteBJtable.SetImage(pic);
                        spriteBJtable.Resize(800,600);
                }


    private:

        sf::Image BJtable;
        sf::Sprite spriteBJtable;

};


 

I have been trying to work out a class that will handle my graphics. The issue that the image and sprite are staying private despite my return. After search around, I have run into similar code and tried some other methods; however, I have been met with a wall here. I apologize if this is a dumb question, but I have just started learning classes, and the book does not show any other methods other than get and set. Any help would be greatly appreciated.   
« Last Edit: January 07, 2013, 08:41:15 am by Laurent »

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Image Class Question
« Reply #1 on: January 07, 2013, 04:56:52 am »
You return a copy and not a reference make the change and the try it.

Also why not try SFML 2.0? It has less bugs and nice features, like better handling in the low level API and more overall control of what you can do. 1.6, despite being the current "official" release hasn't been touched in two years due to the version shift.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Image Class Question
« Reply #2 on: January 07, 2013, 05:05:37 am »
When I started using SFML I had no idea there was a working SFML 2.0, so I started with 1.6. I just found out while doing research that 2.0 was available, I just have not made the switch. I plan on making the switch to SFML 2.0 soon! By the way, thank you for your help!

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Image Class Question
« Reply #3 on: January 07, 2013, 05:29:29 am »
I have not had any luck attempting to return a reference. I may be doing it wrong as my book does not explain references in this context, and it is a concept I have not completely grasped. Is there any good reference to this material?

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Image Class Question
« Reply #4 on: January 07, 2013, 05:39:37 am »
sf::Sprite& GetSprite(){return spriteBJtable;}
 

The & may seem like it changes nothing, but it's actually very important with things such. You don't need to copy around the sprite, you just need a reference to it if you want to make another sprite the same as the sprite contained in the class.

If you want to draw the sprite that is contained in the class then have it inherit from sf::Drawable and draw your sprite in the virtual draw function. The function is public, so you can draw the sprite without directly having access to it.

Quote
I may be doing it wrong as my book does not explain references in this context, and it is a concept I have not completely grasped. Is there any good reference to this material?

A basic C++ book should help you, I learned parts of it from my uni and some others on my own on the net, so I can't really recommend you a specific book about it, sorry.
« Last Edit: January 07, 2013, 05:41:11 am by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Image Class Question
« Reply #5 on: January 07, 2013, 05:49:55 am »
"If you want to draw the sprite that is contained in the class then have it inherit from sf::Drawable and draw your sprite in the virtual draw function."

  I think I see what you are saying. I am getting the same error because I have not inherited from sf::drawable or attempted to draw the sprite in a virtual draw function. I think this was easier when I was okay with the sprite being a public member lol. Alright, inherit and virtual functions, two things I have not worked with. Going to do some reading and testing! Thank you for your help! If you wish to add anything feel free. ;)


masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Image Class Question
« Reply #6 on: January 07, 2013, 06:14:46 am »
The only thing I would add is that having class members private is more often than not a good idea and a good programming practice, it just takes time getting used to, but it's definitely better to start learning good practices first regardless of their difficulty, eventually they'll become natural. Good luck.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Image Class Question
« Reply #7 on: January 07, 2013, 06:18:47 am »
I completely agree. That is the main reason why I am focusing on making sf::image and sf::sprite private variables. It is easy to take the easy way out, but I want to learn good programming practice as it will be of great benefit in the end; however, that can be difficult considering I am not even sure where to start haha. I am just doing some reading to try to figure out how to inherit from sf::drawable and virtual functions. Thank you all for your responses.

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Image Class Question
« Reply #8 on: January 07, 2013, 07:06:43 am »
class Graphics : public sf::Drawable
{

    public:

        Graphics(){};

        ~Graphics(){};


        sf::Sprite &GetSprite(){return spriteBJtable;}
        sf::Image GetImage(){return BJtable;}



    virtual void Draw(RenderWindow& App)
     {
        App.Draw(spriteBJtable);
     }




                void loadBJtable(std::string file)
                {
                        BJtable.LoadFromFile(file);
                }

                void sprite_loadBJtable(sf::Image& pic)
                {
                        spriteBJtable.SetImage(pic);
                        spriteBJtable.Resize(800,600);
                }


    private:


        sf::Image BJtable;
        sf::Sprite spriteBJtable;

};


 

Can anyone let me know if I am on the right track here or am I completely off base?
« Last Edit: January 07, 2013, 08:41:27 am by Laurent »

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Image Class Question
« Reply #9 on: January 07, 2013, 08:37:18 am »
I'm continuing to read and plug away at this.

 Currently:
class Graphics : public sf::Drawable // this should set up the inhert

//and

virtual void Render(sf::RenderTarget& App) const
{

    sf::Image bjtable;
    bjtable.LoadFromFile("realbj.jpg");

    sf::Sprite spriteBtab;
    spriteBtab.SetImage(bjtable);
    spriteBtab.Resize(800,600);

    App.Draw(spriteBJtable);
}

//is what I have for my draw function
 

I noticed that if I tried to use any other parameters for the virtual function the compiler complained about it being a pure virtual function. I am assuming it is pure because it is taking
virtual void Render(sf::RenderTarget& Target) const
 

from drawable.hpp...

The issue is that my current draw function is not drawing anything. I am getting a default black screen.


Also

virtual void Render(sf::RenderTarget& App) const
{

    BJtable.LoadFromFile("realbj.jpg");

    spriteBJtable.SetImage(BJtable);
    spriteBJtable.Resize(800,600);

    App.Draw(spriteBJtable);
}

 

I tried this to see if the sprite and image were being passed in, and I am receiving some errors.

what am I missing here?
« Last Edit: January 07, 2013, 08:41:47 am by Laurent »

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Image Class Question
« Reply #10 on: January 07, 2013, 08:44:29 am »
Solved!!! Ty everyone for your help..  here is the solution for anyone else running into this problem ;)

class Graphics : public sf::Drawable
{

    public:

        Graphics(){};

        ~Graphics(){};


        sf::Sprite &GetSprite(){return spriteBJtable;}
        sf::Image GetImage(){return BJtable;}


virtual void Render(sf::RenderTarget& App) const
{


    App.Draw(spriteBJtable);
}

                void loadBJtable()
                {
                        BJtable.LoadFromFile("realbj.jpg");
                }

                void sprite_loadBJtable()
                {
                        spriteBJtable.SetImage(BJtable);
                        spriteBJtable.Resize(800,600);
                }



    private:

        sf::Image BJtable;
        sf::Sprite spriteBJtable;

};

 
« Last Edit: January 07, 2013, 08:49:20 am by Laurent »

 

anything