From 8b89a2bb8c68a712fb61c7df707ed8342541c975 Mon Sep 17 00:00:00 2001 From: downthecrop Date: Sun, 27 Oct 2024 20:22:16 -0700 Subject: [PATCH] Faster transforms --- .../src/main/kotlin/KondoKit/plugin.kt | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/plugin-playground/src/main/kotlin/KondoKit/plugin.kt b/plugin-playground/src/main/kotlin/KondoKit/plugin.kt index aa3c242..8ee2e9f 100644 --- a/plugin-playground/src/main/kotlin/KondoKit/plugin.kt +++ b/plugin-playground/src/main/kotlin/KondoKit/plugin.kt @@ -155,8 +155,15 @@ class plugin : Plugin() { class AltCanvas : Canvas() { private var gameImage: VolatileImage? = null + private var op: AffineTransformOp? = null + private var transform: AffineTransform? = null + + private var flippedImage: BufferedImage? = null // Only used in HD private var bufferImage = BufferedImage(FIXED_WIDTH, FIXED_HEIGHT, BufferedImage.TYPE_INT_BGR) + private var lastImageWidth = -1 + private var lastImageHeight = -1 + init { isFocusable = true requestFocusInWindow() @@ -225,16 +232,6 @@ class plugin : Plugin() { } } - private fun calculateDrawDimensions(image: VolatileImage): Pair { - val imageAspect = image.width.toDouble() / image.height.toDouble() - val panelAspect = width.toDouble() / height.toDouble() - return if (imageAspect > panelAspect) { - width to (width / imageAspect).toInt() - } else { - (height * imageAspect).toInt() to height - } - } - private fun relayMouseEvent(e: MouseEvent) { requestFocusInWindow() val scale = minOf(width.toDouble() / gameImage!!.width, height.toDouble() / gameImage!!.height) @@ -259,22 +256,32 @@ class plugin : Plugin() { private fun renderGlRaster() { val width = gameImage!!.width val height = gameImage!!.height - val pixelData = GlRenderer.pixelData - bufferImage.setRGB(0, 0, width, height, pixelData, 0, width) + bufferImage.setRGB(0, 0, width, height, GlRenderer.pixelData, 0, width) - val transform = AffineTransform.getScaleInstance(1.0, -1.0) - transform.translate(0.0, -height.toDouble()) - val op = AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR) - val flippedImage = op.filter(bufferImage, null) + // Check if dimensions have changed + if (width != lastImageWidth || height != lastImageHeight) { + // Initialize or update transform and operation + transform = AffineTransform.getScaleInstance(1.0, -1.0).apply { + translate(0.0, -height.toDouble()) + } + op = AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR) + flippedImage = BufferedImage(width, height, bufferImage.type) + lastImageWidth = width + lastImageHeight = height + } + + // Apply the transform operation + op!!.filter(bufferImage, flippedImage) + + // Draw the flipped image onto gameImage gameImage?.createGraphics()?.apply { drawImage(flippedImage, 0, 0, null) dispose() } } - private fun renderSoftwareRaster() { gameImage?.createGraphics()?.apply { SoftwareRaster.frameBuffer.draw(this)