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;
}
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;
}