Sign in to follow this  
Followers 0
hawksprite

Getting started with C++

17 posts in this topic

I've done a lot of programming in C# with XNA in both 2D and 3D environments. But due to its performance issues I've recently begun looking into moving into the world of C++ with Direct X.

What's the best way to get started with game development in C++? Should I use a wrapper/graphic engine or just let it all hit the fan and try using the Direct X SDK straight up.

Thanks.
0

Share this post


Link to post
Share on other sites
Not specifically to do with Direct X, though it does cover it for 2d, but you could checkout this.

http://msdn.microsoft.com/en-us/library/ff381399%28VS.85%29.aspx
0

Share this post


Link to post
Share on other sites
Understandable, but even if it's a small increase I'd rather learn the superior platform to develop games on.
[quote name='saejox' timestamp='1341804088' post='4957133']
c++ is not something you move to and gain 9001x performance.

badly written c++ code runs worse than c#
if xna wasnt enough you did it wrong.

in reality gpu is almost always the bottleneck.
[/quote]
0

Share this post


Link to post
Share on other sites
Thanks for the tutorial, i'll look into as soon as I can.

[quote name='timnwells' timestamp='1341804663' post='4957134']
Not specifically to do with Direct X, though it does cover it for 2d, but you could checkout this.

[url="http://msdn.microsoft.com/en-us/library/ff381399%28VS.85%29.aspx"]http://msdn.microsof...399(VS.85).aspx[/url]
[/quote]
0

Share this post


Link to post
Share on other sites
If you only had C# experience and no C++ experience, I suggest you learn the things which are in C++ and not in C#.
Such like memory management (RAII, etc), C++ idioms (Google it). Wrongly understanding them may lead you crazy. :-)
1

Share this post


Link to post
Share on other sites
good luck with your adventure then :)

tips:
- Every new call should have a delete counterpart.
- pass parameters by reference when they are larger than 4bytes.
- stack memory is almost always faster than heap, but less flexible.
1

Share this post


Link to post
Share on other sites
[quote name='saejox' timestamp='1341804088' post='4957133']
c++ is not something you move to and gain 9001x performance.

badly written c++ code runs worse than c#
if xna wasnt enough you did it wrong.

in reality gpu is almost always the bottleneck.
[/quote]

This,

