Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

First of all i would like to have a real answer, i'm always trying to get more from various sources and articles, and when I read things like C++ is slow because it has virtual functions and because of this C is better, i really don't know what to say and think as an human being with a brain. So please avoid to reach this level in your answer/s.

My question is about a massive switch to C++ that was completed, more or less, with Doom 3.

The interesting thing for me is that before this milestone, most of the game engines and the games itself were written in C, just like it was since the Quake era. It's also interesting to note that the ID software decide to completely rewrite the codebase for the IdTech 4 in C++, a massive amount of work that honestly i can't understand without a really good list of reasons.

I'm focusing on Doom 3 because I am mainly interested in the OpenGL world and in my journey i try to stay focused on this topic, so i read a lot about this, but i think that a question like that can be render-API-agnostic without too much problems.

Why at a certain point in time the industry switched massively to C++ ? What are the reasons for the choice that ID made ?

The last thing that i would like to say is that the C language is much more simple to implement and provides a less number of features, because of this has much less chance to be "fragmented" in pieces unlike the C++ really often does. In simplest terms i have much more chances to find a really good C compiler rather than a good C++ compiler with all the features implemented in a good way.

For example the NDK for Android still doesn't have a good C++ support ( with the r8b release ) with all the latest and greatest features, and it's the native toolkit for the most popular mobile OS in the world!

If I had wrote my code in a modern C++ I would probably be in pain now because one of the most popular OS in the world would be off-limits for me. And like Android, many other compilers are not that great.

I should write C++ code referring to a C++ version that is 2-3 release old ?

share|improve this question
1  
I suspect its just that C++ is a quasi extension of C. People started to write C with C++ features such as std::vector, from there on C++ was teached instead of C and thus people started to write full blown C++. –  API-Beast Aug 18 '12 at 20:25
1  
Well, C vs. C++ when developing a large application? OOP is a great paradigm and.. C++ supports it. If you're gonna write a game in C, then consider data oriented design. Otherwise, let's ponder on why all major current industry players use C++. Software engineering, be it for financial applications or leisure ones, delves on the shoulders of the OOP paradigm and that of architectural and design patterns. Simply put, C cannot offer the big picture that encapsulation offers you. Are there any modern large pieces of software written in C? Maybe kernels and embedded systems. –  teodron Aug 18 '12 at 20:41
    
@teodron 1) I have to produce a good executable, i care about about the language like i care about an important asset, but it's not everything and it's not what i sell 2) encapsulation it's not that good for performance, if someone wants to convince me to leave the C++ world, the encapsulation is one of the good topic for this. –  user827992 Aug 18 '12 at 20:48
2  
"The interesting thing for me is that before this milestone, most of the game engines and the games itself were written in C, just like it was since the Quake era." I would like to see some evidence for that. Besides Id-software's open-source engines. They're not the whole of the videogame industry. –  Nicol Bolas Aug 19 '12 at 1:53
    
@NicolBolas i was explaining the reason why i was talking about ID, please get the meaning of the entire question. –  user827992 Aug 19 '12 at 4:18

4 Answers 4

up vote 21 down vote accepted

C++ does everything C does. You can trivially mix C and C++ in cases where the advantages of C outweigh those of C++. This is a very intentional design decision of C++.

C++ does things that C does not. This includes easy polymorphism, but also easy compile time code generation via templates. This is really handy for things like containers, which are easily C's biggest weakness. It also allows user-defined pointer-like types (smart handles) that save lots of time, as well as user-defined primitive-like types such as vectors and matrices with operator support (also saves lots of time).

Virtual functions are slower than non-virtual functions. However, you must opt in to virtual functions, and a competent programmer does so only when they're beneficial. C programmers have function pointers and often store collections of those in struct referenced by other structs; in other words, they go through extra work to recreate the exact same thing as virtual function tables. In cases where only a single function pointer is needed and no table is required, C++ still fully allows that and is equivalent to C. With a modern compiler, C++ is only slower than C in the specific cases the programmer is opting in to a feature. Also, the virtual function overhead in practice is very small on modem CPUs. Hardware today is designed for C++'s usage patterns, and is even increasingly designed for high-level interpreted langiages' needs.

C++ exceptions historically impose a lot of overhead, making C++ slower even if you're not using them. Exceptions were a terrible thing to add to C++, if for no other reason than the immense increase on complexity involved in writing exception-safe code, and in fact some container designs are literally impossible to make exception safe. Game programmers often ignore exceptions' existence, and even disable them on the compiler. Modern compilers have zero- overhead exceptions (that is, you only pay th cost for them when you actually use them).

