History: Mogre porting example
Source of version: 6 (current)
Copy to clipboard
Here you can see how to port C++ to C# code.
As an example we port the snipit ((Easy debug text)).
The result C# code is in [http://www.ogre3d.org/phpBB2addons/viewtopic.php?p=49262#49258|this thread].
''__This example is not ready. Please help to complete and correct it.__''
''You also can add comments to this page.''
''Important: start each code line with a space char " " (also blank lines) --> wiki will show it in monospace box''
!!Ogre code
__acDebug.h__
{CODE(wrap="1", colors="c++")}#ifndef acDebug_H
#define acDebug_H
#pragma once
#include <string>
using namespace std;
#include "Ogre.h"
using namespace Ogre;
class _acSystemExport acDebug : public Ogre::Singleton<acDebug>
{
public:
acDebug(SceneManager* sm,RenderWindow* window);
~acDebug();
static acDebug& getSingleton(void);
static acDebug* getSingletonPtr(void);
void addDebugText(String text);
void printText();
protected:
SceneManager* mSceneMgr;
RenderWindow* mWindow;
std::list<String>* mDebugLines;
};
#endif{CODE}
__acDebug.cpp__
{CODE(wrap="1", colors="c++")}#include "acDebug.h"
template<> acDebug* Singleton<acDebug>::ms_Singleton = 0;
acDebug* acDebug::getSingletonPtr(void)
{
return ms_Singleton;
}
acDebug& acDebug::getSingleton(void)
{
assert( ms_Singleton ); return ( *ms_Singleton );
}
acDebug::acDebug(SceneManager* sm,RenderWindow* window){
mSceneMgr = sm;
mWindow = window;
mDebugLines = new std::list<String>;
}
acDebug::~acDebug(){
if (mDebugLines)
delete mDebugLines;
}
void acDebug::addDebugText(String text){
mDebugLines->push_back(text);
}
void acDebug::printText(){
String output;
std::list<String>::iterator itr = mDebugLines->begin();
std::list<String>::iterator itrEnd = mDebugLines->end();
for (;itr!=itrEnd;itr++){
output += (*itr) + ", ";
}
mWindow->setDebugText(output);
mDebugLines->clear();
}{CODE}
!!Mogre code
{DIV(class="Mogre_porting_header")}header file{DIV}
{DIV(class="Mogre_porting_cpp")}C++ code{DIV}
{DIV(class="Mogre_porting_cs")}C# code{DIV}
{DIV(class="Mogre_porting_header")}
{CODE(wrap="1", colors="c++")} // #ifndef acDebug_H // .h
// #define acDebug_H // .h
// // .h
// #pragma once // .h
// // .h
// #include <string> // .h
// using namespace std; // .h
// // .h
// #include "Ogre.h" // .h
// // .h
// using namespace Ogre; // .h{CODE}
{DIV}
{DIV(class="Mogre_porting_cs")}
{CODE(wrap="1", colors="c#")} using System;
// other usings
using Mogre;
using std; // ??{CODE}
{DIV}
{DIV(class="Mogre_porting_cpp")}
{CODE(wrap="1", colors="c++")} // template<> acDebug* Singleton<acDebug>::ms_Singleton = 0; // .cpp{CODE}
{DIV}
{DIV(class="Mogre_porting_cs")}
{CODE(wrap="1", colors="c#")} // C# ??{CODE}
{DIV}
{DIV(class="Mogre_porting_header")}
{CODE(wrap="1", colors="c++")} // class _acSystemExport acDebug : public Ogre::Singleton<acDebug> // .h
// { // .h{CODE}
{DIV}
{DIV(class="Mogre_porting_cs")}
{CODE(wrap="1", colors="c#")} public class acDebug // inheritance of something? ... I found no Mogre.Singleton
{{CODE}
{DIV}
{DIV(class="Mogre_porting_header")}
{CODE(wrap="1", colors="c++")} // public: // .h
// acDebug(SceneManager* sm,RenderWindow* window); // .h
// ~acDebug(); // .h
// // .h
// static acDebug& getSingleton(void); // .h
// static acDebug* getSingletonPtr(void); // .h
// // .h{CODE}
{DIV}
{DIV(class="Mogre_porting_cpp")}
{CODE(wrap="1", colors="c++")} // acDebug* acDebug::getSingletonPtr(void) // .cpp
// { // .cpp
// return ms_Singleton; // .cpp
// } // .cpp
// acDebug& acDebug::getSingleton(void) // .cpp
// { // .cpp
// assert( ms_Singleton ); return ( *ms_Singleton ); // .cpp
// } // .cpp
// // .cpp
// // .cpp
// acDebug::acDebug(SceneManager* sm,RenderWindow* window){ // .cpp
// mSceneMgr = sm; // .cpp
// mWindow = window; // .cpp
// mDebugLines = new std::list<String>; // .cpp
// } // .cpp
// // .cpp
// acDebug::~acDebug(){ // .cpp
// if (mDebugLines) // .cpp
// delete mDebugLines; // .cpp
// } // .cpp{CODE}
{DIV}
{DIV(class="Mogre_porting_cs")}
{CODE(wrap="1", colors="c#")} public static acDebug SingletonPtr // needed in C# ??
{
get { return ms_Singleton; } // not defined ??
}
public static acDebug Singleton // I think this is wrong ... ??
{
get
{
assert(ms_Singleton); // what is assert ??
return ms_Singleton; // ... return pointer ?? (C# has no pointer)
}
}
public void acDebug(SceneManager sm, Renderwindow window)
{
mSceneMgr = sm;
mWindow = window;
mDebugLines = new ArrayList(); // ??
}{CODE}
{DIV}
The destructor ''~acDebug()'' is not needed under C# / .NET, because of garbage collection.
Destructors are defined by "__~__". Just ignore such lines.
{DIV(class="Mogre_porting_header")}
{CODE(wrap="1", colors="c++")} // void addDebugText(String text); // .h{CODE}
{DIV}
{DIV(class="Mogre_porting_cpp")}
{CODE(wrap="1", colors="c++")} // void acDebug::addDebugText(String text){ // .cpp
// mDebugLines->push_back(text); // .cpp
// } // .cpp{CODE}
{DIV}
{DIV(class="Mogre_porting_cs")}
{CODE(wrap="1", colors="c#")} public void addDebugText(String text)
{
mDebugLines.Add(text);
}{CODE}
{DIV}
{DIV(class="Mogre_porting_header")}
{CODE(wrap="1", colors="c++")} // void printText(); // .h{CODE}
{DIV}
{DIV(class="Mogre_porting_cpp")}
{CODE(wrap="1", colors="c++")} // void acDebug::printText(){ // .cpp
// String output; // .cpp
// std::list<String>::iterator itr = mDebugLines->begin(); // .cpp
// std::list<String>::iterator itrEnd = mDebugLines->end(); // .cpp
// for (;itr!=itrEnd;itr++){ // .cpp
// output += (*itr) + ", "; // .cpp
// } // .cpp
// mWindow->setDebugText(output); // .cpp
// mDebugLines->clear(); // .cpp
// } // .cpp{CODE}
{DIV}
{DIV(class="Mogre_porting_cs")}
{CODE(wrap="1", colors="c#")} public void printText()
{
String output = "";
foreach (String line in mDebugLines) // instead of iterator
output += itr + ", ";
mWindow.setDebugText(output); // I didn't found a member like this... ??
mDebugLines.Clear();
}{CODE}
{DIV}
{DIV(class="Mogre_porting_header")}
{CODE(wrap="1", colors="c++")} // protected: // .h
// SceneManager* mSceneMgr; // .h
// RenderWindow* mWindow; // .h
// std::list<String>* mDebugLines; // .h{CODE}
{DIV}
{DIV(class="Mogre_porting_cs")}
{CODE(wrap="1", colors="c#")} protected SceneManager mSceneMgr;
protected RenderWindow mWindow;
protected ArrayList mDebugLines;{CODE}
{DIV}
{DIV(class="Mogre_porting_header")}
{CODE(wrap="1", colors="c++")} // }; // .h
// #endif // .h{CODE}
{DIV}
{DIV(class="Mogre_porting_cs")}
{CODE(wrap="1", colors="c#")} } // class acDebug{CODE}
{DIV}
The __complete result code__ you will find in ((Easy debug text MOGRE)).
!!Remarks
Questions can be asked in the [http://www.ogre3d.org/phpBB2addons/viewtopic.php?t=6204|forum thread].
__Some words of user ((User:Smernesto|Smernesto))__
{DL()}
Well if you want to port from a language to another the best is to know about both languages. If you don´t know C++ will be a little more hard to port code.
{DL}
{DL()}
Porting code from C++ and Ogre to C# and Mogre is really __not very hard__, but if the code es very large it will take you much much time.
{DL}
{DL()}
Well in C++ __templates is similar to generics__ in C# 2.0.
{DL}
{DL()}
Generally for OOP you use the __.h file__ to write the __interface__ of the classes and the __.cpp file__ to write the __implementation of the methods__ (member functions).
{DL}
{DL()}
For example I ported the OSM Loader 2, and the original code uses tinyxml, a xml parsing library for C++, when I ported the code I used System.Xml from .Net, also changed the Ogre Exceptions to .NET exceptions, also used .NET Containers.
{DL}
{DL()}
I recommend you if you want to port many code that you read some general information about C++.
{DL}
''More helping comments can be places here ...''