• ### Popular Now

• 10
• 9
• 12
• 14
• 14

#### Archived

This topic is now archived and is closed to further replies.

# 2d engine

This topic is 5777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

sorry, I make a lot of posts, but I have a quick question. I''ve decided to write a 2d engine in Direct3d. It will support sprites that have multiple frames. Now, this is very similiar to my DirectDraw 2d engine. The sprite contains 1 (or in this case maybe more than 1 because of texture size) image that has all of the frames in a table of cels. Then the program extracts the frame that is supposed to be used at that time. Now, how I was planning on doing this in Direct3D was to divide the x by the total x, and the y by the total y to get the tu and tv coordinates for the vertices. so, every time the sprite animates, I have to update the vertex buffer. Is that the wisest idea? that seems like it would go pretty slow changing the vertex buffer each time... BTW--Take a look at the new layout of SpongeFactory''s site! Sponge Factory --Muzlack

##### Share on other sites
hmm... quick question.. I''ve never run into this problem, because I must have just been lucky, but what order do the vertices have to be in in drawprimitive? I figured out that it has to go from 0,0, to width,0 to 0,height. But why won''t it work if it is 0,0 to 0,height to width,0? How will I know in the future the order these vertices have to be in?

##### Share on other sites
one more quick question.. I can''t even use matrices in my application! I know how to use them in a 3d application, and I set it up right, but when I DrawPrimitive, it is still at 0,0... here''s my code:

  	D3DXMatrixTranslation(&move,(float)x,(float)y,0.0f);	D3DDevice->SetTransform(D3DTS_WORLD, &move);

##### Share on other sites
VERTICES MUST BE IN CLOCKWISE ORDER.

##### Share on other sites
You don''t need to apologise before asking a question, as long as you''ve looked for the answer on your own first.

Now, your first question. As far as I know there are basically two ways to write a 2D engine in D3D.
In one way, you have a static vertex buffer which contains only 4 vertices (usually), and you use matrices to move the rectangle around, and texture matrices to pick which part of the selected texture to display. This has the advantage that you never have to lock the vertex buffer once you''ve made it. But it has the large disadvantage that you can only draw a single quad in each DrawPrimitive call.
The other way is to have a dynamic vertex buffer which you lock each frame and fill with the vertex information for most/all of the quads displayed that frame. You then cycle through the textures used that frame and display (ideally) a very large number of quads with each DrawPrimitive call.

Which one is faster depends on the hardware and your implementation, but there shouldn''t really be too much difference between them if implemented well.

Now, your second question. Direct3D implements a form of culling (ignoring triangles that aren''t visible) called Backface culling, because it ignores triangles which are facing the wrong way, ie you''re looking at the back of them. It works on the principal that if you turn a triangle around, so that you''re looking at the other side, the vertices will be ordered differently. For example, if your triangle''s vertices are ordered clockwise from the front, when you turn the triangle around, the vertices will now be ordered counter-clockwise.
Anyway, Direct3D lets you decide which order you want your vertices to be in, or if you want to turn backface culling off completely (ie draw triangles whichever way they''re facing). In a 2D application you would usually want culling turned off. To do this, call:
p_device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

Finally, your third question. In Direct3D you can specify vertices as either "Transformed" or "Untransformed". Untransformed vertices will be affected by matrices, but transformed vertices will not. If your vertices have "RHW" coordinates in them (ie, the vertex FVF includes D3DFVF_XYZRHW), then the vertex is transformed. If the vertex FVF is specified with D3DFVF_XYZ (no RHW coordinates), then the vertices are untransformed.
I''m assuming you''ve already used untransformed vertices, so I''ll ignore those, but to use transformed vertices, you would usually set the RHW coordinate of the vertices to 1.0f, and use the X and Y coordinates of your vertices as screen/pixel coordinates.

Hope that helps,

John B