Hi all,
(I did a very canonical Win32/GDI sample to demonstrate this issue. It can be downloaded above)
erratum : the sample is bogus. I will post it again later.
I am trying to run my game rendering and physics in separate threads.
Rendering is done in the main thread while physics are processed in "child" thread.
This pseudo-code may explain better what I am doing
-> The first piece is in the main message loop (main thread)
-> The second piece is in the "physics thread" (child thread)
The trouble I am running in is that the system seems not to give the same CPU time to the two threads. This makes the rendering to hang (which make the displayed objects to "lag").
I am sure I am not explaining well the problem so below are some details
Consider a square that traverses a horizontal line on the screen at a speed of 100pixel/second.
The physics routine processes the real time position (based on system
ticks) for our square. When threaded as explained, this routine is called about 2millions times in a second(on my PC). So the square logical position is not supposed to move by more than 1 pixel at a time.
The rendering routine simply displays our square at its position. This routine is called about a thousand times in a second. So, as for physics, the square should occupy each single pixel on its trajectory.
It seems reality doesn't meet the theory because the given square moves smoothly during some pixels and then lag and "jump" 10 further pixels.
I tried synchronizing data access for the two threads (even if physics only writes and rendering only reads). Nothing changed.
I tried changing thread/process priorities. Same behaviour.
I tried on Windows CE (PPC2002 and WM5) and it's the same behaviour. The only thing that seemed to make it better is to delay the physics with a call to Sleep.
Any idea?
PS: The GDI sample provided only demonstrates the problem. The real program uses PocketFrog but I though it was more appropriate to post this here. So this sample does not use PF/PHAL.
(I did a very canonical Win32/GDI sample to demonstrate this issue. It can be downloaded above)
erratum : the sample is bogus. I will post it again later.
I am trying to run my game rendering and physics in separate threads.
Rendering is done in the main thread while physics are processed in "child" thread.
This pseudo-code may explain better what I am doing
-> The first piece is in the main message loop (main thread)
- Code:
while(1)
{
// checking for possible window message to process
if (::PeekMessage( &l;_oMessage, 0, 0, 0, PM_REMOVE ))
// Do message processing stuffs...
else
// If there was no message to process, render a single frame
l_oTheGameCore.DoRendering();
}
-> The second piece is in the "physics thread" (child thread)
- Code:
DWORD WINAPI PhysicsThread(LPVOID p_pGameCore) {
GameCore *l_pGameCore = (GameCore*)p_pGameCore;
while (g_bDoPhysics)
l_pGameCore->DoPhysics();
return 0;
}
The trouble I am running in is that the system seems not to give the same CPU time to the two threads. This makes the rendering to hang (which make the displayed objects to "lag").
I am sure I am not explaining well the problem so below are some details
Consider a square that traverses a horizontal line on the screen at a speed of 100pixel/second.
The physics routine processes the real time position (based on system
ticks) for our square. When threaded as explained, this routine is called about 2millions times in a second(on my PC). So the square logical position is not supposed to move by more than 1 pixel at a time.
The rendering routine simply displays our square at its position. This routine is called about a thousand times in a second. So, as for physics, the square should occupy each single pixel on its trajectory.
It seems reality doesn't meet the theory because the given square moves smoothly during some pixels and then lag and "jump" 10 further pixels.
I tried synchronizing data access for the two threads (even if physics only writes and rendering only reads). Nothing changed.
I tried changing thread/process priorities. Same behaviour.
I tried on Windows CE (PPC2002 and WM5) and it's the same behaviour. The only thing that seemed to make it better is to delay the physics with a call to Sleep.
Any idea?
PS: The GDI sample provided only demonstrates the problem. The real program uses PocketFrog but I though it was more appropriate to post this here. So this sample does not use PF/PHAL.


