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

Author Topic: Entire game in single cpp file?  (Read 12377 times)

0 Members and 1 Guest are viewing this topic.

GnoEvenimente

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Entire game in single cpp file?
« on: March 27, 2021, 10:28:12 am »
Hello all, I'm an SFML beginner. I have a bit of C++ experience, so I'm familiar with OOP and all that (maybe intermediate level), but I was just wondering: I'm making a very simple text based game, with a few basic graphics and sound elements. would it be really frowned upon to have my entire game in the main.cpp?

For me it's just easier to keep track of everything than dealing with multiple class files, but all the SFML tutorials I see say that's the norm. For bigger projects I can see how keeping classes and functions separated makes sense, but is there really a downside doing it all in one file for a small little game?     

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Entire game in single cpp file?
« Reply #1 on: March 28, 2021, 04:22:47 pm »
i think this section is about other languages (and your post will probably be moved), but anyway...
talking specifically about c++, dividing your code across multiple files is useful not only to organize it, but also to improve compile time. if you have 100 source code files and you change one file with 100 lines of code, you just recompile that one file.
on the other hand, if all you code is together, when you change a letter you have to recompile all your project again. it becomes really time consuming in big projects.

for a single-man project, there is no rule for that if you feel more comfortable doing everything in a single source file. but believe me, soon you'll want to split it  ;D
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Entire game in single cpp file?
« Reply #2 on: April 11, 2021, 04:27:25 pm »
Multiple files only decrease compile if you carefully check dependencies, and possibly use the PImpl idiom. If you have a #include <SFML/Graphics.hpp> + several standard includes in every header file, your code is almost guaranteed to compile slower than doing the same thing in a single header file. After all, the compiler needs to re-compile the same code over and over (unless you use precompiled headers).

You can definitely start with one file, C++ leaves it up to you how you organize your code. But once your code grows beyond a few hundred lines, it really gets harder to navigate. At some point, compile times do come into play, because that one file (and thus your entire code) needs to be recompiled on every change, no matter where.

Consider this: you have files Zombie.hpp and Zombie.cpp which implement a class. The header is simply the public interface + some member variables. The logic how the zombie moves is written in the .cpp file. Now if you want to modify something, e.g. make it faster to move or change how it interacts with certain objects, the compiler only has to recompile Zombie.cpp + all the headers it includes. But it does not have to recompile Player.cpp, Item.cpp and Orc.cpp. They are independent translation units.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MaxWilson

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Entire game in single cpp file?
« Reply #3 on: April 14, 2021, 03:12:28 pm »
Can you suggest some intensive C++ tutorial?

Paul

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Entire game in single cpp file?
« Reply #4 on: April 14, 2021, 09:46:56 pm »
Cherno's C++

https://www.youtube.com/watch?v=18c3MTX0PK0&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb
« Last Edit: April 14, 2021, 09:49:06 pm by Paul »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Entire game in single cpp file?
« Reply #5 on: April 21, 2021, 05:17:19 pm »
https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Youtube tutorials are often low-quality and gloss over lots of important topics. C++ is probably the most complex language (at least of those actually used in the industry), it's entirely impossible to learn it in a few days.

That being said, if you want to learn a new language in 2021, I'm not sure if C++ is still the best choice. I would rather recommend Rust (if you want to have the same level of control), which is significantly more modern and easier to use than C++, and does a lot of things better.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything