views:

356

answers:

3

I've got a medium scale project (a turn-based game) that's currently written in C/C++. Only one person is working on it. It will complie with /clr, although I haven't tried /clr:pure.

I'm looking for advice on the overall process of converting it to C#.

It's mainly C with a thin veneer of C++ on it (C with static/singleton classes for scoping). Yes, it's not 'real' OO, I've been planning to do the C# conversion first to avoid creating any conversion issues while converting.

It makes very limited use of the STL (The queue class) in the pathfinding module, and because it's a game, it also doesn't do any memory allocation outside of the third party sound library in a DLL, and what's necessary for loading the graphics bitmaps.

I want to convert it into idiomatic C#. I'd rather not hold a discussion on whether this is a good idea, I understand that it isn't, let's please let part go. Assume that I've got overriding reasons please.

I've also done my research, and there is a thread that is somewhat relevant about converting the methods using reflector. I plan to look more deeply into it when I get a chance.

http://stackoverflow.com/questions/73879/translate-c-cli-to-c

There is also one pay applicaiton that will convert from C++ to C# that I may look at if that doesn't work as well as I'd like.

The hardest part is going to be rewriting the interface in either WPF/Silverlight or XNA (or both) There are pros and cons to each one, but I'm leaning towards WPF right now because of the font support, and because that way I won't have to write all the widgets. I may end up doing both, an XNA quick port, and a WPF one later.

There are several possible approaches to this, and I wanted to know if anyone had any experience with a conversion like this, and any suggestions or pitfalls.

1) Create the UI first, This involves either leaving the Graphics module as is, and converting from GDI to raw GDI like calls in XNA, and converting later to WPF one dialog at a time.

2) Convert the guts first, and leave the main UI in C++/CLI for now, after the guts are converted, switch over the interface one dialog at a time to WPF.

A related question is whether it's worth doing this module by module, or basically all at once, and whether it's better to do a rough conversion into C# and clean everythig up, or clean up everything in C++ then convert to C#.

The thought right now is to rewrite the event loops to use a home grown common loop like MFC, then try to convert everything at once to C#. Leave the graphics in C++ and see what breaks. After that, move to XNA and provide a WPF layer later. The last two steps are arbitrary, I think that the XNA port is going to be simpler, but using WPF basic panel may be pretty simple too.

I'm open to any suggestions that may help.

Thanks, Ralph

+3  A: 

I must say that the best best advice I can give you is to rewrite the application. C++ and C# are so different that if you are certain that you want to convert the entire thing then a rewrite will most likely be your best option.

I guess my main question is why do you want to convert this project to C#? I understand that this is not your question but since you are undertaking what I consider to be a considerable effort to do this, I hope that you realize what you are getting into.

Andrew Hare
Yes, I fully understand the amount of work involved. I've been doing C++ since it used CFront and C# casually since 1.0. I was hoping for some meaningful advice, but thanks anyway.
Ralph
I don't mean to blow you off but I think I *have* offered meaningful advice. It is impossible for anyone to know the depths of this project and to help you figure out a good way to attack it. You are looking to change an unmanaged application into a managed one - this is not trivial! If we knew *why* you wanted to switch we may be able to help. In other words if you were excited to use WPF then we could advise you to only rebuild the UI and leave the rest of the application as is. Without more information is is very difficult to offer any other advice other than "rewrite the whole thing".
Andrew Hare
Strange, for some reason I wasn't notified that you'd posted a comment. The main reason is that I expect to maintain this program for 10+ years, and my day job is C#. I also want to be able to do WPF and possibly XNA. The whole UI needs to be replaced, and by then cleaning it up so that I can do C# with everything is fairly simple. I was hoping that someone had done something similar and might have advice on what worked for them. (UI first, everything into C#, then UI to WPF, C++ Native into DLLs vs Managed C++ first, etc.) I know full well how much work it is.
Ralph
+1  A: 

I found out that CodeRush (which I already own) has a 'smart paste' operation which does a reasonable job of converting what can be conferted. There's also a CR_Paste add-in on googleplex which does something similar. (the CR_Paste add-in may not require Coderush, only the free DXCore applicattion.

Since I'll have full acess to the parse tree (and understand parse trees), I may (and may not) customize it to change char * to String, etc. If I do much customization, I'll probably create an open source version, probably on CodePlex.

Ralph

Ralph
OK, Here's my tentative plan I'll add more later.1) change all the methods which look like unit_agility(the_unit) to units[the_unit].agility(). That should be a lot of the code outside of the utility graphics functions.2) Put all other methods into static classes, just so it will compile in C# later.3) Split out into sepearate 'Dialog" classes all the code which touches the UI. Much of it is in the same files right now, but it should be easy to split out.4) Convert all strings to std::string and look at using DXCore to convert them to System::string (or do a global search/replace.)
Ralph
A: 

It sounds like you need to redesign the app, not just a code conversion. Converting a non-OO C/C++ app to C# doesn't seem like a realistic goal.

alexD