# 2D programming

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

## 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 on other sites
This site has a tutorial on 2D animation using ID3DXSprites:
http://triplebuffer.devmaster.net/tutorials/stepping3.php

...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 on other sites
http://www.codesampler.com/ <- really nice tutorials there..

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

##### 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 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 yourD3DXCreateSprite(device, &spriteptr);D3DXCreateTextureFromFile(device, filename, &textureptr);  // filename is pathdevice->BeginScene();// then you do yoursprite->Begin(D3DXSPRITE_ALPHABLEND);sprite->Draw(blahblahblahb);sprite->End();device->EndScene();

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 toymaker.info

##### Share on other sites
here's one article on gamedev that i found extremely useful:
Dissecting Sprites in Direct3D

its not using the sprite class but i think it works very well and i understand it so maybe it will help you.
good luck,

##### 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->Begin(D3DXSPRITE_ALPHABLEND);	spritest->Draw(iset("SPRITE"), NULL, 0, &position, 0xffffffff);spritest->End();

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 on other sites
gotta turn off comp now, if u dont get it tonight i'll post something tomorrow!

~j

##### 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 Direct3DD3DXCreateSprite(g_pDevice, &g_pSprite);D3DXCreateTextureFromFile(g_pDevice, "TestImage.bmp", &g_pTexture);// Start rendering loopg_pDevice->Clear(0, 0, D3DCLEAR_TARGET, 0xff00ff00, 1.0f, 0);g_pDevice->BeginScene();g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);g_pSprite->Draw(g_pTexture, 0, 0, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), 0xffffffff);g_pSprite->End();g_pDevice->EndScene();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]