Sign in to follow this  
Montynis

[.net] XNA sprite problem

Recommended Posts

Hello, I have loaded two blue panels in my XNA-based project (have a look at this): Free Image Hosting at www.ImageShack.us Everything is fine till I try to change the size of the window. While changing the size of the window, these srites change its size: Free Image Hosting at www.ImageShack.us When I stop changing the size of my program's window (when I release the left mouse button), these two sprites return to its origial size. Where is the problem and how to solve it (I really don't want these sprites to change its size while changing the size of my program's window)? Thanks in advance. [Edited by - Montynis on July 8, 2007 2:44:53 PM]

Share this post


Link to post
Share on other sites
Not sure why you wouldn't want your sprites re-sized with the window but..:-

How are you handling drawing your sprites?

IIRC the SpriteBatch class draws all sprites by rendering your texture onto a quad of which it renders into the scene as opposed to blitting a surface rect onto the frame buffer (like gud ol' directDraw does..)

I know you can use a blit function to draw WindowRect(?) objects directly into the frame buffer however I don't recall off head exactly how this works off head (you'll have to check the documentation)..

NOTE - if you do use the SpriteBatch system then you have to added benefit of shader support for adding rendering effects etc..

Share this post


Link to post
Share on other sites
Quote:
Original post by ArchangelMorph
Not sure why you wouldn't want your sprites re-sized with the window but..:-

How are you handling drawing your sprites?

IIRC the SpriteBatch class draws all sprites by rendering your texture onto a quad of which it renders into the scene as opposed to blitting a surface rect onto the frame buffer (like gud ol' directDraw does..)

I know you can use a blit function to draw WindowRect(?) objects directly into the frame buffer however I don't recall off head exactly how this works off head (you'll have to check the documentation)..

NOTE - if you do use the SpriteBatch system then you have to added benefit of shader support for adding rendering effects etc..


I am doing it like this:



spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
spriteBatch.Draw(myTexture, spritePosition, null, a, 0f, Vector2.Zero, 0.4f, SpriteEffects.None, 0f);
spriteBatch.End();


I don't want these sprites to be resized because they are UI elements.

Share this post


Link to post
Share on other sites
Quote:
Original post by Montynis
Quote:
Original post by ArchangelMorph
Not sure why you wouldn't want your sprites re-sized with the window but..:-

How are you handling drawing your sprites?

IIRC the SpriteBatch class draws all sprites by rendering your texture onto a quad of which it renders into the scene as opposed to blitting a surface rect onto the frame buffer (like gud ol' directDraw does..)

I know you can use a blit function to draw WindowRect(?) objects directly into the frame buffer however I don't recall off head exactly how this works off head (you'll have to check the documentation)..

NOTE - if you do use the SpriteBatch system then you have to added benefit of shader support for adding rendering effects etc..


I am doing it like this:



spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
spriteBatch.Draw(myTexture, spritePosition, null, a, 0f, Vector2.Zero, 0.4f, SpriteEffects.None, 0f);
spriteBatch.End();


I don't want these sprites to be resized because they are UI elements.

By original size do you mean original aspect ratio? In other words, is it the same shape as it was before you changed the window size?

I suspect what you want is to maintain the sprite's aspect ratio during the window resize event. I haven't messed with this before in XNA, but you may find the solution by creating a Window.ClientSizeChanged event and updating the spritePosition's right/bottom variables during it.

edit: Just messed with this and it looks like it doesn't become triggered during the resize but rather after a resize has happened. If there isn't some hidden event that I'm not seeing, maybe you could go with just "updating" the spritePosition in your Update loop. Trouble is that by default it doesn't seem to Update while resizing.

[Edited by - ferr on July 15, 2007 8:54:12 PM]

Share this post


Link to post
Share on other sites
you could always just use virtual coordinates for your user interface. As an example, the game window will be 1000x1000 in virtual units (not real screen coordinates). Now for screen coordinates, lets say your using 1024x768 resolution. So to plot your window...
Let x and y be the screen coordinates of the top left corner of the window
Let vx and vy be the virtual coordinates of the top left of the window
Let sw and sh be screen width and screen height respectively
sw = 1024;
sh = 768;
x = (vx/1000) * sw;
y = (vy/1000) * sh;

This will allow you to set your virtual coordinates for the window just once, you just update the screen width and height variables every time the window changes shape/resolution.

Think thats right...its late

Share this post


Link to post
Share on other sites
The problem with all of that is that the backbuffer size doesn't get updated until after the resize is complete. So you have no way to control the visual size of the items until resizing is done. That's why the sprites enlarge until you stop resizing the window.

Since things go back to the right size after the user stops resizing, I would just not worry about it.

If it really bothers you, you may be able to get the Form from the window handle and hook into the Resize event. Then you can adjust the backbuffer size on the fly. I don't know if hooking the Resize event will be successful, though.

I think what XNA does is call the Window.ClientSizeChanged event when the EndResize event occurs, rather than calling it for each Resize event.

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