Best way to make a zooming 2D game

Started by
12 comments, last by DvDmanDT 17 years, 1 month ago
I didn't find anything like that on codesamplers, although I did try playing around with the 2D sprite example with no luck. The zoom function won't even work if I use it in their example.

If I could get this to work, it'd be a dream come true... but I'm having a lot of trouble figuring out why it won't, because I'm not experienced enough to know what's going on behind the scenes [sad]
Advertisement
.. Just create your own sprite rendering function, using a textured quad for now. If speed becomes a problem, you can later on optimize it fairly easy. I can send you a code example of this if you want.

ID3DXSprite is a bit wierd on some points you see.. It'll ignore any world, projection and view matrix you pass to SetTransform, that's why zooming doesn't work.

Point sprites - Great for particle effects and stuff
ID3DXSprite - Just some example of that sprite thingy.


[edit]
Sorry, misread something. Try changing the color you clear to to something way different, like

d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,0,128), 1.0f, 0);

Then you can sometimes spot errors more easily. One common error is to not disable lightning, which causes everything to appear black instead of real color (unless you add lights and stuff ofcourse). Another common error is to misplace the camera, that is, the View matrix. Shouldn't affect ID3DXSprite, but very well all other things. If I were you, I'd make sure to always set world, view and projection before rendering, even if you use the sprite thingy which isn't affected.

[edit 2]
Also, you shouldn't only clear your target, but also your Z-buffer if you are using one:
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255,0,128), 1.0f, 0);

But then you should also enable it:
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);

To disable lightning: (recommended for now)
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);

And another thing I'd recommend to disable for now is culling:
d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

Normally d3d won't draw triangles if they are not facing you, and that is determined by the order of the vertices. I use to disable this, at least when I do 2d, and if you want to, you can always enable it later.

You can add all three of those to your init code.

[Edited by - DvDmanDT on March 27, 2007 8:23:59 AM]
Well, I tried that stuff... it didn't work. :(

You said that my zoom function "seemed to be working for you." Right? So, is it actually possible to do this using ID3DXSPRITE? Or did I misunderstand?

If you could send me an example, I would very much appreciate it. You can send it to an_angry_walrus@hotmail.com.
Hello, you cannot use that zoom function with ID3DXSprite, no.

You can use something like this for now: (might be errors, taken more or less from the top of my head)
struct Vertex{	FLOAT x, y, z;	FLOAT tu, tv;	enum {FVF = (D3DFVF_XYZ | D3DFVF_TEX1)};};void DrawSprite(LPDIRECT3DTEXTURE9 tex, float x, float y, float w, float h){	Vertex Verts[4];	Verts[0].x=x;	Verts[0].y=y;	Verts[1].x=x+w;	Verts[1].y=y;	Verts[2].x=x;	Verts[2].y=y+h;	Verts[3].x=x+w;	Verts[3].y=y+h;	for(int i=0;i<4;i++)	{		Verts.z=0.0f;		//Uncomment if you wish to use the middle of the sprites for positions, instead of upper left corner:		//Verts.x -= w/2;		//Verts.y -= h/2;	}	Verts[0].tu=0.0f;	Verts[0].tv=0.0f;	Verts[1].tu=1.0f;	Verts[1].tv=0.0f;	Verts[2].tu=0.0f;	Verts[2].tv=1.0f;	Verts[3].tu=1.0f;	Verts[3].tv=1.0f;	d3ddev->SetTexture(0,tex);	d3ddev->SetFVF(Vertex::FVF);	d3ddev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,Verts,sizeof(Vertex));}

If this turns out to be way too slow for you, it is very possible to optimize it, without having to modify your calling code (at least not much). I really think you should focus on getting something working before worrying about speed.

This topic is closed to new replies.

Advertisement