Advertisement

How many of you use C for game programming?

Started by January 24, 2011 04:33 AM
107 comments, last by Washu 13 years, 6 months ago
I learned game programming in C++, and I program in C at work (for database systems).

I know C and C++ nowadays are real handy for dealing with hardware and almost kernel-level instructions to do things quickly or to interface with some other module, but personally I never had a reason to use C in game programming. If I wanted to do something quickly (like a CAS, for a bad example) I can call the assembly code inside of C++.
Keith M. Programming - My Game Dev Blog.
Tutorials. Games. Code Snippets. Bad Jokes. I got 'em all.

Follow me on Twitter. [twitter]KeithMaggio[/twitter]
Listen to me yap about programming and games and junk.
Has anyone ever finished a game in C, and wished they had used C++? Which language features did you miss the most?

I work in C frequently when I'm not working as part of a team. It's clean, pure, simple, and fun. RIAA isn't that useful when using a factoryish pattern where objects are only created/destroyed in one place. I use composition rather than inheritance, so I don't miss virtual functions much. C string manipulation is trivial. C abstract types are better than C++ pure virtual classes. Interop with other languages or C APIs is cleaner. Container classes are the only things I miss, fortunately there are solid libraries to do hashtables, lists and vectors, and that's really all you need.
Anthony Umfer
Advertisement
Let's be clear, in comparison to most modern languages, c++ is pretty terrible. But it's still a step up from C. Even if you only used the string class, and wrote everything else as procedural C, you're instantly ahead.

