I want to create a Rectangle struct which has bytes for x, y, width and height fields instead of ints. It should be compatible with the usual Microsoft.Xna.Framework.Rectangle (casting as (bytesRectangle) should be ok). How do I do that?
Upd: using this struct helped me save 12 bytes per tile.
I’m interested to know the design choice to implement this struct. Looking at Microsoft.Xna.Framework.Rectangle we see there are more than 30 properties and members. This is a lot to reproduce to save a few bytes. If your only goal is to save some bytes there might be a better way. For example if you want to save the data in a smaller space you could define an object and a cast to store it. Something like this:
Of course a complete implementation would include testing to make sure there is no data loss on the casting etc.
Based on your comment below: This is not the direction to go. If you really need to save memory over XNA while using XNA, then you should make an Small rectangle manager class that does not store them the same way with the same attributes. But this class is good at creating the XNA ones when you need them. Don’t make the small rectangle class (or the manager) “compatable”.
The BEST way to store the data you need in memory is to just store the four BYTES with no overhead. The best way to store two bytes with no overhead is with the Tuple class.
So I suggest you make a rectangle manager class that internally stores the rectangles as Tuples and then “boxes” and “unboxes” them as needed into Xna objects.