From 78af1ffd0344f58ecd6559e89c26c70c6222a570 Mon Sep 17 00:00:00 2001 From: Torsten Kammer Date: Sun, 5 May 2024 20:57:30 +0200 Subject: [PATCH] Accept the fact that PowerPC isn't coming back Not yet touching the SIMD code, though, because I'm lazy. --- GLLara/GLLModelMesh.swift | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/GLLara/GLLModelMesh.swift b/GLLara/GLLModelMesh.swift index 71141d5..affde19 100644 --- a/GLLara/GLLModelMesh.swift +++ b/GLLara/GLLModelMesh.swift @@ -295,22 +295,17 @@ import simd var elementSize: Int = 4 var countOfElements: Int = 0 - // Returns the element fo the index. If there is no element buffer (i.e. directly), returns its index + // Returns the element of the index. If there is no element buffer (i.e. directly), returns its index func element(at index: Int) -> Int { - guard let elementData = elementData else { + guard let elementData else { return index } - switch elementSize { - case 1: - return try! Int(elementData.readUInt8(at: index * 1)) - case 2: - return try! Int(elementData.readUInt16(at: index * 2)) - case 4: - return try! Int(elementData.readUInt32(at: index * 4)) - default: - assertionFailure("Unknown element component type") - return 0 + // This trickery only works on little endian machines. But nowadays, that's all of them + var result: Int = 0 + _ = withUnsafeMutableBytes(of: &result) { bytes in + elementData.copyBytes(to: bytes, from: index * elementSize ..< (index + 1) * elementSize) } + return result } // Returns the count of indices you can use with elementAt - that is either the number of elements, if there is an element buffer, or the number of vertices if drawing directly without one