Skip to main content
GameDev.net gamedev.net
🔒 Locked

2d Sprites and Sizing using D3D

Started by Todd Geraty Mar 10, 2001 at 3:19 PM 12 replies 1.2k views
Original Post
Todd Geraty
Todd Geraty
Hi there, I''ve been trying to learn DX8, so I thought I would just create a background and slap a sprite on it as an example. However, I''m running into problems sizing them correctly. I''ve heard the arguements to just use DX7, but I would really like to figure this out in DX8 (since I''m already familiar with DX7). I can get the background to display properly if I use D3DXCreateTextureFromFile(...), SetTexture(...), & DrawPrimitiveUP(...). I''m sure I can get the sprite to display properly using the same method, but I wanted to use D3DXSprite::Draw(...) because it allows you to specify a rectangle in the texture to use as your sprite (rather than the whole thing). So when I use D3DXCreateTextureFromFile(...), D3DXCreateSprite(...), & Draw(...), the sprite appears but is sized wrong (way too large, even though the scaling should have been the normal default size). So I changed the scaling down, but could never quite get it right - the sprite was always just a little off. So, just to experiment, I tried to do the background using the same method of using a sprite - just to see what would happen. When I used the default scaling, my 800x600 background was way too large and went off my 800x600 screen. So I put the scaling to (.5, .5), and that fit into the screen, but in a perfect square (when I thought it would be 400x300 if scaled properly). I guess I''m just not understanding the scaling functionality of the Draw(...) method or maybe I''m loading the texture wrong... Can anyone help? I''m trying to place and size my background and sprites properly. What am I doing wrong? Is there a better way to handle these? Any help would be GREATLY appreciated! :-) Thanks, Todd Geraty RELEVANT CODE (removing unneeded code): I used this code to create and display the background (the first way) and it seemed to display just fine: hr = D3DXCreateTextureFromFile(m_pID3DDevice, _T("Game.bmp"), &m_pID3DTextureBackground); m_MainVerts[0].v.x = 0; m_MainVerts[0].v.y = SCREEN_HEIGHT - 1; m_MainVerts[0].v.z = 0; m_MainVerts[0].tu = 0; m_MainVerts[0].tv = 1; m_MainVerts[0].rhw = 1; m_MainVerts[0].color = 0xFFFFFF; m_MainVerts[0].specular = 0; m_MainVerts[1].v.x = 0; m_MainVerts[1].v.y = 0; m_MainVerts[1].v.z = 0; m_MainVerts[1].tu = 0; m_MainVerts[1].tv = 0; m_MainVerts[1].rhw = 1; m_MainVerts[1].color = 0xFFFFFF; m_MainVerts[1].specular = 0; m_MainVerts[2].v.x = SCREEN_WIDTH - 1; m_MainVerts[2].v.y = SCREEN_HEIGHT - 1; m_MainVerts[2].v.z = 0; m_MainVerts[2].tu = 1; m_MainVerts[2].tv = 1; m_MainVerts[2].rhw = 1; m_MainVerts[2].color = 0xFFFFFF; m_MainVerts[2].specular = 0; m_MainVerts[3].v.x = SCREEN_WIDTH - 1; m_MainVerts[3].v.y = 0; m_MainVerts[3].v.z = 0; m_MainVerts[3].tu = 1; m_MainVerts[3].tv = 0; m_MainVerts[3].rhw = 1; m_MainVerts[3].color = 0xFFFFFF; m_MainVerts[3].specular = 0; hr = m_pID3DDevice->SetTexture(0, m_pID3DTextureBackground); hr = m_pID3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, m_MainVerts, sizeof(CCustomVertex)); I used this code to create and display the background (the second way) and couldn''t get it to display properly: hr = D3DXCreateTextureFromFile(m_pID3DDevice, _T("Game.bmp"), &m_pID3DTextureBackground); hr = D3DXCreateSprite(m_pID3DDevice, &m_pID3DXSprite); hr = m_pID3DXSprite->Draw(m_pID3DTextureBackground, NULL, &D3DXVECTOR2(0.5f, 0.5f), NULL, 0, &D3DXVECTOR2(0.0f, 0.0f), D3DCOLOR_XRGB(255, 255, 255)); I used this code to create and display the sprite and couldn''t get it to display properly: hr = D3DXCreateTextureFromFile(m_pID3DDevice, _T("Pieces.bmp"), &m_pID3DTextureSprite); hr = D3DXCreateSprite(m_pID3DDevice, &m_pID3DXSprite); hr = m_pID3DXSprite->Draw(m_pID3DTextureSprite, NULL, &D3DXVECTOR2(0.5f, 0.5f), NULL, 0, &D3DXVECTOR2(400.0f, 300.0f), D3DCOLOR_XRGB(255, 255, 255));
Todd Geraty
Todd Geraty
Any ideas anyone? I''d love to figure this one out and see how other people are doing this.

