Example #1
0
        private void SetWindowIcon(IEnumerable <Image <Rgba32> > icons)
        {
            // Turn each image into a byte[] so we can actually pin their contents.
            // Wish I knew a clean way to do this without allocations.
            var images = icons
                         .Select(i => (MemoryMarshal.Cast <Rgba32, byte>(i.GetPixelSpan()).ToArray(), i.Width, i.Height))
                         .ToList();

            // ReSharper disable once SuggestVarOrType_Elsewhere
            Span <GCHandle>  handles    = stackalloc GCHandle[images.Count];
            Span <GlfwImage> glfwImages = new GlfwImage[images.Count];

            for (var i = 0; i < images.Count; i++)
            {
                var image = images[i];
                handles[i] = GCHandle.Alloc(image.Item1, GCHandleType.Pinned);
                var addrOfPinnedObject = (byte *)handles[i].AddrOfPinnedObject();
                glfwImages[i] = new GlfwImage(image.Width, image.Height, addrOfPinnedObject);
            }

            GLFW.SetWindowIcon(_glfwWindow, glfwImages);

            foreach (var handle in handles)
            {
                handle.Free();
            }
        }