How to port c++ to C# (an example)

Beauty

14-01-2008 10:16:45

Hi,

I want to port an Ogre snippit, but I'm confused.
To me there are strange thinks, because I don't know C++.
The code is devided to a .h and a .cpp file. Also e.g. template<> I don't know.

In the wiki is the article OGRE_to_MOGRE how to port, but the information is not enough (for me).

How can I deal with this problem?
Is it also possible to port code or does I have to learn much about C++ programming? Maybe some lines of code are not necessary and can be ignored.

It would be good to add more information about to the wiki article OGRE_to_MOGRE. So other people like me can port code.

I want to port the snipit Easy_debug_text. It's not very long. This could be used as an example, what I would insert to the wiki. (as blank snipit and additionally as a completely Mogre_porting_example with remarks and comparisons).

Beauty

14-01-2008 11:53:38

Ok, I did the first step.
Now in the wiki is an incompletely buggy code.
The sections of .h + .cpp + C# are together on one place for a good comparison.
More I can't do with my knowledge. Now it's up to others to correct and complete it. I would be happy about :wink:

www.ogre3d.org/wiki/index.php/Mogre_porting_example

smernesto

14-01-2008 20:04:40

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

Beauty

15-01-2008 10:01:52

Thanks for your words (-:

I added them to the wiki for helping other people.
Also I made a colour highlighting to the code. So it's more clear to read.

www.ogre3d.org/wiki/index.php/Mogre_porting_example

Beauty

17-01-2008 13:22:19

After a talk with somebody I could port some more code, but now my knowledge is at the end. I think the main work is done.

Questions are marked as in-line comments.
Also I added a wiki [edit]-Link to each code block for more comfortable changing.

RichTufty

23-10-2008 12:07:42

Hey Beauty, didn't mean to hijack the other thread.

When porting code, something to always bare in mind is... what am I trying to achieve and what is the code trying to achieve.

I've found a lot of the time, several lines of C++ can be reduced to a couple of C# lines, and sometimes it means reaproching the way the C++ programmer has tackled the problem.

I have quickly ported the Easy Debug code to C#, I hope it is of some help. I haven't put the code in the Wiki yet, because you have compared line by line and i didn't want to upset your formatting. A lot of the C++ is handled for us by C# so a lot of those lines just can't be ported.


using System;
using System.Collections.Generic;
using Mogre;

namespace your_projects_namespace {

public class DebugWriter {

SceneManager sceneManager;
RenderWindow window;
List<string> debugLines;

public DebugWriter(SceneManager _sceneManager, RenderWindow _window) {

sceneManager = _sceneManager;
window = _window;

//create empty list of strings
debugLines = new List<string>();
}

public void addDebugText(string text) {
//simply add string to our current list
debugLines.Add(text);
}

public void printText() {
string output = "";

//loop through each string in the list and join them together
foreach (string line in debugLines)
output += line + ", ";

//output debug text

// I didn't found a member like this... ??
//window.setDebugText(output);

System.Diagnostics.Debug.WriteLine(output);

debugLines.Clear();
}
}
}


You will notice that in the actual printText() method I also couldn't find the setDebugText() method on the RenderWindow class, so I have put a line in there to write the debug text to the Visual Studio output window. You may have your own way of displaying debug information in your own project so just change that line.

Also the sceneManager and window objects aren't actually used, so they can be completely removed from this class, but i left them in incase you wanted to expand this class.

Is this any help?

RichTufty

23-10-2008 12:23:27

Sorry think i accidentally deleted the following line, just add it to the top...

using System.Text;

Beauty

23-10-2008 14:07:01

Thanks for your work :)

I will test it and put your code to its own wiki page. I think this will also be helpful for other Mogre users.

Right, porting line by line seems not to be well.
When I created this wiki page I thought this is a nice small class.
I will see how I update the comparing wiki page.

jmix90

27-03-2009 14:44:55

Hey,

Long time from the last post but maybe still interested by info... :)

This post for example gives you a good book to read (third reply).

+++