Thanks for any help.
Todd
DrunkenHyena
DrunkenHyena
G''day!

Textures need to be powers of 2 in size. Your 400x300 sprite is being re-sized to 512x512.

I haven''t used the D3DXSprite stuff yet, so I can''t tell you exactly how to get the results you want. Edit your image so it''s 512x512 (add blank space on the edges), then try specifying the source rectangle as 400x300 when you draw the sprite. That might work.


Stay Casual,

Ken
Stay Casual,KenDrunken Hyena</FON
Todd Geraty
Todd Geraty
Ahhh, very interesting. To be honest, I think I may have read that somewhere because that sounds familiar - I must not have been paying attention though ;-)

If that''s the case, then what is the largest size texture that DirectX supports? My background is 800x600, so it must have been resizing to 1024x1024 (thus too large for my screen - which I was experiencing). And when I scaled it to (.5, .5), it must have been the 512x512 you were talking about. But it sounds like at least the 1024x1024 size is supported.

Is the max size of a texture a function of the video card? I thought I also read somewhere that it was. And if so, what would be a safe size to use to support all video cards? Would that mean that I would need to do multiple textures just to do the background image? That seems pretty cumbersome if that''s the case, is there an easier way? It definitely works on my card (Geforce2), but I would like it to work on most of them.

Thanks alot for the help - I really appreciate it!

Todd
SeanHowe
SeanHowe
Also, you might have a problem with your projection matrix, make sure you''re setting the aspect ratio to the width of the viewport divided by its height. However, I''m not really familiar with the ID3DXSprite interface, I''m not sure if uses T&L vertices so I can''t guarantee this will work.
Todd Geraty
Todd Geraty
m_MainVerts was actually just a class I created (from looking at a tutorial''s example somewhere):

class CCustomVertex
{
public:
D3DXVECTOR3 v;
FLOAT rhw;
DWORD color;
DWORD specular;
FLOAT tu, tv;
};

...and it seemed to work just fine. But using this method didn''t allow me to use the ID3DXSprite interface.

BUT, I do have some good news though. I followed DrunkenHyena''s advice, and I changed my textures to be perfect squares (1024x1024 for the background and 128x128 for the sprite) by just adding blank space. And then I ripped out that CCustomVertex and used the ID3DXSprite interface and cut the rectangles out that I needed, and it worked beautifully!

As a matter of fact, before the background seemed kind of ''fuzzy''. I guess that''s how you describe it... But anyway, I had some sharpe edges in the graphic that just didn''t look exactly like they do in Paint Shop Pro, or whatever I used to view it. But now, doing it this way, it looks great! The edges are sharp and everything - exactly like the paint programs...

My only concern is the size of the texture now. Should I be safe and load them in 256x256 at a time? That way I''m covering most cards? Or should I even bother - thinking that most cards handle larger textures? What do other people tend to do? I guess I''m thinking mainly of the background of the game because that''s 1024x1024...

Is there a place that lists the standard device caps for most cards? Man, that would be really helpful for people who don''t have lots of cards to test against. I''ve been wondering this for a while now. But hey, I have a TNT on another machine. I''ll just have to look at the device caps for that, because I don''t mind not supporting anything earlier than that. But that doesn''t help for non-nVidia cards...

Anyway, thanks for the help!
Todd

DrunkenHyena
DrunkenHyena
G''day!

Maximum texture size is video card dependant. Voodoo (I think all of them, not sure) are limited to 256x256. All of the cards I''m aware of require power of 2 sizes, though not all require them to be square. There are CAPS to check all of this, but I can''t recall where it all is.

