10 Replies - 1715 Views - Last Post: 01 July 2013 - 01:10 PM Rate Topic: -----

#1 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 85
  • Joined: 17-June 12

DirectX 9 or DirectX 11 for 2D

Posted 30 June 2013 - 05:20 PM

Hey everyone, I recently took it upon myself to refresh in C++ after leaving it for a year or so, and I'm through a couple books by now, about to start getting back into C++ game programming again. Now, I do have some experience game programming, but in different languages.

I've done minimal DirectX 9 programming a while back, which got to the point where I could move a sprite on the screen. I was wondering if DirectX 9 is still the best way to go for 2D game development, or if it's worth going into DirectX 11. I seem to remember (I was doing DirectX 9 before DirectX 11 came out) DirectX 9 was better for 2D than DirectX 10, so I'm looking to see if this applies for DirectX 11 as well. I've been scouring the interwebs for various opinions and facts about this question, but my lack of understanding of shaders is what keeps me from figuring this out in the first place, as most tutorials seem to use shaders to program 2D. Most books on 2D game development that I own or looked at seem to use DirectX 9 as well, and even on their forums recommend using X9 over X11.

I've also seen something about using a 3D object of some sort and rendering all the 2D graphics on the same Z plane, but with the option of changing that for layering, which seems like a nice option.

If someone can point me in what they think is the best direction, I would greatly appreciate it! Thanks guys :D/>

Also, i'm working on Windows 7 right now. I did read that it seems Windows 8 is starting to use Direct2D again, even though I know that it was sort of left behind back in DX7/8 (think it's 8, not sure off the top of my head).

This post has been edited by Mr_Fraggs: 30 June 2013 - 05:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: DirectX 9 or DirectX 11 for 2D

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 535
  • View blog
  • Posts: 1,189
  • Joined: 24-April 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 30 June 2013 - 06:24 PM

There was a huge jump between DX9 and DX10 that has remained in DX11. From what I've seen, from DX 10 forward, even the most simple things require writing a shader.

I'm in many ways in the same boat as you are. I'm just now exploring DX11 and other options.

Anyway, DX9 is certainly going to be easier all the way around, because I believe you can do a whole lot without getting into shaders. After that, you have to start programming shaders, and I think that's where the primary difference lies.

Keep in mind though, you only need one shader for 2D (or really for 3D for that matter). If you can find one good shader that draws sprites, you're probably going to have most of what you need right there. Just keep reusing that one shader. Basically, you just need a shader that draws quads and textures them with flat lighting in an orthogonal camera view (from what I've seen, not having done exactly what you're talking about here myself).

Even in 3D, one well written shader and instructions on how to use it could probably do 90% of everything you need a shader for. So, if you can find some shader code and just copy it, you may be fine for quite some time to come. And I think that's the primary difficulty for DX after version 9.

But DX9 is starting to get way behind the times. Sure you CAN use it, but does Microsoft even support it anymore? Its certainly getting pretty old.

Since you seem to be really into C++, you may want to give SDL some serious consideration. There are some SDL tutorials here in the tutorial section and StayCrisp just published a book on 2D game programming in SDL. Seems like that's a pretty easy way to get into C++ 2D game programming. I know I'm looking forward to learning about it.
Was This Post Helpful? 0
  • +
  • -

#3 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 85
  • Joined: 17-June 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 30 June 2013 - 06:42 PM

Thanks for the input! Glad to know i'm not the only one around having an internal debate about this.

I had done some work in SDL before, as well as touching on SFML. I haven't worked in OpenGL, but from what I've seen, it's not very friendly towards 2D.

I think my biggest problem is not really knowing much about shaders. Not just programming them, but the idea of them in general. I had just figured before looking into them that they were some 3D lighting stuff used in bigger, 3D games. But apparently I was pretty wrong about that assumption. That leaves me pretty clueless into the idea of shaders altogether.

