XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Direct3D problem under Windows Vista

Last post 04-19-2008 12:23 AM by michel. 2 replies.
  • 04-18-2008 12:37 AM

    Direct3D problem under Windows Vista

    Can anybody tell me why my the following code listing will display correctly in WinXP but NOT Windows Vista?

    // TileApp.cpp

    #include <windows.h>
    #include <d3d9.h>
    #include <d3dx9tex.h>

    // Constants.
    const int COLUMNSINTILEFILE = 2;
    const int TILESIZE = 32;
    const int MAPCOLUMNCOUNT = 6;
    const int XOFFSET = 220;
    const int YOFFSET = 120;

    // Function prototypes.
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
    BOOL InitDirect3D();
    void Render();
    void CleanUpDirect3D();
    void PlaceTile(IDirect3DSurface9* pBackBuffer, IDirect3DSurface9* pTileSurface,
           int TileNumber, int DstCol, int DstRow);
    int ColumnRow2Sector(int c, int r);
    int TileNumber2SourceX(int TileNumber);
    int TileNumber2SourceY(int TileNumber);
    int Column2X(int col);
    int Row2Y(int r);

    // Global variables.
    HWND g_hWnd;
    IDirect3D9* g_pDirect3D = NULL;
    IDirect3DDevice9* g_pDirect3DDevice = NULL;
    IDirect3DSurface9* g_pBitmapSurface = NULL;
    HRESULT g_hResult = D3D_OK;
    IDirect3DSurface9* g_pBackBuffer = NULL;
    char g_Sectors[] =
    {0,0,0,0,0,0,
    0,2,2,2,2,0,
    0,2,1,1,2,0,
    0,2,1,1,2,0,
    0,2,0,2,2,0,
    0,0,0,0,0,3};

    // WinMain()
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
    {
      WNDCLASS wc = {CS_CLASSDC, WndProc, 0, 0, hInstance, NULL, NULL, NULL,
        NULL, "TileApp"};
    if (!RegisterClass(&wc)) return FALSE;
    g_hWnd = CreateWindow("TileApp", "Tile Application", WS_OVERLAPPEDWINDOW,
        100, 100, 648, 514, GetDesktopWindow(), NULL, hInstance, NULL);
      if (g_hWnd == NULL) return FALSE;
    if(InitDirect3D())
      {
       ShowWindow(g_hWnd, SW_SHOWDEFAULT);
        UpdateWindow(g_hWnd);
       MSG msg;
        ZeroMemory(&msg, sizeof(MSG));
       while(msg.message != WM_QUIT)
        {
          if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
       {
         TranslateMessage(&msg);
           DispatchMessage(&msg);
          }
       else
         Render();
        }
       CleanUpDirect3D();
    }
      UnregisterClass("TileApp", hInstance);
    return 0;
    }

    // WndProc()
    LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    case WM_PAINT:
    ValidateRect(g_hWnd, NULL);
    return 0;
    case WM_KEYDOWN:
    switch(wParam)
    {
    case VK_ESCAPE:
    PostQuitMessage(WM_QUIT);
    break;
    }
      default:
       return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    }

    // InitDirect3D()
    BOOL InitDirect3D()
    {
    g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
    if (g_pDirect3D == NULL) return FALSE;
    HRESULT hResult = g_pDirect3D->CheckDeviceType(D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, TRUE);
    if (hResult != D3D_OK) return FALSE;
    D3DPRESENT_PARAMETERS D3DPresentParams;
    ZeroMemory(&D3DPresentParams, sizeof(D3DPRESENT_PARAMETERS));
    D3DPresentParams.Windowed = TRUE;
    D3DPresentParams.BackBufferCount = 1;
    D3DPresentParams.BackBufferWidth = 640;
    D3DPresentParams.BackBufferHeight = 480;
    D3DPresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
    D3DPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    D3DPresentParams.hDeviceWindow = g_hWnd;
    hResult = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
        g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPresentParams, &g_pDirect3DDevice);
    if (hResult != D3D_OK) return FALSE;
    g_hResult = g_pDirect3DDevice->CreateOffscreenPlainSurface(64, 64,
        D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &g_pBitmapSurface, NULL);
    if (hResult != D3D_OK) return FALSE;
    g_hResult = D3DXLoadSurfaceFromFile(g_pBitmapSurface, NULL, NULL,
        "image.bmp", NULL, D3DX_DEFAULT, 0, NULL);
    if (FAILED(g_hResult)) return FALSE;
    g_hResult = g_pDirect3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO,
        &g_pBackBuffer);
    if (FAILED(g_hResult)) return FALSE;
    return TRUE;
    }

    // CleanUpDirect3D()
    void CleanUpDirect3D()
    {
    if (g_pBackBuffer) g_pBackBuffer->Release();
    if (g_pBitmapSurface) g_pBitmapSurface->Release();
    if (g_pDirect3DDevice) g_pDirect3DDevice->Release();
    if (g_pDirect3D) g_pDirect3D->Release();
    }

    // Render()
    void Render()
    {
    g_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0),
        1.0f, 0);
    for (int Row = 0; Row < MAPCOLUMNCOUNT; ++Row)
    {
    for (int Col = 0; Col < MAPCOLUMNCOUNT; ++Col)
    {
    int iSector = ColumnRow2Sector(Col, Row);
    int TileNumber = g_Sectors[iSector];
    PlaceTile(g_pBackBuffer, g_pBitmapSurface, TileNumber, Col, Row);
    }
    }
    g_pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
    }

    // PlaceTile()
    void PlaceTile(IDirect3DSurface9* pBackBuffer, IDirect3DSurface9* pTileSurface,
           int TileNumber, int DstCol, int DstRow)
    {
    RECT SrcTileRect;
    POINT DstPoint;
    SrcTileRect.left = TileNumber2SourceX(TileNumber);
    SrcTileRect.right = SrcTileRect.left + TILESIZE;
    SrcTileRect.top = TileNumber2SourceY(TileNumber);
    SrcTileRect.bottom = SrcTileRect.top + TILESIZE;
    DstPoint.x = Column2X(DstCol) + XOFFSET;
    DstPoint.y = Row2Y(DstRow) + YOFFSET;
    HRESULT hResult = g_pDirect3DDevice->UpdateSurface(g_pBitmapSurface,
        &SrcTileRect, pBackBuffer, &DstPoint);
    }

    // ColumnRow2Sector()
    int ColumnRow2Sector(int c, int r)
    {
    return r * MAPCOLUMNCOUNT + c;
    }

    // TileNumber2SourceX()
    int TileNumber2SourceX(int TileNumber)
    {
    return (TileNumber - ((int)(TileNumber / COLUMNSINTILEFILE))
        * COLUMNSINTILEFILE) * TILESIZE;
    }

    // TileNumber2SourceY()
    int TileNumber2SourceY(int TileNumber)
    {
    return ((int)(TileNumber / COLUMNSINTILEFILE)) * TILESIZE;
    }

    // Column2X()
    int Column2X(int col)
    {
    return (col - ((int)((col / MAPCOLUMNCOUNT)) * MAPCOLUMNCOUNT)) * TILESIZE;
    }

    // Row2Y()
    int Row2Y(int r)
    {
    return r * TILESIZE;
    }

    The image.bmp file is a file with 4 32x32 pixel bitmaps arranged in a square format.

  • 04-18-2008 1:21 PM In reply to

    Re: Direct3D problem under Windows Vista

    We are not psychic. When you say "display correctly," does that mean display at all, display corrupted, display backwards, ... ?
    Screen shots (not too big) would help. And  better description of what you are expecting, and what you are seeing.
    Also, you should use CComPtr<> to manage your interface pointer, not manually AddRef() and Release() them.


    After reading your code, you have a few problems:
    1) Typically, you pass NULL for CreateWindow() hWndParent.
    2) You need to wrap your scene in BeginScene()/EndScene().
    3) You aren't creating your back buffer as a lockable back buffer.


    Not sure that that's all the problems, but you can start there. You should also make sure to install the DirectX SDK, turn on the Debug Direct3D runtime, and turn on maximum logging, and look in the Output window for any complaints from the Direct3D runtime.

    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 04-19-2008 12:23 AM In reply to

    Re: Direct3D problem under Windows Vista

    What I mean is that the bitmaps appear overlapped, you can only see the top-left quarter of the bitmap.  Akso, in Windowed mode the output is not scaled to your display. My display resolution is 1280X1024 the resolution the author uses in Fullscreed mode is 640x480.

    I should also point out that mos of the code was copied from a book when the author assumes that the reader knows nothing about DirectX.  I only updated the code so that it would work uising DirextX 9, the book used DirextX 8.1 whcih is no longer included iin the DirectX SDK.

     

Page 1 of 1 (3 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG