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

Author Topic: Drawing/Displaying sprites from another function  (Read 10362 times)

0 Members and 1 Guest are viewing this topic.

fiksy

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Drawing/Displaying sprites from another function
« on: October 21, 2012, 06:55:26 pm »
Drawing/Displaying sprites from another function

I've been learning c++ and recently started learning how to use SFML (I'm a newbie).

I made this game (see attachment) with Code::Blocks and came over this question.

When i'm using the term "main function" i refer to the "mainLoop function" in the attachment.

Can i draw/display anything in my window from another function?
like, if i had a main function that rendered a window and contained the main loop of my game, Could i make an "Enemy" function that displayed/drew anything in my main functions window?

If my function was returning anything, i would just have a value, which is not what i am looking for. I want the function to display something on my window and therefore i set it as a "void" type of function. However, if the function contains AI that interacts with other functions (eg. the Player) in the game, i would need to connect all the variables for all the sprites on the screen together so that they can interact with each other.

Is there any easy solution for this?  ???

If not, i would love to have some suggestions on how to organize all of my sprites in the main function :) it gets kinda messy in my main function  :-\

[attachment deleted by admin]

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Drawing/Displaying sprites from another function
« Reply #1 on: October 21, 2012, 07:02:06 pm »
Is there any easy solution for this?  ???
Yes, learn C++ first. ;)
Seriously SFML is not a library with which one should learn very basic things like functions, classes, return types, types in general, references, etc. For those topics there exists hundreds of books and thousands of tutorials.
You also might want to read things about the object oriented programming (OOP) pattern.

To answer your question: Yes you can draw from different functions. You can for instance pass the render window as reference to the function and draw things from there. But if you want to make things a bit cleaner I'd use different classes to do so. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

fiksy

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Drawing/Displaying sprites from another function
« Reply #2 on: October 22, 2012, 09:24:01 am »
You can for instance pass the render window as reference to the function and draw things from there.

I tried to pass it as reference but whenever i use a command such as *Window.draw(Sprite) in the function i get this error: error: request for member 'draw' in 'Window', which is of non-class type 'sf::RenderWindow*'

Why do i get this error?

Here's the code. It is just a test code with only 2 functions. soooo, it's really easy to understand.

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

void Test(sf::RenderWindow *Window)
{
    sf::Texture Texture;
    Texture.loadFromFile("GreenCircle.png");

    sf::Sprite Sprite;
    Sprite.setTexture(Texture);
    Sprite.setPosition(300, 300);

    *Window.draw(Sprite);
}

int main()
{
    //Declaring and defining the window
    sf::VideoMode VMode (640, 480, 32);
    sf::RenderWindow Window (VMode, "Standard window application", sf::Style::Default);

    //Declaring the Event
    sf::Event Event;

    //The main loop
    while (Window.isOpen())
    {

        Test(&Window);

        while (Window.pollEvent(Event))
        {
            // Escape key : exit
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
                Window.close();
        }
        Window.display();
    }

return 0;
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Drawing/Displaying sprites from another function
« Reply #3 on: October 22, 2012, 09:27:24 am »
Like I said learn C++ first because you used a pointer and not a reference...

Also you should always separate logic & drawing and the drawing block should always be: clear, draw, display
« Last Edit: October 22, 2012, 09:30:23 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Drawing/Displaying sprites from another function
« Reply #4 on: October 22, 2012, 05:18:55 pm »
You should also put event polling at the end of the loop, that way if you close the window it won't keep on drawing, displaying and so on with a closed window.
« Last Edit: October 22, 2012, 05:21:15 pm 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!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Drawing/Displaying sprites from another function
« Reply #5 on: October 22, 2012, 05:25:55 pm »
You should also put event polling at the end of the loop, that way if you close the window it won't keep on drawing, displaying and so on with a closed window.
That's more of a detail and it will mostly work the other way around too, the draw calls will just have no effect. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Drawing/Displaying sprites from another function
« Reply #6 on: October 22, 2012, 05:42:22 pm »
You should also put event polling at the end of the loop, that way if you close the window it won't keep on drawing, displaying and so on with a closed window.
That's more of a detail and it will mostly work the other way around too, the draw calls will just have no effect. ;)

It's still nicer anyway. And depending on what your loop is doing it can save you from ugly errors, even though when ideally it shouldn't happen.
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!

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Drawing/Displaying sprites from another function
« Reply #7 on: October 22, 2012, 08:57:17 pm »
your float to string function can be written in about 5 lines if you use the C++ header <stringstream>


mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Drawing/Displaying sprites from another function
« Reply #8 on: October 24, 2012, 12:29:30 am »
void Test(sf::RenderWindow *Window) // Change * (pointer) to & (reference)
{
    sf::Texture Texture; // HERE you are creating a local texture object
    Texture.loadFromFile("GreenCircle.png");

    sf::Sprite Sprite; // Local sprite
    Sprite.setTexture(Texture);
    Sprite.setPosition(300, 300);

    *Window.draw(Sprite); // Here you are "drawing" to a buffer (it will never be seen on screen until
                      // you call Window.display(), because SFML uses double buffer)

} // HERE your texture and sprite are GONE, because they are local to the function

int main()
{
   ...

        Window.display(); // So here you have nothing to display
    }

return 0;
}
 
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing/Displaying sprites from another function
« Reply #9 on: October 24, 2012, 08:13:23 am »
Quote
// HERE your texture and sprite are GONE, because they are local to the function
// So here you have nothing to display
When the sprite is drawn, it still exists. So there's nothing wrong about it.
Laurent Gomila - SFML developer

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Drawing/Displaying sprites from another function
« Reply #10 on: October 25, 2012, 07:55:22 pm »
Ok, it will be drawn, my bad  :-X
But it´s not the correct way to handle the lifetime of the objects
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing/Displaying sprites from another function
« Reply #11 on: October 25, 2012, 08:06:06 pm »
Sprites can be local temporary objects, but textures should definitely not.
Laurent Gomila - SFML developer