• Advertisement
Sign in to follow this  

[C#] Rectangle filled with Texture

This topic is 3084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI there. I have made a small class which creates a small drag box when a mouse is clicked a dragged, commonly used to select multiple items.
//Here is how I draw it :
public void DrawBox()
        {
            //Draw a Box with Specified with/Height at mousepos
            if (MousePressed)
            {                
                Rectangle areaRect = new Rectangle(
                    (int)StartMousePos.X,
                    (int)StartMousePos.Y,
                    (int)(currMousePos.X - StartMousePos.X),
                    (int)(currMousePos.Y - StartMousePos.Y));
 
                batchRef.Begin();
                batchRef.Draw(texture, areaRect, Color.White);
                batchRef.End();
            }
        }

//this is the Texture creation if its relevant :)
private void CreateTexture()
        {            
            texture = new Texture2D(dDevice, 1, 1, 1, TextureUsage.None, SurfaceFormat.Color);
            Color myColor = new Color(box_bgColor.R, box_bgColor.G, box_bgColor.B, boxTransparency);
            texture.SetData<Color>(new Color[] { myColor });                       
        }

so far it works well. However, it will only draw if the area of the rectangle if Negative (e.g. there is a -width and positive height). And nothing is draw when there is a positive area. (e.g. both width and height are positive and negative) Is this because I am drawing the rectangle incorrectly? or is it because of something XNA does to draw it? or am I simply being ignorant with my maths :P

Share this post


Link to post
Share on other sites
Advertisement
Your code will only work if the user drags the mouse down, to the right, or both. This is because the Rectangle you send must have a positive Width and Height. You'll have to check for this condition and adjust your values accordingly.

Share this post


Link to post
Share on other sites
ok, but then why does it not draw if I have both positive X and Y Value (Dragged UP & Left)?

at the moment it is only drawing if I drag Up & Right, Down and Left.

Share this post


Link to post
Share on other sites
If you use the spritebatch, it acts as if you drew 4 vertices to form the quad on which the rectangle is drawn, and the drawn Texture2D is the texture placed on those 4 points.
There is something called CullMode in GraphicsDevice.RenderState. It defines which triangles to draw. By default, only those triangles that are drawn clockwise will really get drawn. So if your mouse goes in the negative direction, the triangles would be in counterclockwise order, so they won't be drawn.

Try setting GraphicsDevice.RenderState.CullMode to CullMode.None and try again.
If you don't want to set this setting, you can also flip your drawn images, see the spritebatch overloads on how to do that.

I personally would recalculate the top left point and the size of the rectangle each time Update gets called.

//EDIT

Maybe this little picture helps you. It shows the order of the created vertices.
http://img11.imageshack.us/img11/4066/61544231.jpg

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement