XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


Ads

Clearing the screen using DirectX

Last chapter, we created a link between your hardware and the software. Now, we’re actually going to use DirectX to clear the contents of your window to a color of choice. Every iteration of our game loop, we are going to add a call to a method DrawScene, which we will define here and will redraw our window using DirectX.

Every time you redraw a scene to your window, you first have to clear it. Then let the device know you are going to draw the scene by calling the BeginScene method of it. After defining the scene, place a call to EndScene and a call to Present to actually present the buffered new screen. This is done be the code below:

 void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device)
 {
     p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
     p_dx_Device->BeginScene();
 
     p_dx_Device->EndScene();
     p_dx_Device->Present(NULL, NULL, NULL, NULL);
 }

The first line shows that our new method will need a pointer to the device passed to it from the calling function, and that it will not return a value (void). The Clear method can actually clear some specific parts of the screen, specified by a number of rectangles. This number of rectangles is the first parameter, the second is a pointer to the list of rectangles. Because we are going to clear the whole window, we define 0 and NULL. Then we define to what color the window should be cleared and at what distance this layer should be, where 1f means infinity. The last pixel clears the stencil buffer, which is more advanced and is used to mask certain pixels in the screen.

The first, second and last parameters of the Present method are too advanced for now, the third parameter is a handle to the window to draw to, or NULL to draw to the window specified in the Present_Parameters structure.

All we need to do now is add a call to our new method from within the while loop:

 DrawScene(p_Device);

Okay, that should give you a window, cleared by DirectX to the color of your choice. Not too exciting yet, but hey…




DirectX Tutorial 4 - Clearing your window

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!

Or click on one of the topics on this chapter to go there:
  • Problem with clearing
          Hello I wrote whole program in VC 6.0. I have com...



    This is the code you should have up to this point:


    #include<windows.h>
    #include<d3d9.h>
    #include<d3dx9.h>
    int int_AppRunning = 1;

    LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2)
    {
        switch(uint_Message)
        {
            case WM_KEYDOWN:
            {
                int_AppRunning = 0;
                break;
            }
            break;
        }

        return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);
    }

    HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height)
    {
        WNDCLASSEX wnd_Structure;

        wnd_Structure.cbSize = sizeof(WNDCLASSEX);
        wnd_Structure.style = CS_HREDRAW | CS_VREDRAW;
        wnd_Structure.lpfnWndProc = OurWindowProcedure;
        wnd_Structure.cbClsExtra = 0;
        wnd_Structure.cbWndExtra = 0;
        wnd_Structure.hInstance = GetModuleHandle(NULL);
        wnd_Structure.hIcon = NULL;
        wnd_Structure.hCursor = NULL;
        wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
        wnd_Structure.lpszMenuName = NULL;
        wnd_Structure.lpszClassName = "WindowClassName";
        wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

        RegisterClassEx(&wnd_Structure);

        return CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", str_Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, int_XPos, int_YPos, int_Width, int_Height, NULL, NULL, GetModuleHandle(NULL), NULL);
    }

    LPDIRECT3DDEVICE9 InitializeDevice(HWND han_WindowToBindTo)
    {
        LPDIRECT3D9 p_dx_Object;
        LPDIRECT3DDEVICE9 p_dx_Device;

        p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION);
        if (p_dx_Object == NULL)
        {
            MessageBox(han_WindowToBindTo,"DirectX Runtime library not installed!","InitializeDevice()",MB_OK);
        }

        D3DPRESENT_PARAMETERS dx_PresParams;
    ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );
    dx_PresParams.Windowed = TRUE;
    dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN;

    if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, han_WindowToBindTo, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
    {
            if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, han_WindowToBindTo, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))
            {
                MessageBox(han_WindowToBindTo,"Failed to create even the reference device!","InitializeDevice()",MB_OK);
            }
    }

        return p_dx_Device;
    }


     void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device)
     {
         p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
         p_dx_Device->BeginScene();
     
         p_dx_Device->EndScene();
         p_dx_Device->Present(NULL, NULL, NULL, NULL);
     }
     
     int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
     {
         MSG msg_Message;
     
         HWND han_Window = NewWindow("DirectX C++ Tutorial",100,100,500,500);
         LPDIRECT3DDEVICE9 p_Device = InitializeDevice(han_Window);
     
         while(int_AppRunning)
         {
             if(PeekMessage(&msg_Message,han_Window,0,0,PM_REMOVE))
             {
                 DispatchMessage(&msg_Message);
             }
             DrawScene(p_Device);
         }
     
         p_Device->Release();
         DestroyWindow(han_Window);
     
         return 0;
     }


    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2011 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2011 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Tutorials (160)
    XNA 4.0 using C# (89)
    DirectX using C# (54)
    DirectX using C++ (15)
    Series 1: Terrain (15)
    Opening a window
    Ending the game loop
    Linking to the Device
    Clearing your window
    Drawing a triangle
    Culling
    Camera
    Rotation - Translation
    Indices
    Terrain creation
    Terrain from file
    DirectInput
    Importing .bmp files
    Adding colors
    DirectX Light basics
    DirectX using VB (2)
    -- Expand all --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!