Drawing LPDIRECT3DTEXTURE9 onto a LPDIRECT3DSURFACE9

1.1k views Asked by At

Is there are specific function in DirectX9 that allows you to specify where LPD3DXSPRITE blits the sprite? I'm trying to load a surface with tile information, and then display a portion of that surface to the backbuffer. I realized that with LPD3DXSPRITE::Draw() that there is no parameter for destination surface, it's probably automatically the backbuffer. Then my drawing of the gameworld surface covers the sprites.

Is there a solution, or a different way I have to approach this problem? I need the transparency which is why I thought of using the LPDIRECT3DTEXTURE9 object to handle my tile sprites, but the map won't fit on the backbuffer which is why I thought of loading the map onto a surface, and then drawing from that.

//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);

}
1

There are 1 answers

4
VuVirt On BEST ANSWER

Since you are using ID3DXSprite, the easiest way to specify the render target would be using ID3DXRenderToSurface like this:

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

// draw your sprites

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