Question

I am writing a WIN32 DirectX 9 program in which I load a few textures (a person and a background) and display them on the screen. However, they become warped (stretched), not being a one-to-one representation of what I drew in Paint. I tried different window sizes and different screen resolutions, but it still doesn't work.

I can force a very close imitation of the original thing by calling D3DXMatrixTransformation2D before drawing the sprite. However, that doesn't fix the problem. My program relies on me knowing exactly where the person is in relevence to the background (in reality, the background moves and the person stays in the middle of the screen).

Is there something simple I am missing or is it that I just can't do that for some reason? I can give code if it more than a simple fix, but I hope not. For the record, I recieve no errors or warnings; it's just a visual and movement-tracking issue. Thank you.

EDIT: Here's some code:

    //start of program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

    //Define and register the Windows Class ***Function
    createWindowClass(hInstance);

    //Create the window (still not shown)
    HWND hWnd = CreateWindow("Sample Window Class", "Person With Ship", WS_OVERLAPPEDWINDOW, 
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);

    //Define and create the DirectX9 object ***Function
    HRESULT hr = createDirectX(hWnd);

    D3DXCreateSprite(d3dDevice, &sprite);
    D3DXCreateTextureFromFile(d3dDevice, "landingPad2.png", &texture);
    D3DXCreateTextureFromFile(d3dDevice, "person.png", &person);
    //D3DXCreateTextureFromFile(d3dDevice, "secondRoom.png", &secondRoom);





    //Set up text
    LPD3DXFONT mFont;
    D3DXCreateFont(d3dDevice, 20, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
        DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &mFont );


    //Finally show window
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    //setup keyboard
    RAWINPUTDEVICE Rid[1]; // array of structs for input devices
    Rid[0].usUsagePage = 1; // use 1 for most inputs
    Rid[0].usUsage = 6; //2-mouse, 4-joystick, 6-keyboard
    Rid[0].dwFlags = 0; //use 0
    Rid[0].hwndTarget=NULL; //use NULL
    RegisterRawInputDevices(Rid,1,sizeof(RAWINPUTDEVICE)); // registers all of the input devices


    //MAIN LOOP!!
    MSG msg;
    ZeroMemory(&msg, sizeof(msg));
    while (msg.message!=WM_QUIT) 
    {
        while(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        updateGraphics(hr, mFont);
        updatePosition();

    }
    mFont->Release();
    texture->Release();
    return msg.wParam;
}


void createWindowClass(HINSTANCE hInstance)
{
    const LPCSTR CLASS_NAME  = "Sample Window Class";

    //create windows object
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX); 
    wcex.style= CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc= (WNDPROC)WndProc;
    wcex.cbClsExtra= 0;
    wcex.cbWndExtra= 0;
    wcex.hInstance= hInstance;
    wcex.hIcon= 0;
    wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName= 0;
    wcex.lpszClassName= CLASS_NAME;
    wcex.hIconSm= 0;

    //register windows class
    RegisterClassEx(&wcex);
}

