How do games multitask (or give the illusion of it)?

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
In a typical game, there are many things going on at once: multiple enemies moving around and shooting, jumping, etc., music playing, and your actions as well. My question is how does this all take place at once? Are there commands in a language that instruct a program to run two or more portions of code simultaneously? Or do all of these things really happen one at a time, but because processors are so fast we just cant tell?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Some support comes directly from the Operating System -- allows you multiple threads to simulate working in parallel. Rest of it is coded directly in the code (stuff like AI for example).
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
Actually, they don't do things simultaneously, and you don't even really need threads...

If your videocard would be able to display 1,000,000 fps, you could for example see the one guy moves 1 microsecond before the other guy.

These days, with 1GHz + processor, you can complete 1 simple command in a nanosecond or less, giving you much headroom for 1 microsecond, which you can't even see.

100 fps = 10 ms, which is ages in terms of computer commands...

Most games are designed to react to you, so if you sneak around a corner in Quake 3, and all of a sudden there are 3 enemies there, the program checks: Who can view you, and then for all of the ones who spot you, let the logic decide what to do, and start animating them.
The animation actually takes most of the cycles...
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
They released the Q2 source? I knew they released Q1 source quite awhile ago. Of course I probably couldn't make much sense of their source code given my limited knowledge but I may give it a look.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: RSMemphis
... don't even really need threads ...

Yes you do - otherwise the performance would suffer quite a bit. Threads are critical for Network play for example. Music is also usually run in a separate thread.

I remember that in the days of DOS, programmers used to use the Timer Interrupts to simulate threads :)

 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: SpecialK
They released the Q2 source? I knew they released Q1 source quite awhile ago. Of course I probably couldn't make much sense of their source code given my limited knowledge but I may give it a look.

You'll have better luck comprehending Linux source or something :p The code's horrible for people unfamiliar with ID's code style.
 

zephyrprime

Diamond Member
Feb 18, 2001
7,512
2
81
Do games borrow on some of the techniques of operating systems and have thier own "processes" that they manage themselves or do they just keep a list of stuff they have to do and iterate through the list.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: zephyrprime
Do games borrow on some of the techniques of operating systems and have thier own "processes" that they manage themselves or do they just keep a list of stuff they have to do and iterate through the list.

Really depends on what kind of services the Operating System provides. For example, if you have multiple threads running, you can change their priority so that they only get a certain % of the CPU. If the game needs even finer control, then yes, the game will simulate taks running like processes. But it is quite time consuming to code, and most programmers will not bother.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Originally posted by: SpecialK
They released the Q2 source? I knew they released Q1 source quite awhile ago. Of course I probably couldn't make much sense of their source code given my limited knowledge but I may give it a look.


They actually released Q3 source code as well. It's written in hardcore c.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
They actually released Q3 source code as well. It's written in hardcore c.

Only parts that are relvant to mod makers was released from Q3, for Q1 and Q2 the whole engine is released under the GPL, only the media (textures, maps, models, etc) is missing.