Archived

This topic is now archived and is closed to further replies.

ANTnet

[Sprites] One thread per sprite ?

Recommended Posts

ANTnet    122
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

Share this post


Link to post
Share on other sites
Vangelis    122
As WillM threads are way to complex for what you''re trying to do. Every frame just go through your sprites and process them.

Share this post


Link to post
Share on other sites
ANTnet    122
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?

Share this post


Link to post
Share on other sites
DreadID4    133
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by granat
NOOOOOOOOOOOOOOOO !


Share this post


Link to post
Share on other sites
ANTnet    122
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]

Share this post


Link to post
Share on other sites
granat    122
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
ANTnet    122
Ok I think it''s the reason, just flipping, nothing drawn in it but...
I have put a SDL_GetKeyState and if LEFT: x--;

DrawScene()
{
locksurface
putpixel(x,y);
unlock
}

and even if I don''t release the left key, the pixel is moving but I still have a blinking screen (black to my image, I mean I can see that the screen is all black and my image are on it after, that''s why it blinks: black then image...)

Do you see for the left moves... ?

Share this post


Link to post
Share on other sites