Sign in to follow this  
uckevin111

C# Sprite Class

Recommended Posts

uckevin111    190
I just started doing some C# with DirectX and wanted to get some 2D graphics up and running. Things started out just fine, but when I use the Sprite class it seems that the bottom pixels and right pixels of my images are being cut off. I even tried to just add 1 to the width and height of the source rectangle to see if it would get the full image and it would get the bottom pixels but still not the right ones. I don't have the code on this computer, but essentially I have a class that controls all aspects of the sprite, including the actual Sprite object, the Texture object, the sourceRectangle, etc. I store all that information, and set the sourceRectangle to: _srcRect = new Rectangle(0, 0, width, height); Where width and height are passed in. I have been hard-coding them in, but I also tried this: Bitmap img = new Bitmap("image.bmp"); And used img.width and img.height to pass in the values. Then I have my call to Draw: _sprite.Draw(_texture, _srcRect, _center, _position, _alphaColor); The texture was loaded in with the device and filename, the srcRect as shown above, the _center as new Vector3(0,0,0), _position passed in as a Vector3, and _alpha as Color.White. Anyone have any ideas what might be my problem here? Sorry if this is confusing. I can get the actual code up later tonight if it's needed. Thanks. - Kevin [Edited by - uckevin111 on July 20, 2005 8:51:20 AM]

Share this post


Link to post
Share on other sites
uckevin111    190
Doesn't anyone have an idea of what might be wrong or what I might try to do? No one has had any similar problems?

Even if you don't know this problem, have you ever used the Sprite class with good results? If so, could you post some code on setting up the source rectangles, loading in the texture, and calling your Draw method?

I also tried _sprite.Draw2D and I was getting some really weird results on coordinates. I had the window size set to 800x600, and had a ball bouncing around the screen with tests for when it would hit the wall, but it would be way off. For instance, I had a check to make sure it didn't go past the right wall:

if( (_position.X + _width) > 800 )
{
_position.X = 800;
_xVelocity *= -1;
}

But it wouldn't even be close to the wall and would bounce away. There were other problems with coordinates as well with the Draw2D, so I just switched over to Draw, which alleviated most of my problems, but now it is cutting off my images.

Can anyone help please?

Share this post


Link to post
Share on other sites
uckevin111    190
Thanks for the reply. I actually did that at first, and just tried it again, and what happens is that it does display the whole graphic, but it also stretches all of my graphics. So, even though I have a block that is 75 pixels wide, and I confirmed this by printing it out from the code, if I put two blocks next to each other I have to space them apart 128 pixels so that they don't overlap.

I just can't make sense of all of this. It seems like I am having a lot of problems with the Draw2D and Draw methods from the Sprite class, even if I do very simple examples and follow the SDK. I don't know if it's something I'm doing wrong or something wrong with the methods, but what seems should be very simple is becoming very difficult.

Share this post


Link to post
Share on other sites
uckevin111    190
Hmmm, I did not know that. So if I'm going to use the Sprite class to make a 2D game, I have to make all my texture sizes a power-of-two? This seems that, unless your sprite has a repeating texture, it would impose quit a restriction on the sizes of your sprites. Do you know of any other way to create sprites or a 2D-based game, or am I pretty much stuck with this?

Thanks again.

Share this post


Link to post
Share on other sites
cyric74    100
I had a lot of problems with sprites as well, but after quite a bit of tinkering I was able to build a very reliable graphics class.

You can use Sprite.Transform before the draw call, so it's possible to flatten and squish an object into how you want it to be shaped using Matrix.Scale. This helps defeat power-of-2 restrictions, and remember that just because a texture is 128x128 pixels does not mean you have to fill every inch of the image with graphical data--just cut and paste your image into a power-of-2 texture file, then adjust the TextureRectangle to center on your image data.

Share this post


Link to post
Share on other sites
Daniel Miller    218
Quote:
Original post by uckevin111
Hmmm, I did not know that. So if I'm going to use the Sprite class to make a 2D game, I have to make all my texture sizes a power-of-two? This seems that, unless your sprite has a repeating texture, it would impose quit a restriction on the sizes of your sprites. Do you know of any other way to create sprites or a 2D-based game, or am I pretty much stuck with this?

Thanks again.


You can still have your same image, but you have to put it on a larger texture and surround it with something transparent (alpha or color key). Yep, it's a pain. [grin]

edit: I think there is another way, but it's harder and I don't know it. Try a search, or maybe someone else will know it.

edit2: Wow, the guy above me gave it.

[Edited by - Daniel Miller on July 20, 2005 11:27:08 AM]

Share this post


Link to post
Share on other sites
uckevin111    190
Thanks again guys. This is working so for now I'll stick with just this. I might research some other stuff later, but I don't want to get in too deep yet [grin].

Share this post


Link to post
Share on other sites

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