Thread: D3D Windowed Application

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    D3D Windowed Application

    I'm trying to setup a D3D windowed app (to draw 2D-sprites, but still). Generally it works ok, you can draw images n stuff.
    However, I get an annoying frame with random pixels around the drawing area. What causes this?
    By random pixels, I mean it shows what was rendered at that point before (like when you move the window those parts aren'tupdated). That frame is about 1 pixel wide.

    This is how I set things up. I thought it looked ok:
    Code:
    //Creates a Direct3D object
    Direct3D = Direct3DCreate9(D3D_SDK_VERSION);
    
    //Aborts if Direct3D creation failed
    if(Direct3D == NULL)
    {
    	return FALSE;
    }
    
    //Sets the present parameters
    ZeroMemory(&PresentParameters, sizeof(D3DPRESENT_PARAMETERS));
    PresentParameters.BackBufferWidth = 0;
    PresentParameters.BackBufferHeight = 0;
    PresentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
    PresentParameters.BackBufferCount = 1;
    PresentParameters.SwapEffect = D3DSWAPEFFECT_COPY;
    PresentParameters.Windowed = TRUE;
    PresentParameters.EnableAutoDepthStencil = TRUE;
    PresentParameters.AutoDepthStencilFormat = D3DFMT_D16;
    PresentParameters.hDeviceWindow = WindowHandle;
    PresentParameters.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    
    //Creates a Direct3D device object
    if(FAILED(Direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, TargetWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParameters, &Direct3DDevice)))
    {
    	return FALSE;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Another related question. When creating a window and setting its size to, say 800x600 it seems it is the outer dimension of the window. The actual region where things are drawn will be smaller. Is there any good way of setting the inner dimension to exactly that resolution?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay for your first post:

    Try setting your swap effect to D3DSWAPEFFECT_DISCARD. Take out the fullscreen presentation rate because you are windowed. And also take out the D3DFMT_UNKNOWN for the back buffer. Make a call to GetAdapterDisplayMode and use the Format member of the struct that gets filled in from that method to set the back buffer format.

    Second post:

    When you specify width and height you are indeed talking about the entire window and not the client area. What you want to do is call AdjustWindowRect or AdjustWindowRectEx (depending on which CreateWindow function you called ). This function takes a rectangle with the deminsions you want the client area to have and the styles of the window and changes the rect to be accurate. Then when you call CreateWindow you pass in rect.Right - rect.Left for the width and rect.Bottom - rect.Top for height.

    Cheers
    "...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
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    According to the DX docs you can have D3DFMT_UNKNOWN instead of calling the GetDisplayMode:
    In fact, D3DFMT_UNKNOWN can be specified for the BackBufferFormat while in windowed mode. This tells the runtime to use the current display-mode format and eliminates the need to call IDirect3DDevice9::GetDisplayMode.
    I already tried different swap effects, but none of them solved the problem.

    I'll check out the AdjustWindow function. Thanks!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I used the AdjustWindowRectEx (with the same flags as the window was created with) and used the rect I got in SetWindowPos to update the width and height.

    EDIT: It works ok now! I used the wrong flags ^^.

    As for my first problem, I'm attaching this screenie of how it looks like. The red backgorund color is rendered through the clear() method, so it's not me drawing at the wrong places.
    Last edited by Magos; 03-13-2004 at 07:14 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay, didn't know that about the unknown format. Anyways, I'm not sure why it wouldn't be clearing the entire window unless you manually set the viewport wrong. You can attach your project, if you feel comfortable with that, and I will look at it. Or you can e-mail me it also, if you prefer.
    "...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

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A question about the AdjustWindowRectEx. It seems like scrollbars (WS_HSCROLL and WS_VSCROLL) aren't taken into account when calculating the size.
    And yes, I do pass those flags into the function call too.

    I'd prefer not post my entire graphics class, but the setup looks like this (and I think it is the setup where the problem lies).
    Apart from the stupid pixels around the frame, it works fine.

    EDIT: Weird, the problem has disappeared. Must've touched some unknown setting. Too bad, I'd like to know what caused it. Oh well!
    Last edited by Magos; 03-14-2004 at 02:47 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  2. Replies: 0
    Last Post: 05-23-2005, 11:39 AM
  3. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 04:33 AM
  4. Adding a skin to a windowed application
    By BruceLeroy in forum Windows Programming
    Replies: 1
    Last Post: 08-24-2004, 03:51 PM