It is highly unlikely that you've hit a performance wall with C# as properly written C# code running on Microsoft .Net has pretty much the same performance as standard C++ code compiled with Microsofts C++ compiler. The advantages you get with C++ lies primarily with memory management (you can get better cache utilization if you know what you're doing (or far, far, far worse if you don't) and the ability to use non standard extensions for things like SSE.

For C# i'd recommend taking a look at Mono.SIMD if you need a performance boost and try to think about how you allocate memory (you actually have quite a bit of control over this, avoid excessive creation of temporary objects as it tends to both give the garbage collector a hard time and fragment your heap(which greatly reduces cache performance)
1

Share this post


Link to post
Share on other sites
[quote name='saejox' timestamp='1341805793' post='4957140']
tips:
- Every new call should have a delete counterpart.
- pass parameters by reference when they are larger than 4bytes.
- stack memory is almost always faster than heap, but less flexible.
[/quote]

True, although if you use exceptions in your software, then you really shouldn't ever be calling delete. You need to be using a smart pointer container such as std::auto_ptr, std::tr1::shared_ptr.

What is quite cool about the newer smart pointers is you can pass a deleter function to it for memory that needs to be freed differently (such as C libraries or abstracting wierd memory systems like in Irrlicht). For example you can pass fclose to a smart pointer containing a file descriptor created by fopen (i.e during it's construction.). This is perfect for opengl stuff such as glFreeTextures or glFreeBuffers

Although in C# (or java) you should really be doing something similar using either Close or Dispose with certain IDisposable derived classes. So frankly I don't see any reall added difficulty here. If anything the smart pointers look a lot neater than loads of [b]try, catch, finallys[/b] around the place. Or even [b]using[/b] which only works in single functions anyway. Edited by Karsten_
0

Share this post


Link to post
Share on other sites
Read these articles/guides, should be good for starters:
[url="http://geekswithblogs.net/mikebmcl/archive/2012/02/02/c-to-c-ndash-a-somewhat-short-guide.aspx"]http://geekswithblog...hort-guide.aspx[/url] // general intro, "C# to C++ – A Somewhat Short Guide", incl. some C++11 features
[url="http://www.hackcraft.net/raii/"]http://www.hackcraft.net/raii/[/url] // RAII intro
[url="http://visualstudiomagazine.com/articles/2012/05/30/pointers-get-smart.aspx"]http://visualstudiom...et-smart.aspx[/url]// smart-pointers intro
[url="http://akrzemi1.wordpress.com/2012/02/03/value-semantics/"]http://akrzemi1.word...alue-semantics/[/url] // value-semantics intro

If you need a more general reference, this one is pretty good (also includes C++11 additions):
http://cppannotations.sourceforge.net/cppannotations/html/ Edited by Matt-D
1

Share this post


Link to post
Share on other sites
Thanks for all the guides and advice.

I'm not reconsidering the switch and wondering if there's a better way to use C# other then XNA.

But at the same time i'm still going to learn C++ as a platform and as I get better at it slowly transition into it.
0

Share this post


Link to post
Share on other sites
[quote name='hawksprite' timestamp='1341855561' post='4957341']
Thanks for all the guides and advice.

I'm not reconsidering the switch and wondering if there's a better way to use C# other then XNA.

But at the same time i'm still going to learn C++ as a platform and as I get better at it slowly transition into it.
[/quote]

Other than XNA you also got OpenTK (crossplatform), SlimDX (DX11 IIRC) and ofcourse Unity.

For C++ there are fewer really good options that are easily accessible, CryEngine 3 however is free for non commercial use and is available at a 20% of revenue royalty rate for indies (So you only have to pay if you make any money, allthough if you succeed it will get very expensive(still cheaper than UDK though)), getting familiar with OpenGL / Direct3D is a good idea aswell, if you plan on writing your own engine (Which really is the only reason one would willingly touch C++ for a game these days) you should take a look at lua, python or angelscript (or something else, there are tons of nice options) to add scripting support (an engine that requires the game to go through a 20 minute recompile each time you adjust the AI or tune your heat seeking missiles will become horribly painful to work with)
0

Share this post


Link to post
Share on other sites
Have you profiled your XNA game? Where are the bottlenecks?

If you haven't identified the specific parts which are limiting performance then you're almost certainly not hitting the limits of XNA.

Also, I feel I have to call you out for referring to c++ as 'superior'. It may or may not be the most appropriate choice given a particular set of requirements, but is not superior in general. In many ways, and in many applications, C# is probably the better choice. It's true that somebody who is sufficiently experienced can usually squeeze out an extra bit of performance in C/C++/assembly, but it is becoming increasingly rare that it is worth the considerable extra effort (particularly since more and more work is being offloaded to the GPU these days).
0

Share this post


Link to post
Share on other sites
I don't think there is anything wrong with exploring C++, especially if you have logged some programming with C# or other languages. If nothing else, it will reveal a newfound respect and love for reflection, a sane build model, sane debugging system, fleshed-out standard library, and memory management [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

I also agree with those that have questioned hitting a performance bottleneck in XNA. From what I hear there are a few gotcha's when targetting XBOX (and other limited platforms), but I can't imagine where a PC would have trouble CPU side that would magically be solved by just switching to C++.
0

Share this post


Link to post
Share on other sites
I've decided to go with SlimDX for my next project. Thanks to all who gave me advice, i actually found a handful of engines and platforms I never knew existed.

To clear up the rest of the questions most of the bottleknecking in XNA was my own poor code just little bits in A* alogrithims that were dropping the FPS by like 10 and so on so forth. I understand that C++ wouldn't fix this but I simply wanted to start my next project on a better platform. I didn't quite write my original post all to amazingly.

To WavyVirus I agree and probably shouldn't have jumped to saying C++ was better, I simply meant for game programming. When it comes to desktop application programming or basically any other situation i'd never consider using C++ over C# or something like ruby.

While I still want to learn C++ and memory management aspects, etc. I'm going to stick with C# for now but use SlimDX so I have a little more availability instead of being limited to just DX9.

Finally; i've ran into a bit of an issue with SlimDX but I made a second post for that [url="http://www.gamedev.net/topic/627705-slimdx-with-direct2d-and-direct3d11/"]here[/url].
0

Share this post


Link to post
Share on other sites
[quote name='hawksprite' timestamp='1341886498' post='4957475']
To WavyVirus I agree and probably shouldn't have jumped to saying C++ was better, I simply meant for game programming. When it comes to desktop application programming or basically any other situation i'd never consider using C++ over C# or something like ruby.
[/quote]

Still, I think that saying that C++ is better for game programming is a very bold claim. There are some situations (e.g. developing the latest and greatest big-budget title, with cutting edge graphics being a selling point) where it is still necessary to use C/C++ for performance reasons. Also, sometimes the platform you are developing for (console/mobile etc) restricts your options. But for many of the games that an indie or hobbyist developer is likely to have the resources or desire to create, C++ is not the optimal choice as the same result can be achieved more quickly and easily by other means. Often the lack of a team of artists means that a small developer will naturally gravitate away from game ideas which depend on super-detailed graphical content, and instead focus on areas where they can be more competitive like original gameplay, story, stylised art etc.

A not-so-terrible analogy would be cars. Is a Formula One car the best for driving? Really this depends on the situation (what sort of track surface? lots of corners or long straights? Are you driving around town? A rally car, Mini or drag racer might be superior in some situations) and also on the driver (what are they familiar with? I could probably drive my own little car around a track faster than an F1 car as I would spend most of the time either stalling or killing myself in the more powerful and complex machine).

Having said that, I don't want to discourage you from learning C++ and getting deeper into things like memory management - it can be a lot of fun, is still the appropriate choice in some situations, and will improve the code you produce in other languages. Just reminding you that these things aren't black and white - c++ is just another tool in your toolbox to pull out when you recognise that it the right one for the job.
0

Share this post


Link to post
Share on other sites
I'd actually recommend looking at ANSI C first. There's a lot of subtle stuff that C++ carries over from C (not surprisingly). That said I wouldn't treat C++ as an extension of C so that isn't really a necessity.

Also, I'd definitely look at COM before you look at DirectX. The book [b]Inside COM[/b] is great (old though). Edited by BinaryPhysics
-1

Share this post


Link to post
Share on other sites
True true, thats mostly while I decided to go with SlimDX for now but still plan on learning C++ in the future. Thanks for the analogy BTW, clears it up pretty well.

[quote name='WavyVirus' timestamp='1341961060' post='4957815']
[quote name='hawksprite' timestamp='1341886498' post='4957475']
To WavyVirus I agree and probably shouldn't have jumped to saying C++ was better, I simply meant for game programming. When it comes to desktop application programming or basically any other situation i'd never consider using C++ over C# or something like ruby.
[/quote]

Still, I think that saying that C++ is better for game programming is a very bold claim. There are some situations (e.g. developing the latest and greatest big-budget title, with cutting edge graphics being a selling point) where it is still necessary to use C/C++ for performance reasons. Also, sometimes the platform you are developing for (console/mobile etc) restricts your options. But for many of the games that an indie or hobbyist developer is likely to have the resources or desire to create, C++ is not the optimal choice as the same result can be achieved more quickly and easily by other means. Often the lack of a team of artists means that a small developer will naturally gravitate away from game ideas which depend on super-detailed graphical content, and instead focus on areas where they can be more competitive like original gameplay, story, stylised art etc.

A not-so-terrible analogy would be cars. Is a Formula One car the best for driving? Really this depends on the situation (what sort of track surface? lots of corners or long straights? Are you driving around town? A rally car, Mini or drag racer might be superior in some situations) and also on the driver (what are they familiar with? I could probably drive my own little car around a track faster than an F1 car as I would spend most of the time either stalling or killing myself in the more powerful and complex machine).

Having said that, I don't want to discourage you from learning C++ and getting deeper into things like memory management - it can be a lot of fun, is still the appropriate choice in some situations, and will improve the code you produce in other languages. Just reminding you that these things aren't black and white - c++ is just another tool in your toolbox to pull out when you recognise that it the right one for the job.
[/quote]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0