Skip to content

Commit

Permalink
Merge pull request #47 from JD557/put-pixel-try-catch
Browse files Browse the repository at this point in the history
Wrap rendering operations inside a try-catch
  • Loading branch information
JD557 authored Jan 3, 2021
2 parents 36cc2a8 + d71efd9 commit ac06590
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ class HtmlCanvas(val settings: Canvas.Settings) extends LowLevelCanvas {
buffer.data(baseAddr + 2) = c.b
}

def putPixel(x: Int, y: Int, color: Color): Unit =
def putPixel(x: Int, y: Int, color: Color): Unit = try {
if (settings.scale == 1) putPixelUnscaled(x, y, color)
else putPixelScaled(x, y, color)
} catch { case _: Throwable => () }

def getBackbufferPixel(x: Int, y: Int): Color = {
val baseAddr = 4 * (y * settings.scale * settings.scaledWidth + (x * settings.scale))
Expand Down Expand Up @@ -123,10 +124,10 @@ class HtmlCanvas(val settings: Canvas.Settings) extends LowLevelCanvas {
}
}
if (resources.contains(Canvas.Resource.Keyboard)) {
keyboardInput = keyboardInput.clearPressRelease
keyboardInput = keyboardInput.clearPressRelease()
}
if (resources.contains(Canvas.Resource.Pointer)) {
pointerInput = pointerInput.clearPressRelease
pointerInput = pointerInput.clearPressRelease()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ class AwtCanvas(val settings: Canvas.Settings) extends LowLevelCanvas {
y * settings.scaledWidth + x % settings.scaledWidth,
c.argb)

def putPixel(x: Int, y: Int, color: Color): Unit =
def putPixel(x: Int, y: Int, color: Color): Unit = try {
if (settings.scale == 1) putPixelUnscaled(x, y, color)
else putPixelScaled(x, y, color)
} catch { case _: Throwable => () }

def getBackbufferPixel(x: Int, y: Int): Color = {
Color.fromRGB(javaCanvas.imagePixels.getElem(y * settings.scale * settings.scaledWidth + (x * settings.scale)))
Expand Down Expand Up @@ -92,12 +93,12 @@ class AwtCanvas(val settings: Canvas.Settings) extends LowLevelCanvas {
}
}

def redraw(): Unit = {
def redraw(): Unit = try {
val g = javaCanvas.buffStrategy.getDrawGraphics()
g.drawImage(javaCanvas.image, 0, 0, settings.scaledWidth, settings.scaledHeight, javaCanvas)
g.dispose()
javaCanvas.buffStrategy.show()
}
} catch { case _: Throwable => () }

def getKeyboardInput(): KeyboardInput = keyListener.getKeyboardInput()
def getPointerInput(): PointerInput = mouseListener.getPointerInput()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ class SdlCanvas(val settings: Canvas.Settings) extends LowLevelCanvas {
surface.pixels(baseAddr + 2) = c.r.toByte
}

def putPixel(x: Int, y: Int, color: Color): Unit =
def putPixel(x: Int, y: Int, color: Color): Unit = try {
if (settings.scale == 1) putPixelUnscaled(x, y, color)
else putPixelScaled(x, y, color)
} catch { case _: Throwable => () }

def getBackbufferPixel(x: Int, y: Int): Color = {
// Assuming a BGRA surface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object CanvasIO {
* This operation can be perfomance intensive, so it might be worthwile
* to implement this operation on the application code.
*/
val getBackbuffer: CanvasIO[Vector[Vector[Color]]] = accessCanvas(_.getBackbuffer)
val getBackbuffer: CanvasIO[Vector[Vector[Color]]] = accessCanvas(_.getBackbuffer())

/** Gets the current keyboard input. */
val getKeyboardInput: CanvasIO[KeyboardInput] = accessCanvas(_.getKeyboardInput())
Expand Down

0 comments on commit ac06590

Please sign in to comment.