Page 1 of 1
The use of threads in video games
Posted: Fri Feb 24, 2006 1:42 am
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.
Re:The use of threads in video games
Posted: Fri Feb 24, 2006 9:55 am
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
Re:The use of threads in video games
Posted: Fri Feb 24, 2006 1:28 pm
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).
Re:The use of threads in video games
Posted: Fri Feb 24, 2006 6:10 pm
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?
Re:The use of threads in video games
Posted: Sat Feb 25, 2006 4:06 am
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.
Re:The use of threads in video games
Posted: Sat Feb 25, 2006 11:13 am
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)