exit gracefully and call destructor of the current active example

fflush after printf
implemented stepForward and Shutdown for the SharedMemory client/server
This commit is contained in:
=
2015-05-29 15:04:05 -07:00
parent ff15d36edf
commit bc806ab68c
10 changed files with 206 additions and 50 deletions

View File

@@ -79,7 +79,7 @@
defines {"_DEBUG=1"}
flags { "Symbols", "StaticRuntime" , "NoMinimalRebuild", "NoEditAndContinue" ,"FloatFast"}
if os.is("Linux") then
if os.is("Linux") or os.is("macosx") then
if os.is64bit() then
platforms {"x64"}
else

View File

@@ -42,13 +42,17 @@ class MyMenuItems : public Gwen::Controls::Base
public:
b3FileOpenCallback m_fileOpenCallback;
b3QuitCallback m_quitCallback;
MyMenuItems() :Gwen::Controls::Base(0),m_fileOpenCallback(0)
{
}
void myQuitApp( Gwen::Controls::Base* pControl )
{
exit(0);
if (m_quitCallback)
{
(*m_quitCallback)();
}
}
void fileOpen( Gwen::Controls::Base* pControl )
{
@@ -74,6 +78,7 @@ struct MyTestMenuBar : public Gwen::Controls::MenuStrip
{
m_menuItems = new MyMenuItems();
m_menuItems->m_fileOpenCallback = 0;
m_menuItems->m_quitCallback = 0;
m_fileMenu = AddItem( L"File" );
@@ -230,6 +235,11 @@ void GwenUserInterface::registerFileOpenCallback(b3FileOpenCallback callback)
m_data->m_menuItems->m_fileOpenCallback = callback;
}
void GwenUserInterface::registerQuitCallback(b3QuitCallback callback)
{
m_data->m_menuItems->m_quitCallback = callback;
}
void GwenUserInterface::init(int width, int height,Gwen::Renderer::Base* renderer,float retinaScale)
{
m_data->m_curYposition = 20;

View File

@@ -6,6 +6,7 @@ struct GwenInternalData;
typedef void (*b3ComboBoxCallback) (int combobox, const char* item);
typedef void (*b3ToggleButtonCallback)(int button, int state);
typedef void (*b3FileOpenCallback)();
typedef void (*b3QuitCallback)();
namespace Gwen
{
@@ -53,6 +54,7 @@ class GwenUserInterface
void registerFileOpenCallback(b3FileOpenCallback callback);
void registerQuitCallback(b3QuitCallback callback);
GwenInternalData* getInternalData()
{

View File

@@ -88,7 +88,18 @@ int gGpuArraySizeZ=15;
void deleteDemo()
{
if (sCurrentDemo)
{
sCurrentDemo->exitPhysics();
s_instancingRenderer->removeAllInstances();
delete sCurrentDemo;
sCurrentDemo=0;
delete s_guiHelper;
s_guiHelper = 0;
}
}
b3KeyboardCallback prevKeyboardCallback = 0;
@@ -163,6 +174,7 @@ void MyKeyboardCallback(int key, int state)
#endif
if (key==B3G_ESCAPE && s_window)
{
s_window->setRequestExit();
}
@@ -270,15 +282,8 @@ void selectDemo(int demoIndex)
{
demoIndex = 0;
}
if (sCurrentDemo)
{
sCurrentDemo->exitPhysics();
s_instancingRenderer->removeAllInstances();
delete sCurrentDemo;
sCurrentDemo=0;
delete s_guiHelper;
s_guiHelper = 0;
}
deleteDemo();
CommonExampleInterface::CreateFunc* func = gAllExamples->getExampleCreateFunc(demoIndex);
if (func)
{
@@ -495,6 +500,12 @@ struct GL3TexLoader : public MyTextureLoader
}
};
void quitCallback()
{
s_window->setRequestExit();
}
void fileOpenCallback()
{
@@ -581,6 +592,7 @@ OpenGLExampleBrowser::OpenGLExampleBrowser(class ExampleEntries* examples)
OpenGLExampleBrowser::~OpenGLExampleBrowser()
{
deleteDemo();
gAllExamples = 0;
}
@@ -790,7 +802,7 @@ bool OpenGLExampleBrowser::init(int argc, char* argv[])
}
gui->registerFileOpenCallback(fileOpenCallback);
gui->registerQuitCallback(quitCallback);
return true;
}

View File

@@ -48,6 +48,7 @@ void dumpInfo(void)
NSOpenGLContext* m_context;
int m_lastWidth;
int m_lastHeight;
bool m_requestClose;
b3ResizeCallback m_resizeCallback;
}
@@ -56,6 +57,8 @@ void dumpInfo(void)
-(void) MakeCurrent;
-(float) GetWindowWidth;
-(float) GetWindowHeight;
-(BOOL) GetRequestClose;
- (BOOL)windowShouldClose:(id)sender;
-(void) setResizeCallback:(b3ResizeCallback) callback;
-(b3ResizeCallback) getResizeCallback;
-(NSOpenGLContext*) getContext;
@@ -67,6 +70,15 @@ float loop;
@implementation TestView
- (BOOL)windowShouldClose:(id)sender
{
m_requestClose = true;
return false;
}
-(BOOL) GetRequestClose
{
return m_requestClose;
}
-(float) GetWindowWidth
{
return m_lastWidth;
@@ -140,7 +152,7 @@ float loop;
// NSWindow *w;
NSOpenGLPixelFormat *fmt;
m_requestClose = false;
@@ -776,7 +788,6 @@ void MacOpenGLWindow::startRendering()
//NSShiftKeyMask = 1 << 17,
//NSControlKeyMask
if ([event type] == NSFlagsChanged)
{
int modifiers = [event modifierFlags];
@@ -1050,7 +1061,8 @@ void MacOpenGLWindow::endRendering()
bool MacOpenGLWindow::requestedExit() const
{
return m_internalData->m_exitRequested;
bool closeme = m_internalData->m_myview.GetRequestClose;
return m_internalData->m_exitRequested || closeme;
}
void MacOpenGLWindow::setRequestExit()

View File

@@ -3,11 +3,14 @@
#include "../CommonInterfaces/CommonMultiBodyBase.h"
#include "PosixSharedMemory.h"
#include "SharedMemoryCommon.h"
class PhysicsClient : public CommonMultiBodyBase
class PhysicsClient : public SharedMemoryCommon
{
SharedMemoryInterface* m_sharedMemory;
SharedMemoryExampleData* m_testBlock1;
int m_counter;
bool m_wantsTermination;
public:
@@ -27,11 +30,17 @@ public:
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
}
virtual bool wantsTermination()
{
return m_wantsTermination;
}
};
PhysicsClient::PhysicsClient(GUIHelperInterface* helper)
:CommonMultiBodyBase(helper),
m_testBlock1(0)
:SharedMemoryCommon(helper),
m_testBlock1(0),
m_counter(0),
m_wantsTermination(false)
{
b3Printf("Started PhysicsClient");
m_sharedMemory = new PosixSharedMemory();
@@ -55,21 +64,10 @@ void PhysicsClient::initPhysics()
b3Error("Error: please start server before client");
m_sharedMemory->releaseSharedMemory(SHARED_MEMORY_KEY, SHARED_MEMORY_SIZE);
m_testBlock1 = 0;
}
}
}
void PhysicsClient::stepSimulation(float deltaTime)
{
static int once = true;
if (m_testBlock1)
{
if (once)
} else
{
once=false;
//submit a 'load urdf' command to get things started
b3Printf("Client created CMD_LOAD_URDF");
m_testBlock1->m_clientCommands[0].m_type =CMD_LOAD_URDF;
@@ -78,6 +76,66 @@ void PhysicsClient::stepSimulation(float deltaTime)
}
}
}
void PhysicsClient::stepSimulation(float deltaTime)
{
if (m_testBlock1)
{
//check progress and submit further commands
//we ignore overflow right now
if (m_testBlock1->m_numServerCommands> m_testBlock1->m_numProcessedServerCommands)
{
btAssert(m_testBlock1->m_numServerCommands==m_testBlock1->m_numProcessedServerCommands+1);
const SharedMemoryCommand& serverCmd =m_testBlock1->m_serverCommands[0];
//consume the command
switch (serverCmd.m_type)
{
case CMD_STEP_FORWARD_SIMULATION_COMPLETED:
case CMD_URDF_LOADING_COMPLETED:
{
//submit a 'step simulation' request
if (m_counter<10)
{
m_testBlock1->m_clientCommands[0].m_type =CMD_STEP_FORWARD_SIMULATION;
m_testBlock1->m_clientCommands[0].m_stepSimulationArguments.m_deltaTimeInSeconds = 1./60.;
m_testBlock1->m_numClientCommands++;
m_counter++;
} else
{
m_wantsTermination = true;
m_testBlock1->m_clientCommands[0].m_type =CMD_SHUTDOWN;
m_testBlock1->m_numClientCommands++;
}
break;
}
case CMD_URDF_LOADING_FAILED:
{
b3Printf("Server failed loading the URDF...");
break;
}
default:
{
b3Error("Unknown server command");
btAssert(0);
}
};
m_testBlock1->m_numProcessedServerCommands++;
}
}
}