HRESULT createDirectX(HWND hWnd)
{
    //create directx object
    d3dObject = Direct3DCreate9(D3D_SDK_VERSION);
    if (d3dObject==NULL)
    {
        exit(1);
    }

    //Present Parameters struct
    D3DPRESENT_PARAMETERS presParams;

    //Sets everything to 0
    ZeroMemory(&presParams, sizeof(presParams));

    presParams.Windowed = TRUE;
    presParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    presParams.BackBufferFormat = D3DFMT_UNKNOWN;
    presParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;

    //DIRECT3D Stuff (not used currently)
    //presParams.EnableAutoDepthStencil = TRUE;
    //presParams.AutoDepthStencilFormat = D3DFMT_D16;
    //d3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
    //D3DXMatrixIdentity( &worldMatrix );

    HRESULT hr = d3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &d3dDevice);
    return hr;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
    {
        case WM_INPUT:
        {


            UINT bufferSize;
            GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &bufferSize, sizeof (RAWINPUTHEADER));

            // Create a buffer of the correct size
            BYTE *buffer=new BYTE[bufferSize];

            // Call the function again, this time with the buffer to get the data
            GetRawInputData((HRAWINPUT)lParam, RID_INPUT, (LPVOID)buffer, &bufferSize, sizeof (RAWINPUTHEADER));

            PRAWINPUT raw = (RAWINPUT*) buffer;

            getInput(raw);

            break;
        }
        case WM_COMMAND:
        {
            int wmId    = LOWORD(wParam); 
            int wmEvent = HIWORD(wParam); 

            // Parse the menu selections:
            switch (wmId)
            {
                case IDM_EXIT:
                   DestroyWindow(hWnd);
                break;
            }
            break;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

void getInput(PRAWINPUT raw)
{
    if (raw->header.dwType== RIM_TYPEKEYBOARD)
            {
                USHORT keyCode = raw->data.keyboard.VKey;

                switch(keyCode)
                {
                    case VK_LEFT:
                        keyUp=raw->data.keyboard.Flags & RI_KEY_BREAK;
                        if (keyUp == false)
                            counterTrue = true;
                        else
                            counterTrue = false;
                        break;
                    case VK_RIGHT:
                        keyUp=raw->data.keyboard.Flags & RI_KEY_BREAK;
                        if (keyUp == false)
                            clockwiseTrue = true;
                        else
                            clockwiseTrue = false;
                        break;
                    case VK_UP:
                        keyUp=raw->data.keyboard.Flags & RI_KEY_BREAK;
                        if (keyUp == false)
                            upTrue = true;
                        else
                            upTrue = false;
                        break;
                    case VK_DOWN:
                        keyUp=raw->data.keyboard.Flags & RI_KEY_BREAK;
                        if (keyUp == false)
                            downTrue = true;
                        else
                            downTrue = false;
                        break;
                    default:
                        break;
                }
                if (keyCode == 'A')
                {
                    keyUp=raw->data.keyboard.Flags & RI_KEY_BREAK;
                    if (keyUp == false)
                        leftTrue = true;
                    else
                        leftTrue = false;
                }
                if (keyCode == 'D')
                {
                    keyUp=raw->data.keyboard.Flags & RI_KEY_BREAK;
                    if (keyUp == false)
                        rightTrue = true;
                    else
                        rightTrue = false;
                }
                if (keyCode == 'W')
                {
                    keyUp=raw->data.keyboard.Flags & RI_KEY_BREAK;
                    if (keyUp == false)
                        upTrue = true;
                    else
                        upTrue = false;
                }
                if (keyCode == 'S')
                {
                    keyUp=raw->data.keyboard.Flags & RI_KEY_BREAK;
                    if (keyUp == false)
                        downTrue = true;
                    else
                        downTrue = false;
                }
        }
}

void updateGraphics(HRESULT hr, LPD3DXFONT mFont)
{
        hr = d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET| D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
        hr = d3dDevice->BeginScene();



        sprite->Begin(D3DXSPRITE_ALPHABLEND);

        // Texture being used is 64 by 64:
        D3DXVECTOR2 spriteCentre=D3DXVECTOR2(rotationCenter.x, rotationCenter.y);

        // Screen position of the sprite
        D3DXVECTOR2 trans=D3DXVECTOR2(pos.x, pos.y);

        // Build our matrix to rotate, scale and position our sprite
        D3DXMATRIX mat;

        D3DXVECTOR2 scaling(0.5798f, 0.784f);

        // out, scaling centre, scaling rotation, scaling, rotation centre, rotation, translation
        D3DXMatrixTransformation2D(&mat,NULL, NULL, &scaling,&spriteCentre,rotation,NULL/*&trans*/);

        // Tell the sprite about the matrix
        sprite->SetTransform(&mat);

        sprite->Draw(texture, NULL, &rotationCenter, &pos, 0xFFFFFFFF);

        scaling.x = 0.53;
        scaling.y = 0.57f;

        D3DXMatrixTransformation2D(&mat,NULL,0.0, &scaling,&spriteCentre, 0,NULL/*&trans*/);
        sprite->SetTransform(&mat);

        sprite->Draw(person, NULL, NULL, &personPos, 0xFFFFFFFF);

        sprite->End();

        DisplaySomeText(mFont);

        d3dDevice->EndScene();
        d3dDevice->Present(NULL, NULL, NULL, NULL);
}

void updatePosition()
{
        if (clockwiseTrue == true)
        {
            rotation -= (float)0.03;
        }
        else if (counterTrue == true)
        {
            rotation += (float)0.03;
        }

        if (rotation >(PI))
        {
            rotation -= (float)(2*PI);
        }
        if (rotation <= -(PI))
        {
            rotation += (float)(2*PI);
        }

        if (upTrue == true)
        {
            pos.y += (3*cos(rotation));
            pos.x += (3*sin(rotation));
        }
        else if (downTrue == true)
        {
            pos.y -= (3*cos(rotation));
            pos.x -= (3*sin(rotation));
        }

        if (leftTrue == true)
        {
            pos.x += (3*cos(rotation));
            pos.y -= (3*sin(rotation));
        }
        else if (rightTrue == true)
        {
            pos.x -= (3*cos(rotation));
            pos.y += (3*sin(rotation));
        }

        //collision detection
        if (rotation >=0 && rotation < (PI/2))
        {
            if (pos.y - (30*cos(rotation)) - (30*sin(rotation)) < -138 && pos.x < 350 && pos.x > -550)
            {
                pos.y = -142 + (30*cos(rotation)) + (30*sin(rotation));
            }
        }
        if (rotation < 0 && rotation > -(PI/2))
        {
            if (pos.y - (51*cos(rotation)) + (14*sin(rotation)) < -142 && pos.x < 350 && pos.x > -550)
            {
                pos.y = -142 + (51*cos(rotation)) - (14*sin(rotation));
            }
        }
        if (rotation < -(PI/2) && rotation > -(PI))
        {
            if (pos.y + (51*cos(rotation)) + (14*sin(rotation)) < -142 && pos.x < 350 && pos.x > -550)
            {
                pos.y = -142 - (51*cos(rotation)) - (14*sin(rotation));
            }
        }
        if (rotation > (PI/2) && rotation <= (PI))
        {
            if (pos.y + (51*cos(rotation)) - (14*sin(rotation)) < -142 && pos.x < 350 && pos.x > -550)
            {
                pos.y = -142 - (51*cos(rotation)) + (14*sin(rotation));
            }
        }
}

void DisplaySomeText(LPD3DXFONT mFont)
{

// Create a colour for the text - in this case blue
D3DCOLOR fontColor = D3DCOLOR_ARGB(255,0,0,255);    

// Create a rectangle to indicate where on the screen it should be drawn
RECT rct;
rct.left=200;
rct.right=780;
rct.top=10;
rct.bottom=rct.top+20;





TCHAR cX[30] = "x";
TCHAR cY[30] = "y";
TCHAR cR[30] = "r";
TCHAR cQ[30] = "q";
size_t cchDest = 30;

LPCTSTR pszFormat = TEXT("%f");

HRESULT har = StringCchPrintf(cX, cchDest, pszFormat, pos.x);
HRESULT her = StringCchPrintf(cY, cchDest, pszFormat, pos.y);
HRESULT hir = StringCchPrintf(cR, cchDest, pszFormat, rotation);
HRESULT hur = StringCchPrintf(cQ, cchDest, pszFormat, (pos.y - (43*cos(rotation))));


mFont->DrawText(NULL, cX, -1, &rct, 0, fontColor);

rct.left += 100;

mFont->DrawText(NULL, cY, -1, &rct, 0, fontColor);

rct.left += 100;

mFont->DrawText(NULL, cR, -1, &rct, 0, fontColor);

rct.left += 100;

mFont->DrawText(NULL, cQ, -1, &rct, 0, fontColor);

}
Was it helpful?

Solution

So, finally: the issue was due to the texture dimensions not being powers of two :) This behaviour is quite usual, but it is still hardware-dependent: in theory there may exist some videocards which handle arbitrary texture sizes the same way as power-of-two-sized ones. To know that they do we'd have to check their capabilities via IDirect3DDevice9::GetDeviceCaps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top