将 LPDIRECT3DTEXTURE9 绘制到 LPDIRECT3DSURFACE9 上

Drawing LPDIRECT3DTEXTURE9 onto a LPDIRECT3DSURFACE9

DirectX9 中是否有特定的函数允许您指定 LPD3DXSPRITE 在何处 blit 精灵?我正在尝试加载带有图块信息的表面,然后将该表面的一部分显示到后备缓冲区。我意识到 LPD3DXSPRITE::Draw() 没有目标表面的参数,它可能自动成为后缓冲区。然后我绘制的游戏世界表面覆盖了精灵。

是否有解决方案,或者我必须以不同的方式来解决这个问题?我需要透明度,这就是为什么我想使用 LPDIRECT3DTEXTURE9 对象来处理我的瓷砖精灵,但地图不适合后备缓冲区,这就是为什么我想将地图加载到表面上,然后从中绘制。

//Filename: MyDirectX.h
//Direct3D objects
extern LPDIRECT3D9 d3d; 
extern LPDIRECT3DDEVICE9 d3ddev; 
extern LPDIRECT3DSURFACE9 backbuffer;
extern LPD3DXSPRITE spriteobj;

LPDIRECT3DTEXTURE9 LoadTexture(string filename, D3DCOLOR transcolor = D3DCOLOR_XRGB(0,0,0));

void DrawTile(LPDIRECT3DTEXTURE9 source, int frame, int width, int height, int columns, LPDIRECT3DSURFACE9 dest, int destx, int desty, D3DCOLOR color);
void BuildGameWorld();
void ScrollScreen();
/////////////////////////////////////////////////////////////////////////////
//Filename: MyGame.cpp
#include "MyDirectX.h"

//THIS FUNCTION HERE
void DrawTile(LPDIRECT3DTEXTURE9 source, int frame, int width, int height, int columns, LPDIRECT3DSURFACE9 dest, int destx, int desty, D3DCOLOR color)
{
    //get dimensions for the tile
    RECT r1;
    r1.left = (frame % columns) * width;
    r1.top = (frame / columns) * height;
    r1.right = r1.left + width;
    r1.bottom = r1.top + height;

    //can only draw to the backbuffer? Any work around?
    spriteobj->Draw(source, &r1, &D3DXVECTOR3(r1.right / 2, r1.bottom / 2, 0), &D3DXVECTOR3(destx, desty, 0), color);
}

void BuildGameWorld()
{
    int x, y;
    LPDIRECT3DTEXTURE9 tiles = nullptr;

    //load the texture file
    tiles = LoadTexture("goodly-2x.png", D3DCOLOR_XRGB(255, 0, 255));

    spriteobj->Begin(D3DXSPRITE_ALPHABLEND);

    for(y = 0; y < MAPHEIGHT; y++)
    {
        for(x = 0; x < MAPWIDTH; x++)
        {
            DrawTile(tiles, MAP_LAYER1[y * MAPWIDTH + x], 32, 32, 16, gameworld, x * 32, y * 32, D3DCOLOR_XRGB(255,255,255));
        }
    }

    spriteobj->End();

    //release the texture file
    tiles->Release();
}

//Without this function, and placing BuildGameWorld() inside the Game_Run()
//rendering phase the screen displays stripes of blue which I can only 
//assume is the transparent tiles working incorrectly*/
void ScrollScreen()
{
    //update horizontal scrolling position and speed
    ScrollX += SpeedX;
    if (ScrollX < 0) 
    {
        ScrollX = 0;
        SpeedX = 0;
    }
    else if (ScrollX > GAMEWORLDWIDTH - SCREENW)
    {
        ScrollX = GAMEWORLDWIDTH - SCREENW;
        SpeedX = 0;
    }

    //update vertical scrolling position and speed
    ScrollY += SpeedY;
    if (ScrollY < 0)
    {
        ScrollY = 0;
        SpeedY = 0;
    }
    else if (ScrollY > GAMEWORLDHEIGHT - SCREENH)
    {
        ScrollY = GAMEWORLDHEIGHT - SCREENH;
        SpeedY = 0;
    }

    //set dimensions of the source image
    RECT r1 = {ScrollX, ScrollY, ScrollX+SCREENW-1, 
        ScrollY+SCREENH-1};

    //set the destination rect
    RECT r2 = {0, 0, SCREENW-1, SCREENH-1};

    d3ddev->StretchRect(gameworld, &r1, backbuffer, &r2, 
        D3DTEXF_NONE);

}

由于您使用的是 ID3DXSprite,因此指定渲染目标的最简单方法是像这样使用 ID3DXRenderToSurface:

// inside IDirect3DDevice9::BeginScene() / EndScene() block
ID3DXRenderToSurface::BeginScene()
ID3DXSprite::Begin()

// draw your sprites

ID3DXSprite::End()
ID3DXRenderToSurface::EndScene()