[Sprites] One thread per sprite ?

Started by
10 comments, last by ANTnet 20 years, 11 months ago
Hi~ I want to know how to manage sprites in my 2D game. Do I have to make one thread per sprite? This thread will check for collision, moves, gfx blit,..., and once the sprite is dead, the thread dies. Is it the correct way to proceed? thanks ANT
Advertisement
Threads would be way overkill. Simply process each sprite every frame..
As WillM threads are way to complex for what you''re trying to do. Every frame just go through your sprites and process them.
---------------------Ryan Bujnowicz[ vangelis ]
NOOOOOOOOOOOOOOOO !
-------------Ban KalvinB !
ok, can you explain me, "each frame processing" :
in my game (sdl), I have a message loop, and I create a thread to move my character:

global down;

createthread(moving);

loop
{
if keydown: down=true;
}


moving()
{
while(true)
{
if (down)
moveMyShip();
sleep(100)
}
}

is it wrong to move my ship like that?
You have to ask yourself, why do you want to use threads? Threads are usually only useful if your making a procedure call which has to access resources that have a high ''wait time''. Examples are: the hard drive, the network (for multiplayer games), and the video card. The way i have my game structured is to separate the graphics pipeline (like the DirectX rendering API), the update routines (when you update the objects physics, animations, etc...), and the network (and if access to the harddrive is absolutely necessary during gameplay, i put that in a separate module). Upon separating them, you can split them into different threads so as to maximize speed (especially on multiple processor systems). At max, i''ve only used 3 threads in my game at any time. You definately want to keep threads to a minimum.

Here''s an example: Say you make a request to the video card or hard drive for resources and you haven''t multi-threaded your application. Your program is stuck until the procedure you called returns back to your program (which can be a very long time in the case of the HD). But if you have threads, you can do other things with the processor while your waiting for the other command to finish (like updating your objects physics while the video card is busy rendering the objects)

As for creating a thread for every single sprite? That''s massively excessive and might be a tragedy waiting to happen with shared memory. Remember: Threads add considerable overhead if they''re not used right (like if you create 100 threads for all 100 objects in your game, that''s a strain). You also have to take into consideration critical sections for shared peices of memory as well.

DreadID4
quote:Original post by granat
NOOOOOOOOOOOOOOOO !


I have found a good tutorial on that but I have a big problem:
in my function DrawScene which is called in each loop of the loop message, in DrawScene i have a SDL_Flip(screen), like in the example, but when I run, the screen is "flashing", "blinking", I don't understand why...

[edited by - ANTnet on May 28, 2003 4:15:52 AM]
Sounds like the flip is not synced with the monitor refresh rate. You should be able to set this somewhere. Look in the docs under the flip command.
-------------Ban KalvinB !
quote:Original post by ANTnet
I have found a good tutorial on that but I have a big problem:
in my function DrawScene which is called in each loop of the loop message, in DrawScene i have a SDL_Flip(screen), like in the example, but when I run, the screen is "flashing", "blinking", I don''t understand why...


You are probably not drawing anything into the buffers, only flipping them. In my experience (no apparent reason, just my experience) the front buffers seem to be all white at default in the beginning but the back buffer all black (or vice versa). This would explain the blinking when you are only switching between buffers. As soon as you start blitting something into those buffers the blinking will stop (as long as you cover them with the blitted objects).
The buffer thing has been the way I describe in DirectDraw always and also in OpenGL as I best recall.

Kári.

This topic is closed to new replies.

Advertisement