When making any decision about game development, there's two huge things that I put at the front of my decision-making process: The long run, and my control over the code. I want to learn what's going to be best in the future, instead of jumping from library to library, and DirectX doesn't seem to be going away. In regards to control, it may be my mild OCD, or it might just be me in general, but I like to not only have control over every line of code, I want to know exactly how it runs as well. I don't mean I want to know every single in and out of DirectX or other libraries and exactly how they work underneath the hood (not saying I don't want to know either!), but just in terms of my projects, exactly how the libraries work with my code. I hope that makes sense to you guys the way it does in my head :P

The other problem is finding books and tutorials related to 2D game development with DX, but i'm not asking you guys to do all that stuff for me (not turning it away either!).
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 535
  • View blog
  • Posts: 1,189
  • Joined: 24-April 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 30 June 2013 - 09:33 PM

I think your original concept of shaders was not off track. It's DX10 that went off track. They cut out the fixed function pipeline. In other words, they made it impossible to do even something simple without a shader, such as 2D.

So, even drawing 2D sprites became a 3D task. You're actually drawing the 2D image in 3D but mostly throwing away the third dimension.

So, I have a little experience with shaders albeit I'm no expert. You have a vertex shader and a pixel shader (and some other stages that are unimportant for this discussion starting in DX 11). So you pass a triangle to the vertex shader to calculate how to draw the triangle. The output of that is the individual pixels that compose the triangle image. Those pixels go to the pixel shader to be altered, if needed.

The most basic shader just simply passes a color for the pixel to the pixel shader which then does nothing but pass that colored pixel on. What you end up with is a silhouette in the color specified. That's about 4 lines of shader code. Since there's nothing but one solid color you get the shape, but with only a single color you get no sense of shape or depth, let alone multiple colors in the object.

So for a 2D shader, I believe you would pass it a triangle into the vertex shader. The three vertices would have their positions, their normals to show what direction each vertex is facing for lighting calculations, and a UV texture coordinate (which is basically like pinning a certain part of a picture to that vertex and stretching the picture between the three vertices of the triangle). The output of that will take all that info into consideration and pass a pixel on to the pixel shader for every pixel within the triangle. You'll likely just pass that on through for this unless there's a need to further process it.

Shaders can get fairly complex, but for a 2D sprite all you need is two triangle arranged into a square (quad), UV coordinates to pin parts of the texture to that square, and the texture itself. The whole shader is probably less than 10 lines of code. And that's the only shader I can think of that you would ever use in 2D. You might be able to come up with some more creative shaders for special stuff, but I can't think of any.

Most of the shader stuff that I'm aware of is for 3D and even then I'm thinking 8 shaders will probably do 99% of anything you will ever do. I think I could write 1 good 3D shader and it would take you through 90% of everything you would ever want to do even in 3D. Shader technology IS a bit over-rated.

I've been doing a lot of work in XNA and XNA has one shader called BasicEffect that most people use for everything. There are 4 other shaders built in, but I've only used one of those 4 others and I've never heard of anyone else using any of them. Granted, that's where I've gotten most of my shader experience. I recently wrote a shader to do water with the waves and all (it also reflected as water is partially reflective). And I did another shader to do point lights allowing me to light a scene for interiors (you don't even need that for a scene that's outside).

Anyway, in the newer versions of DX you are actually doing 2D in 3D. You just set the camera to orthogonal and put your sprites on textured squares. The squares themselves are in 3D space. But since they are 2D planes, no one knows the difference.
Was This Post Helpful? 0
  • +
  • -

#5 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 85
  • Joined: 17-June 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 30 June 2013 - 10:42 PM

Well I had an idea that 2D in DirectX 10 and 11 was to do with rendering 3D in a 2D view, but I guess I have a little more learning to do! So what I took from that is that I guess I should learn a bit about shaders, then move on with DirectX 11? That certainly seems like the better option than DirectX 9, and I don't see a reason to use DirectX 10 if 11 is there anyway. So DirectX 11 seems like my best option you would say?
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: DirectX 9 or DirectX 11 for 2D

Posted 30 June 2013 - 11:01 PM

So there is confusion about shaders. It is important to remember that the purpose of a shader is not to store geometric data, but to provide a language and set of effects that enhances the visual representation of geometry. Before shaders there was the 'fixed function pipeline' - the reason why it was fixed function is because each graphics card manufacturer back in the day had their own instruction set for their dedicated graphics hardware that was incompatible between the card manufacturers. This meant that different capabilities had to be coded into the graphics engines of games in order that they can support the various graphics chipsets out there. Of course this wasn't ideal for game developers nor the card manufacturers. Enter the shader languages, whose job it is to abstract away the particular chipsets so that the graphics part of the game engine need not have 'knowledge' of the hardware being run on.

As said, the shader files do not contain data on how to construct geometry but the shader system needs to have knowledge of the geometry and rendering environment.

1. The Vertex Shader - This is responsible for taking geometric data and applying the calculation for vertex-based effects, such as enhanced lighting and shadows.
2. The Pixel/Fragment Shader - This is responsible for pixel-based effects like color detail, etc.

You can pull off more basic effects like Phong (diffuse) and Gouraud (specular) lighting effects, but I feel such effects won't be very noticeable in 2D since they rely on 3D-based calculations like surface normals, etc.

I wouldn't worry about shaders for 2D work in the slightest if that is what you are thinking.

This post has been edited by ButchDean: 30 June 2013 - 11:04 PM

Was This Post Helpful? 1
  • +
  • -

#7 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 85
  • Joined: 17-June 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 30 June 2013 - 11:33 PM

I'm not really looking to work with advanced lighting and shading and all that right now, more just 2D programming in general using DirectX. It just seems to me that the only way to actually do that is through shaders, or so I thought. I haven't gone through many tutorials in depth yet, and I'm still actually trying to figure out how to set up DirectX 11 with Visual Studio 2012 on Windows 7. Last time I used DirectX, I downloaded the SDK, linked some files and that was that. Now it seems either it's already built in and i'm just trying to hard to set something up that has already been done for me, or I have yet to actually figure out how to set it up. But we digress :P/>

Basically my original question was to decide whether to use DirectX 9 or 11 for 2D game programming, but it seems that DirectX 11 was the better choice. Like I said, nothing too fancy, just basic 2D game programming. I'm a little lost with DirectX 11 as a whole right now after spending some time trying to get it up and running on Windows 7.

If I don't need to use shaders to do 2D programming in DirectX 11, i'm absolutely fine with not using them. But if they are needed, I need to figure that out. Learning it isn't the problem. Figuring out WHAT to learn is :P

This post has been edited by Mr_Fraggs: 30 June 2013 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 85
  • Joined: 17-June 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 30 June 2013 - 11:55 PM

I apologize for the double post (again!) but my time to edit my previous post ran out.

I've been doing some reading about OpenGL as well, and I did know about it prior, but I was told by a few sources that it wasn't useful for 2D graphics programming. What I have found is that it can do 2D possibly easier than DirectX? Would it be better for a solo programmer to work with OpenGL, as it seems to be the only thing that can compare to DirectX? I don't remember why I have this feeling, but after doing minimal work with SDL, SFML and Allegro, I feel that those three are not as good for a distributed game as OpenGL and DirectX (I know I can always be wrong!).

So I'm looking at OpenGL, wondering if this is the better alternative than to try to over-complicate things in DirectX? Also, cross-platform support seems well worth it, even though I only do PC right now.

If you guys think that OpenGL would be better for a lone 2D dev, may I ask what the options are for dealing with input? Sound is also a concern, but i'm more worried about getting graphics and input up before I worry about spicing my projects up with sounds.

Thanks for the help everyone, I appreciate it more than you guys know :)
Was This Post Helpful? 0
  • +
  • -

