SFML community forums
Help => General => Topic started by: Clockwork on July 16, 2013, 08:07:51 pm
-
Hello everybody!
I'm relatively new with C++ (I have a basic understanding and so far I really like it) and I'm pretty proficient with Java. Learning Java really helped learning C++ but the structures within cpp files and java classes is TOTALLY different. I've just been following CodingMadeEasy's tutorials and have my code has just gotten bigger and bigger. However, it's really ugly and sometimes hard to follow, so I split everything into different functions and I call those functions in the constructor. Then I call the constructor in the main method. Here's my code.
https://github.com/ChaoticCactus/SFMLProject
You can ignore the first 2 files.
I'm just curious how I can improve my coding.
Thanks!
-
Youtube tutorials are famous for teaching very bad code style, so that is not suprising ;)
The only way to learn C++ and good coding pratices is reading books. You should start with a beginner book (e.g. C++ Primer) that explains all the language and standard library feature. Even if you don't consider yourself a beginner, there are a lot of subtle differences between Java and C++ that you should know, and only good books cover those in-depth. Tutorials omit many important details, and of video tutorials I don't even want to begin to speak.
After that, you should read an advanced book that teaches modern idioms like RAII and techniques like exception safety or efficient use of STL. The classical book for this was Effective C++, but some of the rules taught by it are not up-to-date anymore, but in general you can probably still learn a lot.
It may sound demotivating, but reading such books still takes far less time than the maintenance and debug sessions arising from bad code. Furthermore, it is not nearly as frustrating. You should really invest some time, C++ is a very complex language.
-
Ok, thanks! I'll definitely check that book out. Anything for making programming less frustrating. It's an addicting puzzle but sometimes it can be so frustrating. :)
Just as a side note, how does the code in the github link look?
EDIT: Forgot to mention. I've heard of a lot of people that learned C++ through TheNewBoston. Would those tutorials be a decent substitute? Or should I just stick with the book?
-
EDIT: Forgot to mention. I've heard of a lot of people that learned C++ through TheNewBoston. Would those tutorials be a decent substitute? Or should I just stick with the book?
Although I like TheNewBoston as Nexus said, you can't really learn C++ from a few Youtube videos. Go grab yourself a book (I mean many libraries should have them if you don't want to buy it) and start reading! :D
-
Ok, I'll definitely get the book.
-
Which one? There is a whole list (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) of possibilities :)
The C++ Primer is often recommended (as opposed to C++ Primer Plus).
-
Woops, I meant I'll definitely get C++ Primer, I read through the "Look Inside!" section on amazon, and it seems pretty informative.
-
Well I remember when I first started learning C++, I was very messy with my code. But these are some things I've learned that help me write better code.
Choose a coding standard and naming convention and stick to it. I know this sounds like a minor thing, but it really does lead to more readable code. When I work on GQE(https://code.google.com/p/gqe/ (https://code.google.com/p/gqe/)) I tend to stick with Pascal Case Notation(http://en.wikipedia.org/wiki/CamelCase (http://en.wikipedia.org/wiki/CamelCase)). It really doesn't matter what naming convention you use. As long as its readable and constant. This is why I cant even use some of my older projects, I can even figure out my old code. Take a look at at the coding standards for GQE: https://code.google.com/p/gqe/wiki/CodingStandards (https://code.google.com/p/gqe/wiki/CodingStandards). Some things you probably wont have to worry about, like documentation. But its nice to have consistency in your code.
Look around other open source projects. Its good to see how other people do things. Sometimes you might even get the chance to help with a project. That's how I became a contributor to GQE.
Finally one thing i would always do is. If you program any thing Late at night, While Tired, out of it, Etc. Look it over the next day. You might not even remember what it does :P. Always good to review your own code (Especially sleepy-coding)
Just my two cents.
-
You could as well try to grab the source code of the SFML book, the code style there is really clean, modern.
Don't even need to buy the book in order to grab the source :)
That would give you a full example of a project using SFML to look at.
Surprised Nexus didn't bring it up.
-
Here's my code.
https://github.com/ChaoticCactus/SFMLProject
Overall, IMHO, it doesn't look too bad. Of course, it's also really simple. As the complexity of your code increases, you may want to encapsulate stuff: For instance, maybe put your player in 1 class. Then you'll probably discover that's not enough, and want to separate the visual aspects of that class from the logic and from the input, and put each of those in separate classes. And so forth...
I think it's ok to work iteratively and just do what works first, slowly abstracting stuff as you deem it necessary.
Here (http://hg.icculus.org/icculus/lugaru/file/97b303e79826/Source/GameTick.cpp#l5593) is an example of what not to do. Mind you, that (commercial) game still shipped.
-
Here (http://hg.icculus.org/icculus/lugaru/file/97b303e79826/Source/GameTick.cpp#l5593) is an example of what not to do. Mind you, that (commercial) game still shipped.
How did you find that... ??? But seriously that is insane :o 10047 lines of code all in one single file ::)
Reminds me of some of the ancient code I have gone through at work (old VB6 stuff) :P
-
Old software tends to be messy on the lots-of-code-that-does-ritchie-knows-what-but-works-super-wonderfully side. Duke Nukem 3D has raw calls to os, globals, assuming many things about platform, mad code full of tricks, arcane assembly and was meant for pcs without fpu so it doesn't ever use floats. ;D
http://fabiensanglard.net/duke3d/code_legacy.php
I've seen people on the internet say that if everything is in one file then compilers long ago were (supposedly) optimizing it much better and back then performance was everything.
-
How did you find that... ??? But seriously that is insane :o 10047 lines of code all in one single file ::)
Reminds me of some of the ancient code I have gone through at work (old VB6 stuff) :P
That code may be old, but this kind of practice happens all the time, even in modern day AAA titles.
This kind of thing tends to happen when you have a company try and milk a franchise as much as possible, so they just tack on new code to an old code base and replace the textures. It's fun times sifting through the mess afterwards.
-
Here (http://hg.icculus.org/icculus/lugaru/file/97b303e79826/Source/GameTick.cpp#l5593) is an example of what not to do. Mind you, that (commercial) game still shipped.
Oh god. I can't even look at that. I opened up the link and cringed. :-\ It's hard to believe that that actually shipped...
And I'm working on putting everything into different classes right now actually, it's much easier to look at. :P
-
Holy crap, even I don't write code that badly and I'm still a noob, as others have said Primer C++ is definitely a good option.
That code may be old, but this kind of practice happens all the time, even in modern day AAA titles.
This kind of thing tends to happen when you have a company try and milk a franchise as much as possible, so they just tack on new code to an old code base and replace the textures. It's fun times sifting through the mess afterwards.
I was wondering about this but is this why in the code that the book provides they've made seperate headers/files for all the main functions? I was wondering why that was, it does look a lot neater but I still think it's a bit disorganised somehow but I guess that's because I don't know the full thing yet.
-
I was wondering about this but is this why in the code that the book provides they've made seperate headers/files for all the main functions? I was wondering why that was, it does look a lot neater but I still think it's a bit disorganised somehow but I guess that's because I don't know the full thing yet.
What book are you referring to? SFML Game Dev?
It's a common and good practice to separate declarations and definitions and keep the code files as small as needed. Functions shouldn't contain hundreds of lines, but should be kept minimal, that way you'll get a very clean code base. You might get a few more files etc, but if you need to find a specific section it's much easier to locate.
-
Yes the SFML game dev book, I've been looking at the source code occasionally as well as reading through the chapters :D I can understand the reasoning behind how it's set up now looking at the example code that was posted here.
-
I love bucky! His videos got me started in programming after I quit c++ for a while(because I had spent days and days trying everything in my power to understand pointers, but alas at the time nothing would click) and decided to learn java. After I started joining some professional communities like stackoverflow and started talking to people who actually knew what they were doing I realized that thenewboston really didn't understand java, and a lot of things weren't clicking to me because he skipped the difficult pieces without telling me why he wrote certain methods, also some of his java is fairly outdated and outright incorrect for example: He often overrode paint in top level containers, which is a BIG nono in java. Also I hope you weren't trying to learn c++ from cplusplus.com, there are some fairly outdated and overcomplicated examples on that site, mostly when it comes to pointers and dynamic allocation.
I think you should pick up a good book on the Basics of C++, after you've got the basics down and have a good understanding of the basic things to do and not to do start using the internet. You can learn how to do pretty much anything coding related through google. Let me ask you a question though:
Why are you wanting to learn C++? From the context of your questions it doesn't sound like you are a CS or CE major(perhaps I am incorrect). I'm going to give you a common list of reasons people want to learn programming and some things you can do that will keep you interested in the language. I've always hated reading, but loved the knowledge gained from reading, I'm slightly ADHD so focusing long enough to read books has always been a struggle for me, but I am self disciplined so when it comes to school I force myself.
1)A lot of people just want to learn how computers work.
Try learning some general Windows API(If you're running on windows) and than after you've got some of that stuff down try learning assembly, through learning assembly you really gain a deep insight on how things work.
2)Game Development
You're pretty much in the right place for that. Look through the tutorials, come up with your own ideas, learn how to use this library, also still consider learning how SFML operates as it could be valuable if you ever decide to write your own game engine or work with other libraries/languages. Also if you want to learn how to hack online games(Hacking as in gaining an unfair advantage, not as in Cracking passwords and such) learning some assembly/disassembly and WinApi would definitely help(Consider Reading up on Dll injection).
Let me note: Learning Windows Application Programming Interface is not practical for leisure programming in most cases. It's rewarding to learn but with all the libraries like SFML and such one could argue that its unnecessary for writing general purpose applications.
3) Artificial Intelligence
Consider watching some College lectures on youtube when it comes to this. These people know what theyre talking about 80% of the time and you can really learn a lot(I know its not the most exciting way to learn something so amazing)
MSDN is pretty much the documentation for winapi most questions you have can be answered by reading the documentation.