Yay, talking about scissor rectangles again. I think this issue is more straight forward. I know I can hack my way around it but I’m hoping for a more enlightening answer.
To put it as simply as I can: because scissor rectangle dimensions are relative to your actual backbuffer/screen, I’m running into issue getting precision clipping. Here’s why…
I have a virtual resolution of 1280x720. I drop a 450x650 square into the top-center of the screen. This puts the X position at 415 (415 * 2 = 830 + 450 = 1280).
My real screen size is 1600x900. This gives a scale factor of 1.25. I make a scale matrix with that value and drop it into the SpriteBatch.Begin().
After applying the scale, I should expect my square to become: x: 518.75, y: 0, w: 562.5, h: 812.5
But whoops, we ended up with floats.
Now if I wanted my scissor rectangle to occupy the exact same position and size as my square, I presumed I could scale each of its values by 1.25 and be fine. After all, that’s the same thing SpriteBatch is doing with the matrix, right?
And yet, I’m off by one lousy pixel.
If you look carefully, the yellow bar is 1px outside the bounds of the blue square on the left. The yellow bar is being drawn by my scissored SpriteBatch, and the (scaled) scissorRect is the same dimensions and position as the blue square, so how is the yellow bar 1px outside the bounds of the blue square if the scissor is in the same spot as the blue square?
Now I presumed this was a rounding error… but my confusion rises as I’ve tried floor and ceiling on my scissorRect values and still get this error. I also checked all the values in the actual Draw() functions of my GUI’s classes to be absolutely sure the boxes were in the right position and the right dimensions, and they were. Everything gets reported being in just the place it should be… except the scissorRectangle.
I assumed that when SpriteBatch renders to the screen, it positions and scales by the scale factor of my matrix, which isn’t any different(I assume), than me manually scaling the Rectangle for my scissorRectangle by that same value… and yet the scissorRect doesn’t end up in the same place as the blue square despite them being precisely the same Rectangle getting scaled by precisely the same scaleFactor.
Now I know I could just add/subtract a few pixels here and there for the scissorRect to ensure it’s always inside the blue square, but have its size always be a few pixels smaller in each dimension. It wouldn’t be the end of the world or anything. I don’t absolutely NEED pixel-perfect precision for scissoring my GUI… but it sure would be nice just to KNOW why it’s happening.