#9 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 535
  • View blog
  • Posts: 1,189
  • Joined: 24-April 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 01 July 2013 - 06:16 AM

Quote

Well I had an idea that 2D in DirectX 10 and 11 was to do with rendering 3D in a 2D view, but I guess I have a little more learning to do! So what I took from that is that I guess I should learn a bit about shaders, then move on with DirectX 11? That certainly seems like the better option than DirectX 9, and I don't see a reason to use DirectX 10 if 11 is there anyway. So DirectX 11 seems like my best option you would say?



All 3D work renders in a 2D view; your computer screen is 2D and thus the final output for all 3D graphics is 2D. The conversion is the job of your Projection Matrix. I haven't done any real work with DX11 (I've mostly been reading about it and going through some tutorials). But the difference for 2D in DX9 and 2D in DX11, from what I've seen, is that you had built in sprite functionality in DX9 that made it easy to draw sprites on the screen, but now that's gone.

I know in XNA(which is basically a wrapper for DX. Nine I think) they said that the built in functionality for sprites was actually rendering using two 3D triangles behind the scenes.

And I believe that's what you have to do in DX11: draw a rectangle of two triangles and texture it with your 2D sprite, since there's no built in sprite functionality.

And there's also no built in shader. So you have to use a HLSL shader to even draw a texture onto a triangle.

I don't think you really need to learn anything about shaders to get started in 2D. You just need some shader code for 2D that you can copy and paste. If it works, you don't need to know how it works. One big difference though is that you may not be used to defining your matrices just to draw sprites. In XNA, I spent almost all my time in 3D and so the matrices have really become second nature to me. But they can be quite intimidating if you're not used to them. And I'm not sure you can do any shader, even for 2D, without sending it the 3 matrices.

The way that it works is this:
Let's say we have a triangle (this could be a complete 3D mesh, a square for 2D drawing, or anything compose of many triangles but we'll start with just a single triangle because this just repeats for every triangle in the object). And we are going to draw this triangle on the 2D screen even though this triangle is coded as being in 3D space.

Now we created this triangle in some modeling program like 3D Max or Blender. And so all of the coordinates were defined relative to the x,y,z origin in the center of that world. But usually we don't want all objects in our scene to be placed right at the center of our 3D universe. We want to move them around. You might think to do that by changing the points or vertices that define our object. But if you create what's called a "World Matrix", this matrix will contain ALL of the math (including rotation and scaling) necessary to place our triangle, or other object, into the correct place in our 3D world. So, if you apply this world matrix against every point in the triangle, or other object, all of the vertices will then be placed correctly into the 3D world.

So, to say that again, you have a world matrix for the object that is built to contain all the math necessary to place the 3D object somewhere in the world. And by multiplying every vertex of the triangle, or other object, against this matrix the final result is all the vertices placed into the world. Now I'm careful to say world instead of scene because at this point we can't move the camera. With only a world matrix the camera is permenantly fixed in one spot.

That's where the View Matrix comes in. The view matrix contains all of the math to define our camera position in the scene. In actuality this is all relative. What's really happening is the entire scene moves around the camera rather than the camera moving through the entire scene. The camera is actually your computer monitor which never moves. The actual movement is absolutely everything in the scene moving to make it look like the camera moved. This is the job of the view matrix.

The view matrix holds all the math necessary to move the scene so that it feels like the view matrix is controlling your "camera". Again, if you multiply this matrix times a vertex in your triangle, or other object, it places the vertex in the scene so that it feels like you're controlling the camera. Apply this matrix, along with the world matrix, and everything is placed in the scene from the perspective of the camera.

But there's still a problem in that everything is still 3D and you need to draw it on a 2D surface (your computer monitor or more accurately a "back buffer"). This is where the Projection Matrix come in. I often compare it to camera lenses, but its real job is to flatten all the 3D geometry out into a 2D surface for drawing on you computer screen. It's called a "projection" matrix because it basically holds all the math necessary to flatten the scene out into 2D for drawing on the screen.

