mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-03 21:41:32 +00:00
Gjallarhorn: Reuse 2D graphics when building individual images.
This commit is contained in:
@ -10,19 +10,19 @@ class BlockDiversityRenderer(val expanse: BlockExpanse, quadPixelSize: Int = def
|
||||
BlockGridRenderer(quadPixelSize) {
|
||||
private val randomColorKey = RandomColorKey()
|
||||
|
||||
override fun render(map: BlockMap): BufferedImage = buildPixelQuadImage(expanse) { x, z ->
|
||||
override fun render(map: BlockMap): BufferedImage = buildPixelQuadImage(expanse) { graphics, x, z ->
|
||||
val maybeYBlocks = map.blocks[x]?.get(z)
|
||||
if (maybeYBlocks == null) {
|
||||
setPixelQuad(x, z, Color.white.rgb)
|
||||
setPixelQuad(graphics, x, z, Color.white.rgb)
|
||||
return@buildPixelQuadImage
|
||||
}
|
||||
val maxBlockState = maybeYBlocks.maxByOrNull { it.key }?.value
|
||||
if (maxBlockState == null) {
|
||||
setPixelQuad(x, z, Color.white.rgb)
|
||||
setPixelQuad(graphics, x, z, Color.white.rgb)
|
||||
return@buildPixelQuadImage
|
||||
}
|
||||
|
||||
val color = randomColorKey.map(maxBlockState.type)
|
||||
setPixelQuad(x, z, color.rgb)
|
||||
setPixelQuad(graphics, x, z, color.rgb)
|
||||
}
|
||||
}
|
||||
|
@ -2,24 +2,23 @@ package cloud.kubelet.foundation.gjallarhorn.render
|
||||
|
||||
import cloud.kubelet.foundation.gjallarhorn.state.BlockExpanse
|
||||
import java.awt.Color
|
||||
import java.awt.Graphics2D
|
||||
import java.awt.Rectangle
|
||||
import java.awt.image.BufferedImage
|
||||
|
||||
abstract class BlockGridRenderer(val quadPixelSize: Int = defaultQuadPixelSize) : BlockImageRenderer {
|
||||
protected fun BufferedImage.setPixelQuad(x: Long, z: Long, rgb: Int) {
|
||||
drawSquare(x * quadPixelSize, z * quadPixelSize, quadPixelSize.toLong(), rgb)
|
||||
protected fun setPixelQuad(graphics: Graphics2D, x: Long, z: Long, rgb: Int) {
|
||||
drawSquare(graphics, x * quadPixelSize, z * quadPixelSize, quadPixelSize.toLong(), rgb)
|
||||
}
|
||||
|
||||
protected fun BufferedImage.drawSquare(x: Long, y: Long, side: Long, rgb: Int) {
|
||||
val graphics = createGraphics()
|
||||
protected fun drawSquare(graphics: Graphics2D, x: Long, y: Long, side: Long, rgb: Int) {
|
||||
graphics.color = Color(rgb)
|
||||
graphics.fill(Rectangle(x.toInt(), y.toInt(), side.toInt(), side.toInt()))
|
||||
graphics.dispose()
|
||||
}
|
||||
|
||||
protected fun buildPixelQuadImage(
|
||||
expanse: BlockExpanse,
|
||||
callback: BufferedImage.(Long, Long) -> Unit
|
||||
callback: BufferedImage.(Graphics2D, Long, Long) -> Unit
|
||||
): BufferedImage {
|
||||
val widthInBlocks = expanse.size.x
|
||||
val heightInBlocks = expanse.size.z
|
||||
@ -28,11 +27,13 @@ abstract class BlockGridRenderer(val quadPixelSize: Int = defaultQuadPixelSize)
|
||||
val bufferedImage =
|
||||
BufferedImage(widthInPixels, heightInPixels, BufferedImage.TYPE_4BYTE_ABGR)
|
||||
|
||||
val graphics = bufferedImage.createGraphics()
|
||||
for (x in 0 until widthInBlocks) {
|
||||
for (z in 0 until heightInBlocks) {
|
||||
callback(bufferedImage, x, z)
|
||||
callback(bufferedImage, graphics, x, z)
|
||||
}
|
||||
}
|
||||
graphics.dispose()
|
||||
return bufferedImage
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ abstract class BlockHeatMapRenderer(quadPixelSize: Int = defaultQuadPixelSize) :
|
||||
clamp: FloatClamp,
|
||||
calculate: (Long, Long) -> Long?
|
||||
): BufferedImage =
|
||||
buildPixelQuadImage(expanse) { x, z ->
|
||||
buildPixelQuadImage(expanse) { graphics, x, z ->
|
||||
val value = calculate(x, z)
|
||||
val color = if (value != null) {
|
||||
val floatValue = clamp.convert(value)
|
||||
@ -21,6 +21,6 @@ abstract class BlockHeatMapRenderer(quadPixelSize: Int = defaultQuadPixelSize) :
|
||||
Color.white
|
||||
}
|
||||
|
||||
setPixelQuad(x, z, color.rgb)
|
||||
setPixelQuad(graphics, x, z, color.rgb)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user