-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #149 textures with higher resolutions #163
base: master
Are you sure you want to change the base?
Changes from all commits
33dadc6
8cf23a2
03de723
eb8ce02
483e528
902f8e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ public final class TextureManager { | |
/// Which mode to use in case an error was encountered when loading a texture (default = .ignore) | ||
/// This is considered only in DEBUG mode. | ||
public var errorMode: ErrorMode = .ignore | ||
/// The maximum screen scale value allowed | ||
/// It's used when there are no assets for textures in proper scales are found | ||
private let maxAllowedScale: Int = Int(Screen.maxScreenScale) | ||
|
||
internal private(set) var cache = [String : LoadedTexture]() | ||
|
||
|
@@ -47,8 +50,11 @@ public final class TextureManager { | |
|
||
// MARK: - Internal | ||
|
||
internal func load(_ texture: Texture, namePrefix additionalNamePrefix: String?, scale: Int?) -> LoadedTexture? { | ||
internal func load(_ texture: Texture, namePrefix additionalNamePrefix: String?, | ||
scale: Int?, otherAllowedScales: [Int]? = nil) -> LoadedTexture? { | ||
let scale = scale ?? defaultScale | ||
var otherAllowedScales = otherAllowedScales ?? allowedScales(beside: scale) | ||
|
||
let format = texture.format ?? defaultFormat | ||
var name = texture.name | ||
|
||
|
@@ -76,7 +82,7 @@ public final class TextureManager { | |
} | ||
|
||
guard let image = imageLoader.loadImageForTexture(named: name, scale: scale, format: format) else { | ||
guard scale > 1 else { | ||
guard otherAllowedScales.count > 0 else { | ||
#if DEBUG | ||
let errorMessage = "Texture image named '\(name)' could not be found" | ||
|
||
|
@@ -93,13 +99,24 @@ public final class TextureManager { | |
return nil | ||
} | ||
|
||
return load(texture, namePrefix: namePrefix, scale: scale - 1) | ||
return load(texture, namePrefix: namePrefix, | ||
scale: otherAllowedScales.removeLast(), otherAllowedScales: otherAllowedScales) | ||
} | ||
|
||
let texture = LoadedTexture(image: image, scale: scale) | ||
cache[cacheKey] = texture | ||
return texture | ||
} | ||
|
||
// MARK: - Private | ||
|
||
func allowedScales(beside defaultValue: Int?) -> [Int] { | ||
let minScale = 1 | ||
let maxScale = maxAllowedScale | ||
|
||
return [Int](minScale...maxScale) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this code is in a very performance-ciritical path (loading textures, which can occur many times per frame), I don't think we should create multiple collections like this. Instead, how about simply iterating by incrementing the scale up to the maximum point, just like we do when scaling downwards? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JohnSundell, I'm sorry, I somehow missed your report 😏 So you're saying I should just iterate up, and then down to 1? As for the tests, it's like I've stated in the opening of the pull request. They got broken because they've been searching for images that did not exist, so multiple resolutions filenames were added to names collection, without any images corresponding to them. As you say, I also think that new code should not break the tests, but this one broke. Also, I've forgotten to actually add test for the new scaling :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just have this suspicion that the tests worked because the scaling went only downwards. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I mean is, when the test looked like this: func testAnimatingWithSpriteSheet() {
let imageSize = Size(width: 300, height: 100)
let image = ImageMockFactory.makeImage(withSize: imageSize)
game.textureImageLoader.images["sheet.png"] = image.cgImage
var animation = Animation(
spriteSheetNamed: "sheet",
frameCount: 6,
rowCount: 2,
frameDuration: 1
)
animation.textureScale = 1
let actor = Actor()
actor.animation = animation
game.scene.add(actor)
XCTAssertEqual(actor.size, Size(width: 100, height: 50))
game.timeTraveler.travel(by: 1)
game.update()
XCTAssertEqual(actor.size, Size(width: 100, height: 50))
// game.textureImageLoader.images["sheet2.png"] = image.cgImage
// Assigning new sprite sheet mid-animation should update the animation
var newAnimation = Animation(
spriteSheetNamed: "sheet2",
frameCount: 6,
rowCount: 2,
frameDuration: 1
)
newAnimation.textureScale = 1
actor.animation = newAnimation
game.update()
XCTAssertEqual(game.textureImageLoader.imageNames, ["sheet.png", "sheet2.png"])
} I got There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I have probably broken something |
||
.filter { defaultValue == nil || $0 != defaultValue } | ||
} | ||
} | ||
|
||
public extension TextureManager { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.