2016-03-03 02:01:33 +00:00
# include "InProcessExampleBrowser.h"
//#define EXAMPLE_CONSOLE_ONLY
# ifdef EXAMPLE_CONSOLE_ONLY
# include "EmptyBrowser.h"
typedef EmptyBrowser DefaultBrowser ;
# else
# include "OpenGLExampleBrowser.h"
typedef OpenGLExampleBrowser DefaultBrowser ;
# endif //EXAMPLE_CONSOLE_ONLY
# include "Bullet3Common/b3CommandLineArgs.h"
# include "../Utils/b3Clock.h"
# include "ExampleEntries.h"
2016-05-04 07:16:53 +00:00
# include "Bullet3Common/b3Scalar.h"
2016-03-10 22:36:46 +00:00
# include "../SharedMemory/InProcessMemory.h"
2016-03-03 02:01:33 +00:00
void ExampleBrowserThreadFunc ( void * userPtr , void * lsMemory ) ;
void * ExampleBrowserMemoryFunc ( ) ;
2018-05-02 21:32:43 +00:00
void ExampleBrowserMemoryReleaseFunc ( void * ptr ) ;
2016-03-03 02:01:33 +00:00
# include <stdio.h>
//#include "BulletMultiThreaded/PlatformDefinitions.h"
2016-05-04 07:16:53 +00:00
# include "Bullet3Common/b3Logging.h"
2016-05-04 06:34:48 +00:00
# include "ExampleEntries.h"
# include "LinearMath/btAlignedObjectArray.h"
# include "EmptyExample.h"
# include "../SharedMemory/PhysicsServerExample.h"
2017-05-31 02:54:55 +00:00
# include "../SharedMemory/PhysicsServerExampleBullet2.h"
2016-05-04 06:34:48 +00:00
# include "../SharedMemory/PhysicsClientExample.h"
2016-05-04 07:16:53 +00:00
# ifndef _WIN32
# include "../MultiThreading/b3PosixThreadSupport.h"
static b3ThreadSupportInterface * createExampleBrowserThreadSupport ( int numThreads )
{
b3PosixThreadSupport : : ThreadConstructionInfo constructionInfo ( " testThreads " ,
ExampleBrowserThreadFunc ,
ExampleBrowserMemoryFunc ,
2018-05-02 21:32:43 +00:00
ExampleBrowserMemoryReleaseFunc ,
2016-05-04 07:16:53 +00:00
numThreads ) ;
b3ThreadSupportInterface * threadSupport = new b3PosixThreadSupport ( constructionInfo ) ;
return threadSupport ;
}
# elif defined( _WIN32)
# include "../MultiThreading/b3Win32ThreadSupport.h"
b3ThreadSupportInterface * createExampleBrowserThreadSupport ( int numThreads )
{
2018-05-02 21:32:43 +00:00
b3Win32ThreadSupport : : Win32ThreadConstructionInfo threadConstructionInfo ( " testThreads " , ExampleBrowserThreadFunc , ExampleBrowserMemoryFunc , ExampleBrowserMemoryReleaseFunc , numThreads ) ;
2016-05-04 07:16:53 +00:00
b3Win32ThreadSupport * threadSupport = new b3Win32ThreadSupport ( threadConstructionInfo ) ;
return threadSupport ;
}
# endif
2016-05-04 06:34:48 +00:00
class ExampleEntriesPhysicsServer : public ExampleEntries
{
struct ExampleEntriesInternalData2 * m_data ;
public :
ExampleEntriesPhysicsServer ( ) ;
virtual ~ ExampleEntriesPhysicsServer ( ) ;
static void registerExampleEntry ( int menuLevel , const char * name , const char * description , CommonExampleInterface : : CreateFunc * createFunc , int option = 0 ) ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
virtual void initExampleEntries ( ) ;
virtual void initOpenCLExampleEntries ( ) ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
virtual int getNumRegisteredExamples ( ) ;
virtual CommonExampleInterface : : CreateFunc * getExampleCreateFunc ( int index ) ;
virtual const char * getExampleName ( int index ) ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
virtual const char * getExampleDescription ( int index ) ;
virtual int getExampleOption ( int index ) ;
} ;
struct ExampleEntryPhysicsServer
{
int m_menuLevel ;
const char * m_name ;
const char * m_description ;
CommonExampleInterface : : CreateFunc * m_createFunc ;
int m_option ;
ExampleEntryPhysicsServer ( int menuLevel , const char * name )
: m_menuLevel ( menuLevel ) , m_name ( name ) , m_description ( 0 ) , m_createFunc ( 0 ) , m_option ( 0 )
{
}
ExampleEntryPhysicsServer ( int menuLevel , const char * name , const char * description , CommonExampleInterface : : CreateFunc * createFunc , int option = 0 )
: m_menuLevel ( menuLevel ) , m_name ( name ) , m_description ( description ) , m_createFunc ( createFunc ) , m_option ( option )
{
}
} ;
struct ExampleEntriesInternalData2
{
btAlignedObjectArray < ExampleEntryPhysicsServer > m_allExamples ;
} ;
static ExampleEntryPhysicsServer gDefaultExamplesPhysicsServer [ ] =
{
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
ExampleEntryPhysicsServer ( 0 , " Robotics Control " ) ,
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
ExampleEntryPhysicsServer ( 1 , " Physics Server " , " Create a physics server that communicates with a physics client over shared memory " ,
2017-05-31 02:54:55 +00:00
PhysicsServerCreateFuncBullet2 ) ,
2016-05-18 23:21:40 +00:00
ExampleEntryPhysicsServer ( 1 , " Physics Server (RTC) " , " Create a physics server that communicates with a physics client over shared memory. At each update, the Physics Server will continue calling 'stepSimulation' based on the real-time clock (RTC). " ,
2017-05-31 02:54:55 +00:00
PhysicsServerCreateFuncBullet2 , PHYSICS_SERVER_USE_RTC_CLOCK ) ,
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
ExampleEntryPhysicsServer ( 1 , " Physics Server (Logging) " , " Create a physics server that communicates with a physics client over shared memory. It will log all commands to a file. " ,
2017-05-31 02:54:55 +00:00
PhysicsServerCreateFuncBullet2 , PHYSICS_SERVER_ENABLE_COMMAND_LOGGING ) ,
2016-05-04 06:34:48 +00:00
ExampleEntryPhysicsServer ( 1 , " Physics Server (Replay Log) " , " Create a physics server that replay a command log from disk. " ,
2017-05-31 02:54:55 +00:00
PhysicsServerCreateFuncBullet2 , PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG ) ,
2016-05-04 06:34:48 +00:00
} ;
ExampleEntriesPhysicsServer : : ExampleEntriesPhysicsServer ( )
{
m_data = new ExampleEntriesInternalData2 ;
}
ExampleEntriesPhysicsServer : : ~ ExampleEntriesPhysicsServer ( )
{
delete m_data ;
}
void ExampleEntriesPhysicsServer : : initOpenCLExampleEntries ( )
{
}
void ExampleEntriesPhysicsServer : : initExampleEntries ( )
{
m_data - > m_allExamples . clear ( ) ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
int numDefaultEntries = sizeof ( gDefaultExamplesPhysicsServer ) / sizeof ( ExampleEntryPhysicsServer ) ;
for ( int i = 0 ; i < numDefaultEntries ; i + + )
{
m_data - > m_allExamples . push_back ( gDefaultExamplesPhysicsServer [ i ] ) ;
}
}
void ExampleEntriesPhysicsServer : : registerExampleEntry ( int menuLevel , const char * name , const char * description , CommonExampleInterface : : CreateFunc * createFunc , int option )
{
}
int ExampleEntriesPhysicsServer : : getNumRegisteredExamples ( )
{
return m_data - > m_allExamples . size ( ) ;
}
CommonExampleInterface : : CreateFunc * ExampleEntriesPhysicsServer : : getExampleCreateFunc ( int index )
{
return m_data - > m_allExamples [ index ] . m_createFunc ;
}
int ExampleEntriesPhysicsServer : : getExampleOption ( int index )
{
return m_data - > m_allExamples [ index ] . m_option ;
}
const char * ExampleEntriesPhysicsServer : : getExampleName ( int index )
{
return m_data - > m_allExamples [ index ] . m_name ;
}
const char * ExampleEntriesPhysicsServer : : getExampleDescription ( int index )
{
return m_data - > m_allExamples [ index ] . m_description ;
}
2016-03-03 02:01:33 +00:00
struct ExampleBrowserArgs
{
ExampleBrowserArgs ( )
: m_fakeWork ( 1 ) , m_argc ( 0 )
{
}
b3CriticalSection * m_cs ;
float m_fakeWork ;
int m_argc ;
char * * m_argv ;
} ;
struct ExampleBrowserThreadLocalStorage
{
2016-03-10 22:36:46 +00:00
SharedMemoryInterface * m_sharedMem ;
2016-03-03 02:01:33 +00:00
int threadId ;
} ;
enum TestExampleBrowserCommunicationEnums
{
eRequestTerminateExampleBrowser = 13 ,
2016-03-07 22:56:16 +00:00
eExampleBrowserIsUnInitialized ,
eExampleBrowserIsInitialized ,
eExampleBrowserInitializationFailed ,
2016-03-03 02:01:33 +00:00
eExampleBrowserHasTerminated
} ;
2016-12-29 05:51:54 +00:00
static double gMinUpdateTimeMicroSecs = 4000. ;
2016-03-03 02:01:33 +00:00
void ExampleBrowserThreadFunc ( void * userPtr , void * lsMemory )
{
2017-07-14 22:12:16 +00:00
2016-07-09 22:09:09 +00:00
printf ( " ExampleBrowserThreadFunc started \n " ) ;
2016-03-03 02:01:33 +00:00
ExampleBrowserThreadLocalStorage * localStorage = ( ExampleBrowserThreadLocalStorage * ) lsMemory ;
ExampleBrowserArgs * args = ( ExampleBrowserArgs * ) userPtr ;
2017-01-16 06:26:11 +00:00
//int workLeft = true;
2016-03-03 02:01:33 +00:00
b3CommandLineArgs args2 ( args - > m_argc , args - > m_argv ) ;
b3Clock clock ;
2016-05-18 23:21:40 +00:00
2016-05-04 06:34:48 +00:00
ExampleEntriesPhysicsServer examples ;
2016-03-03 02:01:33 +00:00
examples . initExampleEntries ( ) ;
2016-03-10 22:36:46 +00:00
DefaultBrowser * exampleBrowser = new DefaultBrowser ( & examples ) ;
exampleBrowser - > setSharedMemoryInterface ( localStorage - > m_sharedMem ) ;
2016-03-03 02:01:33 +00:00
bool init = exampleBrowser - > init ( args - > m_argc , args - > m_argv ) ;
clock . reset ( ) ;
if ( init )
{
2016-05-18 23:21:40 +00:00
2016-03-07 22:56:16 +00:00
args - > m_cs - > lock ( ) ;
args - > m_cs - > setSharedParam ( 0 , eExampleBrowserIsInitialized ) ;
args - > m_cs - > unlock ( ) ;
2016-05-18 23:21:40 +00:00
do
2016-03-03 02:01:33 +00:00
{
2017-07-14 22:12:16 +00:00
clock . usleep ( 0 ) ;
//B3_PROFILE("ExampleBrowserThreadFunc");
2016-03-03 02:01:33 +00:00
float deltaTimeInSeconds = clock . getTimeMicroseconds ( ) / 1000000.f ;
2016-12-29 05:51:54 +00:00
{
if ( deltaTimeInSeconds > 0.1 )
{
deltaTimeInSeconds = 0.1 ;
}
if ( deltaTimeInSeconds < ( gMinUpdateTimeMicroSecs / 1e6 ) )
{
2017-07-14 22:12:16 +00:00
//B3_PROFILE("clock.usleep");
2017-02-16 22:19:09 +00:00
exampleBrowser - > updateGraphics ( ) ;
2016-12-29 05:51:54 +00:00
} else
{
2017-07-14 22:12:16 +00:00
//B3_PROFILE("exampleBrowser->update");
2016-12-29 05:51:54 +00:00
clock . reset ( ) ;
2017-07-14 22:12:16 +00:00
exampleBrowser - > updateGraphics ( ) ;
2016-12-29 05:51:54 +00:00
exampleBrowser - > update ( deltaTimeInSeconds ) ;
}
}
2016-03-03 02:01:33 +00:00
} while ( ! exampleBrowser - > requestedExit ( ) & & ( args - > m_cs - > getSharedParam ( 0 ) ! = eRequestTerminateExampleBrowser ) ) ;
2016-03-07 22:56:16 +00:00
} else
{
args - > m_cs - > lock ( ) ;
args - > m_cs - > setSharedParam ( 0 , eExampleBrowserInitializationFailed ) ;
args - > m_cs - > unlock ( ) ;
2016-03-03 02:01:33 +00:00
}
2016-03-07 22:56:16 +00:00
2016-03-03 02:01:33 +00:00
delete exampleBrowser ;
args - > m_cs - > lock ( ) ;
args - > m_cs - > setSharedParam ( 0 , eExampleBrowserHasTerminated ) ;
args - > m_cs - > unlock ( ) ;
printf ( " finished \n " ) ;
//do nothing
}
void * ExampleBrowserMemoryFunc ( )
{
//don't create local store memory, just return 0
return new ExampleBrowserThreadLocalStorage ;
}
2018-05-02 21:32:43 +00:00
void ExampleBrowserMemoryReleaseFunc ( void * ptr )
{
ExampleBrowserThreadLocalStorage * p = ( ExampleBrowserThreadLocalStorage * ) ptr ;
delete ptr ;
}
2016-03-03 02:01:33 +00:00
struct btInProcessExampleBrowserInternalData
{
ExampleBrowserArgs m_args ;
b3ThreadSupportInterface * m_threadSupport ;
2016-03-10 22:36:46 +00:00
SharedMemoryInterface * m_sharedMem ;
2016-03-03 02:01:33 +00:00
} ;
2016-05-04 07:16:53 +00:00
2017-08-15 00:02:20 +00:00
btInProcessExampleBrowserInternalData * btCreateInProcessExampleBrowser ( int argc , char * * argv2 , bool useInProcessMemory )
2016-03-03 02:01:33 +00:00
{
btInProcessExampleBrowserInternalData * data = new btInProcessExampleBrowserInternalData ;
2017-08-15 00:02:20 +00:00
data - > m_sharedMem = useInProcessMemory ? new InProcessMemory : 0 ;
2016-03-03 02:01:33 +00:00
int numThreads = 1 ;
int i ;
data - > m_threadSupport = createExampleBrowserThreadSupport ( numThreads ) ;
printf ( " argc=%d \n " , argc ) ;
for ( i = 0 ; i < argc ; i + + )
{
printf ( " argv[%d] = %s \n " , i , argv2 [ i ] ) ;
}
for ( i = 0 ; i < data - > m_threadSupport - > getNumTasks ( ) ; i + + )
{
ExampleBrowserThreadLocalStorage * storage = ( ExampleBrowserThreadLocalStorage * ) data - > m_threadSupport - > getThreadLocalMemory ( i ) ;
b3Assert ( storage ) ;
storage - > threadId = i ;
2016-03-10 22:36:46 +00:00
storage - > m_sharedMem = data - > m_sharedMem ;
2016-03-03 02:01:33 +00:00
}
data - > m_args . m_cs = data - > m_threadSupport - > createCriticalSection ( ) ;
2016-03-07 22:56:16 +00:00
data - > m_args . m_cs - > setSharedParam ( 0 , eExampleBrowserIsUnInitialized ) ;
2016-03-03 02:01:33 +00:00
data - > m_args . m_argc = argc ;
data - > m_args . m_argv = argv2 ;
for ( i = 0 ; i < numThreads ; i + + )
{
data - > m_threadSupport - > runTask ( B3_THREAD_SCHEDULE_TASK , ( void * ) & data - > m_args , i ) ;
}
2016-03-07 22:56:16 +00:00
while ( data - > m_args . m_cs - > getSharedParam ( 0 ) = = eExampleBrowserIsUnInitialized )
{
2016-08-13 19:21:18 +00:00
b3Clock : : usleep ( 1000 ) ;
2016-03-07 22:56:16 +00:00
}
2016-03-03 02:01:33 +00:00
return data ;
}
bool btIsExampleBrowserTerminated ( btInProcessExampleBrowserInternalData * data )
{
return ( data - > m_args . m_cs - > getSharedParam ( 0 ) = = eExampleBrowserHasTerminated ) ;
}
2016-03-10 22:36:46 +00:00
SharedMemoryInterface * btGetSharedMemoryInterface ( btInProcessExampleBrowserInternalData * data )
{
return data - > m_sharedMem ;
}
2016-03-03 02:01:33 +00:00
void btShutDownExampleBrowser ( btInProcessExampleBrowserInternalData * data )
{
int numActiveThreads = 1 ;
data - > m_args . m_cs - > lock ( ) ;
data - > m_args . m_cs - > setSharedParam ( 0 , eRequestTerminateExampleBrowser ) ;
data - > m_args . m_cs - > unlock ( ) ;
while ( numActiveThreads )
{
int arg0 , arg1 ;
if ( data - > m_threadSupport - > isTaskCompleted ( & arg0 , & arg1 , 0 ) )
{
numActiveThreads - - ;
printf ( " numActiveThreads = %d \n " , numActiveThreads ) ;
} else
{
// printf("polling..");
2017-10-10 18:10:42 +00:00
b3Clock : : usleep ( 0 ) ;
2016-03-03 02:01:33 +00:00
}
} ;
2016-07-09 22:09:09 +00:00
printf ( " btShutDownExampleBrowser stopping threads \n " ) ;
2017-01-10 22:57:16 +00:00
data - > m_threadSupport - > deleteCriticalSection ( data - > m_args . m_cs ) ;
2016-03-03 02:01:33 +00:00
delete data - > m_threadSupport ;
2016-03-10 22:36:46 +00:00
delete data - > m_sharedMem ;
2016-03-03 02:01:33 +00:00
delete data ;
}
2016-04-14 15:51:20 +00:00
struct btInProcessExampleBrowserMainThreadInternalData
{
2016-05-04 06:34:48 +00:00
ExampleEntriesPhysicsServer m_examples ;
2016-04-14 15:51:20 +00:00
DefaultBrowser * m_exampleBrowser ;
SharedMemoryInterface * m_sharedMem ;
b3Clock m_clock ;
} ;
2017-08-15 00:02:20 +00:00
btInProcessExampleBrowserMainThreadInternalData * btCreateInProcessExampleBrowserMainThread ( int argc , char * * argv , bool useInProcessMemory )
2016-04-14 15:51:20 +00:00
{
btInProcessExampleBrowserMainThreadInternalData * data = new btInProcessExampleBrowserMainThreadInternalData ;
data - > m_examples . initExampleEntries ( ) ;
data - > m_exampleBrowser = new DefaultBrowser ( & data - > m_examples ) ;
2017-08-15 00:02:20 +00:00
data - > m_sharedMem = useInProcessMemory ? new InProcessMemory : 0 ;
2016-04-14 15:51:20 +00:00
data - > m_exampleBrowser - > setSharedMemoryInterface ( data - > m_sharedMem ) ;
2017-01-16 06:26:11 +00:00
bool init ;
init = data - > m_exampleBrowser - > init ( argc , argv ) ;
2016-04-14 15:51:20 +00:00
data - > m_clock . reset ( ) ;
return data ;
}
bool btIsExampleBrowserMainThreadTerminated ( btInProcessExampleBrowserMainThreadInternalData * data )
{
return data - > m_exampleBrowser - > requestedExit ( ) ;
}
void btUpdateInProcessExampleBrowserMainThread ( btInProcessExampleBrowserMainThreadInternalData * data )
{
float deltaTimeInSeconds = data - > m_clock . getTimeMicroseconds ( ) / 1000000.f ;
data - > m_clock . reset ( ) ;
2017-07-14 22:12:16 +00:00
data - > m_exampleBrowser - > updateGraphics ( ) ;
2016-04-14 15:51:20 +00:00
data - > m_exampleBrowser - > update ( deltaTimeInSeconds ) ;
}
void btShutDownExampleBrowserMainThread ( btInProcessExampleBrowserMainThreadInternalData * data )
{
2016-05-18 23:21:40 +00:00
2016-04-14 15:51:20 +00:00
data - > m_exampleBrowser - > setSharedMemoryInterface ( 0 ) ;
delete data - > m_exampleBrowser ;
delete data ;
}
class SharedMemoryInterface * btGetSharedMemoryInterfaceMainThread ( btInProcessExampleBrowserMainThreadInternalData * data )
{
return data - > m_sharedMem ;
}