Advertisement Jump to content
Sign in to follow this  

2D programming

This topic is 4967 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've written a couple of games so far (tetris, snake etc...) and want to try to start a more complicated project. All the games I've done so far have used simple IDirect3DSurfaces and I've just blitted them around. However, these surfaces seem to have limitations (the killer for me was that transparency didn't seem to work--I could do a colorkey replace with alpha but everything displayed as opaque). I've been looking into ID3DXSprites as an alternative, but have not found any good example or tutorial on how these work (all I can find is the MSDN documentation, which is somewhat lacking in this case). If someone could drop me a link to some working code using ID3DXSprites that would be great. A tutorial would be even better. Thanks in advance for any help you can give me.

Share this post

Link to post
Share on other sites
This site has a tutorial on 2D animation using ID3DXSprites:

Of course, Google:

...and you already said you checked MSDN. There's a good bit of information to be found on Google as well as searching the forums for "ID3DXSprite", but that would be a lot more links than it would be practical for me to post here. I'd reccommend doing a search though and reading through some of the threads.

forum search

Hope this helps you get started.

Share this post

Link to post
Share on other sites <- really nice tutorials there..

Oh, and that transparency thingy.. You must enable some flag for alpha to work.. Samples on that site.. :)

Share this post

Link to post
Share on other sites
Thanks for the responses, but my code for rendering sprites looks nearly identical to everything I've looked at, and yet all I get when I run my program is a black screen. The only thing I can think is, maybe, do I have to set up some sort of orthogonal projection since I'm working with 2D? I've played around with stuff like D3DXMatrixOrthoLH but haven't got anything to work. Do I even have to mess around with these matrices when I'm doing 2D, or can I just set all three (world, view, and projection) to the identity? I'm pretty sure my actual render code for sprites is right--it's so simple I don't see how I could have it wrong, and I've checked and rechecked it--so I guess what I'm asking is: what exactly do I have to initialize beforehand so that I can draw a 2D scene?

Share this post

Link to post
Share on other sites
considering i worked hard and long to figure all this 2d crap out and i finally got it... i'll share my wisdom

use some graphical image that supports transparency (otherwise u have to use colorkey which sucks)... such as png

// do your
D3DXCreateSprite(device, &spriteptr);
D3DXCreateTextureFromFile(device, filename, &textureptr); // filename is path


// then you do your


hopefully that's what you are looking for... if not let me know

ALSO... if you need a more indepth, the best D3D 2d stuff explained (although i found i used the documentation alot) is at

Share this post

Link to post
Share on other sites
I feel so stupid... this looks like it should be the easiest thing. Here's what my code looks like:

D3DXCreateSprite(pd3dDevice, &spritest);

// set up textures here, to be called with iset
// I've debugged this stuff pretty intensively so I know my
// texture loading works.

D3DXVECTOR3 position(10.0f, 10.0f, 0.0f);


spritest->Draw(iset("SPRITE"), NULL, 0, &position, 0xffffffff);


Again, this looks just like everyone else's sprite code so I can only assume that something's wrong with my initialization. I obviously have set up a d3d object and d3dDevice, as well as loaded in a texture for my sprite. Do I have to mess around with projection matrices? Lighting? I've looked at sites like toymaker, but they tend to just describe the ID3DXSprite's functionality and don't really mention any setup I need for 2D rendering. Sorry if I'm just being really slow about all this :(

Share this post

Link to post
Share on other sites
You don't really need to set anything up for 2D rendering:

Edit: I have a bunch of time on my hands, so I'll put a working example together in a zip file :D.

Here it is: D3DXSprite Tutorial.
Its not really a tutorial as of yet, its just the source, image, and the exe. There is also a second set of these that uses a different cursor and a timer (the cursor is white and the timer changes the color of the cursor. So the cursor changes color). I'd like to say that I got the StaticCursorB.bmp from somebody's game. I didn't make it and if the artist want's me to remove it or would like to get props for it then please let me know. The game was a donut factory puzzle game.

ID3DXSprite* g_pSprite = 0;
IDirect3DTexture9* g_pTexture = 0;

// Setup Window
// Setup Direct3D
D3DXCreateSprite(g_pDevice, &g_pSprite);
D3DXCreateTextureFromFile(g_pDevice, "TestImage.bmp", &g_pTexture);

// Start rendering loop
g_pDevice->Clear(0, 0, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);
g_pSprite->Draw(g_pTexture, 0, 0, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), 0xffffffff);
g_pDevice->Present(0, 0, 0, 0);

// Clean up here

And I have to disagree with -justin-. Colorkey isn't hard at all (as you can see from my tutorial). I'm not saying that it's easier, but its not that hard (even when you're not using the D3DXSprite class.)

[Edited by - Programmer16 on June 14, 2005 12:13:02 AM]

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!