C is simpler to learn all the rules of. C++ is a very big, complex language. C++ allows writing higher-level code, producing easier and simpler APIs. Some people want to understand the language easier, some people want to write advanced code easier. It's a trade off between simplicity of understanding what the compiler is doing with a specific piece of code vs the simplicity of writing large complex inter-connected applications. Some folks value one far more than the other, for various reasons.

In the end, C++ is a superset of C. In my opinion, there is no such this as a highly competent C++ programmer who is not also a passable C programmer (though there are a lot of C++ programmers who fall below my bar who would be lost in pure C). While C++ adds facilities to insulate the programmer from much of C, non- trivial C++ code often does need to use C to get things done. This is one of the primary difference between C++ and Java and C#. There's a reason that you often see "C/C++" lumped together, after all.

My personal belief -- which is shared with most other games industry professionals I've interacted with -- is that the enhanced expressiveness and high-level programming facilities of C++ outweigh the increased complexity of the language over C, and most of the other frequent anti-C++ claims made are simply out of date with today's technology.

share|improve this answer
12  
"There is no such this as a competent C++ programmer who is not also a competent C programmer" I strongly disagree with this statement. Idiomatic C is very different from idiomatic C++. The two styles have diverged, one style emphasizing opaque pointers and static function, the other using language-based encapsulation and so forth. Someone who only knows idiomatic C++ would be competent in C++, but still rather confused by idiomatic C. –  Nicol Bolas Aug 19 '12 at 2:18
    
I don't agree that you can be competent in C++ and only know "idiomatic C++." The hard parts of C++ are the same hard parts of C (plus templates). If a C++ programmer gets confused by standalone functions, plain structs, and raw or opaque pointers, he has not yet written any non-trivial C++ code in any real-world non-academic scenario. I'd go so far as to argue that anyone who's only proficient in "idiomatic" anything has yet to achieve competency. –  Sean Middleditch Aug 19 '12 at 2:36
2  
"Idiomatic" is another way of saying "comfortable," which—apart from being relative—also means that there is, almost by definition, a uncomfortable region of the language in which an otherwise competent C++ programmer will not be particularly proficient. (I don't necessarily think that means that they're "confused" by those elements, merely that they're uncomfortable with or not fluent with them.) And, to a degree, people learning idiomatic C++ are taught to be uncomfortable upon merely glimpsing a raw pointer. So I don't think that a competent C++ programmer implies a competent C programmer. –  John Calsbeek Aug 19 '12 at 4:21
    
Put another way, there is definitely a higher-level C++ realm upon which you can completely avoid using any lower-level feature which would be required to write any serious C code. I'm sure that most real-world non-academic codebases don't stay entirely within this realm, but I suspect you could find a person who is only comfortable with this realm and still call them "competent." Of course, you wouldn't compare such a person to a C programmer—you'd compare them to someone programming in any number of other high-level languages. –  John Calsbeek Aug 19 '12 at 4:24
    
The lower the language, the more opportunities for optimization, but it also takes longer to program, longer to bugfix and is harder to optimize. If this wasn't the case everyone would be programming in Assembler. Which was how C functions were optimized sometimes back in the day. Now we have C++ as base and sometimes C optimizations. I don't know if anyone still optimizes down to Assembler level. With the increase in computer power your usually better off writing slightly less efficient code that saves you weeks of manpower. Nowadays the real optimizations are in programming parallel code. –  Daniel Carlsson Aug 19 '12 at 14:32

Why at a certain point in time the industry switched massively to C++ ? What are the reasons for the choice that ID made ?

Id Software is not "the industry". They are one company. While they may be influential, they aren't everyone.

I've worked on a couple of game engines that date back to 1999, and they used C++.

The principle reasons for the adoption of C++ "around that time" are:

  1. It was standardized. C++98 is named that because it was released as an ISO standard in 1998. Until then, there were multiple dialects of C++ with no clear idea which was "real" or "correct". Once it was standardized, and compilers started implementing the standard, game developers could rely on the actual standard.

  2. Remember the old joke: A guy walks into a doctor's office and says, "It hurts when I raise my arm like this." So the doctor says, "So don't raise your arm like that."

    If virtual functions are slow for your needs, C++ doesn't force you to use them. Everything in C++ is opt-in. You choose to use each particular feature. If you're in performance-critical code and you don't want virtual overhead, you don't use virtual functions. Just like if you're in performance-critical code in C, you don't use function pointers.

    Indeed, this led to a lot of C++ games not being idiomatic C++ (at least, not modern idiomatic C++), but merely "C with classes". And this is a perfectly functional way to program C++. Hell, just being able to not have to type typedef struct when you create a new type is a benefit.

    C++ is a big bag of features, and you can pick which ones you want.

For example the NDK for Android still doesn't have a good C++ support ( with the r8b release ) with all the latest and greatest features, and it's the native toolkit for the most popular mobile OS in the world!

Your point being? Google barely tolerates people using NDK; it's clear that they want everyone to use Java. The only reason NDK exists is because there are certain important developers who simply will not use the platform without it. Thus, NDK exists to service them and their feature needs.

Yes, C++ is a bigger, more complicated specification to implement. So is Java. So is any language except C.

Also, what do you mean "all the latest and greatest features"? If you're talking about C++11 stuff, well, nobody fully implements that yet. The spec is barely a year old now. Also, I don't know if NDK support C11 either; yes, C also has "latest and greatest features" that aren't supported everywhere.

Which brings up an important point: if you want your pure-C application to compile on Visual Studio, then it needs to conform to C89, and nothing higher than that. So fragmentation with C already exists. Some platforms support only C89. Some support C99. Some support C11, to varying degrees. Etc.

If you want to use C, then use C. The fact that other people didn't make that choice does not mean your choice is wrong or that their choice is wrong. You don't need to justify yourself to them, and they don't need to justify themselves to you.

share|improve this answer
1  
I don't think that Google barely tolerates who use the NDK because the development of the NDK is on-going and there are several release available for Android, also even if Google hates the C/C++ i don't think that i can achieve a good level of performance for my game in Java or the most important players in the market can rewrite their code just for java and Android, Probably is a necessary evil for Google, i don't know, my point is to ask about a broad view and not a lame question about C vs C++ vs Java vs the world, i was looking for a more generalized answer from the language continue –  user827992 Aug 19 '12 at 4:04
    
to the compiler considering all the language features, your answer also allows me to stress my initial point about the availability of C compilers on the market, on Windows i have almost all the software house that produces a compiler with their own compiler, i also have other free options like MinGW. The real problem that i found with the C is the lack of native support for data structures, the containers, in C++ you can get an idea of how much cost you a particular container or a particular statement, –  user827992 Aug 19 '12 at 4:12
    
in C probably the code that does the same thing has more than just 1 implementation, also in C++ the compiler can optimize more as a consequence of this. I probably should study C++ more but i think that i will switch to C++ only for a good support for some particular features, not for the language itself. –  user827992 Aug 19 '12 at 4:13

It's also interesting to note that the ID software decide to completely rewrite the codebase for the IdTech 4 in C++, a massive amount of work that honestly i can't understand without a really good list of reasons.

It's been common for them to rewrite pretty much the whole engine for every release (at least until recently - I don't know much about the last few games), and since C++ is growing in popularity it makes sense to use that rather than stick with C. Over time fewer and fewer people are going to be competent with C.

