When programming a game with multiplethreads

Is it smart to program a thread for every object or is that doomed to fail?

I would love to create one thread that can hold multiple objects but how would I make them move&die independently?

How do game programmers these days use their threads?

Also I'm having some trouble passing attribute refrences to a thread from another thread.
Creating threads using other threads is that tabu?

[code]typedef struct
{
int x;
int y;
int armour;
int character;
bool active;
} unit;
DWORD WINAPI MISSILEThread(LPVOID lpParam)
{
unit missile = *(unit*)lpParam;
int x=missile.x;//<------------SEEMS TO BE LOST IN TRANSLATION NOT SURE WHAT LPPARAM DOES. GIVES THE WRONG VALUE I'VE TESTED IT.
int y=23;
int prevx=x;
int prevy=y;
while(y>0)
{
nextpicture[coordinateconvert(x,y)]=missile.character;
Sleep(100);
nextpicture[coordinateconvert(prevx,prevy)]=0;
prevx=x;
prevy=y;
y--;
}
return 1;
}
void missile(int x,int y,int direction) //int directions 0=up 1=down 2=left 3=right
{
HANDLE missileThread;
DWORD dwmissileThread_id;
unit missilexy={x,y,1,179,true};
missileThread = CreateThread(NULL,0,MISSILEThread,(LPVOID)&missilexy,0,&dwmissileThread_id);
//WaitForSingleObject(missileThread, INFINITE); //only one shot
}[/code]

