endlesstravel / Love2dCS

C# Wrapper for LÖVE, a 2d game engine
MIT License
166 stars 14 forks source link

creating new imageData from color[,] instead of vector4[,] #98

Open Shadowblitz16 opened 4 years ago

Shadowblitz16 commented 4 years ago

please add these overloads

Shadowblitz16 commented 4 years ago

note this should probably be done with some sort of memory copying instead of looping so its faster

endlesstravel commented 4 years ago

ok, your are right, this overrid is convenient. :+1: i think i will add them to next version.

but i have no idea how to use memory copy to speed up. beacuse i use 4 byte on Color(byte,byte,byte,byte) but use (4 * 4 = 16)byte on Vector4(float,float,float,float). and with more complex situation, it depending on how ImageDataPixelFormat it used,

So, In so many cases, only two are suitable for memory replication, and [x, y] and [y, x] are not considered. So I'm not interested in doing this optimization. :tired_face:

Shadowblitz16 commented 4 years ago

I was using span and unsafe to do conversions from Color[,] to Color[]


            public Color[] Flatten(Color[,] v)
            {
                Color[] value;
                var length = v.GetLength(0) * v.GetLength(1);

                unsafe
                {
                    fixed (Color* p = v)
                    {
                        var span = new Span<Color>(p, length);
                        var slice = span.Slice(0);
                        value = slice.ToArray();
                    }
                }
                return value;
            }

not sure if it works since I never tested it but it would require unsafe. I would suggest postponing this until we actually know that you can wrap unsafe code in a managed library without needing the unsafe option to be checked.

keep this open I will get back to you