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

Author Topic: Help with splitting my project into multiple files  (Read 2191 times)

0 Members and 1 Guest are viewing this topic.

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Help with splitting my project into multiple files
« on: July 23, 2012, 05:50:11 pm »
I'm splitting my large main.cpp into multiple files for better organization right now.  What I was wondering was where I put the SFML include headers. Here's what my project looks like so far:

main.cpp //This is the file that contains all my code
graphics2D.h //These are the files that my code will be distributed into
graphics2D.cpp
soundAndMusic.h
soundAndMusic.cpp
networking.h
networking.cpp

Do I #include <SFML/component.hpp> in the component header and .cpp files, or just the header files? Do I need to include SFML in main.cpp, even if I'm only calling functions in the seperate modules?
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10924
    • View Profile
    • development blog
    • Email
Re: Help with splitting my project into multiple files
« Reply #1 on: July 23, 2012, 06:34:48 pm »
I'd suggest to use *.hpp instead of *.h since you're using C++ and not C it will make it easier to determin which headers are written in C++ and which could be written in C. ;)

Since you always include the header file in the corresponding source file, you won't have to include both headers in the X.cpp and the X.hpp, but other than that you should never make assumptions like: 'I don't have to include it in X.hpp since I'm including Y.hpp which has alreay included the SFML header.' Because you never know if you'll include the header file in another file.

Next it's adviced to include as late as possible. For instance most of the time you'll never need the std::cout in a header file since the header files only hold the class decelerations, but you may use it in the coresponding source file. Thus you include it in the source file and not in the header file.

And finally if you're just using a pointer or a reference to a class X in a header file Y, you don't have include the header file for X but you can use a forward decleration (e.g. class X;) and include the header of X later in the source file.

Overall it's just an optimization for compiling and not that important.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Makuto

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
    • Au 79 Games
    • Email
Re: Help with splitting my project into multiple files
« Reply #2 on: July 23, 2012, 10:58:58 pm »
I had some problems with templates, but other than that, everything is working! Thanks for the help!
Macoy Madson-Au 79 Games
Check out my work at http://augames.f11.us/
Most of my SFML-related code is here: https://github.com/makuto/personal/tree/master/gameDev/resources/base2.0
I try to KIS(S), do you?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10924
    • View Profile
    • development blog
    • Email
Re: Help with splitting my project into multiple files
« Reply #3 on: July 23, 2012, 11:48:42 pm »
I had some problems with templates...
Yeah templates have to be defined in the header file. ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything