Mogre porting example        

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 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

#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


acDebug.cpp

#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();
}

Mogre code

header file
C++ code
C# code

// #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

using System;
 // other usings
 using Mogre;
 using std;      // ??

// template<> acDebug* Singleton<acDebug>::ms_Singleton = 0;   // .cpp

// C# ??

// class _acSystemExport acDebug : public Ogre::Singleton<acDebug> // .h
 // {                                                               // .h

public class acDebug        // inheritance of something? ... I found no Mogre.Singleton
 {

// public:                                                     // .h
     //     acDebug(SceneManager* sm,RenderWindow* window);            // .h
     //     ~acDebug();                                                // .h
     //                                                             // .h
     //     static acDebug& getSingleton(void);                        // .h
     //     static acDebug* getSingletonPtr(void);                  // .h
     //                                                             // .h

// 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

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();            // ??
     }


The destructor ~acDebug() is not needed under C# / .NET, because of garbage collection.

Destructors are defined by "~". Just ignore such lines.

//  void addDebugText(String text);                            // .h

// void acDebug::addDebugText(String text){                    // .cpp
     //     mDebugLines->push_back(text);                           // .cpp
     // }                                                           // .cpp

public void addDebugText(String text)
     {
         mDebugLines.Add(text);
     }

//  void printText();                                          // .h

// 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

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();
     }

// protected:                                                  // .h
     //     SceneManager* mSceneMgr;                                   // .h
     //     RenderWindow* mWindow;                                     // .h
     //     std::list<String>* mDebugLines;                            // .h

protected SceneManager mSceneMgr;
     protected RenderWindow mWindow;
     protected ArrayList mDebugLines;

// };                                                              // .h
 // #endif                                                          // .h

} //  class acDebug


The complete result code you will find in Easy debug text MOGRE.

Remarks

Questions can be asked in the forum thread.

Some words of user Smernesto

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.

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.

Well in C++ templates is similar to generics in C# 2.0.

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).

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.

I recommend you if you want to port many code that you read some general information about C++.


More helping comments can be places here ...