I want to convert this into swift, or at least find something that does the same thing.
size_t width = CGImageGetWidth(spriteImage);
size_t height = CGImageGetHeight(spriteImage);
GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));
I need to initialize the spriteData pointer in swift of the right size.
The first two of those are of type size_t
, which maps to Uint
in Swift, and the last is GLubyte
, which maps to UInt8
. This code will initialize spriteData
as an array of GLubyte
, which you could pass to any C function that needs an UnsafeMutablePointer<GLubyte>
or UnsafePointer<GLubyte>
:
let width = CGImageGetWidth(spriteImage)
let height = CGImageGetHeight(spriteImage)
var spriteData: [GLubyte] = Array(count: Int(width * height * 4), repeatedValue: 0)
What do you need to do with spriteData
?
The straightforward way is:
var spriteData = UnsafeMutablePointer<GLubyte>.alloc(Int(width * height * 4))
Note that, if you did this, you have to dealloc
it manually.
spriteData.dealloc(Int(width * height * 4))
/// Deallocate `num` objects.
///
/// :param: num number of objects to deallocate. Should match exactly
/// the value that was passed to `alloc()` (partial deallocations are not
/// possible).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With