YES very cool.
I have also been getting very excited with multithreading, it has so much potential.
The beauty is that it adds almost no significant overhead when running on a single core CPU, and provides huge benefits when running on a multi core CPU.
I will take the opportunity to add the following to this thread as it is relevant to the topic title.
Phoenix73 code sample:
#include "DarkGDK.h"
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <process.h>
#define MaximumSpheres 1000
HANDLE hEvent1, hEvent2;
float rotations[MaximumSpheres][3];
float positions[MaximumSpheres][3];
float sizes[MaximumSpheres];
void RecalculateSpheres(void);
#define MULTITHREAD
void Thread( void* pParams )
{
while ( TRUE )
{
WaitForSingleObject( hEvent2, INFINITE );
RecalculateSpheres();
SetEvent( hEvent1 );
}
}
void RecalculateSpheres(void)
{
for (int i = 0; i < MaximumSpheres; i++ )
{
rotations[i][0] += ((float)rand() / (float)(RAND_MAX)) - 0.5f;
rotations[i][1] += ((float)rand() / (float)(RAND_MAX)) - 0.5f;
rotations[i][2] += ((float)rand() / (float)(RAND_MAX)) - 0.5f;
for (int j = 0; j < MaximumSpheres; j++ )
{
if (j != i)
{
if ( ((positions[i][0] > (positions[j][0] - (sizes[j] / 2))) && (positions[i][0] < (positions[j][0] + (sizes[j] / 2)))) &&
((positions[i][1] > (positions[j][1] - (sizes[j] / 2))) && (positions[i][1] < (positions[j][1] + (sizes[j] / 2)))) &&
((positions[i][2] > (positions[j][2] - (sizes[j] / 2))) && (positions[i][2] < (positions[j][2] + (sizes[j] / 2)))) )
{
// Collide so move it
positions[i][0] += ((float)rand() / (float)(RAND_MAX))*1 - 0.5f;
positions[i][1] += ((float)rand() / (float)(RAND_MAX))*1 - 0.5f;
positions[i][2] += ((float)rand() / (float)(RAND_MAX))*1 - 0.5f;
//break;
}
}
}
}
}
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
// make some 3D objects
for ( int i = 1; i < MaximumSpheres; i++ )
{
rotations[i][0] = 0.0f;
rotations[i][1] = 0.0f;
rotations[i][2] = 0.0f;
positions[i][0] = ((float)rand() / (float)(RAND_MAX)) * 20;
positions[i][1] = ((float)rand() / (float)(RAND_MAX)) * 20;
positions[i][2] = ((float)rand() / (float)(RAND_MAX)) * 20;
sizes[i] = ((float)rand() / (float)(RAND_MAX)) * 5;
// make a sphere
dbMakeObjectCube ( i, sizes[i] );
// position the object in a random location
dbPositionObject ( i, positions[i][0], positions[i][1], positions[i][2]);
// give the object a color
dbColorObject ( i, dbRgb ( dbRnd ( 255 ), dbRnd ( 255 ), dbRnd ( 255 ) ) );
// turn off ambient lighting for this object
dbSetObjectAmbient ( i, 0 );
}
// move our camera back so we can view the objects
dbPositionCamera ( 10, 20, -40 );
#ifdef MULTITHREAD
hEvent1 = CreateEvent( NULL, FALSE, TRUE, NULL );
hEvent2 = CreateEvent( NULL, FALSE, FALSE, NULL );
_beginthread( Thread, 0, NULL );
#endif
while ( LoopGDK ( ) )
{
char txt[255];
sprintf(txt,"FPS=%d, Polygons=%d",dbScreenFPS(), dbStatistic(1));
dbText(0,0,txt);
// move the camera forwards
if ( dbUpKey ( ) )
dbMoveCamera ( 1 );
// move the camera backwards
if ( dbDownKey ( ) )
dbMoveCamera ( -1 );
#ifdef MULTITHREAD
WaitForSingleObject( hEvent1, INFINITE );
#endif
#ifndef MULTITHREAD
RecalculateSpheres();
#endif
// rotate all of our objects and reposition
for ( int i = 1; i < MaximumSpheres; i++ )
{
dbPositionObject ( i, positions[i][0], positions[i][1], positions[i][2]);
dbRotateObject ( i, rotations[i][0], rotations[i][1], rotations[i][2] );
}
#ifdef MULTITHREAD
SetEvent( hEvent2 );
#endif
// here we make a call to update the contents of the screen
dbSync ( );
}
// before quitting delete our objects
for ( int i = 1; i < MaximumSpheres; i++ )
dbDeleteObject ( i );
// and now everything is ready to return back to Windows
return;
}
It would be great if others contribute what they may have to this thread as an easily findable information repository for people interested in multithreading with Dark GDK.
Thanks for sharing your experiments.