LPDIRECT3DDEVICE9 pd3dDevice;
LPDIRECT3DTEXTURE9 pRenderTexture = NULL;
LPDIRECT3DTEXTURE9 pPyramideTexture = NULL;    
LPDIRECT3DSURFACE9 pRenderSurface = NULL,pBackBuffer = NULL;

void init()
{
    pd3dDevice = g_pxD3d->GetDevice();
    D3DXCreateTextureFromFile(pd3dDevice,L"UI_BG_PERSONSHOP.dds",&pPyramideTexture);

    pd3dDevice->CreateTexture(512,
        512,
        1,
        D3DUSAGE_RENDERTARGET,
        D3DFMT_R5G6B5,
        D3DPOOL_DEFAULT,
        &pRenderTexture,
        NULL);


}


void Render()
{    
    D3DXMATRIX matProjection,matOldProjection;

    VTXTYPE_2D_COLOR_TEX1 m_Vertices[4];    

    m_Vertices[0].x = 0;
    m_Vertices[1].x = 500;
    m_Vertices[2].x = m_Vertices[1].x;
    m_Vertices[3].x = m_Vertices[0].x;

    m_Vertices[0].y = 0;
    m_Vertices[1].y = m_Vertices[0].y;
    m_Vertices[2].y = 500;
    m_Vertices[3].y = m_Vertices[2].y;

    m_Vertices[0].z = 0;
    m_Vertices[1].z = m_Vertices[0].z;
    m_Vertices[2].z = m_Vertices[0].z;
    m_Vertices[3].z = m_Vertices[0].z;

    m_Vertices[0].tu = 0.0f; m_Vertices[0].tv = 0.0f;
    m_Vertices[1].tu = 1.0f; m_Vertices[1].tv = 0.0f;
    m_Vertices[2].tu = 1.0f; m_Vertices[2].tv = 1.0f;
    m_Vertices[3].tu = 0.0f; m_Vertices[3].tv = 1.0f;


    pRenderTexture->GetSurfaceLevel(0,&pRenderSurface);

    //D3DXMatrixPerspectiveFovLH(&matProjection,D3DX_PI / 4.0f,1,1,100);
    //pd3dDevice->GetTransform(D3DTS_PROJECTION,&matOldProjection);
    pd3dDevice->GetRenderTarget(0,&pBackBuffer);

    //render-to-texture
    //set new render target
    pd3dDevice->SetRenderTarget(0,pRenderSurface);
    //clear texture
    pd3dDevice->Clear(0,
        NULL,
        D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
        D3DCOLOR_XRGB(100,100,100),
        1.0f,
        0);
    pd3dDevice->BeginScene();

    pd3dDevice->SetTexture(0,pPyramideTexture);

    /*D3DXMatrixRotationY(&matRotationY,fRotation);
    D3DXMatrixTranslation(&matTranslation,0.0f,0.0f,5.0f);
    pd3dDevice->SetTransform(D3DTS_WORLD,
        &(matRotationY * matTranslation));*/
    //set projection matrix
    //pd3dDevice->SetTransform(D3DTS_PROJECTION,&matProjection);

    //pd3dDevice->SetStreamSource(0,pTriangleVB,0,sizeof(D3DVERTEX));
    pd3dDevice->SetFVF(VTXTYPE_2D_COLOR_TEX1::FVF);
    pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_Vertices,    sizeof(VTXTYPE_2D_COLOR_TEX1));


    pd3dDevice->EndScene();

    //set back buffer
    pd3dDevice->SetRenderTarget(0,pBackBuffer);
    pd3dDevice->Clear(0,
        NULL,
        D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
        D3DCOLOR_XRGB(0,0,0),
        1.0f,
        0);
    pd3dDevice->BeginScene();

    //set rendered texture
    pd3dDevice->SetTexture(0,pRenderTexture);

    //pd3dDevice->SetTransform(D3DTS_WORLD,&matTranslation);
    //restore projection matrix
    //pd3dDevice->SetTransform(D3DTS_PROJECTION,&matOldProjection);

    //pd3dDevice->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX));
    //pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,0,2);
    pd3dDevice->SetFVF(VTXTYPE_2D_COLOR_TEX1::FVF);
    pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_Vertices,    sizeof(VTXTYPE_2D_COLOR_TEX1));

    RenderText();

    if(g_pTextRectItem)
        g_pTextRectItem->render(pd3dDevice);

    pd3dDevice->EndScene();
    pd3dDevice->Present(NULL,NULL,NULL,NULL);


}
void programExit(){

    pRenderSurface->Release();
    pRenderSurface = NULL;

    pBackBuffer->Release();
    pBackBuffer = NULL;    

    pRenderTexture->Release();
}