The use of threads in video games

Programming, for all ages and all languages.
Post Reply
beyondsociety

The use of threads in video games

Post by beyondsociety »

Me and a couple of my friends have gotten together and we are in the process of designing a game. Since I know so much Ive been assigned the job of programming the game and coming up with the code design. I was messing around with the SDL graphics library and noticed a function for threads.

Is there any reason why I would want to use threads in a video game? I could see the use of it in the game engine, as you would want to be able to have multiple input and output functions running while your playing the game as well as be able to interrupt or pause the game while your playing.
JoeKayzA

Re:The use of threads in video games

Post by JoeKayzA »

beyondsociety wrote: Is there any reason why I would want to use threads in a video game? I could see the use of it in the game engine, as you would want to be able to have multiple input and output functions running while your playing the game ...
Besides IO threads, you might want to be scalable to multiple processors or cores - but that will require an advanced design which allows for parallel computing...
beyondsociety wrote: ... as well as be able to interrupt or pause the game while your playing.
What I've seen so far this is better accomplished (and also possible in a single-threaded design) by just setting the 'time factor' to zero. You'll need a time factor anyway to deal with different cpu speeds.

cheers Joe
Kemp

Re:The use of threads in video games

Post by Kemp »

You'll need a time factor anyway to deal with different cpu speeds
Actually, it's not a good idea to assume anything about CPU speed, including running your game at a speed determined by a multiplier/divisor of it. Simply grab the current time at the start of each frame and adjust how much happens by how much time elapsed, that'll deal much better with situations where you aren't always receiving an exactly constant amount of CPU time, though you should watch out for a few special conditions (being able to glitch through the Earth in Google Earth is fun, I believe it may be a related issue).
GLneo

Re:The use of threads in video games

Post by GLneo »

could you use threads for cpu players, so you dont need to handle them 1 at a time?, also if you code is more cpu intensive, the cpu players would just think less rather than your whole game slowing down?
JoeKayzA

Re:The use of threads in video games

Post by JoeKayzA »

Kemp wrote:
You'll need a time factor anyway to deal with different cpu speeds
Actually, it's not a good idea to assume anything about CPU speed, including running your game at a speed determined by a multiplier/divisor of it. Simply grab the current time at the start of each frame and adjust how much happens by how much time elapsed, that'll deal much better with situations where you aren't always receiving an exactly constant amount of CPU time, ...
You are right, it's neccessary to dynamically adjust the time factor I've mentioned.
Kemp

Re:The use of threads in video games

Post by Kemp »

btw, sorry if I interpreted what you said wrong, I took time factor to mean a multiplier/divisor for the CPU speed (as old games under DOS did)
Post Reply