开发者

Sprite.Draw() draws my textures too small

开发者 https://www.devze.com 2023-02-03 06:02 出处:网络
I declared a device + sprite in a Windows.Form like this PresentParameters presentParameters = new PresentParameters();

I declared a device + sprite in a Windows.Form like this

PresentParameters presentParameters = new PresentParameters();
presentParameters.Windowed = true;
presentParameters.SwapEffect = SwapEffect.Copy;

var device = new Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParameters);

var sprite = new Sprite(device);

I loaded a开发者_如何学Python texture via TextureLoader.FromFile(device, "image.png");

In my Draw method i startet the device scene, then the sprite scene, then i wrote sprite.Draw2D(texture, PointF.Empty, 0, PointF.Empty, Color.White);

the drawing itself works, but it draws only a big portion of the image scaled up to the screen (like 90%)

i tried it with a source rectangle with the given texture size too, but the same bug occurred

any suggestions?


I am experienced in C++ DirectX, but not C# DirectX, so take this with a grain of salt.

In my experiences with the Sprite interface, you need to scale, rotate, and translate just like you need to with 3D objects. You may be forgetting to scale. Here is the code of my Update function.

void Button::Update()
{
    Sprite->Begin(D3DXSPRITE_ALPHABLEND);
    D3DXMATRIX trans;
    D3DXMATRIX scale;
    D3DXMATRIX world;
    D3DXMatrixIdentity(&world);
    D3DXMatrixTranslation(&trans, pos.x, pos.y, 0.0f);
    D3DXMatrixScaling(&scale, scaleFactor, scaleFactor, 1.0f);
    world = scale * trans;
    Sprite->SetTransform(&world);
    Sprite->Draw(buttonTexture, NULL, NULL, &D3DXVECTOR3(-width2, -height2, 0.0), whitecol);
    Sprite->End();
}

Admittedly, this isn't a very object-oriented way of doing things, but it suits my needs.


Caveat: I am not an DirectX expert, but I had the same problem.

When you load the sprite it expands the sprite to fit a size where each dimension is a power of 2. For example, If you sprite was 200 x 65, the sprite will have a width of 256 (and the image will be expanded to a width of 256, increasing it slightly) by 128 (almost doubling the height).

When you draw the image, it will be almost twice the height you expected.

My solution was to modify my image file to have a height and width of a factor of 2 and then only draw the portion that was the original size.

0

精彩评论

暂无评论...
验证码 换一张
取 消