feat(renderer): Enhance star generation by introducing REFERENCE_STARS for viewport scaling

This commit is contained in:
extio-de
2025-12-25 16:49:55 +01:00
parent 98cb282ab8
commit 4648c1241b

View File

@@ -14,10 +14,10 @@ import java.util.Objects;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import de.extio.game_engine.renderer.RendererControl;
import de.extio.game_engine.renderer.g2d.G2DRendererCondition; import de.extio.game_engine.renderer.g2d.G2DRendererCondition;
import de.extio.game_engine.renderer.g2d.G2DRendererControl; import de.extio.game_engine.renderer.g2d.G2DRendererControl;
import de.extio.game_engine.renderer.model.RenderingBo; import de.extio.game_engine.renderer.model.RenderingBo;
@@ -43,6 +43,8 @@ public class G2DDrawBackground extends G2DAbstractRenderingBo {
private final static List<Star> STARS = new ArrayList<>(); private final static List<Star> STARS = new ArrayList<>();
private final static List<Star> REFERENCE_STARS = new ArrayList<>();
private static CoordI2 STARS_LAST_VIEWPORT = ImmutableCoordI2.create(); private static CoordI2 STARS_LAST_VIEWPORT = ImmutableCoordI2.create();
private final CoordI2 bgrOffset = MutableCoordI2.create(); private final CoordI2 bgrOffset = MutableCoordI2.create();
@@ -232,19 +234,33 @@ public class G2DDrawBackground extends G2DAbstractRenderingBo {
} }
private void generateStars(final CoordI2 viewPort) { private void generateStars(final CoordI2 viewPort) {
if (STARS.isEmpty() || !viewPort.equals(STARS_LAST_VIEWPORT)) { if (REFERENCE_STARS.isEmpty()) {
STARS.clear();
STARS_LAST_VIEWPORT = viewPort.toImmutableCoordI2();
final var rand = ThreadLocalXorShift128Random.current(); final var rand = ThreadLocalXorShift128Random.current();
for (var i = 0; i < 350; i++) { for (var i = 0; i < 350; i++) {
STARS.add(new Star( final var position = ImmutableCoordI2.create(rand.nextInt(RendererControl.REFERENCE_RESOLUTION.getX()), rand.nextInt(RendererControl.REFERENCE_RESOLUTION.getY()));
ImmutableCoordI2.create(rand.nextInt(STARS_LAST_VIEWPORT.getX()), rand.nextInt(STARS_LAST_VIEWPORT.getY())), final var radius = ImmutableCoordI2.create(rand.nextInt(3) + 2, rand.nextInt(3) + 2);
ImmutableCoordI2.create(rand.nextInt(3) + 2, rand.nextInt(3) + 2), final var color = new ImmutableRgbaColor(rand.nextInt(30) + 100, rand.nextInt(30) + 100, rand.nextInt(30) + 130);
new ImmutableRgbaColor(rand.nextInt(30) + 100, rand.nextInt(30) + 100, rand.nextInt(30) + 130)));
REFERENCE_STARS.add(new Star(position, radius, color));
STARS.add(new Star(position, radius, color));
} }
} }
if (!viewPort.equals(STARS_LAST_VIEWPORT)) {
final double scaleX = (double) viewPort.getX() / RendererControl.REFERENCE_RESOLUTION.getX();
final double scaleY = (double) viewPort.getY() / RendererControl.REFERENCE_RESOLUTION.getY();
for (int i = 0; i < REFERENCE_STARS.size(); i++) {
final var refStar = REFERENCE_STARS.get(i);
final var star = STARS.get(i);
star.position = ImmutableCoordI2.create(
(int) (refStar.position.getX() * scaleX),
(int) (refStar.position.getY() * scaleY));
}
STARS_LAST_VIEWPORT = viewPort.toImmutableCoordI2();
}
} }
private static class Star { private static class Star {