Thread: Ellipses (that stretch) in DirectDraw

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Ellipses (that stretch) in DirectDraw

    'ello everybody.

    I searched for this topic (assumed someone had to have
    asked it before -- which they did), but still had questions
    about how to go about this. The math, etc..

    So, I'm making a really simple 2D game. Right now I'd like to
    add ellipses underneath the objects on the screen to simulate
    shadows. These ellipses would shrink as the object gets higher.
    You know, Super Mario Bros shadow effects. :P


    btw, I'm very rusty at programming right now. I haven't really
    done anything in a couple years...


    Now, I know I can do this in GDI, but wouldn't that only
    draw the ellipse to the Device Context of the Window
    and not the DDraw Surface I'm using as a back buffer?
    That would mean they wouldn't be in my screenshots...

    Would locking the surface and coloring each pixel black
    be the best way, then?

    Now, here's my real problem: How do I make an ellipse
    that stretches? I mean, math-wise. And if I am able to draw
    the actual ellipse, how would I color it in?
    Staying away from General.

  2. #2
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    i would set the original size of the shadow equals to the width of the characters/objects while they are on the ground. As the height is subtracted/added (depend on the screen perspective you set) from the object's coordinate, you decrease the wide of the ellipse.
    Also, set the X axis of the ellipse relatively to the object's X axis, and the Y axis to the ground's Y axis.
    Last edited by hdragon; 02-26-2006 at 11:20 AM.
    Hello, testing testing. Everthing is running perfectly...for now

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't know how to solve your problem in DX but I do in D3D.

    1. Turn alpha blending on in game.
    2. Create a bitmap in a paint program of a 50% grey circle.
    3. Create a mask for the bitmap which masks out the circle and save to disk. (Circle=white, other=black)
    4. Fire up DX texture tool and load bitmap.
    5. Change surface format to A8R8G8B8.
    6. Select File->Open onto alpha channel of this texture
    7. Select your mask bitmap.
    8. Use blend mode: D3DRS_SRCBLEND,D3DBLEND_SRCALPHA
    9. Use blend mode: D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA
    10. Scale the quad to suit sprite size.
    11. Render the quad using the resulting texture.

    Code:
    #ifndef TLVERTEX
    #define TLVERTEX
    
    #include <d3dx9h>
    
    struct TLVertex
    {
      //Position
      D3DXVECTOR3 Pos;
      //Homogenous W - 1.0f
      float RHW;
    
      //Diffuse color
      D3DCOLOR dwDiffuse;
    
      //Texture coords
      float u,v;
    
      //Flexible Vertex Format
      static const DWORD FVF;
    
      //Various constructors
      TLVertex(void):Pos(D3DXVECTOR3(0.0f,0.0f,0.0f)),RHW(1.0f),dwDiffuse(0),u(0.0f),v(0.0f) {}
      TLVertex(D3DXVECTOR3 _Pos,float _u,float _v,D3DCOLOR _dwDiffuse=0):Pos(_Pos),u(_u),v(_v),dwDiffuse(_dwDiffuse) {}
    
      TLVertex(float x,float y,float z,float _u,float _v,D3DCOLOR _dwDiffuse=0):D3DXVECTOR3(x,y,z),u(_u),v(_v),dwDiffuse(_dwDiffuse) {}
    
      //Assignment operator
      TLVertex &operator=(const TLVertex &vert) 
      {
        Pos=vert.Pos;
        RHW=vert.RHW;
        dwDiffuse=vert.dwDiffuse;
        u=vert.u;
        v=vert.v;
        FVF=vert.FVF;
        return *this;
      }
    };
    #endif
    Code:
    #include "CObject.h"
    #include "TLVertex.h"
    
    //Define FVF for TLVertex
    const DWORD TLVertex::FVF=D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;
    
    void CObject::Create(IDirect3DDevice9 *pDevice,D3DXVECTOR3 Pos,float fXSize,float fYSize)
    {
      //Init class vars
      m_pDevice=pDevice;
      m_fXSize=fXSize;
      m_fYSize=fYSize;
      m_vPos=Pos;
      
      //Create vertices
      m_pVerts[0]=TLVertex(0.0f,0.0f,1.0f,0.0f,0.0f);
      m_pVerts[1]=TLVertex(1.0f,0.0f,1.0f,1.0f,0.0f);
      m_pVerts[2]=TLVertex(0.0f,1.0f,1.0f,0.0f,1.0f);
      m_pVerts[3]=TLVertex(1.0f,1.0f,1.0f,1.0f,1.0f);
    }
    
    void CObject::LoadTexture(std::string File)
    {
      //Load and create texture
      D3DXCreateTextureFromFile(m_pDevice,File.c_str(),&m_pTexture);
    }
    
    void CObject::PreRender(float fTimeDelta)
    {
      //Set source and dest blends
      m_pDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
      m_pDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
    }
    
    void CObject::Render(float fTimeDelta)
    {
      //Setup scaling matrix
      D3DXMATRIX matScale;
      D3DXMatrixScaling(&matScale,m_fSize.x,m_fSize.y,0.0f);
    
      //Setup translation matrix
      D3DXMATRIX matTrans;
      D3DXMatrixTranslation(&matTrans,m_vPos.x,m_vPos.y,1.0f);
        
      TLVertex Verts[4];
    
      for (int i=0;i<4;i++)
      {
         //Copy source verts
         Verts[i]=m_pVerts[i];
    
         //Transform verts - scale and then translate
         D3DXVec3TransformCoord(&Verts[i],&matScale,m_pVerts[i]);
         Verts[i]*=matTrans;
      }
    
      //Set FVF
      m_pDevice->SetFVF(TLVertex::FVF);
    
      //Set Texture
      m_pDevice->SetTexture(m_pTexture);
    
      //Draw
      m_pDevice->DrawPrimitive(D3PT_TRIANGLESTRIP,2,&Verts,sizeof(TLVERTEX));
    }
    Last edited by VirtualAce; 02-26-2006 at 04:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. D3D vs DirectDraw
    By Vicious in forum Game Programming
    Replies: 13
    Last Post: 10-08-2004, 03:13 PM
  2. Replies: 3
    Last Post: 08-14-2003, 01:37 PM
  3. Windowed DirectDraw problems
    By Magos in forum Windows Programming
    Replies: 0
    Last Post: 01-20-2003, 01:00 PM
  4. Drawing lines and ellipses with DirectDraw
    By Hunter2 in forum Game Programming
    Replies: 9
    Last Post: 01-08-2003, 01:25 PM
  5. DirectDraw Tutorials
    By Robert602 in forum Game Programming
    Replies: 3
    Last Post: 12-14-2001, 10:29 AM