So, you basically only have one projection matrix that you setup once and then leave alone. And you have a view matrix that you constantly change to change camera position (although in 2D you may never move the camera and so this may go unchanged throughout the game). And you have separate world matrices for every object in the scene to place them in the correct spots (including rotation and scaling and such).

Now you can begin your shader. I've never seen a shader that doesn't need to be fed these three matrices. Typically, the first thing you do, either inside or outside the shader, is to combine these three matrices into one matrix by multiplying them together (multiplying matrices combines them). The shader applies this combined matrix to every vertex in the object to put it in the correct place for drawing.

Now for 2D, you'll likely need a rectangle of two triangle that is sent flat on the X,Y plane with a constant depth of Z=0. An orthogonal projection matrix (I haven't done 2D like this, only 3D and so haven't used orthogonal), I believe, will basically remove any "perspective" for the final drawing to the screen, but will mostly just pass the coordinates straight through to be drawn on the 2D surface. Anything applied at a different Z will still appear as if it is at Z=0 but will be above or below anything actually at Z=0.

So for our triangle, all we've really done is define three vertices that define the triangle. There's no actual triangle, just 3 points. We have to shade in (or create pixels for) the area between the vertices to draw a triangle. Actually, that's done right after the vertex shader. But the vertex shader tells the graphics card how to perform the shading. A simple shader would just apply the combined matrices to each vertex to properly position them and then output a constant color to shade the triangle evenly in one color all the way across.

You can get a bit more fancy here for 3D work and shade so that the surface appears to be curved by taking account of whether each part of the triangle is closer to facing or further from facing a light source. But I can't imagine you would do even that for 2D.

You can also shade using a texture. Instead of applying color, you can steal color from an actual image and basically draw the image across the triangle as shading rather than a single color. You use UV coordinates to "pin" part of the picture to each of the three vertices (or four vertices for a rectangle of two triangles). This tells the shader how to map, or draw, the texture image across the surface of the triangle.

The shader has to have the image/texture passed in as a parameter. And each vertex has to have not only position info, but also UV info to define how the image is supposed to match up across the surface. You can use UV coordinates to distort or draw the image multiple times across the surface, but for most of your 2D work you will likely pin the image to the corners of a rectangle so that all of your UV coordinates are either 1 or 0 with 1,1 being the bottom right of the rectangle and 0,0 being the upper left.

The image gets shaded/drawn across the triangle and is a 2D triangle at this point. It is then passed to the pixel shader where you can do any manipulation on the individual pixels, if you want. I'm not sure you'll do much (or really anything) here for 2D work.

And that's basically how all shading works. DX11 has some additional stages that you might want to use for 3D work, but I don't think you are likely to have to have them for 3D let alone 2D work. You can write your shader code to do some fancy things like turn the surface being shaded across the triangle into a mirror, or liquid water, or make it glow, or something else fancy, but that's all just basically the same as this with just more steps to do fancy stuff.

View PostMr_Fraggs, on 01 July 2013 - 12:55 AM, said:

I apologize for the double post (again!) but my time to edit my previous post ran out.

I've been doing some reading about OpenGL as well, and I did know about it prior, but I was told by a few sources that it wasn't useful for 2D graphics programming. What I have found is that it can do 2D possibly easier than DirectX? Would it be better for a solo programmer to work with OpenGL, as it seems to be the only thing that can compare to DirectX? I don't remember why I have this feeling, but after doing minimal work with SDL, SFML and Allegro, I feel that those three are not as good for a distributed game as OpenGL and DirectX (I know I can always be wrong!).

So I'm looking at OpenGL, wondering if this is the better alternative than to try to over-complicate things in DirectX? Also, cross-platform support seems well worth it, even though I only do PC right now.

