abstract render system: pixel types, screenshot saving

This commit is contained in:
Bixilon 2021-06-20 13:43:40 +02:00
parent 694e5a9489
commit 2c900298aa
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 56 additions and 22 deletions

View File

@ -63,10 +63,6 @@ class RenderWindow(
private lateinit var renderThread: Thread
val renderStats = RenderStats()
@Deprecated(message = "", replaceWith = ReplaceWith("window.size"))
val screenDimensions
get() = window.size
val inputHandler = RenderWindowInputHandler(this)
private var deltaFrameTime = 0.0
@ -328,11 +324,6 @@ class RenderWindow(
connection.sender.sendFakeChatMessage(RenderConstants.DEBUG_MESSAGES_PREFIX + message)
}
@Deprecated(message = "", replaceWith = ReplaceWith("window.clipboardText"))
fun getClipboardText(): String {
return window.clipboardText
}
fun assertOnRenderThread() {
check(Thread.currentThread() == renderThread) { "Current thread (${Thread.currentThread().name} is not the render thread!" }
}

View File

@ -94,7 +94,7 @@ open class TextField(
KeyCodes.KEY_V -> {
if (renderWindow.inputHandler.isKeyDown(KeyCodes.KEY_LEFT_CONTROL, KeyCodes.KEY_RIGHT_CONTROL)) {
// paste
textInput(renderWindow.getClipboardText())
textInput(renderWindow.window.clipboardText)
}
}
// ToDo: Up and down for line breaks, shift and ctrl modifier, ...

View File

@ -0,0 +1,24 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.system.base
enum class PixelTypes(val bytes: Int = 1) {
RED,
GREEN,
BLUE,
ALPHA,
RGB(3),
RGBA(4),
;
}

View File

@ -14,6 +14,8 @@
package de.bixilon.minosoft.gui.rendering.system.base
import de.bixilon.minosoft.gui.rendering.shader.Shader
import glm_.vec2.Vec2i
import java.nio.ByteBuffer
interface RenderSystem {
var shader: Shader?
@ -47,4 +49,6 @@ interface RenderSystem {
var depth: DepthFunctions
var depthMask: Boolean
fun readPixels(start: Vec2i, end: Vec2i, type: PixelTypes): ByteBuffer
}

View File

@ -16,15 +16,15 @@ package de.bixilon.minosoft.gui.rendering.system.opengl
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.modding.events.ResizeWindowEvent
import de.bixilon.minosoft.gui.rendering.shader.Shader
import de.bixilon.minosoft.gui.rendering.system.base.BlendingFunctions
import de.bixilon.minosoft.gui.rendering.system.base.DepthFunctions
import de.bixilon.minosoft.gui.rendering.system.base.RenderSystem
import de.bixilon.minosoft.gui.rendering.system.base.RenderingCapabilities
import de.bixilon.minosoft.gui.rendering.system.base.*
import de.bixilon.minosoft.modding.event.CallbackEventInvoker
import de.bixilon.minosoft.util.KUtil.synchronizedMapOf
import de.bixilon.minosoft.util.KUtil.synchronizedSetOf
import glm_.vec2.Vec2i
import org.lwjgl.BufferUtils
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL20.*
import java.nio.ByteBuffer
class OpenGLRenderSystem(
private val renderWindow: RenderWindow,
@ -113,6 +113,11 @@ class OpenGLRenderSystem(
field = value
}
override fun readPixels(start: Vec2i, end: Vec2i, type: PixelTypes): ByteBuffer {
val buffer: ByteBuffer = BufferUtils.createByteBuffer((end.x - start.x) * (end.y - start.y) * type.bytes)
glReadPixels(start.x, start.y, end.x, end.y, type.gl, GL_UNSIGNED_BYTE, buffer)
return buffer
}
companion object {
private val RenderingCapabilities.gl: Int
@ -160,5 +165,18 @@ class OpenGLRenderSystem(
else -> throw IllegalArgumentException("OpenGL does not support depth function: $this")
}
}
private val PixelTypes.gl: Int
get() {
return when (this) {
PixelTypes.RED -> GL_RED
PixelTypes.GREEN -> GL_GREEN
PixelTypes.BLUE -> GL_BLUE
PixelTypes.ALPHA -> GL_ALPHA
PixelTypes.RGB -> GL_RGB
PixelTypes.RGBA -> GL_RGBA
else -> throw IllegalArgumentException("OpenGL does not support pixel type: $this")
}
}
}
}

View File

@ -16,14 +16,12 @@ package de.bixilon.minosoft.gui.rendering.util
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.system.base.PixelTypes
import de.bixilon.minosoft.util.Util
import de.bixilon.minosoft.util.logging.Log
import de.matthiasmann.twl.utils.PNGDecoder
import org.lwjgl.BufferUtils
import org.lwjgl.opengl.GL11.*
import glm_.vec2.Vec2i
import java.awt.image.BufferedImage
import java.io.File
import java.nio.ByteBuffer
import java.text.SimpleDateFormat
import javax.imageio.ImageIO
@ -40,10 +38,9 @@ class ScreenshotTaker(
path = "${basePath}_${i++}.png"
}
val width = renderWindow.screenDimensions.x.toInt()
val height = renderWindow.screenDimensions.y.toInt()
val buffer: ByteBuffer = BufferUtils.createByteBuffer(width * height * PNGDecoder.Format.RGBA.numComponents)
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer)
val width = renderWindow.window.size.x
val height = renderWindow.window.size.y
val buffer = renderWindow.renderSystem.readPixels(Vec2i(0, 0), Vec2i(width, height), PixelTypes.RGBA)
Minosoft.THREAD_POOL.execute {
try {