Like Tree2Likes
  • 2 Post By Serapth

A Complete C++ Game Programming Tutorial

This is a discussion on A Complete C++ Game Programming Tutorial within the C++ Programming forums, part of the General Programming Boards category; Hello all, long time lurker; first time poster. I know this site has a great many excellent tutorials on all ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    48

    Lightbulb A Complete C++ Game Programming Tutorial

    Hello all, long time lurker; first time poster. I know this site has a great many excellent tutorials on all facets of C++ programming, so I hope it isn't a breach of etiquette to post one more. I have put together a moderate-advanced ( read, long ) tutorial that walks you through the process of creating a simple C++ game from scratch using SFML. It has been well received by others and I hope some of you here find it useful.


    Essentially, I found there was a gap in tutorials that spanned the distance between the basics of learning C++ and cover the gap of actually using it in a production environment, writing readable, maintainable and performant code using modern C++. So that is what I set out to write. One of the big reasons few such tutorials exist is because frankly, its a long topic, an extremely long one in fact. Which is why my simple pong clone is already 8! chapters long and isn't in fact complete. In the end it will probably end up around the 12 chapter mark I would estimate. It is however to the point you can extrapolate what you have learned and create a game ( or other project ) of your own. As I like to call it, it is the worlds most over engineered pong clone.

    It's not really about teaching you how to program a game, although that is the end result. It doesn't in fact teach you how to program at all. It assumes you have prior knowledge of the basics of C++ programming, or at least C programming. If you understand variables, loops and flow constructs like ifs and while loops, this is the perfect tutorial for you. It is also an iterative process, so in some cases you actually will write some code then re-write it later in a better manner, as to know why something is good, you often need to experience how bad it could be! In essense, this tutorial is about taking the basics you already know and guiding you to the next step of actually using them.

    Along the way you will learn about splitting your code into multiple pieces, class design, precompiled headers, statics, enums, templates, structs/classes, inheritance ( virtual/abstract/pure virtual ), constructors, stl data types ( map specifically ), asserts, const correctness and a myriad of other topics and shiny bits of trivia I found interesting at the time. All demonstrated in actual code you would write in the real world, not as some abstract sample. It is often easier to learn about inheritance using it in a real world example than it is using some abstract example about animals and making a dog bark and a cat meow. It even touches a bit on design patterns towards the end.

    Anyways, if that sounds interesting to you give it a read. If you aren't using Visual Studio or don't care about setting up SFML, you can freely skip ahead to chapter 2, as the first two chapters are about setting up your project. The code compiles with minimal changes on Linux and Mac OS as well. Although I have a number of years of experience both with C++ and programming in general, C++ is a tricky language and I make mistakes. If you see any glaring mistakes, of course let me know, and I will address them!

    Tutorial link:
    Link removed by moderator
    Jump directly to the coding bits:
    Link removed by moderator
    This is just one of many intended tutorials I am writing, so any and all feedback is appreciated! This is ultimately about teaching, so if you don't understand something, I have failed at my task. Any and all comments are encouraged and will only assist me in creating better content going forward.



    Also, hello all. Now that I am a post-lurker, I will be much more active in these part. Next time, with less text!

    Cheers



    /Oh, and moderators, if this post was a breach of proper etiquette, I apologize and feel free to delete it without offence taken!
    Last edited by VirtualAce; 11-27-2011 at 10:08 AM.
    manasij7479 and Swoorup like this.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Well, I'm a Linux user but I'll give it a shot. I mostly program in C right now and I haven't used C++ for a while. I need to brush up on a whole lot of the OO stuff. Do you think you'll release a .pdf version of the tutorial?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Quote Originally Posted by failure67 View Post
    Well, I'm a Linux user but I'll give it a shot. I mostly program in C right now and I haven't used C++ for a while. I need to brush up on a whole lot of the OO stuff. Do you think you'll release a .pdf version of the tutorial?

    Hello failure67,

    I will probably release a PDF version once complete. Given the constant revisions I make currently, maintaining a PDF export while a PDF just ads too much work.

    As to using linux, there are only two potential gotchas.

    First I make use of _tmain for unicode support, which is not supported on Linux. Simply replace the main with a typical main() and remove the header files it reports missing in the stdafx file.

    Second, I (think, I may have removed them ) I used strongly typed enums, a C++11 feature. If you get warnings around enums, they are fairly easily resolved.


    A strongly typed enum looks like

    Code:
    enum Color { Red, Green, Blue };
    
    Color::Red // in use, this may cause an error, as its a c++11 feature
    if you encounter this, merely remove the prefix, to simply be

    Red



    Let me know how it goes.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Serapth View Post
    Second, I (think, I may have removed them ) I used strongly typed enums, a C++11 feature. If you get warnings around enums, they are fairly easily resolved.
    What is wrong with using C++11 on Linux?
    AFAIK... GCC currently has better C++11 support than all other compilers.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    I am not sure it will, however when I compiled with GCC 4.2 on Mac OS, it complained about the use of strongly typed enums. I am not running a linux distro currently, so it seemed safe to warn that it *may* happen.

    It is possible it was a Mac specific problem, or that C11 support is enabled via some compiler switch. It is also possible it works out of the box.



    EDIT:

    Googled and found this ( http://gcc.gnu.org/gcc-4.4/cxx0x_status.html )

    It is enabled in 4.4 and is enabled via compiler switch.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    > GCC 4.2
    GCC 4.2 was released on May 13, 2007 and last updated on May 19, 2008 ... http://gcc.gnu.org/gcc-4.2/
    C++11 wasn't even a baby then.
    and...GCC 4.7 is 'almost' released by now..
    Using an outdated compiler is not the environment's problem...but the users' .
    (I use 4.6.2....and have found almost everything of C++11 I know about, to be working.)

    >It is enabled in 4.4 and is enabled via compiler switch.
    -std=c++0x
    It isn't made default because some conflicts about lambda functions...I read somewhere.
    Last edited by manasij7479; 11-24-2011 at 11:53 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    I might be pulling the compiler version out of the air, but I believe 4.2 is what I used. It's XCode 4.2, so perhaps I am mixing versions up. If not, and it is a relic of a compiler, blame Apple, not me.

    Again, I am primarily a VC++ developer and just checked things in XCode to verify I was writing portable code. It isn't a comment on the ability of the compiler in any way, just a warning that you might run into issues, as I did. If 4.6.x still requires the switch be enabled to run, my original warning stands.


    /Edit, checked and confirmed, XCode 4.2 works with GCC 4.2 out of the box. Not sure what is involved in moving to a later version ( and still supporting iOS that is ).

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    > If 4.6.x still requires the switch be enabled to run, my original warning stands.
    Fair enough..
    BTW... If you're thinking a switch is something alien...; It isn't; it is just a extra word to add to the compilation command...or to the project settings on the IDE.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Oh, I realize that, but when dealing with a tutorial for relative beginners, even the most minor deviation from reality can be a show stopping problem. What you or I find rote, is to someone that hasn't been exposed to it yet, potentially a giant problem. I mean, almost everything is easier once you've done it once :-)

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Serapth View Post
    Oh, I realize that, but when dealing with a tutorial for relative beginners, even the most minor deviation from reality can be a show stopping problem. What you or I find rote, is to someone that hasn't been exposed to it yet, potentially a giant problem. I mean, almost everything is easier once you've done it once :-)
    "Deviation from reality" ! <--Nicely said!
    That is exactly what I felt (a few years ago...and knew nothing about templates and teh STL) ..when I saw template code for the first time.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by Serapth View Post
    A strongly typed enum looks like

    Code:
    enum Color { Red, Green, Blue };
    
    Color::Red // in use, this may cause an error, as its a c++11 feature
    if you encounter this, merely remove the prefix, to simply be

    Red
    AFAIK, this isn't standard C++. This is a Microsoft extension.
    If you wish to use strongly typed enums, I believe the proper syntax is
    Code:
    class enum Color { ... };
    Aside from that, I commend you for doing this guide. Great initiative, and I just hope that you will be around to maintain it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Quote Originally Posted by Elysia View Post
    AFAIK, this isn't standard C++. This is a Microsoft extension.
    If you wish to use strongly typed enums, I believe the proper syntax is
    Code:
    class enum Color { ... };
    Aside from that, I commend you for doing this guide. Great initiative, and I just hope that you will be arounshalld to maintain it.
    Thanks for the heads up, if it is an MS specific extension I shall remove it. I had thought Microsoft vowe ad to be completely standard compliant after the Managed C++ debacle,I guess not.

    It's a shame though, that when style is much more readable.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by Serapth View Post
    Thanks for the heads up, if it is an MS specific extension I shall remove it. I had thought Microsoft vowe ad to be completely standard compliant after the Managed C++ debacle,I guess not.
    They are standard compliant. They just have a lot of language extensions. GNU does, too.

    It's a shame though, that when style is much more readable.
    I'm not certain if you intend to say that style was much readable or something else.
    Anyway, that style (strongly typed enums) is definitely standard, but the syntax is different (use class enum). This is not supported by Microsoft compilers, however (but GCC does).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    I meant syntax and of course it is a matter of opinion

  15. #15
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Looks like a plug for a website. Closed.

    /Oh, and moderators, if this post was a breach of proper etiquette, I apologize and feel free to delete it without offence taken!
    7. Please construct posts with content that furthers the community or holds some merit. Messages posted solely to increase post counts, SPAM, and pointless one-liner posts are discouraged. Solicitation of any product without the consent of the Administration is forbidden. Do not post requests for clicks on referrer links of any kind. This includes indirect solicitation such as linking to a website whose purpose, in whole or in part, is such solicitation. Advertising for programming groups and jobs is allowed only on the recruitment board. (Please note the Recruitment Board Rules.) Anyone with questions about this policy, or who wants permission to post something of this nature, may PM me.
    This is spelled out clearly in our forum guidelines. The guidelines are stickied at the top of each forum. We have a game programming resources section in the Game Programming forum and you could post your information there. It it stickied at the top of the Game programming forum.
    Last edited by VirtualAce; 11-27-2011 at 10:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WINDOWS/GAME Programming Tutorial CD
    By paulogrady in forum Game Programming
    Replies: 9
    Last Post: 11-17-2008, 06:59 PM
  2. Dostris (a complete game in 3 hours)
    By Jeremy G in forum Game Programming
    Replies: 8
    Last Post: 08-30-2003, 02:17 PM
  3. Okay, game is ALMOST complete, but i need an AI
    By Blizzarddog in forum Game Programming
    Replies: 4
    Last Post: 12-07-2002, 09:08 PM
  4. Complete Game programming forum
    By gogo in forum Game Programming
    Replies: 5
    Last Post: 03-25-2002, 09:53 PM
  5. Yay! My first game is complete! Come look!
    By Leeman_s in forum Game Programming
    Replies: 2
    Last Post: 11-05-2001, 09:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21