Multiple textures is the common way of doing it, especially if supporting older cards is important to you. If you''re just doing this for fun and don''t plan on sharing, then as long as it works for you it''s fine. Of course having your texture blown up to 1024x1024 is going to waste quite a bit of memory.

I limit myself to 256x256 which can definitely be a pain, but I want everyone to enjoy the results of my genius.

Stay Casual,

Ken
www.drunkenhyena.com
Stay Casual,KenDrunken Hyena</FON
DrunkenHyena
DrunkenHyena
G''day!

quote:
Original post by Todd Geraty

As a matter of fact, before the background seemed kind of ''fuzzy''. I guess that''s how you describe it... But anyway, I had some sharpe edges in the graphic that just didn''t look exactly like they do in Paint Shop Pro, or whatever I used to view it. But now, doing it this way, it looks great! The edges are sharp and everything - exactly like the paint programs...



Yeah, it would be fuzzy. First it gets filtered when blown up, then filtered again when you scaled it down. Nasty.

quote:

Is there a place that lists the standard device caps for most cards? Man, that would be really helpful for people who don''t have lots of cards to test against. I''ve been wondering this for a while now. But hey, I have a TNT on another machine. I''ll just have to look at the device caps for that, because I don''t mind not supporting anything earlier than that. But that doesn''t help for non-nVidia cards...



A database of device caps has been proposed many times. The problem is no one seems to have the resources to pull it off. I was going to, but I just don''t have the time. My web site has a mySQL database and all that good stuff but it''d still be a lot of effort to get it all going. Then you have to get all the CAPS from the cards. And CAPS can change based on the driver version you''re using as well!

Stay Casual,

Ken
www.drunkenhyena.com
Stay Casual,KenDrunken Hyena</FON
DrunkenHyena
DrunkenHyena
quote:
Original post by Anonymous Poster
What type is m_MainVerts[]? It does not look like the TLVERTEX type, which has .sx instead of v.x, etc. Coordinates. This refer to absolute screen coordinates. Also, you may need to set up a viewport to the size of your screen.



G''day!

D3D8 threw out all the old vertex structure (TLVERTEX etc). Now you define a custom structure (or class) and then use flags to specify what type of vertex it is.

Stay Casual,

Ken
www.drunkenhyena.com
Stay Casual,KenDrunken Hyena</FON
Todd Geraty
Todd Geraty
Great information! Thanks!

I agree with you - and I think I'll limit myself to 256x256 for my textures. For right now, it only affects my background.

I didn't realize that every different driver for the card can have different device caps - but that does make a lot of sense now that I think of it. Very interesting... I can see how that would almost be impossible to get a database of device caps then. Even if someone wrote a program that was widely distributed that people can query the caps and send the results in - it would still leave out lots of driver versions even though it would cover the market for cards pretty well. Oh well, if I plan for the worst, it should work fine.

Anyway, thanks for all the info - it's helped a lot. Now on to a couple DirectInput problems! ;-)

Todd


Edited by - Todd Geraty on March 12, 2001 9:58:12 PM
DrunkenHyena
DrunkenHyena
quote:
Original post by Todd Geraty
I didn''t realize that every different driver for the card can have different device caps - but that does make a lot of sense now that I think of it. Very interesting... I can see how that would almost be impossible to get a database of device caps then. Even if someone wrote a program that was widely distributed that people can query the caps and send the results in - it would still leave out lots of driver versions even though it would cover the market for cards pretty well. Oh well, if I plan for the worst, it should work fine.



G''day!

I haven''t completely given up on the CAP database yet. Right now though with my tutorials web site, writing a game (or 3) on the side and my day job I don''t have the time.

I''m thinking of contacting the companies and seeing if they''d be willing to provide the basic CAPS for their cards in a standard format. NVidia & ATI both seem quite willing to help developers, not sure about the others.


Stay Casual,

Ken
Drunken Hyena
Stay Casual,KenDrunken Hyena</FON
Todd Geraty
Todd Geraty
Yeah, I think it''s a great idea.

If I weren''t so busy with work and learning DirectX 8 to finish my first game, I''d probably take on this side project - because I think it would be extremely useful for people.

Maybe someday I''ll get around to it though. I just have to convince myself to do that rather than a game for a short time!!

Thanks again for the info! My game''s working great right now using the Sprite interface, and I''ll probably be finished soon.

Todd

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.