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

Author Topic: Member function sprite call  (Read 8184 times)

0 Members and 1 Guest are viewing this topic.

Jiggered

  • Newbie
  • *
  • Posts: 9
    • View Profile
Member function sprite call
« on: August 29, 2012, 02:43:24 pm »
Hi,
I used the example that is on the sfml doc pages that uses member functions as image management. http://www.sfml-dev.org/tutorials/1.4/graphics-sprite.php

And I implemented it fine, no errors. But I'm confused as how to call it? Because you need to apply it to the render window and call the sprite itself, but the render window is defined in my main, and my sprite is defined inside my class.

sgehlich

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Member function sprite call
« Reply #1 on: August 29, 2012, 03:11:42 pm »
Pass the window to your class and call window->draw(mySprite);, maybe? I'm pretty new to SFML, but that's kind of how I did it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Member function sprite call
« Reply #2 on: August 29, 2012, 04:52:39 pm »
Are you aware that the tutorial you're looking at is for SFML 1.4 which is even older than SFML 1.6 and that is already over 2 years old? :D
Sprites are drawn with a window.draw(sprite) call. So it highly depends on what your class structur looks like.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jiggered

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Member function sprite call
« Reply #3 on: August 30, 2012, 12:37:34 am »
Yeah I'm aware that the usual call is "window.draw(sprite)". But because my sprite and image are loaded via my class file, I can't call them using my main function because the window is defined in my main, not my class.

My class:

class Invador
{
public :

    static bool Init(const std::string& ImageFile)
    {
        return alien.LoadFromFile("sprite.png");
    }

    Invador()
    {
        sprite.SetImage(alien);
                sprite.SetPosition(100,100);
    }

private :

    static sf::Image alien;

    sf::Sprite sprite;
};

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Member function sprite call
« Reply #4 on: August 30, 2012, 07:40:49 am »
hi
this question bothered for a long time, too. it's a C++ question, not SFML.
for this, you should use the reference and dereference operators.
you need a function inside your class that gets the variable from main(). like this:

int classFunction (sf::Renderwindow* window){
    window->draw(sprite); //here you use -> instead of a dot.
    return 0;
}

and in the main(), when you call the classFunction:

invadorObject.classFunction(&window);

hope that it helps :)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Member function sprite call
« Reply #5 on: August 30, 2012, 08:22:12 am »
Another way would be to derive from sf::Drawable and then implement a draw function, thus you can then call from outside window.draw(yourClassInstance) and inside your class in the void draw(sf::RenderTarget& target, RenderStates states) const function you can call target.draw(mySprite, states).
See the documentation for an example.

If you don't like this way you can pass a reference to the RenderWindow in the constructor.

There's no perfect way that matches every possibility, it's pure code design, i.e. what fits the given problem of yours best. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jiggered

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Member function sprite call
« Reply #6 on: August 30, 2012, 03:16:48 pm »

int classFunction (sf::Renderwindow* window){
    window->draw(sprite); //here you use -> instead of a dot.
    return 0;
}

and in the main(), when you call the classFunction:

invadorObject.classFunction(&window);

hope that it helps :)

Thanks for the replies, I'm just testing both and seeing which would help me in the long run.
But in this example I don't really know what you meant by

invadorObject.classFunction(&window);

I just get the error "a nonstatic member reference must be relative to a specific object" when I use the call

Invador.draw(&window);

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Member function sprite call
« Reply #7 on: August 30, 2012, 03:23:09 pm »
If you want to define a draw function, then It's suggested to derive from sf::Drawable, so you can call
window.draw(object);
rahter than
object.draw(window);

But both work fine. ;)

