OT (ish): Quake2 Source Code

I don’t know how it happened, but this suddenly turned into a c vs c++ debate.
How tedious - I thought the contributors to this forum were above that sort of thing.

1st rule: C is generally faster than C++.

I’d call that “first myth”.

Y.

I just wanted to say C++ allows writing programs easier, so eliminating some part of performance oriented style (are you allways thinking about const/dest, copy, etc. functions?), but allowing to finish project faster.
But, fact is, sometimes, non-rational code can run well
OK, plzzz forget it

I don’t know how it happened, but this suddenly turned into a c vs c++ debate.

I know what happened. I should have never made that damned post. I knew it would turn into that, sorry. What I just wanted to say was that I’m fed up with all the hype about how the only way to write structured code is through C++ and classes. You can break down your app in small chunks which only do specific things with well-defined interfaces in C just as well. And C++ isn’t slower than C, how can it be? C++ is a superset of C so you can just use C features if you like. Ok so some features can have a lot of overhead but using C the wrong way can be slow too. Besides not all parts of a program need to be super-fast. Not that I personally like C++,I don’t, but still the truth has to be said.
Now about that distance thing. As you said it would take a moment to figure out what it does if you see it in some elses code. So this won’t really impair readability if put inside a well defined function. Say if a function calculates the distance of a bounding volume from a frustum plane for culling and only that, then a line like the long one I made the mistake to post would be obvious as to its purpose. On the other hand I’ve seen enough 600-line I-do-it-all functions where something like that might makes things worse but if you get to that point you’ve got bigger problems.
So to sum up (and hopefully be done with) what I want to say: There’s more to code organization than nice, cute tricks to make the code look more like natural language.

PS:
Is

Matrix.Copy(MatrixA, MatrixB);

so much clearer and easier than

copy_matrix(m1, m2);

Is a class with a bunch of members so much more organized than a source file with a bunch of(nicely prefixed if you want) functions?
I don’t know…

Errm, well if you’re using an IDE (like I do, guess which one ), then organising stuff into classes or namespaces helps tremendously. If you’re using VI, then I suppose it may be a little annoying toggling between files, but hey it’s 2003. Get a mouse!

You’ve just got to think about other people - the style of coding you’re advocating (distinguish functionality by shoving things in separate files, isn’t it?) is certainly not one that many people I’ve worked with would be happy to deal with on a modify/debug basis. You’d certainly have to make your own coffee in the office, I would think!
It’s fine to be like that if your code is never going to be touched by anyone else, but otherwise it’s just bloody-minded.
I imagine ID softwares new employees would just keep their mouths shut

> C vs C++
It should be procedural programming vs OOP, as you can do OOP in C. =P

Please don’t think I just want to make a childish point, I’m doing OOP in C in my job, I know it can be a pain in the ass at the beginning.

> C faster that C++
Everyone should have know how stupid this phrase is for ten years.

> If you’re using VI, then I suppose it may be
> a little annoying toggling between files,
> but hey it’s 2003. Get a mouse!

LOL =)

Ok, seriously, using only keyboard is still faster than switching between the keyboard and the mouse. And vim is really fast at this game.

Ok, seriously, using only keyboard is still faster than switching between the keyboard and the mouse. And vim is really fast at this game.

Ho hum, I don’t agree. Most of my time is not spent typing, most of it is spent constructing a mental model of the architecture of the application I’m trying to write/debug. An IDE definately helps enormously with this process.

A lot of you are probably not in the “industry”. There’s something to be said for knowing how to get things done vs. academia. Carmack knows how to get things done. Who cares if nobody can understand or read it, the point is it works and he made a LOT of money on it. I don’t think any gamer cares that he wrote 10 duplicate copies of the distance function versus doing it the “right way” and having a templatized C++ Point class that can do it for you.

