Advertisement Jump to content
Sign in to follow this  

Question regading D3DXLoadSurfaceFromMemory Function?

This topic is 4930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a bitmap image of 1024 x 768 x 3. I'm using DirectX 9.0. I read it and now it is memory. Now I want to display it using D3DXLoadSurfaceFromMemory Function. I'm assuming it is the right function to be used. Earlier, I used the function D3DXLoadSurfaceFromFile, and passed image name. It works fine, but I want it via memory. Can anyone provide me with the steps i should follow to display bitmap from memory. The reason is that I don't have to physically go and change the name of the bmp everytime. I just pass teh image and it gets displayed. Please help.. thanks regards VA

Share this post

Link to post
Share on other sites
Simply load your file into a buffer and pass (don't forget the size!!!). This is how I do it:
fstream file("file.txt",ios::in | ios::binary);
file.seekg(0,ios::end); //Goto the end of the file
ULONG fsize=file.tellg(); //Get the size of the file
file.seekg(0,ios::beg); //Goto the beginning of the file
char *fdata=new char[fsize]; //Buffer,fsize); //Put the file into a buffer
file.close(); //Don't need the file anymore
//Now you can pass it off to D3DXLoadSurfaceFromMemory()
delete [] fdata; //Don't forget to delete it when you're done!!!

If you need the arguments for that function, look it up on the MSDN.
EDIT: Well crap, didn't see that you already had it in memory. Oh well.
Now, if you're just loading it for the purpose of displaying it, I would suggest using a texture instead of a surface, since you can only derrive images from textures in Direct3D (or, at least that's the most common way).
To display your bitmap, which should now be a texture, you simply make a textured quad. This function is from Dissecting Sprites in Direct3D that I adapted some:
//Variables and Classes:
//Floating rectangle structure
struct FLOAT_RECT {
float left, top, right, bottom;
//Vertex Class for Quads
struct D3DTLVERTEX {
float x, y, z; //Position
float rhw; //Reciprocal of homogeneous w
D3DCOLOR color; //Vertex colour
float tu, tv; //Texture coordinates
//Create a matrix using a center point, scaling, and an angle
D3DMATRIX* D3DBuildMatrix(D3DMATRIX* pOut,D3DXVECTOR2* center,D3DXVECTOR2* scaling,float angle) {
D3DXMATRIX matrices[5];
D3DXMatrixTranslation(&matrices[0], -center->x, -center->y, 0);
D3DXMatrixScaling(&matrices[1], scaling->x, scaling->y, 1);
D3DXMatrixRotationZ(&matrices[2], angle);
D3DXMatrixTranslation(&matrices[3], center->x, center->y, 0);
matrices[4] = matrices[0] * matrices[1] * matrices[2] * matrices[3];
*pOut = matrices[4];
return pOut;
void D3DTransformVertices(D3DTLVERTEX vertices[],D3DXVECTOR2* pCenter,D3DXVECTOR2* pScaling,float angle) {
D3DXMATRIX matTransf, matVerts, matNew;
D3DBuildMatrix(&matTransf, pCenter, pScaling, angle); //Create the matrix for calculations
int i;
for (i=0;i<4;i++) {
//4 vertices fit nicely into a 4x4 matrix, put each vertex point into a matrix row.
matVerts(i,3)=1.0f; //The scaling factor, w
//Reuse D3D matrix multiplication code to transform our vertices.
matNew = matVerts * matTransf;
for (i=0;i<4;i++) {
//Retrieve the newly transformed points.
vertices.x = matNew(i,0);
vertices.y = matNew(i,1);
vertices.z = matNew(i,2);
//The function:
FLOAT_RECT frQuadDefault={0.0f,0.0f,1.0f,1.0f}; //Default floating rectangle for source points of a quadrilateral
SIZE sizeDestDefault;
bool D3DBlit(LPDIRECT3DTEXTURE9 pTexture,POINT *pDest,SIZE *sizeDest=NULL, D3DXVECTOR2* pCenter=NULL,D3DXVECTOR2* pScaling=NULL,
float angle=0,FLOAT_RECT *pSrcRect=NULL,D3DCOLOR color=0xFFFFFFFF) {
//Get surface description. If "pTexture" is NULL, get the previous surface description from "d3dsdBLIT"
pTexture->GetLevelDesc(0, &surfDesc);
if (sizeDest==NULL) { sizeDest=&sizeDestDefault; sizeDest->cx=surfDesc.Width; sizeDest->cy=surfDesc.Height; }
float left = (float)pDest->x;
float top = (float)pDest->y;
float right = left + sizeDest->cx; //surfDesc.Width;
float bottom = top + sizeDest->cy; //surfDesc.Height;
const float z = 0.0f, rhw = 1.0f;
if (pSrcRect==NULL) pSrcRect=&frQuadDefault;
D3DTLVERTEX vertices[4]={
//x, y, z, rhw, color, tu, tv
{ left, top, z, rhw, color, pSrcRect->left, pSrcRect->top },
{ right, top, z, rhw, color, pSrcRect->right, pSrcRect->top },
{ left, bottom, z, rhw, color, pSrcRect->left, pSrcRect->bottom },
{ right, bottom, z, rhw, color, pSrcRect->right, pSrcRect->bottom }
D3DXVECTOR2 center, scaling;
center.x=(float)pDest->x; center.y=(float)pDest->y;
if (pCenter!=NULL) {
else {
center.x+=(float)sizeDest->cx/2; //surfDesc.Width/2; //Might as well get the actual center
center.y+=(float)sizeDest->cy/2; //surfDesc.Height/2;
pCenter = &center; // Don't want to modify the argument passed in.
if (pScaling == NULL) { //Use identity: no scaling performed.
scaling.x =1.0f; scaling.y = 1.0f;
D3DTransformVertices(vertices, pCenter, pScaling, angle);
//Draw the sprite
//NOTE: Make the drawing changes with lpD3DDevice->SetFVF() and SetTexture() here
HRESULT hRes=lpD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2 , vertices, sizeof(D3DTLVERTEX));
return (!FAILED(hRes));

Share this post

Link to post
Share on other sites
Can you give me a sample code using LoadSurfacefromfile function .I can't load my bitmap on my background. I want to do a program : A blue sky on background (a bitmap) ,a plane move random on that.
Thanks a lot.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!