r/cpp • u/CelesticRose • Apr 23 '16
Just started coding with C++
I just started coding with C++ not too long ago, but I've had alot of experience with other coding languages (Java, Javascript, HTML, Python.) But if I'm aiming to make games with C++ one day (After years and years of practice of course) what do I need to learn? What books do you recommend? What exercises should I practice? As far as I know there's no website to 'teach' the subject (Like CodeAcademy and what not), so what would you recommend to get me started?
9
u/glaivezooka Apr 23 '16
By far the best advice I can give you is don't go looking for someone to give you the right answer. Aside from just learning the fundamentals, reading books on a language is mostly a waste of time. Make something. As in all languages, write simple code that does its job and nothing else.
Casey Muratori has a long running series where he makes a game/game engine from scratch, you can learn almost everything youll need to know from this series: https://www.youtube.com/user/handmadeheroarchive
Just remember that good code is simple, not complicated. So don't waste lots of time learning how to write complicated code.
Go start making something. Today.
5
u/mm256 Apr 23 '16
Just remember that good code is simple, not complicated. So don't waste lots of time learning how to write complicated code
Ironically, write simple code is complicated.
8
u/dodheim Apr 23 '16 edited Apr 23 '16
Aside from just learning the fundamentals, reading books on a language is mostly a waste of time.
Agreed in general, but not for C++ – this is a dense language.
(Not a criticism.)
Edit: ((Of the language..))
1
u/btlk48 Apr 23 '16
Copy that. Real world experience is dreadfully effective, when one comes to c++ (any language?)
1
u/lex99 Apr 23 '16
Wonderful advice. I'll add this: no need to be overly ambitious in your learning projects. You'll learn a ton from even a simple game done on a weekend.
8
u/_lerp Apr 23 '16
Effective Modern C++ by Scott Meyers
and IMO the thing that sets C++ apart is the metaprogramming, i.e. templates.
17
u/lex99 Apr 23 '16
I strongly disagree with advising newcomers to learn metaprogramming. It's the most advanced aspect of C++, hard to follow, write, and debug. You can go far in c++ without it.
I would instead advise the newcomer that c++ gives you total control of your system (modulo the compiler) and with it you can really understand how every action the computer takes is due to some value at a specific location in memory being operated on, or some system call being executed, and there's NO magic anywhere. For folks that came up in Python, ruby, JS, etc, there's a large abstraction between the code and the machine.
-1
u/flyingcaribou Apr 23 '16
I would instead advise the newcomer that c++ gives you total control of your system (modulo the compiler) and with it you can really understand how every action the computer takes is due to some value at a specific location in memory being operated on, or some system call being executed, and there's NO magic anywhere. For folks that came up in Python, ruby, JS, etc, there's a large abstraction between the code and the machine.
Why not just do C, then?
5
u/DarkLordAzrael Apr 23 '16
Generic code ( templates without metaprogramming ), function overlaoding, RAII, a standard library that is actually useful, operator overloading, a type system that isn't mostly just throwing out type info at every opportunity, need I go on?
-1
u/LowB0b Apr 24 '16
???
That has nothing to do with the first answer, he was advocating C++ because he says there "is nothing going on behind the scenes" (no magic) while in fact there is. The examples you cited are "magic" and I would even dare to say that the concepts you are talking about are for people who have been programming for a while.
C is so simple, the hardest thing about it is pointers. C++ is way more complex. I think the former is better for someone who wants to learn how code is treated by the machine.
3
u/lex99 Apr 24 '16
Actually, the Magic I was referring to was runtime magic, whereas a lot of C++'s features are compile-time. I would agree though that there's a lot of overlap or maybe perceived overlap. A new person might regard constructors and destructors as magic, yes.
The kind of magic I was referring to, out of langs like JS or Python, are things like function closures (and associated variable capture) and the fact that without pointers and memory references, we just have "objects" that somehow float in memory. Due to the pains of mem mgmt in C/C++, you'll never avoid the concrete reality of what an allocated object really is (otherwise, you just segfaulted or leaked memory).
1
u/lex99 Apr 24 '16
I'm saying metaprogrammer is too out there, hard to do, and unnecessary, for a new C++ to aim to learn. I don't think it's THE reason one would use c++.
But even without metaprogramming, C++ has lots of critical improvements over C.
I wasn't saying though "pick the absolute least-magical lang."
6
u/slavik262 Apr 23 '16
EMC++ is a valuable read, but is more about "pitfalls you should be aware of" and not "how to write modern, idiomatic C++" IMO.
4
u/lex99 Apr 23 '16
For a new person, the earlier Meyers Effecrtive C++ books might be better, pre-Modern. A lot of the items hold up, and modern focuses on 11/14 features.
8
u/MoTTs_ Apr 23 '16
That's a great book for OP to read eventually, but I think it's the wrong book for someone who's just starting with C++.
"A Tour of C++" is a thin book that can serve as an intro. "Programming: Principles and Practice Using C++" is a thicker and more detailed book that's still meant for newer programmers. And finally "The C++ Programming Language" is a highly comprehensive book meant for advanced programmers.
1
u/DarkLordAzrael Apr 23 '16 edited Apr 24 '16
It seems to me that a lot of what needed to be done with template metaprogramming is now possible with constexpr and metaprogramming is a lot less important than it was in c++98 or even 11.
1
u/dodheim Apr 24 '16 edited Apr 24 '16
It seems to me that you're contradicting yourself..? In reality, metaprogramming is a lot less important because of
constexpr...1
1
u/Crazy__Eddie Apr 25 '16
The effective series is not a beginner set of books. They don't teach the language at all in fact. They teach best practices.
Dude needs to start simpler and move UP to the effective and exceptional series. Accelerated C++.
3
u/newocean Apr 23 '16
Couple of things to note - you might also like the following subs:
/r/gamedev
/r/cpp_questionsI seem to be getting downvoted for this somewhat lately - but I recommend people learn Lua, IUP, CD and IM. (By Tecgraf) Lua is arguably the most used scripting language in games. Iup sets up windows and provides cross-platform widgets, IM loads, saves and processes images etc... CD is a 'canvas' tool for drawing in memory.
I also use SFML for controller support and audio.
Networking wise on the server end, I prefer Twisted/Python. I am actually currently shopping around for a good client-side C++ AMP pluggin. (Let me know if anyone has a good suggestion.)
If you want to PM me, I'll even send some contact info so you at least have someone to ask questions to.
2
Apr 23 '16
If you don't plan on using an ide learn GDB and valgrind.
1
Apr 25 '16
I find valgrind not too useful for games as a lot of testing has to be done in real gameplay, and valgrind slows things down way too much to make it possible to play while profiling.
I usually just us a class that takes the time between the constructor and destructor calls and dumps it into a file at shutdown. Crude but it catches the worst bottlenecks.
Also I have a shit computer.
1
u/Nunuvin Apr 23 '16
Well years is an exaggeration. Learn basic stuff such as different variables, arrays, structures. Then go to functions. Here you can choose. You can try to learn OpenGL and start working on graphics or try to make something in console. You can also learn classes, but honestly I am not really sure how helpful they are. At least for me they are confusing.
Try to start small projects. Ie tictactoe in a console with 2 players playing it on one pc. Then add saves and over email support (make it possible to save it to disk so it can be sent via email to another player). Then add ai if you want to. Feel free to PM, I am not the best at c++ but will try to help. On topic of books, I heard that effective c++ is decent. You can go to your nearest library or book store and look through avaliable literature. If you find something interesting consider reading it (if in store read first part and see how interested you would be). There is no one right answer see what works for you. If you do know all other languages well it should not be a difficult to have a rough idea where to start. If you a bit new then be ready to fail over and over again before you succeed (I have 10+yrs of that before I started finishing projects which I would start). EDIT: Again feel free to pm if you need help. I have lots of free time and reviewing c++ can be helpful for me XD
1
u/TheQuantumZero Apr 24 '16 edited Apr 26 '16
https://www.reddit.com/r/gamedev/wiki/index
C++
SDL/SFML
OpenGL
Box2D/Bullet
Lua for Scripting
1
u/heyheyhey27 Apr 24 '16
If you just want to make games, and aren't doing anything super special, I'd highly recommend looking into an engine with a higher-level language like Unity or Unreal. Unreal in particular basically uses a simpler subset of c++ along with a highly-polished visual scripting language.
1
u/RotsiserMho C++20 Desktop app developer Apr 24 '16
Stroustrup's book is a great way to learn the idiomatic basics of C++(11 and 14) but if you're already an experienced programmer it's probably going to be a bit tedious. There's also the C++ Core Guidelines which in addition to the Scott Meyers books will help you avoid common pitfalls.
1
u/zahirtezcan 42 Errors 0 Warnings Apr 25 '16
For years and years of learning: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
1
u/cpt-red-beard Apr 28 '16
I'd aim for https://www.safaribooksonline.com/library/view/c-primer-fifth/9780133053043/ and perhaps get a safari books subscription. It's fairly cheap, and worth it if you learn in stereo like I do(like multiple references, each explaining a different way). Otherwise, I also suggest learning by example. Pick a project, go through GitHub and find N ways to do what you want to do. There are hundreds of examples (I usually judge quality/impact by stars...maybe not the best way).
0
u/acemagex Apr 24 '16
As far as I know there's no website to 'teach' the subject.
Literally google "website to learn C++" and this is the second link. The first link is someone recommending this site.
3
u/[deleted] Apr 25 '16
/u/TheQuantumZero recommended the best libraries but I would add that raw opengl is difficult and not neccessary. I use Horde3d for rendering as it's really simple to use, the code is short and simple so it's easy to modify if there is a need.
Further for GUI stuff libRocket is a good choice if you can get it to work.
Squirrel is a good scripting language for embedding in games.
Also there's this book which is a must read.
Furthermore game development requires writing high-performance code. I would recommend reading up on the following topics:
Noticing that all the languages you listed are dynamically typed, you should get familiar with static typing as well as understanding how it benefits performance. learn about how classes and templates in C++ are implemented
Learn the difference between stack and heap memory, value and reference semantics, and how to use containers and smart pointers, look up RAII and move semantics.
Multi-threading is fairly important, so make sure to learn the basics of threads and atomics (atomics might be a bit more advanced, and unnecessary). OpenMP is very useful in this domain.
Finally, one of the most important things for performance is memory locality, learn to understand the cache and possibly also how to use placement new to take control over memory layout.
I might update this list with some links to resources but you can probably google yourself, I'm at school at the moment so ain't got the time.
Oh and since I didn't see anyone else mention it cppreference.