double buffering

This commit is contained in:
downthecrop 2024-10-27 21:38:12 -07:00
parent 8b89a2bb8c
commit c307db1e11

View file

@ -158,7 +158,7 @@ class plugin : Plugin() {
private var op: AffineTransformOp? = null private var op: AffineTransformOp? = null
private var transform: AffineTransform? = null private var transform: AffineTransform? = null
private var flippedImage: BufferedImage? = null // Only used in HD private var flippedImage: BufferedImage? = null
private var bufferImage = BufferedImage(FIXED_WIDTH, FIXED_HEIGHT, BufferedImage.TYPE_INT_BGR) private var bufferImage = BufferedImage(FIXED_WIDTH, FIXED_HEIGHT, BufferedImage.TYPE_INT_BGR)
private var lastImageWidth = -1 private var lastImageWidth = -1
@ -190,6 +190,7 @@ class plugin : Plugin() {
override fun addNotify() { override fun addNotify() {
super.addNotify() super.addNotify()
createBufferStrategy(2) // Double-buffering
validateGameImage() validateGameImage()
} }
@ -218,8 +219,11 @@ class plugin : Plugin() {
} }
} }
override fun paint(g: Graphics) { override fun paint(g: Graphics) {
val g2d = g as Graphics2D bufferStrategy?.let { strategy ->
val g2d = strategy.drawGraphics as? Graphics2D ?: return
g2d.color = Color.BLACK g2d.color = Color.BLACK
g2d.fillRect(0, 0, width, height) g2d.fillRect(0, 0, width, height)
@ -228,7 +232,10 @@ class plugin : Plugin() {
val x = ((width - image.width * scale) / 2).toInt() val x = ((width - image.width * scale) / 2).toInt()
val y = ((height - image.height * scale) / 2).toInt() val y = ((height - image.height * scale) / 2).toInt()
g2d.drawImage(image, x, y, (image.width * scale).toInt(), (image.height * scale).toInt(), null) g2d.drawImage(image, x, y, (image.width * scale).toInt(), (image.height * scale).toInt(), null)
Toolkit.getDefaultToolkit().sync() }
g2d.dispose() // Release the graphics context
strategy.show() // Display the buffer
} }
} }
@ -259,23 +266,18 @@ class plugin : Plugin() {
bufferImage.setRGB(0, 0, width, height, GlRenderer.pixelData, 0, width) bufferImage.setRGB(0, 0, width, height, GlRenderer.pixelData, 0, width)
// Check if dimensions have changed
if (width != lastImageWidth || height != lastImageHeight) { if (width != lastImageWidth || height != lastImageHeight) {
// Initialize or update transform and operation
transform = AffineTransform.getScaleInstance(1.0, -1.0).apply { transform = AffineTransform.getScaleInstance(1.0, -1.0).apply {
translate(0.0, -height.toDouble()) translate(0.0, -height.toDouble())
} }
op = AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR) op = AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR)
flippedImage = BufferedImage(width, height, bufferImage.type) flippedImage = BufferedImage(width, height, bufferImage.type)
lastImageWidth = width lastImageWidth = width
lastImageHeight = height lastImageHeight = height
} }
// Apply the transform operation
op!!.filter(bufferImage, flippedImage) op!!.filter(bufferImage, flippedImage)
// Draw the flipped image onto gameImage
gameImage?.createGraphics()?.apply { gameImage?.createGraphics()?.apply {
drawImage(flippedImage, 0, 0, null) drawImage(flippedImage, 0, 0, null)
dispose() dispose()