Sign in to follow this  
Jedimace

[Solved]Advanced C++ Programming

Recommended Posts

Hello, I know basic C++, up to stuff like pointers. I want to learn how to use things like Singletons, va_lists and other "advanced" C++ function so I am sure I am ready to create my game engine, since while making games I never had to know all of that stuff. Is there some good tutorials on the internet for this? [Edited by - Jedimace on October 14, 2008 12:47:46 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Jedimace
I want to learn how to use things like Singletons

Don't. There are very, very few situations in which a singleton even begins to become a good idea. What will likely happen is you'll learn how to make a singleton and then abuse this knowledge by applying it in unwarrented situations. There are many topics on this discussion that you can find, but I'm sure other people will have some links soon enough.

Quote:
va_lists

Don't. I don't believe a C++ program EVER needs to use 'va_list's. Prefer operator chaining instead. Or, as zahlman's signature quotes "when you evoke the dread ellipses construct you leave the happy world of type safety" or something close to that.

Quote:
and other "advanced" C++ function

Depends what other features you are considering. Templates? Yes. Polymorphism? Yes. Boost? Heck yes.

Quote:
so I am sure I am ready to create my game engine, since while making games I never had to know all of that stuff. Is there some good tutorials on the internet for this?


Look into boost, polymorphism, and templates as well as learning more about the c++ standard library. I don't have any specific tutorials on these topics, sorry, but a google search is usually a step in the right direction.

edit: I don't really mean "don't" as in "don't learn". All knowledge is helpful. No, I mean that be wary when you do decide to learn these things in C++ as there's almost certainly a better solution than the one that uses singletons or va_lists and as such you should probably focus on useful things first.

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
Quote:
Original post by Jedimace
I want to learn how to use things like Singletons

Don't. There are very, very few situations in which a singleton even begins to become a good idea. What will likely happen is you'll learn how to make a singleton and then abuse this knowledge by applying it in unwarrented situations. There are many topics on this discussion that you can find, but I'm sure other people will have some links soon enough.

Quote:
va_lists

Don't. I don't believe a C++ program EVER needs to use 'va_list's. Prefer operator chaining instead. Or, as zahlman's signature quotes "when you evoke the dread ellipses construct you leave the happy world of type safety" or something close to that.

Quote:
and other "advanced" C++ function

Depends what other features you are considering. Templates? Yes. Polymorphism? Yes. Boost? Heck yes.

Quote:
so I am sure I am ready to create my game engine, since while making games I never had to know all of that stuff. Is there some good tutorials on the internet for this?


Look into boost, polymorphism, and templates as well as learning more about the c++ standard library. I don't have any specific tutorials on these topics, sorry, but a google search is usually a step in the right direction.

I am sure I need Singletons and VA_lists because I got it from the Enginuity game engine tutorial. I might not need it more then once, but I need to understand the tutorials. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jedimace
I am sure I need Singletons and VA_lists because I got it from the Enginuity game engine tutorial. I might not need it more then once, but I need to understand the tutorials. Thanks.

:)

Share this post


Link to post
Share on other sites
Go to #gamedev in the IRC chat room (afternet server if you have IRC already, www.gamedev.net/community/chat/ if not) and ask Superpig about what he thinks of Enginuity today. If he's not there ask the other people about it. Basically, conventional thinking suggests that he regrets a lot of his design decisions, that being one of the big ones. I really would give you tutorials on them if I knew of some good ones, but I don't so I'd end up linking to stuff I found on google which you can do yourself. Sorry!

Share this post


Link to post
Share on other sites
I would like to give my opinion on the singleton pattern as the population of programmers is broken into people who like it and those who do not. Not to discount DevFred or nobodynews.

Singletons are useful in a variety of situations, some of which you'll find commonly in game engines and systems in general.

One such use would be for entity/memory managers(entity representing any game object perhaps). You don't want to have more than one EntityManager managing entities. If your class is a singleton you force the user to only have on instance of the manager at a given time. Perhaps if you have an audio engine, you do not want to create separate instances and initialize multiple objects interacting with your sound files as you may not be sure who has the audio file buffered that you wish to access. It can be very useful in some situations.

Besides that, its always good to put another tool in your toolbox. Simply knowing how a singleton works and how you can effectively use it will benefit you down the road, even if you don't implement it in your engine.