Other than Stauricus suggested I'd use a reference to the window rather than a pointer, e.g.
void MyClass::draw(const sf::RenderWindow& window)
{
    window.draw(m_sprite);
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jiggered

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Member function sprite call
« Reply #8 on: August 30, 2012, 03:43:56 pm »
Ok so going from the draw function, I get an error about the pointer

Error:
Quote
error C2662: 'sf::RenderTarget::Draw' : cannot convert 'this' pointer from 'const sf::RenderWindow' to 'sf::RenderTarget &'

void Invador::draw (const sf::RenderWindow& window)
{
        window.Draw(sprite);
}

the "window" part of the "window.draw(sprite)" gets the error.
« Last Edit: August 30, 2012, 03:45:44 pm by Jiggered »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Member function sprite call
« Reply #9 on: August 30, 2012, 03:49:53 pm »
Don't use SFML 1.6 is over 2 years old and has some nasty bugs, also there are already binaries for SFML 2rc. ;)

As for you problem you don't have to call Invador.draw(&window) when using a reference but Invador.draw(window).
I guess it would be good if you read again a bit in your C++, what pointers are, how to derefence them etc and what references are. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TheSilverWebsurfer

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Member function sprite call
« Reply #10 on: September 03, 2012, 03:59:55 pm »
Hi everyone!

I followed those posts, and that helped me a lot, but when I try to draw a instance of my class, derived from sf::Sprite, it appears black on the screen, with no error listed...
I tried many things, being quite a beginner, but nothing seems to work...
My image manager is working, thanks to one of the tutorials on this site, because spriteMenuDebutPetit is correctly displayed and there's no error log in the console.
Could anyone help me?

Here is my class header:

#include <string>
#include <SFML/Graphics.hpp>

#include "ImageManager.h"

class InterfaceElement : public sf::Sprite
{
public:
        InterfaceElement (const std::string& imageToLoad, int positionX, int positionY, ImageManager ImageManager);
       
        InterfaceElement::~InterfaceElement ();

        void draw (sf::RenderWindow* window);

private:
        sf::Sprite m_sprite;
};
 
my class .cpp:
#include <SFML/Graphics.hpp>
#include <map>
#include <string>
#include <iostream>

#include "InterfaceElement.h"
#include "ImageManager.h"

InterfaceElement::InterfaceElement ( const std::string& imageToLoad, int positionX, int positionY, ImageManager imageManager)
{
        sf::Sprite m_sprite;
       
        m_sprite.setTexture (imageManager.getTexture (imageToLoad) ) ;
        m_sprite.setPosition (positionX, positionY);
}

InterfaceElement::~InterfaceElement ()
{
}

void InterfaceElement::draw (sf::RenderWindow* window)
{
        window->draw (m_sprite);       
}

and my main.cpp:
#include <SFML/Graphics.hpp>

#include "ImageManager.h"
#include "InterfaceElement.h"


int main ()
{
        sf::RenderWindow Window (sf::VideoMode (1024, 640), "", sf::Style::Titlebar | sf::Style::Close);

        ImageManager imageManager;
       
        InterfaceElement background ("ressources/menu/fond_menu_lancement.png", 0, 0, imageManager);

        sf::Sprite spriteMenuDebutPetit;
        spriteMenuDebutPetit.setTexture (imageManager.getTexture ("ressources/menu/menu_debut_petit.png") );
        spriteMenuDebutPetit.setPosition ((1024/2-384/2), (640-384));

        InterfaceElement secondMenu ("ressources/menu/pwned-DH.jpg", (1024/2 - 384/2), (640-384), imageManager);

        while (Window.isOpen())
        {
                sf::Event Event;
                while (Window.pollEvent (Event) )
                {
                        switch (Event.type)
                        {
                        case sf::Event::Closed:
                                Window.close();
                                break;
                        default:
                                break;
                        }
                }

                background.draw (&Window);
                Window.draw (spriteMenuDebutPetit);
                Window.draw (secondMenu);
                               
                Window.display ();
        }

        return 0;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Member function sprite call
« Reply #11 on: September 03, 2012, 04:25:31 pm »
Quote from: TheSilverWebsurfer
I followed those posts, and that helped me a lot, but when I try to draw a instance of my class, derived from sf::Sprite, it appears black on the screen, with no error listed...
I tried many things, being quite a beginner, but nothing seems to work...
My image manager is working, thanks to one of the tutorials on this site, because spriteMenuDebutPetit is correctly displayed and there's no error log in the console.
Could anyone help me?
You shouldn't hijack someone else's thread. Nexttime if you have a problem, create your own thread. ;)

Within your constructor of the InterfaceElement class, you create sf::Sprite m_sprite in the local scope of the constructor. Then you set the texture to that local sprite. But since it's only in the constructor scope it will get 'deleted' as soon as the constructor is left and the member variable hasn't been touched at all.
Remove the line with the local sprite decleration and it should work.

Also where do you derive from sf::Sprite? ???
« Last Edit: September 03, 2012, 04:27:42 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TheSilverWebsurfer

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Member function sprite call
« Reply #12 on: September 03, 2012, 05:04:01 pm »
Dammit, I hesitated many times before hijacking this thread, guess I failed this one... ^^
So my apologies to Jiggered, that was pretty rude.

Thanks for your answer.

I deleted the "sf::Sprite m_sprite" line in the constructor, and my sprites don't appear black anymore...
Now they're white... ^^
Nothing else changed.

Deleted the "public: sf::Sprite" line in the class header.
I realized that was pointless after your "where do you derive from sf::Sprite" remark.
Nothing changed.

And nicely done: 1000th post! ;)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Member function sprite call
« Reply #13 on: September 03, 2012, 05:22:50 pm »
Deleted the "public: sf::Sprite" line in the class header.
I realized that was pointless after your "where do you derive from sf::Sprite" remark.
Oh now I've seen it. ;D

And nicely done: 1000th post! ;)
Thanks! ;)

I deleted the "sf::Sprite m_sprite" line in the constructor, and my sprites don't appear black anymore...
Now they're white... ^^
This means that the texture got lost somewhere on the way from loading to display it. My guess would be that this happens at the point where you hand over your image manager by value instead of a reference on the constructor of InterfaceElement. ;)

What happens if you change the decleration to the following code:
InterfaceElement (const std::string& imageToLoad, int positionX, int positionY, ImageManager& ImageManager);
Or maybe even a const ref
InterfaceElement (const std::string& imageToLoad, int positionX, int positionY, const ImageManager& ImageManager);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TheSilverWebsurfer

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Member function sprite call
« Reply #14 on: September 03, 2012, 06:02:37 pm »
Problem solved!

My (beautiful ;) ) background appears at last!

Many thanks for solving this problem!
« Last Edit: September 03, 2012, 06:07:26 pm by TheSilverWebsurfer »