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