That being said, I would recommend taking a look at:
http://www.gamedev.net/columns/books/bookdetails.asp?productid=18&CategoryID=17

This book will cover a lot of the more commonly used design patterns(and some more uncommonly used), including singletons. If you gain nothing else, you will know why there are different design patterns and how they work which may give you insight into a problem you may face in the future.

Regarding advanced features, build a list of what you would like to know and gamedev will probably have some insight as to how it works or where to find the information you require. You could probably check out the current c++ standard and start picking from there. (Keep in mind a new standard is being worked on and things may change.) In my opinion, you can never learn too much and nothing learned is wasted(within reason).

Cheers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tang of the Mountain

One such use would be for entity/memory managers(entity representing any game object perhaps). You don't want to have more than one EntityManager managing entities. If your class is a singleton you force the user to only have on instance of the manager at a given time.


I'm sorry but I think your example is flawed, because you could simply pass in a valid EntityManager when you instantiate a new object. This is exactly the "problem" with singletons, everyone has access to this global object. And my question is, why does every system potentially have/need access to this information?

Singletons are good know/understand, but there's very few (if any) cases that really need them. If you are writing code from scratch (and not inheriting an existing code base), you can generally design your code in a way that you do not need them. I'm not trying to start a flame war, just a friendly difference of opinion. :)

Share this post


Link to post
Share on other sites
What if I want to change the implementation of some aspect of the game, but I want to check that I haven't changed results? It would make sense to have two games running in parallel (each with its own EntityManager) so I can compare the state of both after each frame.

If lack of imagination is the only reason you have to force your users to have a single instance, you should probably not impose this restriction on them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jedimace
I am sure I need Singletons and VA_lists because I got it from the Enginuity game engine tutorial.


"I am sure I need a spoiler on the back of my car and stickers all over the sides because I saw it in a video game."

Quote:
I might not need it more then once, but I need to understand the tutorials. Thanks.


There are a zillion other ways you could go about learning the language, besides working through one specific set of tutorials. Further, the point of a tutorial is to explain all the material it introduces.

Share this post


Link to post
Share on other sites
Quote:

There are a zillion other ways you could go about learning the language, besides working through one specific set of tutorials. Further, the point of a tutorial is to explain all the material it introduces.

Might I point out that these are the ONLY game engine design tutorials with code. As far as I am concerned, Singletons are there for a reason, and I should learn it, because nothing happens without a reason.

Share this post


Link to post
Share on other sites
@ shwasasin and alvaro I have to say i am not in full agreement with you. There are special cases and I'm not saying a singleton is a good idea for all variations of an Entity Manager, for my case of one manager managing everything, it works. Not all people want to run parallel instances of the game(I've never found a need and am not entirely sure it would be an issue or could not be worked around.) I don't consider not serving the needs of very special circumstances to be a lack of imagination. However, not to steal away this topic from the OP, i will gladly continue this discussion on another thread or via pms.


Jedimace, what I commonly do, is keep a list of things I do not know and want to, and every time I find some free time, i start attacking the list(google, blogs, msdn, gamedev, etc). I like to spend 20hrs a week exploring new things, I've heard its good practice to spend at least a few hours a week doing so. You never know when something you learn will prove useful.

Share this post


Link to post
Share on other sites
I'll go ahead add the obligatory "Design a game, not an engine". If you are trying to get a job, people will be more impressed with a fun and fully functional game you designed, not so much with whether you used variable argument lists.

I'd also say you have program at least a small game or two before before you'll have any idea of what comprises a good engine architecture.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jedimace
Quote:

There are a zillion other ways you could go about learning the language, besides working through one specific set of tutorials. Further, the point of a tutorial is to explain all the material it introduces.

Might I point out that these are the ONLY game engine design tutorials with code. As far as I am concerned, Singletons are there for a reason, and I should learn it, because nothing happens without a reason.


Yes but punch cards were invented for a reason, so why do you bother to use a hard-drive?