Those of you who are anti-globals, anti-goto’s, etc have probably never worked on a real project with real deadlines. Sometimes you just gotta get it working and that’s the difference between sitting at home being able to spend months on designing a “neat looking” engine with all the latest OOP-isms and shipping a product. There’s a hell of lot more to shipping a game than the engine.

I think an even bigger myth than “C is faster than C++” is that “C++ is more readable than C”. C is almost always a lot easier to read and learn than C++. There is so much implicit stuff that goes on in C++ that understanding it can be a nightmare. For example,

result = function(a + b);

In C, I can instantly see what this does, and can immediately find the definition of function. In C++, I have to figure out if function is in the global scope or is it a class member? If it is a class member, is it static or not, and is it defined in this method or in a parent? Are there any additional implicit arguments to the function from default arguments? Are there any implicit conversions or overloaded operators in the “a + b” expression? How about in the assignment statement? There could be as many as 4 implicit constructor calls in that simple line of code, not counting any implicit conversions in the called function.

I’ve worked in the quake3 code base for many years and in the quake1 codebase for an entire project. The q3 code base is a lot better than q1, beyond question. I also had the chance to evaluate the latest Unreal source a while ago as a potential engine to use on a project, and it was nearly impossible to understand anything it did, exactly because of its rampant use of C++. The small sampling I’ve taken of other industry people I know who’ve worked in both code bases reveals similar conclusions… the C++ code base tends to be a lot harder to work in.

Doom3 is entirely C++ now, even the renderer. Carmack said this at the last quakecon I believe; the renderer was the last holdout for the longest time.

C++ is not inherently inferior to C, I tend to use it myself instead of C for most code I write, but my C++ code is usually written very similarly to the way I’d write C code so that other programmers (including myself in a few weeks or months) have a chance of understanding it.

Oh yes, I agree with you on that one. Overuse of the more exotic extensions in C++ is pretentious and annoying. But ‘straight’ C++ - in other words, using C++ to give you polymorphism, and only overloading maths operators for maths classes is a gods send, and makes for some beautiful and easy to follow applications.

Those of you who are anti-globals, anti-goto’s, etc have probably never worked on a real project with real deadlines. Sometimes you just gotta get it working and that’s the difference between sitting at home being able to spend months on designing a “neat looking” engine with all the latest OOP-isms and shipping a product. There’s a hell of lot more to shipping a game than the engine.

Sounds like you’ve been working for some quite short-sighted companies, fresh. Maybe in the last few weeks some globals and goto’s may crop up, but not in the fundamentals of the application you’re writing. If your company has no intention of re-using the codebase, then goto away! But if you have any desire to cut development time and cost, then you have to think carefully about what you’re doing - this probably explains carmacks move to c++ - it forces people to think about what they’re doing, to some extent.

I definately switched to C++ the day i recoded a physics engine i was working on, from C to C++. It was 10 times shorter, the code was much cleaner and simple to understand.

I still remember the days, when coding in C, i was spending hours to find good coding conventions to make things as clear as possible. I recently had a look at my old code, it looked like a nighmare (although at the moment it seemed so nice). Yet, for years i never accepted to code in C++.

To be honnest, i think it’s all a matter of habits. When you are used to a language, it’s hard to switch to another, unless you are forced to; and that’s independantly of pros/cons for the languages. That’s just human nature :slight_smile:

Y.

Yeap, that’s right, I thought a bit and realizet that FINALLY

But if we are talking about habits, will MS force us to use C#?

[This message has been edited by M/\dm/
(edited 05-06-2003).]

Yes I tend to like those fanatic anti-gotoers myself. Sure using goto for just about every ‘jump’ in the code can make things pretty bad but what about this:

for(i = 0; i < 10; i++) {
for(j = 0; j < 10; J++) {
if(f(i, j) == 0) goto done;

}
}
done:…

Situations like these come along pretty often anti without evil gotos this would look like:

for(i = 0; i < 10; i++) {
for(j = 0; j < 10; J++) {
if(f(i, j) == 0) {
flag = 1;
break;
}

}
if(flag)
break;
}

Not that the last version would be that much bad but I consider the first cleaner and neater.
In the end of the day it doesn’t matter wether you use C++ or C or gotos but wether you know how to use whatever you’re using.And C++ might help you there but only by enforcing one specific coding style. If you like that in language that’s fine, I prefer general purpose tools tat give you the choice to decide.

Originally posted by fresh:
A lot of you are probably not in the “industry”. There’s something to be said for knowing how to get things done vs. academia. Carmack knows how to get things done.

<rant>

I am in the industry, and I’m really ****ed off with this l33t engine coder attitude that games are somehow exempt from sensible programming practices.

To you it might seem like ‘getting it done’, but in most cases other people are going to have to debug and extend that code, and they won’t have the benefit of all the comments and understanding you’ve got stored in your head.

I have personally spent days on tasks that should have taken half an hour because someone decided that only they needed to understand their code.

Ignoring good practice is not the practical choice.

</rant>

The problem with C++ and generally OOP is that you need proper planning and time spent in designing before you write the code. I think few in the game industry have extra time to spend. Also even if you design well your engine in the beggining it is possible that later you will have to implement something (like a new special FX) you haven’t thought before that will screw completely the OO design.

Anyway, I think carmack writes almost all the engine code himself so he does not need to worry about understanding what the code does. So why whould he bother to write it clean? Moreover the fact that many companies actually buyed that code proves that not only he did a good engine but he also did it fast enough to be ahead of the competition. I guess everyone in the forum given enough time can write a Q3 engine with very good looking code - but by that time Cramck will have finished his Doom4 engine :slight_smile:

Originally posted by passalis:
The problem with C++ and generally OOP is that you need proper planning and time spent in designing before you write the code.

laughs So, you’re saying there’s some other language or technique where you DON’T need proper planning and time spent in designing before you write the code? I’d love to use that language or technique!

This is a rediculous post, passalis. If you’re beginning a large project, you always need to plan and design. Always. Ignoring those stages of development is a sure fire way to write garbage code that’s completely unmaintanable.

I for one find the second part of his post more interesting. A game engine is not likely to be used in industrial plane simulators for 10 years. It is an entertainment product. In this context, shipping the game 4 months before competing products (eg shipping it by Christmas, instead of March the year after) is critical. If so-called bad coding practices can make the difference (especially if the guy codes the entire engine by himslef), then it is ok. Granted, companies that license the engine will spend time (and thus money) to find out what’s going on. But that’s their problem. If they think it will cost too much, then they pick another engine. Experience seems to indicate that many companies think otherwise.

“Two royal scientific commissions advised the King against backing Columbus because they believed his calculations were incorrect. His request was denied. It was at this time that, when told of the negative answer to his request for an impossible venture that Columbus asked for a hard boiled egg and challenged his interlocutors to try to stand it on end – which, of course, they could not. He then cracked one end and stood it up. The meaning was clear – all things are difficult or impossible until you find the solution, then all will follow.”

…and many probably found much cleaner and nicer ways to crack the egg, but while they were busy improving egg-cracking technology, where do you think was Columbus going?

And while you experts are debating the finer points of his last engine Carmack is off working on his next.

Says it all really.

Well, no it doesn’t say anything really, dorbie.
He’s obviously had this debate before (whether with his imaginary friend or with his fellow programmers), otherwise why do you think he changed to c++?
I think question has to be asked though - would you call the quake engines a success considering they’re only used in PC first-person shooters?
Renderware is much better - even though it has it’s own problems, at least it has an architecture open enough to allow optimisations for specific platforms. The Quake2 engine (at least, don’t know about q3/doom4) seems very rigidly hard coded by comparison.

As I said, this was just a lazy sunday afternoon conversation I wanted to have - but it’s tuesday now, so I’m happy to leave it at that.