Even though C++ appears to be king, from what I've been told C is still widely used in games, especially on the consoles. However, would writing an entire game engine in C be unreasonable today? What are, if any, some advantages that C has over C++? Why would someone possibly want to use C over C++?
It's reasonable, but the question is what does it buy you? You likely don't need the extreme portability C offers, and it's a shame to give up all of the features C++ offers unless you're really philosophically set against it.
Better compilation time?
I think it's mostly an aesthetic choice. Many people like C because it's simple and minimal and feels clean. C++ has a lot of nice features (namespaces alone make it worth using), but it's also big and messy. |
|||||||||||||||||||
|
|
I have worked extensively with a pure-C game engine that has shipped several products, so it is absolutely possible. Here's my personal experience with working in both C vs C++ engines:
All 3 of these benefits could also be achieved just as easily using restrained c++ code that refrains from using templates and inheritance on serialized objects, but it was the CTO's decision that it would be easier to enforce the simplicity if the more confusing elements of C++ were not available. Personally I think this was slightly extreme, as I really missed the ability to sanely declare variables in for loops and the many completely legitimate uses for inheritance. But, it really didn't cost us much productivity in the end all things considered |
|||||||||||||||||
|
|
I am rewriting a 2D game engine written in C++ and Lua into C and Lua. So far the experience have been quite good. Obviously doing vector and matrix operations don't end up as nice looking in C. But apart from that I have found the experience quite refreshing after spending 10+ years as a C++ developer. C has a number of advantages over C++:
Apart from that there are a number of advantages to getting into the C way of thinking. In C++ I often find myself making things a bit overgeneralized and abstract. In C I usually cut out things like get-set methods and often preallocate fixed sized arrays rather than use dynamic arrays. Often I end up with shorter and more easily debugged code in C. The data structures are usually flatter and easier to view in a debugger. To be fair I would never make an app exclusively in C. The reason it works it that I combine it with a higher level language like Lua which can complement C very well, were C is not so strong. Id software writes most of their engines in C I believe, you can look at Return to castle Wolfenstein which was written in C. I wrote about some of my experience on scalability of C vs C++ and downsides of STL vector compared to plain arrays. |
|||||||||||
|
|
As someone else pointed out, C++ brings the advantage of big shoulders on which you can stand (BOOST, STL etc.). In the end, it is a personal choice, but I would choose C++ because of the available resources. If there are features in C++ that you don't wish to use, then don't use them. |
|||||||||||||||
|
|
I don't think anyone uses C exclusively these days, it's usually mixed together with a higher level language. Programming in C has some benefits over, say, programming in C++. C++ can do lot of things under the hood that are invisible to the user, which can hurt performance if you're not careful. C++ can also be terrible when it comes to cache usage, which again can hurt your performance. So it may bring some benefits to write the performance critical parts of a game in a C-like way, rather than in a traditional C++ way. I've never heard of anyone, in recent years, actually writing an entire game in C however. On some platforms, like the iPhone, using C++ can increase your executable size with a certain chunk of kilobytes (I forgot how much, sorry), which is a reason why some iPhone developers choose to write their code in a mix of C and Objective-C. |
|||
|
|
|
Of course it's reasonable. I personally wouldn't do it, and most of the C fans I know actually just write C-like code in .cpp files. But the languages are similar enough to where it doesn't really matter. As for why someone would choose to do this, I think it's mostly down to anti-C++ philosophy. Personally I still don't think this is a good reason to choose C over "C-style C++". typedef struct craziness is a good enough reason to steer clear of C, and there are a number of others. Unfortunately C and C++ are both pretty terrible languages when we get down to it. That's one reason people have been trying to do a lot of their code in script in recent years. If you're looking for examples of people working in C, you can ignore id as I recall reading that they've abandoned C long ago. Cryptic Studios (Star Trek Online) does all their engine development in C, though. As far as I can tell, yes, it's because of philosophy more than because of any tangible advantage. |
|||
|
|
|
I'll give you a couple more reasons why it would be unreasonable to write a game engine in C instead of C++ today: STL and BOOST. I can't imagine how it would be worth writing yet another |
|||||||||||||||
|
|
Writing a game engine in C is reasonable. It's fast and can be ported to multiple systems. For example you could use for Android (with the use of the NDK). You could use it for the iPhone (Objective c is just an extension of c). You could also use it for and of the main OS such as Linux, Mac or Windows. If you feel comfortable with c, I suggest you give it a try! |
|||
|
|
|
Yes and no. Yes I have done it a few years back but I needed my game to run in 3D on a unix (not linux) 64 bit remote server with the players on dumb terminals. It was non trivial. C can be nice is you want to integrate LUA but I eventually got lua to work in C++ so I would say yes it is possible but don't do it. |
|||
|
|
|
May a possible good answer be "use both" ? As I heard that panda3d projects could be optimized, killing bottleneck either using some cython or either recode those parts. Most of the time, the parts which need to be optimized are the part with a lot of iterations and nesting or with a lot of numeric computing, so my (wild) guess is that one fair compromise would be to use both language, using C for parts that include a lot of low-level programming, so you can't do any misuse of the C++ language for the part which need speed, and C++ for the rest of the game. Ideally you do the fast part of the engine keeping with the high level in mind, and then use a scripting language or C++ which will use much less nest/loops. Of course you could never make such an engine that would fit all the games developers would need to do, except if you dedicate your engine for a special kind of game... But don't take my advice for granted since I'm not an experienced game developer nor an experienced developer... I think C forces you to write fast code, while writing good and fast C++ code for a project as big as a game is a different thing... |
|||
|
|
|
C worked for John Carmack, you might get a lot of advantages by using C, but until you get there to benefit from them it might take you a while. Here you can find a list of game engines some of them are written in C, maybe you can get some insights on how to do a C game engine by going through their source code. |
|||
|
|
|
C code is normally valid C++ code. The main issues with C++ are using it incorrectly (Linus Torvalds hate it for this reason, he also had some other issues with library portability and so on buy he is working at the operating systems level and has to be able to run things on every random chip out there). For example there's almost no advantage to using a cstyle array[] over a c++ std::vector<> (or similar container). The vectors are typesafe and can be bounds checked (you can access elements using either get() or [], even if you don't use the array checked method you can still query the size rather than carting it around with the pointer). But vectors can be slower if, for example, you don't declare the default size in the constructor. Also adding things to a vector can cause slowdowns if it then needs a resize. C++11 adds many advantages too such as uniform initialization (you can now declare and initialize vectors using the same syntax) and there are move constructors that can allow you to avoid copying. You can even make your own custom initializers (if you wanted to do something other than using malloc for some reason). Or course if you do need to resize things, then vectors are still easier to do it with, you don't have to mess around with malloc, manually copy things and so on. C++ gives you object orientated code. When compiled it's going to be just as efficient since it's really just an abstraction for humans working with the code. Although things like constructors can slow down object creation. But you will either need the constructor to set the default values or otherwise you can initialize objects without using the constructor (by leaving off the ()'s). But object orientation makes programming games much easier. Games often deal with objects. |
|||
|
|
