In the fps I'm making, I am making a 2D maze for the avatar to navigate in. I just finished coding it however whenever I compile it the app instantly crashes. The error that makes it crash is the following "Thread 1: Swift runtime failure: Range requires lowerBound <= upperBound" and occurs in this file Bitmap.swift
import UIKit
public struct Bitmap
{
public private(set) var pixels: [Color]
public let width: Int
public init(width: Int, pixels: [Color], height: Int)
{
self.width = width
self.pixels = pixels
}
}
public extension Bitmap
{
var height: Int
{
let bitmapheight = pixels.count / width
return bitmapheight
}
subscript(x: Int, y: Int) -> Color
{
get { return pixels[y * width + x] }
set {
guard x >= 0, y >= 0, x < width, y < height else {
return
}
pixels[y * width + x] = newValue}
}
init(width: Int, height: Int, color: Color) {
self.pixels = Array(repeating: color, count: width * height)
self.width = width
}
mutating func fill(rect: Rect, color: Color)
{
for y in Int(rect.min.y) ..< Int(rect.max.y)
{
for x in Int(rect.min.x) ..< Int(rect.max.x)
{
self[x, y] = color
}
}
}
}
at the line
for x in Int(rect.min.x) ..< Int(rect.max.x)
When I put a breakout on the line and run the code I see the following when the compiler runs the line:
min Engine.Vector x = (Double) 51.75 y = (Double) 0 max Engine.Vector x = (Double) 2 y = (Double) 51.75
I also get this at the terminal:
Engine was compiled with optimization - stepping may behave oddly; variables may not be available.
Since it's a big project and there's multiple files calling from one another in both the main game files and the game engine files, you can find the rest of the source code in the link below:
https://github.com/KingDecorpel12/RampageFPS
Game Engine files, including Bitmap.swift, will be in Rampage/Source/Engine while the main game files will be in Rampage/Source/Rampage. Any and all help will be greatly appreciated!.
FPS App crashes when compiling
I see the following when the compiler runs the line
The compiler is a tool to generate a binary, and it is not something to run a code. As far as I know, the error you find is a runtime error which would not be shown in compile-time.
Ignoring such terminology problems, it is clear why you see that error:
min Engine.Vector x = (Double) 51.75 y = (Double) 0 max Engine.Vector x = (Double) 2 y = (Double) 51.75
The left hand side of the range operator ..<
cannot be greater than the right hand side.
In your case shown, 51.75 (and Int
value of it) is clearly greater than 2.
You may need to find all the places you create such Rects in your project and fix them to fulfill the requirement of Range. Or create some sort of extension:
public extension Rect {
var minX: Double {Swift.min(min.x, max.x)}
var maxX: Double {Swift.max(min.x, max.x)}
var minY: Double {Swift.min(min.y, max.y)}
var maxY: Double {Swift.max(min.y, max.y)}
}
And use it like this:
for y in Int(rect.minY) ..< Int(rect.maxY) {
for x in Int(rect.minX) ..< Int(rect.maxX) {
self[x, y] = color
}
}