Just because something was invented doesn't mean it should be utilized. I agree that you should read about singletons, but from the perspective that you should know the problems associated with them. As I've mentioned earlier object-oriented code can be written without singletons, so why bother using them. You're essentially making a global variable, which is gross! When you begin writing functionality always ask yourself "why should object X be able to communicate with object Y", then it'll become clear that a singleton really isn't necessary. If it is, perhaps the design isn't well thought out.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tang of the Mountain
@ shwasasin and alvaro I have to say i am not in full agreement with you. There are special cases and I'm not saying a singleton is a good idea for all variations of an Entity Manager, for my case of one manager managing everything, it works. Not all people want to run parallel instances of the game(I've never found a need and am not entirely sure it would be an issue or could not be worked around.) I don't consider not serving the needs of very special circumstances to be a lack of imagination. However, not to steal away this topic from the OP, i will gladly continue this discussion on another thread or via pms.
If you haven't already, you might check out some previous threads on the topic of singletons (and the articles they link to). I think you'll find that most of the arguments you're putting forth have been dealt with (and effectively countered) many times over.

In fact, you've sort of fallen into the classic singleton trap with your entity manager example. Remember, a singleton is an appropriate solution when the following holds true:

1. It is an error for there to be more than one of the specified object type in existence at any one time.

2. Global access is a requirement.

Generally speaking, neither of these hold true for an 'entity manager' object, and therefore a singleton is not necessary (or even appropriate) in this case.

Now, no one's saying that it won't work to make it a singleton. It's just not good design. In any case, it's perfectly possible to create a game framework without the use of singletons (or global objects of any sort), and many would recommend this approach over the 'globally accessable object' approach.

Share this post


Link to post
Share on other sites
Quote:
Original post by FippyDarkpaw
I'll go ahead add the obligatory "Design a game, not an engine". If you are trying to get a job, people will be more impressed with a fun and fully functional game you designed, not so much with whether you used variable argument lists.

I'd also say you have program at least a small game or two before before you'll have any idea of what comprises a good engine architecture.


Please read my posts again. I have made games before.

Share this post


Link to post
Share on other sites
Change the topic of this thread to "Obsolete and Legacy concepts of C-with-classes" then.

The title currently says "Advanced C++ Programming", which would include topics covered in Exceptional C++, More Exceptional C++ and Modern C++ Design.

After all, even CS courses cover legacy computer architectures, so there is a reason to learn them. They just don't call them "Best practices of modern design".

Share this post


Link to post
Share on other sites
Quote:
Original post by Jedimace
As far as I am concerned, Singletons are there for a reason, and I should learn it, because nothing happens without a reason.
The idea behind this all mess is quite simple. Having global variables in a program can easily lead to an unmaintainable ball of buggy mud if you're not careful. Procedural programming has evolved techniques for containing global variables and limiting their impact on their program. However, Object-Oriented programming (which emphasizes code reuse) does not have such techniques at its disposal. Therefore, pure object-oriented programs that use global state lead to pain.

One solution is to avoid using global variables in an object-oriented program (which will generally involve some additional verbosity that you're not certain is going to benefit you in the end) and the other solution is to mix object-oriented programming and procedural programming. The Singleton Design Pattern brings a procedural programming concept (the Subsystem, which is an aggregation of global state elements) a tad closer to object-oriented programming. But this is merely a thin layer of paint: the Singleton remains a procedural concept, and should be treated as such.

Ihave an article about this on my web site: Time is a Global Variable.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
In fact, you've sort of fallen into the classic singleton trap with your entity manager example. Remember, a singleton is an appropriate solution when the following holds true:

1. It is an error for there to be more than one of the specified object type in existence at any one time.

2. Global access is a requirement.


Actually, #1 needs to go even further than that, IMO. Sloppy and lazy engineering of other code often leads to the first condition being satisfied -- In other words, poor engineering and coding practices in the overall system comes to rely on there only being one instance in existence. In my mind, what this condition should really state, is that some condition beyond the programmer's control to engineer away requires a single instance exist in such a way that more than one instance is an error.


I've been in the position of migrating manager classes from a singleton design (based on Scott Balias' Game Programming Gems I article) to a non-singleton design -- The cost was less than a dozen changed lines, some slight re-structuring [an entire (now unnecessary) class went away] and a single new parameter passed to each managed objects constructor -- which can be, of course, hidden neatly behind a factory :) My experience with Singletons is that the "ease of use" argument is a near-total fallacy, and simply an excuse made up by lazy typists who fancy themselves programmers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this