diff --git a/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Plane.scala b/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Plane.scala index e625baa6..60b7b921 100644 --- a/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Plane.scala +++ b/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Plane.scala @@ -46,7 +46,10 @@ trait Plane extends Function2[Int, Int, Color] { outer => new Plane { def getPixel(x: Int, y: Int): Color = { val c1 = outer.getPixel(x, y) - that.getPixel(x, y).fold(c1)(c2 => f(c1, c2)) + if (x >= 0 && y >= 0 && x < that.width && y < that.height) { + val c2 = that.unsafeGetPixel(x, y) + f(c1, c2) + } else c1 } }, width = that.width, diff --git a/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Surface.scala b/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Surface.scala index 73db2509..c23b1ee7 100644 --- a/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Surface.scala +++ b/core/shared/src/main/scala/eu/joaocosta/minart/graphics/Surface.scala @@ -59,9 +59,24 @@ trait Surface { * @return color matrix */ def getPixels(): Vector[Array[Color]] = { - Vector.tabulate(height) { y => - Array.tabulate(width)(x => unsafeGetPixel(x, y)) + val b = Vector.newBuilder[Array[Color]] + b.sizeHint(height) + var y = 0 + while (y < height) { + if (width <= 0) { + b += Array.empty[Color] + } else { + val array = new Array[Color](width) + var x = 0 + while (x < width) { + array(x) = unsafeGetPixel(x, y) + x += 1 + } + b += array + } + y += 1 } + b.result() } /** Copies this surface into a new surface stored in RAM. */