Sign in to follow this  

Sprites Problem

This topic is 3712 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

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 this post


Link to post
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 quad
FLOAT fWidth5 = (FLOAT)backBufferW - 0.5f;
FLOAT fHeight5 = (FLOAT)backBufferH - 0.5f;

FLOAT fTexWidth1 = 1.0f;
FLOAT fTexHeight1 = 1.0f;

//create our vertex buffer
d3dDevice->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 values
vertices[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 texture
device->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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by AHashem
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?


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.

Share this post


Link to post
Share on other sites

This topic is 3712 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this