Sprites Problem

Recommended Posts

I'm making a screensaver that uses the current screen as the background, but I'm having trouble with adjusting the background sprite to exactly fit on the screen. I take a screenshot (or a print screen) and store it in a BMP file then load it as a texture, the BMP file has exactly the same size as the resolution of the screen and also the back buffer that was created with the device has the same coordinates. This is the screenshot of the program: http://www.eipcprograms.com/gamedev/program_screenshot.JPG And this is the correct screenshot: http://www.eipcprograms.com/gamedev/real_screenshot.JPG Loading the texture is something like this:
	V_RETURN(D3DXCreateTextureFromFileExW(pd3dDevice,L"screenshot.tga",sWidth,sHeight,0/*D3DX_FROM_FILE*/,0, D3DFMT_X8R8G8B8/*D3DFMT_FROM_FILE*/,
D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,0,0,&BackTex));
where sWidth and sHeight are the width and height of the back buffer respectively. I don't know how to size the sprite to exactly fit the texture on the screen, if anyone knows how, then please drop me a reply. Atef Hashem

Share on other sites
Well the best way to do this is usually to make a quad consisting of 4 vertices using the D3DFVF_XYZRHW FVF type, which specifies that the vertices are already transformed and in their on-screen location. This way you can simply set the 4 vertices to the 4 corners of the screen and perfectly overlay it. Here's some example code:

//set up vertices for full-screen quadFLOAT fWidth5 = (FLOAT)backBufferW - 0.5f;FLOAT fHeight5 = (FLOAT)backBufferH - 0.5f;FLOAT fTexWidth1 = 1.0f;FLOAT fTexHeight1 = 1.0f; //create our vertex bufferd3dDevice->CreateVertexBuffer(sizeof(SCREEN_VERTEX) * 4, 0, D3DFVF_XYZRHW  | D3DFVF_TEX1,								D3DPOOL_DEFAULT, &fullScreenQuadVB, NULL);SCREEN_VERTEX* vertices = NULL;fullScreenQuadVB->Lock(0, 0, (void**)&vertices, 0);// Fill in the vertex valuesvertices[0].pos = D3DXVECTOR4(fWidth5, -0.5f, 0.0f, 1.0f);vertices[0].tex1 = D3DXVECTOR2(fTexWidth1, 0.0f);vertices[1].pos = D3DXVECTOR4(fWidth5, fHeight5, 0.0f, 1.0f);vertices[1].tex1 = D3DXVECTOR2(fTexWidth1, fTexHeight1);vertices[2].pos = D3DXVECTOR4(-0.5f, -0.5f, 0.0f, 1.0f);vertices[2].tex1 = D3DXVECTOR2(0.0f, 0.0f);vertices[3].pos = D3DXVECTOR4(-0.5f, fHeight5, 0.0f, 1.0f);vertices[3].tex1 = D3DXVECTOR2(0.0f, fTexHeight1);fullScreenQuadVB->Unlock();

Then for drawing...

//set your backround as your texturedevice->SetTexture(0, background);device->SetStreamSource(0, fullScreenQuadVB, 0, sizeof (SCREEN_VERTEX));device->SetFVF(D3DFVF_XYZRHW  | D3DFVF_TEX1);device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

An important thing to note is that the screen coordinates are offset by -0.5, this is due to how D3D maps screen coordinates and textels. There's an article about this in the SDK.

Share on other sites
OK, Thanks! This was the first thing that rushed into my mind, but then I thought to render it using sprites, as I've heard that the sprites are used in 2D rendering.

So my question now is when to use sprites and when not to use it? As for example, if I'm building a 2D game that will involve collisions between the objects and moving objects, but all 2D. Is it better to use the ID3DXSprite interface or just use the transformed vertices and each frame when I need to move an object I lock and unlock the buffer?

Atef Hashem

Share on other sites
I would use the ID3DXSprite interface if you're planning on drawing lots of 2D sprites, since it has built-in functionality for batching of sprites (which will keep your CPU usage to a minimum). What I showed you is good for the background since it ensures that the background image will be directly mapped to your back-buffer in a pixel-perfect manner, so I would suggest you continue to use it for your background image. However for any other images you're planning to overlay on top of the background, the sprite interface should serve you well.

Share on other sites
OK, I will go with the sprites for the rest of the 2D objects other than the background, but I'm expecting some problems with the 2D box/box overlap test because it seems that the coordinates that the sprite is working with are different from the coordinates I use inside the code (the ones I'll test against). Am I right with this?

Share on other sites
Quote:
 Original post by AHashemOK, I will go with the sprites for the rest of the 2D objects other than the background, but I'm expecting some problems with the 2D box/box overlap test because it seems that the coordinates that the sprite is working with are different from the coordinates I use inside the code (the ones I'll test against). Am I right with this?

The sprites work with screen coordinates. It shouldn't be too hard to derive screen coordinates from 2D world coordinates, all you need to know is how big your screen is and where your camera is currently "looking" and then its just simple addition/subtraction. Although it will be trickier if you use scaling and rotation, but certainly very doable.

Thanks! :)

Create an account

Register a new account

• Forum Statistics

• Total Topics
627708
• Total Posts
2978730

• 21
• 14
• 12
• 22
• 35