If you guys think that OpenGL would be better for a lone 2D dev, may I ask what the options are for dealing with input? Sound is also a concern, but i'm more worried about getting graphics and input up before I worry about spicing my projects up with sounds.

Thanks for the help everyone, I appreciate it more than you guys know :)/>/>


I've been looking at OpenGL myself. It seems like a rough road to me. You have a similar change as DX 9 to DX11 with OpenGL 2.0 to OpenGL 4.0. You could use 2.0, but that's basically the same as using DX9. 4.0 is going to have a lot of the same complications as DX11. I would say that its a major handicap that there are only 2 books I can find published on planet earth on the subject of GL4.0, but there are only 3 books I can find published on planet earth for DX11. And I wasn't particularly impressed with 1 of the 3 books for DX11.

I've never used SDL. So if you have you're way ahead of me. But StayCrisp seems pretty jazzed about it. I got the impression that its very easy to use and has plenty sufficient power for 2D work. It seems to lack 3D ability, but it seems that you can combine OpenGL code with it to do 3D work using SDL. That's where you would get your input and sound and such. I'm going to be looking into it in the near future because it sounds like the way to get started with OpenGL.

No matter what you do, OpenGl contains no ability to handle input, windowing, sound, etc. So, you will have to use it together with something else. So, I'm going to see how well SDL fits that role in the near future.

DirectX has all that built in but is Windows only.

This post has been edited by BBeck: 01 July 2013 - 06:19 AM

Was This Post Helpful? 3
  • +
  • -

#10 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 535
  • View blog
  • Posts: 1,189
  • Joined: 24-April 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 01 July 2013 - 08:15 AM

Quote

haven't gone through many tutorials in depth yet, and I'm still actually trying to figure out how to set up DirectX 11 with Visual Studio 2012 on Windows 7. Last time I used DirectX, I downloaded the SDK, linked some files and that was that.


Opps. Almost forgot.

DX11 is built into the Windows SDK. So, if you download VS2012 you will already have all that installed. There's no additional setup.

Now I have had some problems with it locating some of the files and that may be because I have older versions of the DirectX SDK already installed. There are 3 places I've had to regularly modify to get DX11 code to compile in VS2012.

The first is the two include paths.

Under the VC++ Directories, I've had to modify the Include Directories as well as the Library Directories. I haven't found ANY documentation on what it actually needs to include. But it seems to work when I set it to "Inherit from parent or project defaults" for both Include and Library Directories.

If you figure out the exact requirements on that let me know. I know this is supposed to be helping it find the DX header and .lib files but since that's automatically installed I'm not sure where that is and it seems to point to DX9, which is just confusing. But the Inherit has worked for me.

The other was Linker-->Input-->Additional Dependencies which was all hosed up until I made it this:

dxerr.lib;d3d11.lib;d3dx11.lib;d3dcompiler.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)

That seems ridiculous to me. There's obviously stuff in there that has nothing to do with DX like ODBC. But my code would not compile until I changed it to be that (I copied it from a working project).

I came really close to making a post asking if anyone knows if all this is really necessary. I'm pretty sure kernel32.lib and a few others are absolutely necessary, but I'm not sure what the best way to set all this up is.

However, this is what's working for me until I take time to figure out what's actually needed there and what isn't.

I'm running DX11 compiled through VS2012 on a Windows 7 64 bit machine.

This post has been edited by BBeck: 01 July 2013 - 08:16 AM

Was This Post Helpful? 2
  • +
  • -

#11 Mr_Fraggs  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 85
  • Joined: 17-June 12

Re: DirectX 9 or DirectX 11 for 2D

Posted 01 July 2013 - 01:10 PM

That's a lot of interesting information to take in. I'll have to read over it a few more times, and go to google to find some code to go with it to help solidify it in my mind, but that was way more than I could have asked for on a simple forum post. Thanks so much! You definitely helped me out a lot here, and gave me a much better understanding of how newer DirectX rendering works! Very much appreciated my friend :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1