View File

@@ -1,19 +1,19 @@
#include "PhysicsServer.h"
#include "../CommonInterfaces/CommonMultiBodyBase.h"
#include "PosixSharedMemory.h"
#include "../Importers/ImportURDFDemo/MyURDFImporter.h"
#include "../Importers/ImportURDFDemo/MyMultiBodyCreator.h"
#include "../Importers/ImportURDFDemo/URDF2Bullet.h"
#include "SharedMemoryCommon.h"
class PhysicsServer : public CommonMultiBodyBase
class PhysicsServer : public SharedMemoryCommon
{
SharedMemoryInterface* m_sharedMemory;
SharedMemoryExampleData* m_testBlock1;
bool m_wantsShutdown;
public:
@@ -25,6 +25,8 @@ public:
virtual void stepSimulation(float deltaTime);
void releaseSharedMemory();
bool loadUrdf(const char* fileName, const btVector3& pos, const btQuaternion& orn,
bool useMultiBody, bool useFixedBase);
@@ -37,25 +39,39 @@ public:
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
}
virtual bool wantsTermination();
};
PhysicsServer::PhysicsServer(GUIHelperInterface* helper)
:CommonMultiBodyBase(helper),
m_testBlock1(0)
:SharedMemoryCommon(helper),
m_testBlock1(0),
m_wantsShutdown(false)
{
b3Printf("Started PhysicsServer\n");
m_sharedMemory = new PosixSharedMemory();
}
PhysicsServer::~PhysicsServer()
void PhysicsServer::releaseSharedMemory()
{
if (m_testBlock1)
{
m_testBlock1->m_magicId = 0;
b3Printf("magic id = %d\n",m_testBlock1->m_magicId);
btAssert(m_sharedMemory);
m_sharedMemory->releaseSharedMemory(SHARED_MEMORY_KEY, SHARED_MEMORY_SIZE);
}
m_sharedMemory->releaseSharedMemory(SHARED_MEMORY_KEY, SHARED_MEMORY_SIZE);
delete m_sharedMemory;
if (m_sharedMemory)
{
delete m_sharedMemory;
m_sharedMemory = 0;
m_testBlock1 = 0;
}
}
PhysicsServer::~PhysicsServer()
{
releaseSharedMemory();
}
void PhysicsServer::initPhysics()
@@ -86,6 +102,12 @@ void PhysicsServer::initPhysics()
}
}
bool PhysicsServer::wantsTermination()
{
return m_wantsShutdown;
}
bool PhysicsServer::loadUrdf(const char* fileName, const btVector3& pos, const btQuaternion& orn,
bool useMultiBody, bool useFixedBase)
{
@@ -103,17 +125,21 @@ bool PhysicsServer::loadUrdf(const char* fileName, const btVector3& pos, const b
int rootLinkIndex = u2b.getRootLinkIndex();
// printf("urdf root link index = %d\n",rootLinkIndex);
MyMultiBodyCreator creation(m_guiHelper);
bool m_useMultiBody = true;
ConvertURDF2Bullet(u2b,creation, tr,m_dynamicsWorld,useMultiBody,u2b.getPathPrefix());
btMultiBody* mb = creation.getBulletMultiBody();
return false;
return true;
}
return false;
}
void PhysicsServer::stepSimulation(float deltaTime)
{
bool wantsShutdown = false;
if (m_testBlock1)
{
///we ignore overflow of integer for now
@@ -124,6 +150,7 @@ void PhysicsServer::stepSimulation(float deltaTime)
btAssert(m_testBlock1->m_numClientCommands==m_testBlock1->m_numProcessedClientCommands+1);
const SharedMemoryCommand& clientCmd =m_testBlock1->m_clientCommands[0];
m_testBlock1->m_numProcessedClientCommands++;
//consume the command
switch (clientCmd.m_type)
@@ -148,19 +175,45 @@ void PhysicsServer::stepSimulation(float deltaTime)
}
m_testBlock1->m_numServerCommands++;
break;
}
case CMD_STEP_FORWARD_SIMULATION:
{
b3Printf("Step simulation request");
double timeStep = clientCmd.m_stepSimulationArguments.m_deltaTimeInSeconds;
m_dynamicsWorld->stepSimulation(timeStep);
SharedMemoryCommand& serverCmd =m_testBlock1->m_serverCommands[0];
serverCmd.m_type =CMD_STEP_FORWARD_SIMULATION_COMPLETED;
m_testBlock1->m_numServerCommands++;
break;
}
case CMD_SHUTDOWN:
{
wantsShutdown = true;
break;
}
default:
{
b3Error("Unsupported command encountered");
btAssert(0);
}
};
m_testBlock1->m_numProcessedClientCommands++;
//process the command right now
}
}
if (wantsShutdown)
{
m_wantsShutdown = true;
releaseSharedMemory();
}
}

