SFML community forums

Help => Graphics => Topic started by: dusan on May 29, 2012, 06:08:23 pm

Title: Passing sf::RenderWindow to multiple .cpp files
Post by: dusan on May 29, 2012, 06:08:23 pm
Hello,
I have started to create some application witch have a lots of functions. So I wanted to group out that functions in multiple .cpp files, but problem is that in every function there is some drawing so when it is in other .cpp file I cannot draw on window that I have rendered in my main.cpp. Is it possible to pass reference or window handle to function in other .cpp file ? I tried with sf::Window::getSystemHandle() but couldnt make it out.
Is there any other solution ?
Title: Re: Passing sf::RenderWindow to multiple .cpp files
Post by: Perde on May 29, 2012, 06:14:12 pm
Either I'm not getting what you're talking about or you should read through a chapter about using multiple header/source files in the C++ book of your choice. We're talking about really basic stuff here that you should try to understand first.
Title: Re: Passing sf::RenderWindow to multiple .cpp files
Post by: GarrickW on May 29, 2012, 07:06:13 pm
I am by no means a guru, being a self-taught hobbyist myself, but it sounds like you're doing what I did back when I first learned to program using Python - arbitrarily moving functions off into other .py files to avoid clutter. 

I don't think this is generally what is done in C++.  Normally you'd have classes handling various tasks, and you could easily pass a pointer/reference to an sf::RenderWindow as a parameter to whatever function in whatever class needs it (among other possible solutions), or give the class a pointer to the sf::RenderWindow as a data member.  Better programmers than me probably know better ways of doing this.

I'd take Perde's advice, it sounds like you're not too sure how to organize C++ code.  It's not too hard, though, just takes a bit of reading and practice.
Title: Re: Passing sf::RenderWindow to multiple .cpp files
Post by: dusan on May 30, 2012, 02:16:17 pm
Some things may be lost in translation so you didn't understood me well.
Anyway I found what I was looking for (two ways):
-reference
-using external methods
Ty for replays.
Title: Re: Passing sf::RenderWindow to multiple .cpp files
Post by: eXpl0it3r on May 30, 2012, 03:10:36 pm
Some things may be lost in translation so you didn't understood me well.

I don't think so or better said the other way around. ;)
It is possible to just 'export' functions to a seperate .cpp file but since you're writing C++ it's highly suggested to use classes and organize your functions in there.

Also if you don't get on your own to use references then I still advice to read a good C++ book, because that is basic stuff every C++ programmer has to know by heart. :-)
Title: Re: Passing sf::RenderWindow to multiple .cpp files
Post by: capz on June 10, 2012, 02:07:04 am
I would like to point out to watch out where you point those pointers ::)(pun intended :P). Using pointers without an extensive introduction to C++, will lead to problems that make no sense. Like functions that stop working when you uncomment a printf somewhere (happened to me back when I started)

I'd recommend at least reading up on pointers on this website (http://pweb.netcom.com/~tjensen/ptr/cpoint.htm)

Also, taking some time to look at ways to organize (the object oriented way) the parts that make up your application will HUGELY benefit your programming endeavours.

finally, in my SFML projects I make sure to always have a reference to the window in every object when they are instantiated, this way I can draw (and do some other stuff) anywhere in my code, without worrying about having access to the window, and without using global variables (booo, stay away from them).