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) {
|
BlockGridRenderer(quadPixelSize) {
|
||||||
private val randomColorKey = RandomColorKey()
|
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)
|
val maybeYBlocks = map.blocks[x]?.get(z)
|
||||||
if (maybeYBlocks == null) {
|
if (maybeYBlocks == null) {
|
||||||
setPixelQuad(x, z, Color.white.rgb)
|
setPixelQuad(graphics, x, z, Color.white.rgb)
|
||||||
return@buildPixelQuadImage
|
return@buildPixelQuadImage
|
||||||
}
|
}
|
||||||
val maxBlockState = maybeYBlocks.maxByOrNull { it.key }?.value
|
val maxBlockState = maybeYBlocks.maxByOrNull { it.key }?.value
|
||||||
if (maxBlockState == null) {
|
if (maxBlockState == null) {
|
||||||
setPixelQuad(x, z, Color.white.rgb)
|
setPixelQuad(graphics, x, z, Color.white.rgb)
|
||||||
return@buildPixelQuadImage
|
return@buildPixelQuadImage
|
||||||
}
|
}
|
||||||
|
|
||||||
val color = randomColorKey.map(maxBlockState.type)
|
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 cloud.kubelet.foundation.gjallarhorn.state.BlockExpanse
|
||||||
import java.awt.Color
|
import java.awt.Color
|
||||||
|
import java.awt.Graphics2D
|
||||||
import java.awt.Rectangle
|
import java.awt.Rectangle
|
||||||
import java.awt.image.BufferedImage
|
import java.awt.image.BufferedImage
|
||||||
|
|
||||||
abstract class BlockGridRenderer(val quadPixelSize: Int = defaultQuadPixelSize) : BlockImageRenderer {
|
abstract class BlockGridRenderer(val quadPixelSize: Int = defaultQuadPixelSize) : BlockImageRenderer {
|
||||||
protected fun BufferedImage.setPixelQuad(x: Long, z: Long, rgb: Int) {
|
protected fun setPixelQuad(graphics: Graphics2D, x: Long, z: Long, rgb: Int) {
|
||||||
drawSquare(x * quadPixelSize, z * quadPixelSize, quadPixelSize.toLong(), rgb)
|
drawSquare(graphics, x * quadPixelSize, z * quadPixelSize, quadPixelSize.toLong(), rgb)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun BufferedImage.drawSquare(x: Long, y: Long, side: Long, rgb: Int) {
|
protected fun drawSquare(graphics: Graphics2D, x: Long, y: Long, side: Long, rgb: Int) {
|
||||||
val graphics = createGraphics()
|
|
||||||
graphics.color = Color(rgb)
|
graphics.color = Color(rgb)
|
||||||
graphics.fill(Rectangle(x.toInt(), y.toInt(), side.toInt(), side.toInt()))
|
graphics.fill(Rectangle(x.toInt(), y.toInt(), side.toInt(), side.toInt()))
|
||||||
graphics.dispose()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun buildPixelQuadImage(
|
protected fun buildPixelQuadImage(
|
||||||
expanse: BlockExpanse,
|
expanse: BlockExpanse,
|
||||||
callback: BufferedImage.(Long, Long) -> Unit
|
callback: BufferedImage.(Graphics2D, Long, Long) -> Unit
|
||||||
): BufferedImage {
|
): BufferedImage {
|
||||||
val widthInBlocks = expanse.size.x
|
val widthInBlocks = expanse.size.x
|
||||||
val heightInBlocks = expanse.size.z
|
val heightInBlocks = expanse.size.z
|
||||||
@ -28,11 +27,13 @@ abstract class BlockGridRenderer(val quadPixelSize: Int = defaultQuadPixelSize)
|
|||||||
val bufferedImage =
|
val bufferedImage =
|
||||||
BufferedImage(widthInPixels, heightInPixels, BufferedImage.TYPE_4BYTE_ABGR)
|
BufferedImage(widthInPixels, heightInPixels, BufferedImage.TYPE_4BYTE_ABGR)
|
||||||
|
|
||||||
|
val graphics = bufferedImage.createGraphics()
|
||||||
for (x in 0 until widthInBlocks) {
|
for (x in 0 until widthInBlocks) {
|
||||||
for (z in 0 until heightInBlocks) {
|
for (z in 0 until heightInBlocks) {
|
||||||
callback(bufferedImage, x, z)
|
callback(bufferedImage, graphics, x, z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
graphics.dispose()
|
||||||
return bufferedImage
|
return bufferedImage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ abstract class BlockHeatMapRenderer(quadPixelSize: Int = defaultQuadPixelSize) :
|
|||||||
clamp: FloatClamp,
|
clamp: FloatClamp,
|
||||||
calculate: (Long, Long) -> Long?
|
calculate: (Long, Long) -> Long?
|
||||||
): BufferedImage =
|
): BufferedImage =
|
||||||
buildPixelQuadImage(expanse) { x, z ->
|
buildPixelQuadImage(expanse) { graphics, x, z ->
|
||||||
val value = calculate(x, z)
|
val value = calculate(x, z)
|
||||||
val color = if (value != null) {
|
val color = if (value != null) {
|
||||||
val floatValue = clamp.convert(value)
|
val floatValue = clamp.convert(value)
|
||||||
@ -21,6 +21,6 @@ abstract class BlockHeatMapRenderer(quadPixelSize: Int = defaultQuadPixelSize) :
|
|||||||
Color.white
|
Color.white
|
||||||
}
|
}
|
||||||
|
|
||||||
setPixelQuad(x, z, color.rgb)
|
setPixelQuad(graphics, x, z, color.rgb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user