Question:
Should I switch to C#?
2009-07-07 08:31:23 UTC
I've been studying C++ for about a month now.
I'm getting to the part where I can start making some cool games while continuing my study at the same time.

But I can only make games in console because if I want to create a user interface for the game or add some nice graphics I have to learn the win32 api language as well which seems very advanced and would take a long time.

The lessons I'm reading from said that If I was doing C# I wouldn't need to do all that win32 api coding and that the drawback is less speed in the program.

From what I saw C# and C++ looks almost exactly the same except for the cout and cin and that all functions must be inside a class.

So I wouldn't have to learn C# from scratch.. I can pretty much continue from where I am right now at C++.

Or am I wrong?

Otherwise, How much speed would I actually lose by going with C#?
I don't think it would matter to small programs, right?
How big would the game have to be for you to lose a noticable amount of speed?
Six answers:
2009-07-07 09:59:18 UTC
You don't really want a GUI toolkit, but a Graphics engine. To name a few: OpenGL, DirectX, SDL, Allegro. There are some higher level ones like OGRE and Irrlecht, which are powerful enough to be considered complete game engines. Graphics engines interface closely with the hardware to produce efficient code where possible. They also have plenty of already-optimised functions for handling graphics which save you time in writing yourself.



You'll need some GUI knowledge to tie in with the graphics engine, but it's usually just passing a widget's ID to the graphics engine to integrate them. Noting Jordon's reply. I'd agree about the cross-platform GUI toolkits. wxWidgets, QT and GTK+ are the main ones to point out. But there are some smaller known ones that might suit you too, for example Crazy Eddie's GUI.



Using a Rapid Application Development style for your GUI is preferred. An IDE or separate application will allow you to drag and drop the widgets onto a form and generate the code for you (In the same way Visual Studio will do for C#/VB.) An example of such, which I always recommend is Code::Blocks (for many reasons). But it has built in support for wxWidgets RAD, and code templates for several project types (OGRE too).



I would suggest you try out an engine in C++ first, but consider moving to C# if it is difficult. A month of learning C++ isn't really sufficient to know the language, so don't be dissuaded from learning C# for the sake of such a small time. Most of the ideas you've learned from C++ already will be usable in C# with some slight variation.

I've been using it for 12 years and it still catches me out from time to time.



XNA is an nice option, but similar alternatives do exist in C++. (ie, OGRE). If you take a look at the XNA tutorial videos you'll see how powerful it can be in just a few hundred lines of code. I personally have a few dislikes about it though - it kinda forces a design strategy on you, and does things I prefer to do differently. A good note about using .Net is, you aren't forced to use a single language. You can use XNA with the F# language for instance, which is an ideal language for the math formulae you'll stick in a game (among many other thing).



On Jordon's note about the approximate 5% performance difference. I've no idea where he pulled such a figure, but it's complete crap. You can't really measure an approximate performance difference, but only an average difference over lots of code. You might, for example, have a frequently iterating loop that is slow performing, which would cripple the overall average performance of your app.



The main performance issue in JIT-compiled code is in register allocation. A CPU typically has only a few registers, and all other variables need to be held in memory. Memory access is significantly slower than register access, so knowing when to move data in and out of registers is non-trivial. A normal C++ compiler will optimize this during compile time (slows down compilation time, but greatly improves code performance), but for JIT-compiled applications, it needs to be compiled instantly, and such optimizations cannot be made, thus slower code.



I've no idea where 5% comes from, but it seems wrong. The last I read, some JIT register allocating optimizations were made which could narrow down average performance to around 10% slower than native code. This could become significant if the slower code is something you iterate through frequently.

Java performs better than CIL because the JRE has several runtime-optimizations that will improve code performance dynamically during code execution, but this kind of support is much weaker in the CLR.



On the plus side - Some of the more performance intensive functions will be those inside the Graphics API and other libraries that are written in lower-level code, and imported into .Net. There is no aformentioned performance loss in these, because they are DLLs which were compiled by an optimizing compiler.



An argument some C++ elitist might have is about garbage collection. Ignore these fallacies, because you can manage memory just like you do in C++ by creating your own destructors (by inheriting from the IDisposable interface) which can be invoked manually (or automatically with the using statement). You can also manually control the garbage collector, which can sometimes work to your favor, because you can chose to perform collection when few other events are occurring, and not waste time freeing your memory during busy events. Admittedly though, it's less predictable than manually controlling allocation for everything.
2009-07-07 08:49:11 UTC
C++ and C# only seem to be similar. Although the syntax is the same, they are very very very different languages. Tons of differences that aren't necessarily easy to understand; the synopsis is that C# is "safer, simpler, easier".



The speed penalty for going to C# is minimal. You will lose approximately 5% of your performance. C# is at approximately the same speed as Java (excepting that Java runs much better cross-platform than C# does or ever will). This speed difference can be demonstrated in any application, but usually only with a stopwatch.



There are numerous other GUI libraries for C++ than the win32 api, including several that are cross-platform (Windows, MacOSX, Linux). These are probably more valuable to learn.
DC2000
2009-07-07 08:38:46 UTC
On the surface C# and C++ may look very similar but once you get in and really start coding you will see that they have profound differences. To many to list here.



There used to be librarys (long time ago) out there that were GUIs for console/DOS games written in C++, have you tried looking for those? Might want to see what you can find before you abandon C++ :)
?
2009-07-07 10:44:33 UTC
Depends.. C++ is very different to C#. C# is easier because it's sort of in the middle of languages Visual Basic and C++.



C++.NET would obviously be very similar to C# because it's based around a similar language structure.

C++ STD would be very different.



For Example, here's a Console Application displaying Hello World, waiting for keyboard input then closing in the three languages.



C#:

Console.WriteLine("Hello World");

Console.ReadKey();



C++.NET

Console::WriteLine(L"Hello World");

Console::ReadKey();



C++ STD

std::cout << "Hello World" << std::endl;

std::cin.get();



You can see what I mean how C++.NET is like C#.



Programming in .NET would require whoever uses your programs to have the .NET framework - which is sometimes a problem.
2009-07-07 09:00:51 UTC
It depends on your goal for your game.

I think it might be worth looking at XNA in association with C#.



"Microsoft® XNA® Game Studio 3.1 enables hobbyists, academics, and independent game developers to easily create video games for Xbox 360, Windows, and Zune by using optimized cross-platform gaming libraries based on the .NET Framework in the C# programming language.



XNA Game Studio 3.1 is required to create games for Xbox LIVE Community Games."
Bobdarshy
2009-07-07 08:38:58 UTC
Speed loss from C++ to C# is very significant. You won't notice it with simple programs, but when you make a complex game, you will really see the difference.



That said, C# is easier in my opinion. However, if you are truly going into complex 3D graphics, you'll be working more with DirectX APIs.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...