Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made tiling happen #35

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions src/snorri/world/BackgroundLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ public class BackgroundLayer implements Layer {

// TODO(lambdaviking): Probably want to make this a SavableLayer.

protected World world;
protected BufferedImage bitmap;
protected Vector dimensions;

public static final BufferedImage DEFAULT_BACKGROUND = Main.getImage("/textures/backgrounds/splash.png");
public static final int CUSHION = 0;

public BackgroundLayer() {
this(DEFAULT_BACKGROUND);
}

public BackgroundLayer(BufferedImage bitmap) {
public BackgroundLayer(World world, BufferedImage bitmap) {
this.bitmap = bitmap;
this.dimensions = new Vector(bitmap.getWidth(), bitmap.getHeight());
this.world = world;
}

public static Layer fromYAML(World world, Map<String, Object> params) {
Expand All @@ -40,34 +36,31 @@ public static Layer fromYAML(World world, Map<String, Object> params) {
File file = new File(world.getDirectory(), path);
bitmap = Main.getImage(file);
}
return new BackgroundLayer(bitmap);
return new BackgroundLayer(world, bitmap);
}

@Override
public void render(FocusedWindow<?> g, Graphics2D gr, double deltaTime, boolean renderOutside) {
int minX, minY;
public void render(FocusedWindow<?> window, Graphics2D gr, double deltaTime, boolean renderOutside) {
Vector center = window.getCenterObject().getPos();
Vector windowDimensions = window.getDimensions();
Vector origin = windowDimensions.divide(2).sub(center);

Vector center = g.getCenterObject().getPos();
Vector dim = g.getDimensions();
BufferedImage clipped = bitmap.getSubimage(0, 0, bitmap.getWidth(), world.getHeight());

if (bitmap == null) {
return;
for (int x = 0; x < world.getWidth(); x += bitmap.getWidth()) {
if (x + bitmap.getWidth() >= world.getWidth()) {
clipped = clipped.getSubimage(0, 0, world.getWidth() - x, clipped.getHeight());
}
gr.drawImage(clipped, origin.getX() + x, origin.getY(), null);
}

minX = center.getX() - dim.getX() / 2;
minY = center.getY() - dim.getY() / 2;
int adjMinX = Math.max(0, minX), adjMinY = Math.max(0, minY);
BufferedImage image = bitmap.getSubimage(adjMinX, adjMinY, Math.min(bitmap.getWidth() - adjMinX, dim.getX()), Math.min(bitmap.getHeight() - adjMinY, dim.getY()));

gr.drawImage(image, Math.max(0, -minX), Math.max(0, -minY), null);
}

public int getWidth() {
return dimensions.getX();
return world.getWidth();
}

public int getHeight() {
return dimensions.getY();
return world.getHeight();
}

@Override
Expand All @@ -78,19 +71,19 @@ public boolean canShootOver(Vector position) {
/** Background layers are not modified by transforms. */
@Override
public BackgroundLayer getTransposed() {
return new BackgroundLayer(this.bitmap);
return new BackgroundLayer(this.world, this.bitmap);
}

/** Background layers are not modified by transforms. */
@Override
public BackgroundLayer getXReflected() {
return new BackgroundLayer(this.bitmap);
return new BackgroundLayer(this.world, this.bitmap);
}

/** Background layers are not modified by transforms. */
@Override
public BackgroundLayer getResized(int newWidth, int newHeight) {
return new BackgroundLayer(this.bitmap);
return new BackgroundLayer(this.world, this.bitmap);
}

}
2 changes: 1 addition & 1 deletion src/snorri/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public WorldLayerException(String msg) {
*/
public static World createDefaultWorld(Vector dims) {
World world = new World(dims.getX(), dims.getY());
world.addLayer(new BackgroundLayer());
world.addLayer(new BackgroundLayer(world, BackgroundLayer.DEFAULT_BACKGROUND));
world.addLayer(new TileLayer(dims.getX(), dims.getY(), UnifiedTileType.EMPTY));
world.addLayer(new EntityLayer(world));
return world;
Expand Down