What is UnsafeMutablePointer? How to modify the underlying memory?

后端 未结 2 1528
梦如初夏
梦如初夏 2020-12-28 22:33

I am trying to work with SpriteKit\'s SKMutableTexture class but I don\'t know how to work with UnsafeMutablePointer< Void >. I have a vague idea that it

2条回答
  •  悲哀的现实
    2020-12-28 23:08

    From the docs for SKMutableTexture.modifyPixelDataWithBlock:

    The texture bytes are assumed to be stored as tightly packed 32 bpp, 8bpc (unsigned integer) RGBA pixel data. The color components you provide should have already been multiplied by the alpha value.

    So, while you’re given a void*, the underlying data is in the form of a stream of 4x8 bits.

    You could manipulate such a structure like so:

    // struct of 4 bytes
    struct RGBA {
        var r: UInt8
        var g: UInt8
        var b: UInt8
        var a: UInt8
    }
    
    let tex = SKMutableTexture(size: CGSize(width: 10, height: 10))
    tex.modifyPixelDataWithBlock { voidptr, len in
        // convert the void pointer into a pointer to your struct
        let rgbaptr = UnsafeMutablePointer(voidptr)
    
        // next, create a collection-like structure from that pointer
        // (this second part isn’t necessary but can be nicer to work with)
        // note the length you supply to create the buffer is the number of 
        // RGBA structs, so you need to convert the supplied length accordingly...
        let pixels = UnsafeMutableBufferPointer(start: rgbaptr, count: Int(len / sizeof(RGBA))
    
        // now, you can manipulate the pixels buffer like any other mutable collection type
        for i in indices(pixels) {
            pixels[i].r = 0x00
            pixels[i].g = 0xff
            pixels[i].b = 0x00
            pixels[i].a = 0x20
        }
    }
    

提交回复
热议问题