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

Author Topic: Passing sf::RenderWindow to multiple .cpp files  (Read 4087 times)

0 Members and 1 Guest are viewing this topic.

dusan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Passing sf::RenderWindow to multiple .cpp files
« 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 ?
« Last Edit: May 29, 2012, 06:11:14 pm by dusan »

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Passing sf::RenderWindow to multiple .cpp files
« Reply #1 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.
« Last Edit: May 29, 2012, 06:16:31 pm by Perde »

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Passing sf::RenderWindow to multiple .cpp files
« Reply #2 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.

dusan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Passing sf::RenderWindow to multiple .cpp files
« Reply #3 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Passing sf::RenderWindow to multiple .cpp files
« Reply #4 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. :-)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

capz

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Passing sf::RenderWindow to multiple .cpp files
« Reply #5 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

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).
« Last Edit: June 10, 2012, 02:09:48 am by capz »

 

anything