Comments

  • : Is it smart to program a thread for every object or is that doomed
    : to fail?
    :
    : I would love to create one thread that can hold multiple objects but
    : how would I make them move&die independently?
    :
    : How do game programmers these days use their threads?
    :
    : Also I'm having some trouble passing attribute refrences to a thread
    : from another thread.
    : Creating threads using other threads is that tabu?
    :
    : [code]: DWORD WINAPI MISSILEThread(LPVOID lpParam)
    : {
    : unit missile = *(unit*)lpParam;
    : int x=missile.x;//<------------SEEMS TO BE LOST IN TRANSLATION NOT SURE WHAT LPPARAM DOES. GIVES THE WRONG VALUE I'VE TESTED IT.
    : int y=23;
    : int prevx=x;
    : int prevy=y;
    : while(y>0)
    : {
    : nextpicture[coordinateconvert(x,y)]=missile.character;
    : Sleep(100);
    : nextpicture[coordinateconvert(prevx,prevy)]=0;
    : prevx=x;
    : prevy=y;
    : y--;
    : }
    : return 1;
    : }
    : void missile(int x,int y,int direction) //int directions 0=up 1=down 2=left 3=right
    : {
    : HANDLE missileThread;
    : DWORD dwmissileThread_id;
    : unit missilexy={x,y,1,179,true};
    : missileThread = CreateThread(NULL,0,MISSILEThread,(LPVOID)&missilexy,0,&dwmissileThread_id);
    : //WaitForSingleObject(missileThread, INFINITE); //only one shot
    : }[/code]:

    1 Thread per object is very poor design, since that would in some cases mean that you can have 1000+ threads. 1000+ threads is a huge drain on resources and takes a huge amount of time to synchronize, making the game sluggish to say the least.
    Game developers usually use a game loop, and iterate within that loop across all the necessary objects. In pseudocode:
    [code]
    repeat
    Move Player
    for each movable object
    if object is alive then
    move object
    if collides with another then
    handle collision
    if object is dying then
    next death sequence step
    endfor
    update screen
    until game is ended
    [/code]
    As you can see, this turns an seemingly active game into a turn-based game, where each "player" gets a go. Ideally this loop runs 25-29 times per second giving the impression of fluid motion. All too often games run at less than that due to lack of processing power of the hardware.
    As you also can see, this can easily be coded in 1 thread. This greatly reduces (or better eliminates) the need for thread-synchronization and resource-locking, freeing up resources which can be used to improve the game itself.
  • : 1 Thread per object is very poor design, since that would in some
    : cases mean that you can have 1000+ threads. 1000+ threads is a huge
    : drain on resources and takes a huge amount of time to synchronize,
    : making the game sluggish to say the least.
    : Game developers usually use a game loop, and iterate within that
    : loop across all the necessary objects. In pseudocode:
    : [code]:
    : repeat
    : Move Player
    : for each movable object
    : if object is alive then
    : move object
    : if collides with another then
    : handle collision
    : if object is dying then
    : next death sequence step
    : endfor
    : update screen
    : until game is ended
    : [/code]:
    : As you can see, this turns an seemingly active game into a
    : turn-based game, where each "player" gets a go. Ideally this loop
    : runs 25-29 times per second giving the impression of fluid motion.
    : All too often games run at less than that due to lack of processing
    : power of the hardware.
    : As you also can see, this can easily be coded in 1 thread. This
    : greatly reduces (or better eliminates) the need for
    : thread-synchronization and resource-locking, freeing up resources
    : which can be used to improve the game itself.

    Okay but what if I want the objects to move individually? Don't I have to create a diffrent thread for diffrent movement? Also how would I create an object in a single thread in pseudo code. Should it look for them in an array? How would I control the objects from outside the thread?
  • : : 1 Thread per object is very poor design, since that would in some
    : : cases mean that you can have 1000+ threads. 1000+ threads is a huge
    : : drain on resources and takes a huge amount of time to synchronize,
    : : making the game sluggish to say the least.
    : : Game developers usually use a game loop, and iterate within that
    : : loop across all the necessary objects. In pseudocode:
    : : [code]: :
    : : repeat
    : : Move Player
    : : for each movable object
    : : if object is alive then
    : : move object
    : : if collides with another then
    : : handle collision
    : : if object is dying then
    : : next death sequence step
    : : endfor
    : : update screen
    : : until game is ended
    : : [/code]: :
    : : As you can see, this turns an seemingly active game into a
    : : turn-based game, where each "player" gets a go. Ideally this loop
    : : runs 25-29 times per second giving the impression of fluid motion.
    : : All too often games run at less than that due to lack of processing
    : : power of the hardware.
    : : As you also can see, this can easily be coded in 1 thread. This
    : : greatly reduces (or better eliminates) the need for
    : : thread-synchronization and resource-locking, freeing up resources
    : : which can be used to improve the game itself.
    :
    : Okay but what if I want the objects to move individually? Don't I
    : have to create a diffrent thread for diffrent movement? Also how
    : would I create an object in a single thread in pseudo code. Should
    : it look for them in an array?

    You can use an array or linked-list to store the individual objects.
    You don't need extra threads for individual movements. Here's a bigger example (again in pseudo code):
    [code]

    class movable {

    int direction, x, y
    int oldx, oldy

    move() {
    oldx = x;
    oldy = y;
    switch (direction) {
    case up: y = y + 1
    case down: y = y - 1
    }
    }

    draw() {
    draw(x, y)
    }

    clear() {
    draw(oldx, oldy)
    }

    draw(int x, int y) {
    XORDraw(x, y, icon)
    }

    }

    class missile extends movable {

    move() {
    switch (direction) { // Missiles move very fast
    case up: y = y + 3
    case down: y = y - 3
    }
    }

    }

    main() {
    repeat
    switch (Player input) {
    case up: player.direction = up;
    case down: player.direction = down;
    case s: movables.add(new missile(player.x, player.y))
    }
    for (i = 0; i < movables.length; i++) {
    movables[i].move() // each movable makes 1 step
    }
    for (i = 0; i < movables.length; i++) {
    movables[i].clear() // remove previous location on screen
    movables[i].draw() // draw icon at the new location on screen
    }
    until game ended
    }
    [/code]
    In each repeat-loop iteration, each movable object makes 1 step at their speed in the direction it is moving. Because every movable object keeps track of their own location and direction, every object appears to be moving independently of eachother.
    Since the screen is updated after every movable object has gotten a new location, it appears that all movable objects have moved simultaneously.
    The frame rate of the game is determined by the time each iteration of the loop takes.
  • Thanks this helps a lot. I'm still not sure how to add an object non pseudo but I'll post back once I've done some thinking.
  • : Game developers usually use a game loop, and iterate within that
    : loop across all the necessary objects. In pseudocode:
    : [code]:
    : repeat
    : Move Player
    : for each movable object
    : if object is alive then
    : move object
    : if collides with another then
    : handle collision
    : if object is dying then
    : next death sequence step
    : endfor
    : update screen
    : until game is ended
    : [/code]:

    [color=Blue]This is good design for DOS, but for Windows - it is not that simple. Where this loop should be done? If Windows detects a slowdown in message processing - it marks your application as "Not Responding".

    The good way to do Windows game is control the Player (Human) from the keyboard and mouse messages in the main game window.

    However, to 'move' the rest of the game objects - this is where one thread can come in. There should be a collection (or array) of objects and each object should behave according to AI or some other algorithm. Once all objects are done with their behavior - the new frame is drawn - of course use in-memory drawing.

    Every time the access is needed for the objects from the main thread - this should be protected with critical section. Same goes for objects - if they need to know where the Player is and its status - also use critical section.[/color]
  • : : Game developers usually use a game loop, and iterate within that
    : : loop across all the necessary objects. In pseudocode:
    : : [code]: :
    : : repeat
    : : Move Player
    : : for each movable object
    : : if object is alive then
    : : move object
    : : if collides with another then
    : : handle collision
    : : if object is dying then
    : : next death sequence step
    : : endfor
    : : update screen
    : : until game is ended
    : : [/code]: :
    :
    : [color=Blue]This is good design for DOS, but for Windows - it is not
    : that simple. Where this loop should be done? If Windows detects a
    : slowdown in message processing - it marks your application as "Not
    : Responding".
    :
    : The good way to do Windows game is control the Player (Human) from
    : the keyboard and mouse messages in the main game window.
    :
    : However, to 'move' the rest of the game objects - this is where one
    : thread can come in. There should be a collection (or array) of
    : objects and each object should behave according to AI or some other
    : algorithm. Once all objects are done with their behavior - the new
    : frame is drawn - of course use in-memory drawing.
    :
    : Every time the access is needed for the objects from the main thread
    : - this should be protected with critical section. Same goes for
    : objects - if they need to know where the Player is and its status -
    : also use critical section.[/color]

    In the past I've solved the problem of "not responding" by forcing the code to process messages after the frame is drawn in the main loop. That way you still can use the old DOS style coding and don't need critical sections.
  • Yes currently im thinking of creating an array of objects and let a thread controll their movement. Not sure how to do this with classes and stuff. The "extends" "new" and "add" are pretty blurry to me
  • : Yes currently im thinking of creating an array of objects and let a
    : thread controll their movement. Not sure how to do this with classes
    : and stuff. The "extends" "new" and "add" are pretty blurry to me
    :
    I'm not a C++ coder, so the exact syntax of C++ subclasses might be different.
    Basically you should create a base class for your movable objects, and subclass that for each different type of game object. That way you can easily code the AI for each different type of game object.
    The word [b]extends[/b] is used in Java to indicate the superclass of a class.
    [b]add[/b] and [b]new[/b] should also be taken as non-C++ syntax (or perhaps it is).
  • : 1 Thread per object is very poor design, since that would in some
    : cases mean that you can have 1000+ threads. 1000+ threads is a huge
    : drain on resources and takes a huge amount of time to synchronize,
    : making the game sluggish to say the least.
    : Game developers usually use a game loop, and iterate within that
    : loop across all the necessary objects. In pseudocode:
    : [code]:
    : repeat
    : Move Player
    : for each movable object
    : if object is alive then
    : move object
    : if collides with another then
    : handle collision
    : if object is dying then
    : next death sequence step
    : endfor
    : update screen
    : until game is ended
    : [/code]:
    : As you can see, this turns an seemingly active game into a
    : turn-based game, where each "player" gets a go. Ideally this loop
    : runs 25-29 times per second giving the impression of fluid motion.
    : All too often games run at less than that due to lack of processing
    : power of the hardware.
    : As you also can see, this can easily be coded in 1 thread. This
    : greatly reduces (or better eliminates) the need for
    : thread-synchronization and resource-locking, freeing up resources
    : which can be used to improve the game itself.
    For each movable object? Is that done in an array? Or is there some trick to it when using classes? Because I thought that I could make a "static method" in the object class that would stepallobjects forward but my teacher said that it would probly not work.

    The method would look something like this
    [code]class objects
    {
    private int x,y;
    public static void stepforward()
    {
    x++; y++;
    }
    }
    int main()
    {
    objects.stepforward();
    }[/code]

    If I could do something like that I'd save me some trouble instead of making arrays and stuff. Also I've just learned OOP and now I understand inheriatance and stuff.
  • : : 1 Thread per object is very poor design, since that would in some
    : : cases mean that you can have 1000+ threads. 1000+ threads is a huge
    : : drain on resources and takes a huge amount of time to synchronize,
    : : making the game sluggish to say the least.
    : : Game developers usually use a game loop, and iterate within that
    : : loop across all the necessary objects. In pseudocode:
    : : [code]: :
    : : repeat
    : : Move Player
    : : for each movable object
    : : if object is alive then
    : : move object
    : : if collides with another then
    : : handle collision
    : : if object is dying then
    : : next death sequence step
    : : endfor
    : : update screen
    : : until game is ended
    : : [/code]: :
    : : As you can see, this turns an seemingly active game into a
    : : turn-based game, where each "player" gets a go. Ideally this loop
    : : runs 25-29 times per second giving the impression of fluid motion.
    : : All too often games run at less than that due to lack of processing
    : : power of the hardware.
    : : As you also can see, this can easily be coded in 1 thread. This
    : : greatly reduces (or better eliminates) the need for
    : : thread-synchronization and resource-locking, freeing up resources
    : : which can be used to improve the game itself.
    : For each movable object? Is that done in an array? Or is there some
    : trick to it when using classes? Because I thought that I could make
    : a "static method" in the object class that would stepallobjects
    : forward but my teacher said that it would probly not work.
    :
    : The method would look something like this
    : [code]: class objects
    : {
    : private int x,y;
    : public static void stepforward()
    : {
    : x++; y++;
    : }
    : }
    : int main()
    : {
    : objects.stepforward();
    : }[/code]:
    :
    : If I could do something like that I'd save me some trouble instead
    : of making arrays and stuff. Also I've just learned OOP and now I
    : understand inheriatance and stuff.
    :
    A static method is indeed not the way to go, because you cannot change the way the object moves. If you have a limited unchanging number of object, then you can do without arrays, but for any larger number of game objects and especially changing number of game object you will need some sort of list structure (array, linked list, tree, etc).
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories