Skip to content

Commit

Permalink
Lightmap add falloff
Browse files Browse the repository at this point in the history
  • Loading branch information
srcimon committed Jun 12, 2023
1 parent 507b2bb commit d839a4e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.srcimon.screwbox.core.graphics;

import io.github.srcimon.screwbox.core.Percent;
import io.github.srcimon.screwbox.core.loop.Loop;

import java.util.ArrayList;
Expand All @@ -16,6 +17,7 @@ public class GraphicsConfiguration {
private boolean useAntialiasing = false;
private int lightmapBlur = 4;
private int lightmapScale = 4;
private Percent lightFalloff = Percent.max();

/**
* Sets the resolution modifier for the light map. Higher values lower the
Expand Down Expand Up @@ -138,4 +140,13 @@ private void notifyListeners(final GraphicsConfigurationEvent.ConfigurationPrope
}
}

public GraphicsConfiguration lightFalloff(final Percent lightFalloff) {
this.lightFalloff = lightFalloff;
notifyListeners(GraphicsConfigurationEvent.ConfigurationProperty.LIGHT_FALLOFF);
return this;
}

public Percent lightFalloff() {
return lightFalloff;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public enum ConfigurationProperty {
WINDOW_MODE,
ANTIALIASING,
LIGHTMAP_BLUR,
LIGHTMAP_SCALE
LIGHTMAP_SCALE,
LIGHT_FALLOFF
}

public GraphicsConfigurationEvent(final Object source, final ConfigurationProperty changedProperty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,6 @@ private boolean isVisible(final Bounds lightBox) {
}

private void initLightmap() {
lightmap = new Lightmap(screen.size(), configuration.lightmapScale());
lightmap = new Lightmap(screen.size(), configuration.lightmapScale(), configuration.lightFalloff());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.srcimon.screwbox.core.graphics.internal;

import io.github.srcimon.screwbox.core.Percent;
import io.github.srcimon.screwbox.core.graphics.Offset;
import io.github.srcimon.screwbox.core.graphics.Size;
import io.github.srcimon.screwbox.core.graphics.WindowBounds;
import io.github.srcimon.screwbox.core.utils.MathUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
Expand All @@ -11,30 +13,32 @@

class Lightmap {

record PointLight(Offset position, int radius, List<Offset> area, io.github.srcimon.screwbox.core.graphics.Color color) {
record PointLight(Offset position, int radius, List<Offset> area,
io.github.srcimon.screwbox.core.graphics.Color color) {
}

record SpotLight(Offset position, int radius, io.github.srcimon.screwbox.core.graphics.Color color) {
}

private static final java.awt.Color FADE_TO_COLOR = AwtMapper.toAwtColor(io.github.srcimon.screwbox.core.graphics.Color.TRANSPARENT);
private static final float[] FRACTIONS = new float[] { 0.1f, 1f };

private final BufferedImage image;
private final Graphics2D graphics;
private final int resolution;
private final float[] fractions;

private final List<PointLight> pointLights = new ArrayList<>();
private final List<SpotLight> spotLights = new ArrayList<>();
private final List<WindowBounds> fullBrigthnessAreas = new ArrayList<>();

public Lightmap(final Size size, final int resolution) {
public Lightmap(final Size size, final int resolution, Percent lightFade) {
this.image = new BufferedImage(
size.width() / resolution + 1, // to avoid glitches add 1
size.height() / resolution + 1, // to avoid glitches add 1
BufferedImage.TYPE_INT_ARGB);
this.resolution = resolution;
this.graphics = (Graphics2D) image.getGraphics();
final float falloffValue = (float) MathUtil.clamp(0.1f, lightFade.invert().value(), 0.99f);
this.fractions = new float[]{falloffValue, 1f};
}

public void add(final WindowBounds fullBrightnessArea) {
Expand Down Expand Up @@ -108,7 +112,7 @@ private RadialGradientPaint radialPaint(final Offset position, final int radius,
position.x() / (float) resolution,
position.y() / (float) resolution,
usedRadius / (float) resolution,
FRACTIONS, colors);
fractions, colors);
}

public List<PointLight> pointLights() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public void initialize(final Entities entities) {
.add(new AreaTriggerSystem())
.add(new TimeoutSystem())
.add(new ResetSceneSystem())
.add(e -> {
if(e.mouse().justClickedLeft()) {
e.graphics().configuration().setLightmapScale(2);
}
e.window().setTitle(e.graphics().configuration().lightFalloff() + "");
e.graphics().configuration().lightFalloff(Percent.of(e.graphics().configuration().lightFalloff().value() + 0.02 * e.mouse().unitsScrolled()));
})
.add(new AutoFlipSpriteSystem())
.add(new BackgroundSystem())
.add(new CatMovementSystem())
Expand Down

0 comments on commit d839a4e

Please sign in to comment.