XNA Creators Club Online
Page 1 of 1 (19 items)
Sort Posts: Previous Next

Whats the best way to resize textures?

Last post 12/7/2009 10:19 PM by Shawn Hargreaves. 18 replies.
  • 9/21/2009 9:35 AM

    Whats the best way to resize textures?

    I have a problem resizing textures.

    Have a mission screen with a 15 frame animation that plays when you enter the screen and a 17 frame animation when you leave it.
    The plane loadout screen has the same animations but it does not resize. I don't get any error on that screen so I know its the resize.

    When you run the game, there are no errors generated by the 2 screens but if you go back to the screen before the mission select screen and go through again, then I get an error.

    Error message:
    <
    StartRendering : All active render targets must be the same size with the same multisample type.
    Source: Void VerifyDepthRenderTargetCompat()
    Stack trace:    at Microsoft.Xna.Framework.Graphics.GraphicsDevice.VerifyDepthRenderTargetCompat()
       at Microsoft.Xna.Framework.Graphics.GraphicsDevice.Clear(ClearOptions options, Color color, Single depth, Int32 stencil, Rectangle[] regions)
       at Microsoft.Xna.Framework.Graphics.GraphicsDevice.Clear(Color color)
       at ChaseCameraSample.ChaseCameraGame.StartRendering() in G:\Business\GameCo\Dev\PrizeFlyerCanyons1\ChaseCamera\Game.cs:line 749
    >

    The code:

     public Texture2D resizeTexture(Texture2D sourceTexture, int newWidth,int newHeight)
            {
                Texture2D resultTexture;
                GraphicsDevice graphicsDevice;
                RenderTarget2D renderTarget;
                Rectangle destinationRectangle;

                graphicsDevice = graphics.GraphicsDevice;

                renderTarget = new RenderTarget2D(graphicsDevice,
                newWidth, newHeight,
                1,
                graphics.GraphicsDevice.PresentationParameters.BackBufferFormat);//SurfaceFormat.Color

                
                destinationRectangle = new Rectangle(0, 0, newWidth, newHeight);
                   
                graphicsDevice.SetRenderTarget(0, renderTarget);
                spriteBatchl.Begin();
                spriteBatchl.Draw(sourceTexture, destinationRectangle, Color.White);
                spriteBatchl.End();

                graphicsDevice.SetRenderTarget(0, null);
               resultTexture = renderTarget.GetTexture();
                

                return (resultTexture);
            }

    How can I fix this problem?


  • 9/21/2009 1:21 PM In reply to

    Re: Whats the best way to resize textures?

    Why do you need a new resized texture rather than simply drawing your original texture at the new size (the same way you are when you draw on the new render target here)?

    If there is a reason you need to use this approach, are your newWidth and newHeight meant to be different from the screen size? The most direct way to remove the error would be to ensure you are creating the new RenderTarget 2D with the same size as the default render target, which you can get using graphics.GraphicsDevice.Viewport.Width and graphics.GraphicsDevice.Viewport.Height, though if you are trying to generate a smaller texture you would have some unwanted empty space. You could chop off that empty space when drawing the texture by specifying a source rectangle, but then we return to the question of why not draw the original texture at the desired size?

    If I'm misunderstanding your purposes, please do explain further and I'll see if I can help.
  • 9/23/2009 7:50 PM In reply to

    Re: Whats the best way to resize textures?

    Thanks for the reply.

    The reason why I am fussing with resizing bitmaps is because we have an xbox game but people need to test it on windows. 5 people on the team but only 2 have an Xbox.

    In windows the screen size is 640x480 but on an xbox the size could be all sorts of sizes. So I am testing using one set of bitmaps and resizing them to fit.

    The animations that I was talking about earlier are using 1080x720 sized bitmaps and unless you resize the frames its not going to look good.
    The displayed frames get cropped on a smaller screen but if I resize the frames they fit.

    Resizing the bitmaps every time you draw will work but its not going to be as fast as resizing the bitmaps on load and then just displaying the resized bitmap.

    Yeah, there are ways to dance around the issue but I want to know how to resize bitmaps any size I want.
    Do the resize once and then save the resized bitmap.

    If its not possible, hopefully I can get a good explanation why and maybe an alternative.




  • 9/23/2009 8:19 PM In reply to

    Re: Whats the best way to resize textures?

    Actually you won't encounter many screen sizes on the XBox. The Xbox will automatically resize for you so most of us just set the resolution to 1280x720 and let the Xbox do its own thing.

    Check out the best practices guide:
    http://creators.xna.com/en-us/education/bestpractices

    What I normally do is use 1280x720 for widescreen and 956x720 for letterbox. Then I draw the same textures at the same scale, but change the position. When testing on windows I leave it at 1280x720.

    There are many posts about this, search "Resolutions Xbox" and you'll find your answer.

    Good luck!
  • 9/23/2009 11:12 PM In reply to

    Re: Whats the best way to resize textures?

    Thanks for the link, that will help later.

    Actually I want to use 1280x720 sized bitmaps but the people working in windows (640x480) will be screwed.
    Thats why I would like to be able to resize the bitmaps so the folks that don't have an xbox can still test the game.
    Don't want to have 2 sets of bitmaps one set for Windows and another for Xbox.

    If I can find out the proper way to resize bitmaps and store the results I can have 1 set of bitmaps.
  • 9/24/2009 12:23 AM In reply to

    Re: Whats the best way to resize textures?

    Are you planning on only releasing as an Xbox game? If so than I would really try to keep it all at one resolution. Any chance you can just get your testers to run a resolution higher than 1280x720 in order to keep your development time down?


  • 9/24/2009 2:39 AM In reply to

    Re: Whats the best way to resize textures?

    We plan to release an Xbox game but the problem is on the windows side.

    Its a small team of 5 people only 2 people have an xbox and the creator's club premium subscription.
    Everyone else will be testing on windows. I also do most of the testing on windows first too and then test on the xbox.

    I would like to be able to take bitmaps/textures and make smaller or larger copies of them for the windows version so we do not have to fuss with multiple sets of bitmaps.

  • 11/13/2009 7:58 AM In reply to

    Re: Whats the best way to resize textures?

    SevenOfEleven:
    Thanks for the link, that will help later.

    Actually I want to use 1280x720 sized bitmaps but the people working in windows (640x480) will be screwed.


    Maybe this is a silly question but why are the windows people using 640x480?
  • 11/13/2009 5:05 PM In reply to

    Re: Whats the best way to resize textures?

    SevenOfEleven:
    All active render targets must be the same size with the same multisample type.


    You need to create and set a depth buffer of the same size and multisample format as your rendertarget, or if you don't need a depth buffer, set it to null.

    At the moment you are leaving the default depth buffer set on the device, which is not compatible with your rendertarget because it is a different size and/or multisample format.
    XNA Framework Developer - blog - homepage
  • 11/19/2009 3:39 AM In reply to

    Re: Whats the best way to resize textures?

    Thanks for the reply.

    Have a better version of the code that will return a resized copy but there is a problem with disposing with the rendertarg and the function will fail if you use it too many times. Am also using Dispose on the rendertarg too.

    Want to be able to take a texture and make an arbitrary sized copy of it and save that copy.
    Do not want to do the spritebatch draw resize, when (if possible) I can resize a texture once and thats it.

    Is this possible and how can it be done in XNA?


  • 11/19/2009 3:44 AM In reply to

    Re: Whats the best way to resize textures?

    If you Dispose the rendertarget you created, the Texture2D you got from it will be gone too.  RenderTarget.GetTexture() just returns a reference to the texture associated with the rendertarget.  You'll need to keep the rendertarget alive until you are done with the texture.

    I don't understand why you don't just scale the original Texture2D when you draw it and avoid all this render target stuff.  It would be much simpler, and there aren't really any disadvantages.
  • 11/21/2009 9:04 PM In reply to

    Re: Whats the best way to resize textures?

    Thank you for that explanation, now I understand the situation a bit better.

    Do you think that taking a texture and resizing it once and then drawing with it is more
    efficient than resizing the texture over and over?
  • 11/24/2009 4:17 PM In reply to

    Re: Whats the best way to resize textures?

    The resize should essentially be free (this may not be true if you're downsizing the texture a lot, and you're *not* using mipmaps).
  • 11/24/2009 4:52 PM In reply to

    Re: Whats the best way to resize textures?

    SevenOfEleven:
    Do you think that taking a texture and resizing it once and then drawing with it is more
    efficient than resizing the texture over and over?


    Anything you can do once instead of every frame you should do only once. That being said, if your game runs fine using the simplest approach there is no need to worry about efficiency.
  • 12/1/2009 6:38 PM In reply to

    Re: Whats the best way to resize textures?

    You are correct.

    So far no one has really answered the question.
    Is this possible in XNA?



  • 12/1/2009 9:22 PM In reply to

    Re: Whats the best way to resize textures?

    SevenOfEleven:
    So far no one has really answered the question.
    Is this possible in XNA?


    Yes, it's possible. My previous answer says why your code is not working.

    However, I agree with the other posters that it's normally a better idea to just scale your images while you are drawing them to the screen. That's how I'd do it, anyway.
    XNA Framework Developer - blog - homepage
  • 12/2/2009 4:30 AM In reply to

    Re: Whats the best way to resize textures?

    If your only resizing textures on windows, why not resize the image through GDI, then load it into a texture? One thing to remember is DX9 and older gfx cards do not support unlimited number of render targets.
  • 12/7/2009 8:18 PM In reply to

    Re: Whats the best way to resize textures?

    Thank you for your help.

    I have been able to fix that problem and someone in this thread told me that rendertargs can not be made and disposed of easily.

    So I have to keep a copy Rendertarg around until the game quits.

    Is it possible to copy a certain region off of a rendertarg to another texture?



  • 12/7/2009 10:19 PM In reply to

    Re: Whats the best way to resize textures?

    SevenOfEleven:
    Is it possible to copy a certain region off of a rendertarg to another texture?


    You could do that using GetData and SetData.

    But, I really think you are going about this the wrong way!

    You will find it many, many times easier (and also more reliable, more efficient, etc) to leave your textures at their original size and just scale them during the draw call.
    XNA Framework Developer - blog - homepage
Page 1 of 1 (19 items) Previous Next