bryanedds

I have 2D texture font that I'm drawing to the screen. I'm going to make a text box which the font can be drawn on top of. What I want to do is draw the text so that it only draws inside the text box, with any text that overhangs the text box being clipped. How can I acheive this in XNA

Another example would be having a large picture insize a smaller gui panel with a scroll bar. Adjusting the scroll bar would show different parts of the picture while clipping the portions of the picture that extend outside the gui panel.

There are a lot of uses for this functionality. Does XNA provide it easily A Rectangle clip parameter in SpriteBatch.Draw would be the perfect thing, but I don't see anything like it :/




Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

Sonix2001

You can clip the texture using the SpriteBatch.Draw overload function:

SpriteBatch.Draw(Texture2D Texture, Rectangle DestRectangle, Rectangle SourceRectanlge, Color TextureColor)





Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

bryanedds

Unfortunately, that is not what I'm talking about. Maybe re-read the examples I've given It's moreso the destination area that needs the clipping, not the source.




Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

Sonix2001

bryanedds wrote:

Another example would be having a large picture insize a smaller gui panel with a scroll bar. Adjusting the scroll bar would show different parts of the picture while clipping the portions of the picture that extend outside the gui panel.

http://homepages.nildram.co.uk/~sonix/Scroll_and_clip.rar

Take a look at that quick example I wrote. It does exactly what you ask using the SpriteBatch.Draw overload.

I didn't include a scrollbar but you can use the up/down arrows on your keyboard to scroll the image.





Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

Fluxtah

I can imagine that your Text is either made out of small quads or using a spritebatch, if thats the case is there anyway to somehow calculate how many characters should be displayed and trim the quads accordingly

This may not be appropriate, what your after is kind of alpha blending, I am sure this can be done, I have seen something similar on this blog post http://blogs.msdn.com/etayrien/archive/2006/12/07/alpha-blending-part-1.aspx





Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

AndyL

Try changing the graphics device's viewport, that might do the trick. You will have to remember to reset it when you have finished.



Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

neogir

The part of XNAExtras (by former MS guy Gary Kacmarcik) named TextBox performs text rectangular clipping and wrapping  perfectly. With some more nice features.

Here it is (the XNA Beta2 version, but runs with XNA 1.0 with no issue for me)
http://blogs.msdn.com/garykac/archive/2006/12/01/xnaextras-update.aspx
     





Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

Jon Watte

What you really want is to set the scissor rectangle of the device: GraphicsDevice.ScissorRectangle. You have to do this on a per-primitive basis -- i e, per sprite batch, or whatever. DirectX will change the scissor to match the viewport when you change the viewport, but the draw-back is that the projective range -1 through 1 will map to the viewport, so you'd have to re-calculate your screen coordinates if you change the viewport.

If you don't want to change the scissor rectangle, then you can also calculate what sub-areas of textures you want to display in U/V space, and what screen-space coordinates you want to display this sub-area at, and generate your own vertices. I do that for my minimap, and it works pretty well (although overlay icons don't get clipped; they poke out a little bit). When I want to fix that, I'll enable the scissor rectangle.





Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

bryanedds

Thank Jon! That sounds like the answer :) I'll try it out. I wonder if it works on 3D stuff too




Re: XNA Game Studio Express Drawing on only a portion of the screen (2D clipping)

bryanedds

One thing that seems inefficient about ScissorRectangle is that you have to do a whole new sprite batch every time you change it... I would really prefer ScissorRectangle to be a simple parameter in spriteBatch.Draw if at all possible.