An approach using threads, some advice please.
Posted: Wed Jun 28, 2006 1:03 pm
I am thinking of using an approach as outlined below and, as the project is big and even a testbed is going to take some work, I would appreciate any input or advice before committing to it.
For the summary, let:
GE = Game Engine (AI, general work) - A thread
SR = Screen Render - A thread
UI = User Interface (input and general housekeeping)
This is all one program, but internally it will have these three parts.
I want these three parts to run threaded, without too much awareness of each other. However, they are independent, sharing only some information.
(The program will not use threadsafe.)
The UI role is to hang around waiting for user input and either deal with it or store it for the GE (instructions, orders, etc) and SR (eg, advising which way the map has been scrolled and by how much).
Note: UI does the initial startup, then launches the two threads and drops into a listening loop. It also advises the other two to pause or stop completely and does things like saves, the closedown, etc, etc.
The GE role is to deal with the information to hand, eg, user inputs as reported by UI and internal info as created by the game dynamics, and resolve anything that needs resolving.
The SR role is to update the screen as often as possible. That is all it does, using information made available by GE and UI.
Each will have data reserved for itself, and there will be shared data.
There will be a DoNotDoAnythingSignificant flag which all three can view at any time, but can only set with a successful LockMutex(x) or TryLockMutex(x). In general, Mutex will only be used when shared data areas (like the DoNotDoAnythingSignificant flag) are updated.
Each will Delay(1) after a cycle. (Is this good or necessary?)
I am hoping this will make the game graphics flow as smoothly as possible on any machine via SR, and the GE will just flog away moving units, resolving conflicts, etc as fast as possible. Obviously there is some interaction but with what I have concept-designed so far it is (hopefully) minimal.
Is the concept sound? What pitfalls do you see? Will the three conflict with each in competing for system resources (main timeslices)? Provided I ensure that updating shared data is properly governed by mutex-ing, is this safe?
Sorry for such a broad-based question, but game design is not my forte (I have only written small stuff) and I really don't want to go some way down a poor path before I discover it is a poor path, especially at such a fundamental level.
PS: There may be other "supporting" threads. For example GE might use one or more PathFinding requests (threads), eg "give me best path for this unit type from x1,y1 to x2,y2 with 'n' waypoints" and PathChecking requests ("Has this path become blocked/invalid").
PPS: I had considered using client/server even for single user (Quake-ish approach I think) but that adds extra complexity (for me) so I'll stick with single player verses AI and hopefully develop a good and basically re-usable AI.
For the summary, let:
GE = Game Engine (AI, general work) - A thread
SR = Screen Render - A thread
UI = User Interface (input and general housekeeping)
This is all one program, but internally it will have these three parts.
I want these three parts to run threaded, without too much awareness of each other. However, they are independent, sharing only some information.
(The program will not use threadsafe.)
The UI role is to hang around waiting for user input and either deal with it or store it for the GE (instructions, orders, etc) and SR (eg, advising which way the map has been scrolled and by how much).
Note: UI does the initial startup, then launches the two threads and drops into a listening loop. It also advises the other two to pause or stop completely and does things like saves, the closedown, etc, etc.
The GE role is to deal with the information to hand, eg, user inputs as reported by UI and internal info as created by the game dynamics, and resolve anything that needs resolving.
The SR role is to update the screen as often as possible. That is all it does, using information made available by GE and UI.
Each will have data reserved for itself, and there will be shared data.
There will be a DoNotDoAnythingSignificant flag which all three can view at any time, but can only set with a successful LockMutex(x) or TryLockMutex(x). In general, Mutex will only be used when shared data areas (like the DoNotDoAnythingSignificant flag) are updated.
Each will Delay(1) after a cycle. (Is this good or necessary?)
I am hoping this will make the game graphics flow as smoothly as possible on any machine via SR, and the GE will just flog away moving units, resolving conflicts, etc as fast as possible. Obviously there is some interaction but with what I have concept-designed so far it is (hopefully) minimal.
Is the concept sound? What pitfalls do you see? Will the three conflict with each in competing for system resources (main timeslices)? Provided I ensure that updating shared data is properly governed by mutex-ing, is this safe?
Sorry for such a broad-based question, but game design is not my forte (I have only written small stuff) and I really don't want to go some way down a poor path before I discover it is a poor path, especially at such a fundamental level.
PS: There may be other "supporting" threads. For example GE might use one or more PathFinding requests (threads), eg "give me best path for this unit type from x1,y1 to x2,y2 with 'n' waypoints" and PathChecking requests ("Has this path become blocked/invalid").
PPS: I had considered using client/server even for single user (Quake-ish approach I think) but that adds extra complexity (for me) so I'll stick with single player verses AI and hopefully develop a good and basically re-usable AI.