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

Author Topic: Help required with windows and classes  (Read 1715 times)

0 Members and 1 Guest are viewing this topic.

KthulhuHimself

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Help required with windows and classes
« on: March 29, 2017, 11:12:58 am »
Alright, so I know that this may seem a beginner's problem to the most of you, and I am indeed a beginner when it comes to sfml and C++ as a whole; but with that being said, I didn't manage to find a solution to this problem anywhere else.

It's a pretty simple problem:

Let's suppose I have my main(), where I render some window named "window".

Now I create a class (I use visual studio), in which I want to have one of the member functions draw something in the window. If I add the line "window.draw(sprite);" to the function in the class, this obviously generates the error "Identifier 'window' not declared".

So I ask: How am I supposed to have my member function draw something in the main()'s "window"?

I thank you all in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help required with windows and classes
« Reply #1 on: March 29, 2017, 11:23:24 am »
Hi

Please note that this is the SFML forum, we try to avoid generic C++ problems as much as possible. There are many better places where you can ask this kind of questions (stackoverflow, ...) ;)

The solution is simple: if your function needs to access a variable that it doesn't know, pass it as an argument.

void draw(sf::RenderTarget& target) // sf::RenderTarget is the base class of sf::RenderWindow
{
    target.draw(...);
}

int main()
{
    sf::RenderWindow window;
    draw(window);
}
Laurent Gomila - SFML developer

KthulhuHimself

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Help required with windows and classes
« Reply #2 on: March 29, 2017, 05:01:59 pm »
Hi

Please note that this is the SFML forum, we try to avoid generic C++ problems as much as possible. There are many better places where you can ask this kind of questions (stackoverflow, ...) ;)

The solution is simple: if your function needs to access a variable that it doesn't know, pass it as an argument.

void draw(sf::RenderTarget& target) // sf::RenderTarget is the base class of sf::RenderWindow
{
    target.draw(...);
}

int main()
{
    sf::RenderWindow window;
    draw(window);
}

Thanks a lot! This worked perfectly!

Oh, and don't worry, next time I'll find something slightly more sfml related to ask about.  ;)

Turbine

  • Full Member
  • ***
  • Posts: 102
    • View Profile
Re: Help required with windows and classes
« Reply #3 on: March 30, 2017, 02:38:04 am »
I highly recommend reading this tutorial thoroughly before using SFML, it's consensed and straight to the point. But it would cover your question and a lot more:  http://www.cplusplus.com/doc/tutorial/