Skip to main content

History: PUDotNet

Source of version: 11

Copy to clipboard
            PUDotNet is a simple C++/CLI wrapper class to use ((Particle Universe plugin|Particle Universe)) in ((Mogre)).

%help% For questions is this [http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=14037|forum topic].

Look to the ((-Particle|Particle)) page for basic particle information and further links.

Contributer of this code snippet: ((User:mstoyke|mstoyke))
Published in February 2011. 
Updated in March 2011 by user [http://www.ogre3d.org/addonforums/memberlist.php?mode=viewprofile&u=14075|tafkag] for Mogre 1.7 with Particle Universe 1.4.

The source codes for PU 1.2 and 1.4 are attached to this wiki page. (see bottom)


__Note by the contributor:__
This code is a quick-and-dirty solution:
Grab the code and have some fun. It's very simplistic, not a real wrapper of the API, more a kind of binding to the plug-in. I will not have time to support this in any way, but I think I should at least share it with you in case somebody find it useful.


!!How to use

First you need to [http://www.fxpression.com|buy] Particle Universe (for about 10 Euro/Dollars) and compile the wrapper code.
For compilation of the wrapper it's needed to link against the Mogre version of the Ogre libraries when building PU, otherwise it will not work. (means: The Ogre sources, which are modified specific for Mogre)

Additionally you need to add PUDotNet to the file __plugins.cfg__:
* release version:
{CODE(wrap="1")}Plugin=ParticleUniverse{CODE}
* debug version:
{CODE(wrap="1")}Plugin=ParticleUniverse_d{CODE}




%%%
!PUDotNet.h
Source code for Mogre 1.7 with Particle Universe 1.4

{CODE(wrap="1", colors="c++")}
#pragma once
 
#include <Ogre.h>
#include <ParticleUniverseSystem.h>
#include <ParticleUniverseSystemManager.h>
 
using namespace System;

namespace PUDotNet 
{
	public ref class PUManager
	{
		private:
		  Mogre::SceneManager^ mScene;
		  ParticleUniverse::ParticleSystemManager* mPUMgr;
		  Collections::Generic::Dictionary<System::String^, IntPtr>^ mSystems;
	 
		public:
		  PUManager( Mogre::SceneManager^ _scene );
		  !PUManager();
		  ~PUManager();
	 
		  property bool IsDisposed { bool get(); }
	 
		  array<System::String^>^ GetTemplateNames();
		  bool CreateParticleSystem( System::String^ _name, System::String^ _templateName );
		  void DestroyParticleSystem( System::String^ _name );
		  void AddToSceneNode( Mogre::SceneNode^ _node, System::String^ _name );
		  void RemoveFromSceneNode( Mogre::SceneNode^ _node, System::String^ _name );
		  void Start( System::String^ _name );
		  void Stop( System::String^ _name );
		  void Pause( System::String^ _name );
		  void Resume( System::String^ _name );		  

		private:
		  ParticleUniverse::ParticleSystem* GetByName( System::String^ _name );

		  void MarshalString ( System::String^ s, std::string& os );
	};	 

	inline void PUManager::MarshalString ( System::String^ s, std::string& os ) 
	{
	   using namespace Runtime::InteropServices;
	   const char* chars = 
		  (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
	   os = chars;
	   Ogre::String test(os);
	   Marshal::FreeHGlobal(IntPtr((void*)chars));
	}
}
{CODE}



%%%
!PUDotNet.cpp
Source code for Mogre 1.7 with Particle Universe 1.4

{CODE(wrap="1", colors="c++")}
#include "stdafx.h" 
#include "PUDotNet.h"
 
namespace PUDotNet {
 
  PUManager::PUManager( Mogre::SceneManager^ _scene )
  {
    if( _scene == nullptr )
      throw gcnew System::ArgumentNullException( "_scene" );
 
    mScene = _scene;
    mPUMgr = ParticleUniverse::ParticleSystemManager::getSingletonPtr();
    mSystems = gcnew Collections::Generic::Dictionary<System::String^, IntPtr>();
  }
 
  PUManager::!PUManager()
  {
    if( mSystems != nullptr )
    {
      Collections::Generic::Dictionary<System::String^, IntPtr>::Enumerator it;
      it = mSystems->GetEnumerator();
 
      array<System::String^>^ names = gcnew array<System::String^>( mSystems->Keys->Count );
      int pos = 0;
      while( it.MoveNext() )
        names[ pos++ ] = it.Current.Key;
 
      for( int i = 0; i < names->Length; ++i )
        DestroyParticleSystem( names[i] );
 
      mSystems->Clear();
    }
 
    mSystems = nullptr;
    mPUMgr = NULL;
    mScene = nullptr;
  }
 
  PUManager::~PUManager()
  {
    this->!PUManager();
  }
 
  bool PUManager::IsDisposed::get()
  {
    return ( mPUMgr == NULL );
  }
 
  array<System::String^>^ PUManager::GetTemplateNames()
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
 
    ParticleUniverse::vector<ParticleUniverse::String> tNames;

    mPUMgr->particleSystemTemplateNames( tNames );
 
    array<System::String^>^ names = gcnew array<System::String^>( tNames.size() );
 
	ParticleUniverse::vector<Ogre::String>::iterator it;
    int i = 0;
    for ( it = tNames.begin(); it != tNames.end(); ++it, ++i )
    {
      Ogre::String name = *it;
      //names[ i ] = TO_CLR_STRING( name );
      names[ i ] = gcnew ::System::String( name.c_str() );
    }
 
    return names;
  }
 
  bool PUManager::CreateParticleSystem( System::String^ _name, System::String^ _templateName )
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
    if( _name == nullptr )
      throw gcnew System::ArgumentNullException( "_name" );
    if( _templateName == nullptr )
      throw gcnew System::ArgumentNullException( "_templateName" );
 
    ParticleUniverse::ParticleSystem* psys;	
        
	//DECLARE_OGRE_STRING( name, _name )
	std::string nameTemp;
	MarshalString(_name, nameTemp);
	Ogre::String name(nameTemp);

    //DECLARE_OGRE_STRING( templateName, _templateName )
	std::string templateTemp;
	MarshalString(_templateName, templateTemp);
	Ogre::String templateName(templateTemp);
     
    psys = mPUMgr->createParticleSystem( name, templateName, mScene );
    if( psys == NULL )
      return false;
 
    IntPtr psysPtr = IntPtr( psys );
    mSystems->Add( _name, psysPtr );
 
    return true;
  }
 
  void PUManager::DestroyParticleSystem( System::String^ _name )
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
    if( _name == nullptr )
      throw gcnew System::ArgumentNullException( "_name" );
 
    ParticleUniverse::ParticleSystem* psys;
    psys = GetByName( _name );
    if( psys == NULL )
      return;
    mSystems->Remove( _name );
 
    Ogre::SceneNode* node = psys->getParentSceneNode();
    if( node != NULL )
      node->detachObject( psys );
 
    mPUMgr->destroyParticleSystem( psys, mScene );
  }
 
  void PUManager::AddToSceneNode( Mogre::SceneNode^ _node, System::String^ _name )
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
    if( _node == nullptr )
      throw gcnew System::ArgumentNullException( "_node" );
    if( _name == nullptr )
      throw gcnew System::ArgumentNullException( "_name" );
 
    ParticleUniverse::ParticleSystem* psys;
    psys = GetByName( _name );
    if( psys == NULL )
      return;
 
    Ogre::SceneNode* node = (Ogre::SceneNode*) _node->NativePtr;
    if( node == NULL )
      return;
 
    node->attachObject( psys );
  }
 
  void PUManager::RemoveFromSceneNode( Mogre::SceneNode^ _node, System::String^ _name )
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
    if( _node == nullptr )
      throw gcnew System::ArgumentNullException( "_node" );
    if(  _name == nullptr )
      throw gcnew System::ArgumentNullException( "_name" );
 
    ParticleUniverse::ParticleSystem* psys;
    psys = GetByName( _name );
    if( psys == NULL )
      return;
 
    Ogre::SceneNode* node = (Ogre::SceneNode*) _node->NativePtr;
    if( node == NULL )
      return;
 
    node->detachObject( psys );
  }
 
  void PUManager::Start( System::String^ _name )
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
    if(  _name == nullptr )
      throw gcnew System::ArgumentNullException( "_name" );
 
    ParticleUniverse::ParticleSystem* psys;
    psys = GetByName( _name );
    if( psys == NULL )
      return;
 
    psys->start();
  }
 
  void PUManager::Stop( System::String^ _name )
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
    if(  _name == nullptr )
      throw gcnew System::ArgumentNullException( "_name" );
 
    ParticleUniverse::ParticleSystem* psys;
    psys = GetByName( _name );
    if( psys == NULL )
      return;
 
    psys->stop();
  }
 
  void PUManager::Pause( System::String^ _name )
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
    if(  _name == nullptr )
      throw gcnew System::ArgumentNullException( "_name" );
 
    ParticleUniverse::ParticleSystem* psys;
    psys = GetByName( _name );
    if( psys == NULL )
      return;
 
    psys->pause();
  }
 
  void PUManager::Resume( System::String^ _name )
  {
    if( IsDisposed )
      throw gcnew System::ObjectDisposedException( "PUManager" );
    if(  _name == nullptr )
      throw gcnew System::ArgumentNullException( "_name" );
 
    ParticleUniverse::ParticleSystem* psys;
    psys = GetByName( _name );
    if( psys == NULL )
      return;
 
    psys->resume();
  }
 
  ParticleUniverse::ParticleSystem* PUManager::GetByName( System::String^ _name )
  {
    IntPtr sys;
    if( !mSystems->TryGetValue( _name, sys ) )
      return NULL;
 
    ParticleUniverse::ParticleSystem* psys = (ParticleUniverse::ParticleSystem*) sys.ToPointer();
    return psys;
  }
 
}
{CODE}

        

History

Information Version
Fri 24 of Feb, 2012 16:42 GMT-0000 tafkag Added scale methods 12
Thu 28 of Apr, 2011 11:07 GMT-0000 Beauty linked to PU website where to buy it 11
Thu 03 of Mar, 2011 00:37 GMT-0000 Beauty added version notes 10
Thu 03 of Mar, 2011 00:29 GMT-0000 Beauty updated cpp file for PU 1.4 9
Thu 03 of Mar, 2011 00:28 GMT-0000 Beauty updated header file for PU 1.4 8
Thu 03 of Mar, 2011 00:21 GMT-0000 Beauty improved note related to the file plugins.cfg 7
Wed 02 of Mar, 2011 22:42 GMT-0000 Beauty added note related to the file plugins.cfg 6
Sat 19 of Feb, 2011 23:53 GMT-0000 Beauty added PU version number + compilation notes 5
Thu 17 of Feb, 2011 22:35 GMT-0000 Beauty tiny changes 4
Thu 17 of Feb, 2011 22:32 GMT-0000 Beauty added more information + forum link 3
Thu 17 of Feb, 2011 22:20 GMT-0000 Beauty added information about this code snippet 2
Wed 16 of Feb, 2011 18:01 GMT-0000 mstoyke 1