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

Author Topic: [SOLVED] Accessing the window in other functions  (Read 4366 times)

0 Members and 2 Guests are viewing this topic.

dezdapez

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] Accessing the window in other functions
« on: July 14, 2010, 01:11:10 am »
Hi I'm new to SFML and I'm trying to get a basic tile engine working, however when I try to pass a RenderWindow object to my Tile class I get an error: "cannot access private member declared in class 'sf::NonCopyable'".

Is there a way to pass some instance of the window to my class to directly change it? If not is there some way I can pass the window's info to my class so I can draw on it and return it back to the class where it's initialized and update the window with the return value?

If neither of those is possible how can I draw to my window from another class without setting the variable as public and directly calling it?

Thanks for the help,

-dez

Otto

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] Accessing the window in other functions
« Reply #1 on: July 14, 2010, 03:06:04 am »
Sounds like you're passing by value and not by reference.

For the function inside your tile class, if you want to modify the RenderWindow, it should look something like
Code: [Select]

void function(sf::RenderWindow& myRenderWindow)
{
...
}


Note the ampersand(&) next to sf::RenderWindow. If you

If your code looks more like this:
Code: [Select]

void function(sf::RenderWindow myRenderWindow)
{
...
}


then it'll try to pass by value (note the lack of the ampersand), which means its trying to copy the RenderWindow instead of sharing the same instance. Atleast, I'm assuming this is what is causing your problem, since you're getting an issue with sf::NonCopyable.


Although, if all you want to do is render your tile class to the RenderWindow each frame, make sure your Tile Class inherits from sf::Drawable, and define the Render function
Code: [Select]

virtual void Render(sf::RenderTarget& Target) const;


That way, you just put your rendering stuff within Render(..) and call
Code: [Select]

App.Draw(myTileClass)

and it'll draw your tile class. Assuming App is your RenderWindow.

dezdapez

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] Accessing the window in other functions
« Reply #2 on: July 14, 2010, 04:51:03 am »
thanks a bunch man, that fixed it.

That's originally what I thought was wrong when I read "non-copyable". I'm used to programming in Java/C# where pointers are passed instead of values.

I kept trying the asterisk and it didn't work. I'm guessing that points to the memory position and ampersand points to the actual data?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
[SOLVED] Accessing the window in other functions
« Reply #3 on: July 14, 2010, 09:41:25 am »
Yes. Reference (&) have the same (except for polymorphism) behavior than «normal» var.

If you use pointer (*) (which is not the solution I'll take) you must deference it. It becomes harder to use.

A short example :
Code: [Select]
void function(Human* ptr) {
  if (ptr->hasCheese()) {
    std::cout << "Mice are happy";
  }
}
SFML / OS X developer

 

anything