Displaying Static Background with DirectX9 Example?

Can someone help this poor DirectX newbie by pointing me to an example of how to display a static background from an image file using C++ (or C#). I've read that the way to do this is to create two triangles from vertices, load the image into a texture and map the texture to the surface. This sounds great but I can't figure out how to do it from the DirectX docs.

Currently, I'm trying to do it by loading the image onto an offscreen surface and using UpdateSurface() to copy it to the back buffer. This works great, except when I switch to another application or point down at the task bar the background disappears! So I'm trying to do it another way hoping this will work around the problem.

I can supply simple source code to show the problem I'm having. Thanks.

Paul Gauthier
Software Developer
New Boston, NH


Answer this question

Displaying Static Background with DirectX9 Example?

  • amontania

    PaulGauthier wrote:
    Currently, I'm trying to do it by loading the image onto an offscreen surface and using UpdateSurface() to copy it to the back buffer. This works great, except when I switch to another application or point down at the task bar the background disappears!

    Most DirectX applications are constantly rendering one frame after another. When something happens that causes the application's display to be erased it's quickly "restored" when the application draws the next frame. If you're not doing this then you need to redraw your background when necessary. The Direct3D tutorial in the DirectX SDK documentation explains how to write a program like this.


  • sonofseven

    PaulGauthier wrote:
    However, when I move the pointer to the taskbar, the window continues to be refreshed but the background has gone away, leaving the basic color that the backbuffer was set to and any meshes that I'm displaying.

    Might be a problem with how you're handling the device lost condition. Is your offscreen surface in D3DPOOL_DEFAULT or D3DPOOL_SYSTEMMEM Are you calling GetBackBuffer() every frame, or just once and saving the result

    Using a texture and DrawPrimitive() or ID3DXSprite, is the normal way to do this. Creating the texture shouldn't be a problem, you can do it pretty much the same way you're creating your offscreen surface. One difference is that you should use D3DPOOL_MANAGED instead. You can then either use ID3DXSprite to draw it, or you can create a simple "mesh", a vertex buffer, with two triangles that make up a rectangle and draw that using DrawPrimitive(). Read the section on Directly Mapping Texels to Pixels in DirectX SDK documention so your background gets drawn correctly.


  • Oliwa

    Etienne,

    Thanks for the pointer to the very useful tutorial. It provided what I needed to display the static background without using UpdateSurface(), thereby getting around the problem of the disappearing background.

    Thanks for the help.

    Paul G.

  • Roboteux

    What you need is an orthogonal projection...that will get you a 2D drawing
    You can then put the normal Perspective projection and do the 3D over it
    Setup orthogonal projection (2D)
    Draw your vertexbuffer primitive while your texture is set on the device
    Set up the perspective projection (3D)
    Draw your 3D here
    Here is some web site that show you how to set up all this...
    void CGame::Setup2DCamera()
    {
    D3DXMATRIX matOrtho;
    D3DXMATRIX matIdentity;

    //Setup the orthogonal projection matrix and the default world/view matrix
    D3DXMatrixOrthoLH(&matOrtho, (float)m_nScreenWidth, (float)m_nScreenHeight, 0.0f, 1.0f);
    D3DXMatrixIdentity(&matIdentity);

    m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matOrtho);
    m_pD3DDevice->SetTransform(D3DTS_WORLD, &matIdentity);
    m_pD3DDevice->SetTransform(D3DTS_VIEW, &matIdentity);

    //Make sure that the z-buffer and lighting are disabled
    m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
    m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    }

    http://www.toymaker.info/Games/index.html

    http://www.toymaker.info/Games/html/primitives.html

    http://www.toymaker.info/Games/html/textures.html

    http://www.pieterg.com/Tutorials.php


  • Akash tharakan

    Ross, Thanks for the reply. I constantly render in a loop, including the call to UpdateSurface() so that the window get's refreshed on a continual basis. I can move the window around or cover it up and when it's activated it shows the background image just fine. However, when I move the pointer to the taskbar, the window continues to be refreshed but the background has gone away, leaving the basic color that the backbuffer was set to and any meshes that I'm displaying. Paul G.

  • Displaying Static Background with DirectX9 Example?