Why at a certain point in time the industry switched massively to C++ ?

Because it gives you lots of higher level functionality while being almost entirely backwards compatible with C code.

What are the reasons for the choice that ID made ?

Google would have answered this for you: http://fabiensanglard.net/doom3/interviews.php

"...half the programmers really had C++ background in the beginning. I had C and Objective-C background, and I sort of "slid into C++" by just looking at the code that the C++ guys were writing. In hindsight, I wish I had budgeted the time to thoroughly research and explore the language before just starting to use it.

You may still be able to tell that the renderer code was largely developed in C, then sort of skinned into C++.

Today, I do firmly believe that C++ is the right language for large, multi-developer projects with critical performance requirements, and Tech 5 is a lot better off for the Doom 3 experience."

If I had wrote my code in a modern C++ I would probably be in pain now because one of the most popular OS in the world would be off-limits for me.

  1. Programmers can't see into the future. They wouldn't know that there would be a new platform with poor C++ support. Android didn't exist properly until 2009. Doom 3 was from 2004.
  2. Do you think programmers who are writing cutting-edge 3D games for PCs and consoles expect their code to run on phones too? Aiming for that degree of portability is very rare and often pointless due to the massive difference in power available.

I should write C++ code referring to a C++ version that is 2-3 release old ?

Why do you care about this? Don't get trapped chasing the newest thing. If it was good enough 5 years ago, it won't have suddenly got worse now.

share|improve this answer

Well, C++ has been out since the 80s so if a compiler doesn't support it, perhaps you should use a different compiler. And also, C++ is not really much slower than C. It really is only slower if you load it down with virtual functions and other abstractions. However, C++ is used a lot today in the non unix world because of its flexibility and newer features that C doesn't have. Writing modular code in C++ is a lot easier than doing so in C and helps you to write more portable code. Back on the subject of compilers, I don't know any compiler that doesn't support C++ (maybe not C++11 fully, but any half decent compiler should support C++).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.