Skip to content

Commit

Permalink
Cleanup of DDS loader
Browse files Browse the repository at this point in the history
The only user is fully Swift now, so no more "@objc".
  • Loading branch information
cochrane committed May 5, 2024
1 parent 6c71161 commit 8898648
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
51 changes: 24 additions & 27 deletions GLLara/GLLDDSFile.swift
Expand Up @@ -11,20 +11,6 @@ import Foundation
// All information about the DDS file format is taken from
// http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/graphics/reference/ddsfilereference/ddsfileformat.asp

@objc enum GLLDDSDataFormat: Int
{
case dxt1
case dxt3
case dxt5
case argb1555
case argb4
case rgb565
case bgr8
case bgra8
case rgba8
case bgrx8
}

/**
* # Parses DDS files.
*
Expand All @@ -33,7 +19,20 @@ import Foundation
* does not handle decompression and the like; it only provides the data to be
* loaded into the GPU.
*/
@objc class GLLDDSFile: NSObject {
class GLLDDSFile {
enum DataFormat {
case dxt1
case dxt3
case dxt5
case argb1555
case argb4
case rgb565
case bgr8
case bgra8
case rgba8
case bgrx8
}

struct DDSPixelFormat {
var size: UInt32 = 0
var flags: UInt32 = 0
Expand Down Expand Up @@ -66,25 +65,25 @@ import Foundation
var reserved2: UInt32 = 0
}

@objc let fileData: Data
@objc let width: Int
@objc let height: Int
@objc let numMipmaps: Int
@objc let dataFormat: GLLDDSDataFormat
let fileData: Data
let width: Int
let height: Int
let numMipmaps: Int
let dataFormat: DataFormat

@objc var hasMipmaps: Bool {
var hasMipmaps: Bool {
return numMipmaps > 0
}
@objc var isCompressed: Bool {
var isCompressed: Bool {
return dataFormat == .dxt1 || dataFormat == .dxt3 || dataFormat == .dxt5
}

@objc convenience init(contentsOf: URL) throws {
convenience init(contentsOf: URL) throws {
let data = try Data(contentsOf: contentsOf, options: [.mappedIfSafe])
try self.init(data: data)
}

@objc init(data: Data) throws {
init(data: Data) throws {
self.fileData = data

guard data.count >= 128 && data[0] == Character("D").asciiValue! && data[1] == Character("D").asciiValue! && data[2] == Character("S").asciiValue! && data[3] == Character(" ").asciiValue! else {
Expand Down Expand Up @@ -182,11 +181,9 @@ import Foundation
width = Int(header.width)
height = Int(header.height)
numMipmaps = Int(header.mipMapCount)

super.init()
}

@objc func data(mipmapLevel: Int) -> Data? {
func data(mipmapLevel: Int) -> Data? {
// Stupid implementation because I have a headache
var size = 0
var offset = 0
Expand Down
4 changes: 0 additions & 4 deletions GLLara/GLLTexture.swift
Expand Up @@ -168,10 +168,6 @@ import System
break;
case .bgrx8:
descriptor.pixelFormat = .bgra8Unorm
default:
throw NSError(domain:"Textures", code:12, userInfo:[
NSLocalizedDescriptionKey : String(format:NSLocalizedString("DDS File %@ couldn't be opened: Pixel format is not supported", comment: "Can't find pixel format"), self.url.lastPathComponent)
]);
}

texture = device.makeTexture(descriptor: descriptor)
Expand Down

0 comments on commit 8898648

Please sign in to comment.