View File

@@ -18,6 +18,7 @@ enum SharedMemoryClientCommand{
CMD_LOAD_URDF,
CMD_STATE_UPDATED,
CMD_STEP_FORWARD_SIMULATION, //includes CMD_REQUEST_STATE
CMD_SHUTDOWN,
CMD_MAX_CLIENT_COMMANDS
};

View File

@@ -20,8 +20,7 @@ subject to the following restrictions:
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "SharedMemoryCommon.h"
int main(int argc, char* argv[])
@@ -33,18 +32,22 @@ int main(int argc, char* argv[])
DummyGUIHelper noGfx;
CommonExampleOptions options(&noGfx);
CommonExampleInterface* example = 0;
SharedMemoryCommon* example = 0;
if (args.CheckCmdLineFlag("client"))
{
example = PhysicsClientCreateFunc(options);
example = (SharedMemoryCommon*)PhysicsClientCreateFunc(options);
}else
{
example = PhysicsServerCreateFunc(options);
example = (SharedMemoryCommon*)PhysicsServerCreateFunc(options);
}
example->initPhysics();
example->stepSimulation(1.f/60.f);
while (!example->wantsTermination())
{
example->stepSimulation(1.f/60.f);
}
example->exitPhysics();
delete example;

View File

@@ -29,7 +29,8 @@ void b3PrintfFuncDefault(const char* msg)
OutputDebugStringA(msg);
#endif
printf("%s",msg);
//is this portable?
fflush(stdout);
}
void b3WarningMessageFuncDefault(const char* msg)
@@ -38,7 +39,8 @@ void b3WarningMessageFuncDefault(const char* msg)
OutputDebugStringA(msg);
#endif
printf("%s",msg);
//is this portable?
fflush(stdout);
}
@@ -50,6 +52,9 @@ void b3ErrorMessageFuncDefault(const char* msg)
#endif
printf("%s",msg);
//is this portable?
fflush(stdout);
}