Thread: D3D vs DirectDraw

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    D3D vs DirectDraw

    I know there are scattered answers to this question but they are all different. So I was hoping that I could get a good solid answer here.

    If I have a program that has purely pong style graphics.. very primitive.. would DirectDraw have any advantages/disadvantages? or just go ahead and use D3D primitaves? And simple 2d games DirectDraw? or D3D?

    I have DirectX 9 SDK and I know that there hasnt been any updated versions of directdraw since 7 (?). And alot of ddraw tutorials say that the example might not run with DX SDK 9.

    So what are the definitive answers here

    (Bubba?)
    What is C++?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Don't use DirectDraw interfaces. Write directly to the back buffer if you want to do 2D graphics. The blitter and all of that is still supported....but your video driver might not do it nearly as well as the old one's used to. New video drivers are optimized for current graphics processing needs which boils down to 3D math and 3D graphics. The best way to do it is to use Direct3D with TLVertex(es) and let D3D texture them for you. This also allows you to rotate your bitmaps to any angle since they are just textured quads.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Look into the ID3DXSprite interface. That will make your life pretty simple.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Thank you, I will have to look into these things, as I have just started using DirectX tonight.

    Thanks Again

    [edit]
    Trying to use trianglestrips.. but running into some problems.

    Here is the coordinates for the strip.

    Code:
    DxVertex trianglestrip[] = {
    
        { 160.0f, 100.0f, 1.0f, 1.0f, 0xFFFFFFFF },
        { 480.0f, 100.0f, 1.0f, 1.0f, 0xFFFFFFFF },
        { 160.0f, 360.0f, 1.0f, 1.0f, 0xFFFFFFFF },
        { 480.0f, 360.0f, 1.0f, 1.0f, 0xFFFFFFFF },
    
    };
    its { x, y, z, w, color }

    And what I get is only the first three vertecies show up. Shouldnt the last vertex create a "square"?

    I pass it like this
    Code:
    gD3D->Render ( trianglestrip, 4 );
    Here is the Render definition
    Code:
    bool D3Dclass::Render ( DxVertex *VertList, int num_verts )
    {
    
    	int vert_list_size = sizeof ( DxVertex ) * num_verts;
    	assert ( vert_list_size > 0 );
    
    	IDirect3DVertexBuffer9 *VertexBuffer;
    
    	HRESULT result = D3DDevice->CreateVertexBuffer ( vert_list_size, 0, DxVertexType, D3DPOOL_DEFAULT, &VertexBuffer, NULL );
    
    	if ( result != D3D_OK ) return false;
    
    	void *verts = NULL;
    
    	result = VertexBuffer->Lock ( 0, 0, ( void** ) &verts, D3DLOCK_DISCARD );
    
    	if ( result != D3D_OK ) {
    
    		VertexBuffer->Release ();
    		return false;
    
    	}
    
    	memcpy ( verts, VertList, vert_list_size );
    
    	VertexBuffer->Unlock ();
    
    	D3DDevice->SetStreamSource ( 0, VertexBuffer, 0, sizeof ( DxVertex ) );
    	D3DDevice->SetFVF ( DxVertexType );
    
    	D3DDevice->DrawPrimitive ( D3DPT_TRIANGLESTRIP, 0, 1 );
    
    	VertexBuffer->Release ();
    
    	return true;
    
    }
    Last edited by Vicious; 09-21-2004 at 11:56 PM.
    What is C++?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    D3DDevice->DrawPrimitive ( D3DPT_TRIANGLESTRIP, 0, 1 );

    The last parameter is the number of primitives to render. So you probably want 2 triangles instead of 1 to render if you are going for a square.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ah thank you.
    Last edited by Vicious; 09-22-2004 at 10:45 PM.

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ok I need a little help.

    I am trying to make a function return a struct. But it doesnt display.

    Code:
    DxVertex *DrawSquare ( float vx, float vy, float width )
    {
    
    	DxVertex temp [] = {
    
    		{ vx,         vy,         1.0f, 1.0f, 0xFFFFFFFF },
    		{ vx + width, vy,         1.0f, 1.0f, 0xFFFFFFFF },
    		{ vx,         vy + width, 1.0f, 1.0f, 0xFFFFFFFF },
    		{ vx + width, vy + width, 1.0f, 1.0f, 0xFFFFFFFF },
    
    	};
    
    	return temp;
    
    }
    and Here is where I call it

    Code:
    gD3D->Render ( DrawSquare ( 50.0f, 10.0f, 50.0f ), 4 );
    The rest of the code is above.

    [note] I also get this warning
    C:\Program Files\Microsoft Visual Studio\MyProjects\DXtest\main.cpp(21) : warning C4172: returning address of local variable or temporary
    Last edited by Vicious; 09-22-2004 at 11:28 PM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I think that is because the object you are returning is actually destroyed once you return from the function, it has local scope.

  9. #9
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ah right, didnt even think about that.

    I'll play around with it, might be back for some help

    [note]
    Since alot of you have been using direct-x for a while, how would you go about this?
    What is C++?

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Create the square in a class setup function. Then render the sphere through the class render function.

    Square.h
    Code:
     
    struct SquareVertex
    {
      float x,y,z;
      float nx,ny,nz;
      float u,v;
     
      static const DWORD FVF;
    };
     
    class Square
    {
      IDirect3DDevice9 *Device;
      IDirect3DVertexBuffer9 *VertexBuffer;
      IDirect3DIndexBuffer9 *IndexBuffer;
      IDirect3DTexture9 *Texture;  
      D3DXVECTOR3 Position;
      D3DXVECTOR3 Rotation;
     
      int NumFaces;
      int NumVertexes;
      void Setup(std:string TextureFile);
      public:
    	  Square:Device(0),VertexBuffer(0),IndexBuffer(0),NumFaces(0),NumVertexes(0) {}
    	  ~Square(void)
    	  {
    	   VertexBuffer->Release();
    	   IndexBuffer->Release();
    	   Texture->Release();
    	  }
    	  void Init(IDirect3DDevice9 *TDevice,std::string TextureFile) 
    	  {
    	   Device=TDevice;
    	   Setup(TextureFile);
    	  }
    	  void Render(D3DXVECTOR3 &CameraPos);
    };
    Square.cpp
    Code:
     
    //FVF format
    const DWORD FVF=D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX;
     
    void Square::Setup(void)
    {
      //Setup index buffer, vertex buffer, and texture coords, etc. here
      //Setup NumVertices and NumTris value as well
      //NumTris will be NumFaces*2
      //I leave this as an exercise for you to complete
     
      //Load a texture for this object
      D3DCreateTextureFromFile(Device,TextureFile,&Texture);
    }
     
    void Square::Render(D3DXVECTOR3 &CameraPos)
    {
    	//Translate with camera
    	//Otherwise you could use identity matrix
    	D3DXMATRIX Translation;
    	D3DXMatrixTranslation(&Translation,CamerPos.x,CameraPos.y,CameraPos.z);
     
    	//Set world transform to translation matrix
    	Device->SetTransform(D3DTS_WORLD,&Translation);
     
    	//Setup any needed device states here -
     
    	//Set texture for this texture stage - no blending here
    	Device->SetTexture(0,Texture);
     
    	//Set stream source, index source, and fvf format - then render object
    	Device->SetStreamSource(0,VertexBuffer,0,sizeof(SquareVertex));
    	Device->SetIndices(IndexBuffer);
    	Device->SetFVF(SquareVertex::FVF);
    	Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,NumVertices,0,NumTris);
    }
    Although this code is not complete I think it gives a good example of how to setup your class structure to hold the vertex data. It is for Direct3D 9.0, but you should be able to port it to DirectX, although I would recommend buying a book on Direct3D 9.0 and dump DirectDraw or whatever you are using.

  11. #11
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Thank you very much for the code!

    And I actually am using Direct3D 9.0, and a book sounds like a good idea. I will look into it.
    What is C++?

  12. #12
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Um would this work?

    Code:
    else {
    
        gD3D->Begin ();
        gD3D->ClearColor ( 0xFF000000 );
    
        for ( float x = 0; x < 64.0f; x++ ) {
    
            gD3D->DrawSquare ( x, 240.0f );
    
        }
    					
        gD3D->End ();
    
    }
    Here is definition of DrawSquare

    Code:
    void D3Dclass::DrawSquare ( float posx, float posy, float width, float height )
    {
    
        DxVertex temp_square [] = {
    
            {   posx * width          ,   posy * height           , 1.0f, 1.0f, 0xFFFFFFFF },
    	{ ( posx * width ) + width,   posy * height           , 1.0f, 1.0f, 0xFFFFFFFF },
    	{   posx * width          , ( posy * height ) + height, 1.0f, 1.0f, 0xFFFFFFFF },
    	{ ( posx * width ) + width, ( posy * height ) + height, 1.0f, 1.0f, 0xFFFFFFFF },
    
        };
    
        Render ( temp_square, 4 );
    
    }
    Would something like that be ok?

    EDIT:
    I didnt ignore your post bubba, I just dont have a very good grasp on DirectX right now so Im keeping it as simple as possible. If the above method is terribly inefficient I will work on your method.

    I know for a larger project this would probably be bad, but I am probably going to use this to make a Chip8 emulator.

    EDIT2:
    Ah this method is EXTREMELY slow. Back to the drawing board.
    Last edited by Vicious; 09-26-2004 at 10:16 PM.
    What is C++?

  13. #13
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    I did a test at drawing bitmaps, with DirectDraw I could draw over 1,000,000 32*32 bitmaps each loop(with considerable slowdown). The same test with the D3D9 caused the computer to crash, I then tried drawing 10,000 and the speed was about the same as what DirectDraw was doing with 1,000,000.

  14. #14
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Bitmaps are drawn in D3D as textured quads. Since most cards can render over 3 million triangles per second that is about 1.5 millions quads in one second.

    The math doesn't add up to what you are claiming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-23-2005, 11:39 AM
  2. DirectDraw Newbie
    By Scorpion-ice in forum Game Programming
    Replies: 0
    Last Post: 10-13-2003, 02:00 PM
  3. Replies: 3
    Last Post: 08-14-2003, 01:37 PM
  4. Windowed DirectDraw problems
    By Magos in forum Windows Programming
    Replies: 0
    Last Post: 01-20-2003, 01:00 PM
  5. DirectDraw Tutorials
    By Robert602 in forum Game Programming
    Replies: 3
    Last Post: 12-14-2001, 10:29 AM