There are legitimate uses of C, (severely resource constrained environments, platforms where there simply isn't another compiler for a different language). But outside of that, you're just wasting your time.

As for francoispress's 400 lines of c vs 1200 lines of C++, I'm sorry, but that just means you're doing it wrong. Besides, a 400 line program is so trivial that it is meaningless as a real metric.

But hey, if you want to write your hobby project in C, go ahead. It's akin to woodworking with manual tools. People might admire the craft and the handmade look to it, but people who do that stuff for a living? They use powertools.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

But hey, if you want to write your hobby project in C, go ahead. It's akin to woodworking with manual tools. People might admire the craft and the handmade look to it, but people who do that stuff for a living? They use powertools.


This. I still use C/C++ for hobby projects. Nothing gives me a better codegasm than seeing stars, ampersands, lts, and gts in my code.

I would only use C/C++ for larger projects if the IDE is equipped with really good refactoring, memory leak detector, intellsense, and code completion tools. So far, I haven't found any that matches IntelliJ (Java IDE), but, I haven't looked that far.

Let's be clear, in comparison to most modern languages, c++ is pretty terrible. But it's still a step up from C. Even if you only used the string class, and wrote everything else as procedural C, you're instantly ahead.

There are legitimate uses of C, (severely resource constrained environments, platforms where there simply isn't another compiler for a different language). But outside of that, you're just wasting your time.

As for francoispress's 400 lines of c vs 1200 lines of C++, I'm sorry, but that just means you're doing it wrong. Besides, a 400 line program is so trivial that it is meaningless as a real metric.

But hey, if you want to write your hobby project in C, go ahead. It's akin to woodworking with manual tools. People might admire the craft and the handmade look to it, but people who do that stuff for a living? They use powertools.


Wrong! F*ck powertools! I mix concrete in barrow with a shovel.

Anyway, if you don't care about neat code, coding in C can be fast. I coded tools, game prototypes (with full functionality), windowed GUI:s etc in days (2-3 days, 8 hrs a day). Maybe I could code these in C++ in hours though...
A reason (as others have already mentioned) that C++ is hard. And (as I see) only a few can code in it properly. For them, it is faster, for others, the benefits of it outweigh the time it takes to struggle with the classes. Most the stuff I see posted on the forums about implementation and class issues could be solved with a few lines of trivial C code and with trivial logic. And most of us are hobbyists, so I don't really see any point to write so neat code. Just get the job done. And C is capable for that, if you reuse code. Which you can easily to do so, because functions are not sticked into some class. But I may be wrong, I'm not a real programmer.

[quote name='ChaosEngine' timestamp='1296552884' post='4767873']
Let's be clear, in comparison to most modern languages, c++ is pretty terrible. But it's still a step up from C. Even if you only used the string class, and wrote everything else as procedural C, you're instantly ahead.

There are legitimate uses of C, (severely resource constrained environments, platforms where there simply isn't another compiler for a different language). But outside of that, you're just wasting your time.

As for francoispress's 400 lines of c vs 1200 lines of C++, I'm sorry, but that just means you're doing it wrong. Besides, a 400 line program is so trivial that it is meaningless as a real metric.

But hey, if you want to write your hobby project in C, go ahead. It's akin to woodworking with manual tools. People might admire the craft and the handmade look to it, but people who do that stuff for a living? They use powertools.


Wrong! F*ck powertools! I mix concrete in barrow with a shovel.

Anyway, if you don't care about neat code, coding in C can be fast. I coded tools, game prototypes (with full functionality), windowed GUI:s etc in days (2-3 days, 8 hrs a day). Maybe I could code these in C++ in hours though...
A reason (as others have already mentioned) that C++ is hard. And (as I see) only a few can code in it properly. For them, it is faster, for others, the benefits of it outweigh the time it takes to struggle with the classes. Most the stuff I see posted on the forums about implementation and class issues could be solved with a few lines of trivial C code and with trivial logic. And most of us are hobbyists, so I don't really see any point to write so neat code. Just get the job done. And C is capable for that, if you reuse code. Which you can easily to do so, because functions are not sticked into some class. But I may be wrong, I'm not a real programmer.
[/quote]This is why we need a - button on the bottom of posts.
Advertisement

Amazing how big this thread got. Anyways, here are some stats from a projects I did in the past, a level editor:

C: 400 lines of code
C++: 1200 lines of code


In case this is not a joke post: Show us the code. Otherwise take my claim: I wrote a ray tracer in C++. It was 100 lines. The one I wrote in C was 7890438 lines of code.

Here's a small taste of some pseudo C++ code, just for starters:

FooResult try_foo () {
std::ifstream x("resources/foo.txt");
if (!x.is_open()) return FAIL;
std::ifstream y(readValue(x, "path"));
if (!y.is_open()) return FAIL;
std::ifstream z(readValue(z, "path"));
if (!z.is_open()) return FAIL;

return OKAY;
}


The pseudo C counterpart looks like this:

FooResult try_foo (void) {
FooResult ret = OKAY;
FILE *x = fopen ("resources/foo.txt", "r");
if (!x) { ret=FAIL; goto finally; }
FILE *y = fopen (readValue(x, "path"), "r");
if (!y) { ret=FAIL; goto finally; }
FILE *z = fopen (readValue(y, "path"), "r");
if (!z) { ret=FAIL; goto finally; }

finally:
if (z) fclose(z);
if (y) fclose(y);
if (x) fclose(x);
return ret;
}


Or did I miss some modern C idiom?

[quote name='geolycosa' timestamp='1296241113' post='4766298']
Linus Torvalds (somewhat angrily) on the subject: link

EDIT: Also here.


Um, I'm not an expert and lazy to copy that into google, but isn't that the Finnish idiot Linux guy?
[/quote]

And this makes me want a vote down button back. I largely disagree with him in that famous email conversation, and even more with his insults there, but fighting insults with insults is absurd in itself.

FooResult try_foo () {
std::ifstream x("resources/foo.txt");
if (!x.is_open()) return FAIL;
std::ifstream y(readValue(x, "path"));
if (!y.is_open()) return FAIL;
std::ifstream z(readValue(z, "path"));
if (!z.is_open()) return FAIL;

return OKAY;
}


The pseudo C counterpart looks like this:

FooResult try_foo (void) {
FooResult ret = OKAY;
FILE *x = fopen ("resources/foo.txt", "r");
if (!x) { ret=FAIL; goto finally; }
FILE *y = fopen (readValue(x, "path"), "r");
if (!y) { ret=FAIL; goto finally; }
FILE *z = fopen (readValue(y, "path"), "r");
if (!z) { ret=FAIL; goto finally; }

finally:
if (z) fclose(z);
if (y) fclose(y);
if (x) fclose(x);
return ret;
}


Or did I miss some modern C idiom?


You're comparing a few standard library functions, not idiomatic use of the languages.

Also, Linus was flaming in that post, but he did have a point. C++ damn near encourages you to write bad code. You can write bad code in C of course, but it's usually way smellier.
Anthony Umfer

You're comparing a few standard library functions, not idiomatic use of the languages.


Nope. He's showing RAII, a huge advantage of C++ over C.

This topic is closed to new replies.

Advertisement