feat(renderer): implement pattern caching in SteampunkPatternRenderer for improved background rendering

This commit is contained in:
extio-de
2025-12-31 11:14:12 +01:00
parent 974753c239
commit 2495be989b

View File

@@ -3,6 +3,7 @@ package de.extio.game_engine.renderer.g2d.theme;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;
@@ -14,6 +15,8 @@ import de.extio.game_engine.spatial2.model.CoordI2;
@Component
public class SteampunkPatternRenderer implements PatternRenderer {
private java.awt.image.BufferedImage patternCache;
@Override
public void drawDecorativeBorder(final Graphics2D g2d, final int x, final int y, final int width, final int height, final int strength, final Color color) {
final var s = Math.max(1, strength);
@@ -140,17 +143,20 @@ public class SteampunkPatternRenderer implements PatternRenderer {
g2d.fillRect(centerX - t / 2, centerY - crossSize / 2, t, crossSize);
}
@Override
public void drawBackgroundPattern(final Graphics2D g2d, final CoordI2 offset, final CoordI2 viewPort) {
g2d.setColor(new Color(210, 180, 140, 40)); // light brown for steam
private java.awt.image.BufferedImage getPatternCache() {
if (this.patternCache == null) {
final int cellSize = 100;
final int offX = Math.floorMod(offset.getX(), cellSize);
final int offY = Math.floorMod(offset.getY(), cellSize);
this.patternCache = new BufferedImage(cellSize, cellSize, java.awt.image.BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = this.patternCache.createGraphics();
for (int x = -cellSize; x < viewPort.getX() + cellSize; x += cellSize) {
for (int y = -cellSize; y < viewPort.getY() + cellSize; y += cellSize) {
final int cx = x + offX + cellSize / 2;
final int cy = y + offY + cellSize / 2;
// Clear background (transparent)
g2d.setBackground(new Color(0, 0, 0, 0));
g2d.clearRect(0, 0, cellSize, cellSize);
// Draw pattern
g2d.setColor(new Color(210, 180, 140, 40)); // light brown for steam
final int cx = cellSize / 2;
final int cy = cellSize / 2;
final int radius = cellSize / 6;
// Draw gear with more teeth
@@ -163,21 +169,33 @@ public class SteampunkPatternRenderer implements PatternRenderer {
}
// Draw connecting pipes
if (x > -cellSize) {
g2d.drawLine(cx - cellSize, cy, cx, cy); // horizontal pipe
// Horizontal pipe to left
g2d.drawLine(0, cy, cx, cy);
// Vertical pipe to top
g2d.drawLine(cx, 0, cx, cy);
// Draw steam puffs (small circles) at fixed positions
g2d.setColor(new Color(255, 255, 255, 20));
g2d.fillOval(cx - 10, cy - radius - 5, 4, 4);
g2d.fillOval(cx + 5, cy - radius - 10, 4, 4);
g2d.fillOval(cx + 15, cy - radius - 2, 4, 4);
g2d.dispose();
}
if (y > -cellSize) {
g2d.drawLine(cx, cy - cellSize, cx, cy); // vertical pipe
return this.patternCache;
}
// Draw steam puffs (small circles)
g2d.setColor(new Color(255, 255, 255, 20));
for (int i = 0; i < 3; i++) {
final int sx = cx + (int) ((Math.random() - 0.5) * radius * 2);
final int sy = cy - radius - (int) (Math.random() * radius);
g2d.fillOval(sx - 2, sy - 2, 4, 4);
}
g2d.setColor(new Color(210, 180, 140, 40));
@Override
public void drawBackgroundPattern(final Graphics2D g2d, final CoordI2 offset, final CoordI2 viewPort) {
final var cache = this.getPatternCache();
final int cellSize = cache.getWidth();
final int offX = Math.floorMod(offset.getX(), cellSize);
final int offY = Math.floorMod(offset.getY(), cellSize);
for (int x = -cellSize; x < viewPort.getX() + cellSize; x += cellSize) {
for (int y = -cellSize; y < viewPort.getY() + cellSize; y += cellSize) {
g2d.drawImage(cache, x + offX, y + offY, null);
}
}
}