Skip to content

Commit

Permalink
Merge pull request #15 from Homailot/fix/border-artifacts
Browse files Browse the repository at this point in the history
fix: border artifacts on tiles
  • Loading branch information
Homailot authored Oct 15, 2021
2 parents 3fe5c67 + 61857c6 commit f639ee9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import org.pixel.commons.logger.Logger;
import org.pixel.commons.logger.LoggerFactory;
import org.pixel.content.*;
import org.pixel.content.ContentImporter;
import org.pixel.content.ContentImporterInfo;
import org.pixel.content.ImportContext;
import org.pixel.tiled.content.Layer;
import org.pixel.tiled.content.TileMap;
import org.pixel.tiled.content.TileSet;
Expand All @@ -17,7 +19,6 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand All @@ -35,7 +36,7 @@ private void importTilesets(TileMap tileMap, Document tmxDoc, ImportContext ctx)
Element tileset = (Element) tilesetNode;
String tilesetSource = tileset.getAttribute("source");

TileSet tileSet = ctx.getContentManager().load(tilesetSource, TileSet.class);
TileSet tileSet = ctx.getContentManager().load(tilesetSource, TileSet.class, ctx.getSettings());

if(tileSet == null) {
LOG.error("Error loading tileset");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public TileSet process(ImportContext ctx) {

String textureFilePath = image.getAttribute("source");

Texture tileSetImage = ctx.getContentManager().load(textureFilePath, Texture.class);
Texture tileSetImage = ctx.getContentManager().load(textureFilePath, Texture.class, ctx.getSettings());

if(tileSetImage == null) {
LOG.error("Something went wrong processing the Tile Set texture image.");
Expand Down
77 changes: 46 additions & 31 deletions modules/tiled/src/main/java/org/pixel/tiled/view/LayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,56 @@ public class LayerView implements TiledViewer<Layer> {
private static final long VERTICAL_FLIP_FLAG = 0x40000000;
private static final long DIAGONAL_FLIP_FLAG = 0x20000000;

private static class Transform {
private float scaleX, scaleY;
private float rotation;

private Transform() {
scaleX = scaleY = 1f;
rotation = 0f;
}
}

private Transform getTileTransform(long gID) {
boolean horizontalFlip = (gID & HORIZONTAL_FLIP_FLAG) > 0;
boolean verticalFlip = (gID & VERTICAL_FLIP_FLAG) > 0;
boolean diagonalFlip = (gID & DIAGONAL_FLIP_FLAG) > 0;
Transform transform = new Transform();

// diagonal flip in tiled is done before the other flips, since scaling is done before rotation
// that isn't
// possible these are some workarounds
if(diagonalFlip) {
transform.rotation = (float) Math.PI / 2;
transform.scaleX = -1f;

if(horizontalFlip) {
transform.scaleY = -1f;
}
if(verticalFlip) {
transform.scaleX = 1f;
}
} else {
if(horizontalFlip) {
transform.scaleX = -1f;
}
if(verticalFlip) {
transform.scaleY = -1f;
}
}

return transform;
}

@Override
public void draw(SpriteBatch spriteBatch, Layer layer) {
List<TileSet> tileSets = layer.getTileMap().getTileSets();

for(int y = 0; y < layer.getHeight(); y++) {
for(int x = 0; x < layer.getWidth(); x++) {
long gID = layer.getTiles()[y][x];
long originalGID = gID;
ListIterator<TileSet> itr = tileSets.listIterator(tileSets.size());
boolean horizontalFlip = (gID & HORIZONTAL_FLIP_FLAG) > 0;
boolean verticalFlip = (gID & VERTICAL_FLIP_FLAG) > 0;
boolean diagonalFlip = (gID & DIAGONAL_FLIP_FLAG) > 0;

gID &= ~(HORIZONTAL_FLIP_FLAG | VERTICAL_FLIP_FLAG | DIAGONAL_FLIP_FLAG);

Expand All @@ -42,34 +81,10 @@ public void draw(SpriteBatch spriteBatch, Layer layer) {
Vector2 position = new Vector2(x * layer.getTileMap().getTileWidth() + (float)layer.getOffsetX(),
y * layer.getTileMap().getTileHeight() + (float)layer.getOffsetY());

float scaleX = 1f, scaleY = 1f;
float rotation = 0f;

// diagonal flip in tiled is done before the other flips, since scaling is done before rotation
// that isn't
// possible these are some workarounds
if(diagonalFlip) {
if (horizontalFlip && verticalFlip) {
rotation = (float) -Math.PI / 2;
scaleX = -1f;
} else if(horizontalFlip) {
rotation = (float) -Math.PI /2;
} else if(verticalFlip) {
rotation = (float) Math.PI /2;
} else {
rotation = (float) Math.PI /2;
scaleX = -1f;
}
} else {
if(horizontalFlip) {
scaleX = -1f;
}
if(verticalFlip) {
scaleY = -1f;
}
}

spriteBatch.draw(tileSet.getTexture(), position, source, Color.WHITE, Vector2.HALF, scaleX, scaleY, rotation);
Transform transform = getTileTransform(originalGID);

spriteBatch.draw(tileSet.getTexture(), position, source, Color.WHITE, Vector2.HALF,
transform.scaleX, transform.scaleY, transform.rotation);

break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ void drawRotations() throws IOException {
Mockito.eq(0f));
inOrder.verify(spriteBatch).draw(Mockito.same(texture1), Mockito.eq(new Vector2(0, 32)),
Mockito.eq(new Rectangle(16, 0, 16, 16)), Mockito.same(Color.WHITE), Mockito.eq(Vector2.HALF),
Mockito.eq(1f),
Mockito.eq(1f),
Mockito.eq((float) -Math.PI / 2));
Mockito.eq(-1f),
Mockito.eq(-1f),
Mockito.eq((float) Math.PI / 2));
inOrder.verify(spriteBatch).draw(Mockito.same(texture1), Mockito.eq(new Vector2(16, 32)),
Mockito.eq(new Rectangle(16, 0, 16, 16)), Mockito.same(Color.WHITE), Mockito.eq(Vector2.HALF),
Mockito.eq(1f),
Expand All @@ -238,8 +238,8 @@ void drawRotations() throws IOException {
Mockito.eq((float) Math.PI / 2));
inOrder.verify(spriteBatch).draw(Mockito.same(texture1), Mockito.eq(new Vector2(16, 48)),
Mockito.eq(new Rectangle(16, 0, 16, 16)), Mockito.same(Color.WHITE), Mockito.eq(Vector2.HALF),
Mockito.eq(-1f),
Mockito.eq(1f),
Mockito.eq((float) -Math.PI / 2));
Mockito.eq(-1f),
Mockito.eq((float) Math.PI / 2));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.pixel.tiled.view;

import org.junit.jupiter.api.Test;
import org.lwjgl.opengl.GL12;
import org.pixel.commons.DeltaTime;
import org.pixel.content.ContentManager;
import org.pixel.content.importer.settings.TextureImporterSettings;
import org.pixel.core.Camera2D;
import org.pixel.core.PixelWindow;
import org.pixel.core.WindowSettings;
Expand All @@ -15,6 +17,8 @@
import org.pixel.tiled.content.importer.TileMapImporter;
import org.pixel.tiled.content.importer.TileSetImporter;

import static org.lwjgl.opengl.GL11.GL_NEAREST;

public class TileMapViewTestIntegrated {
public static class MockWindow extends PixelWindow {

Expand All @@ -39,17 +43,19 @@ public MockWindow(WindowSettings settings) {
public void load() {
gameCamera.setOrigin(new Vector2(0.5f, 0.5f));
//gameCamera.translate(500, 180);
gameCamera.setZoom(2f);
gameCamera.setZoom(3f);

TileMapImporter importer = new TileMapImporter();
TileSetImporter tileSetImporter = new TileSetImporter();
String tmxFileName = "rotation2.tmx";
String tmxFileName = "rotations.tmx";

ContentManager contentManager = new ContentManager();
contentManager.addContentImporter(importer);
contentManager.addContentImporter(tileSetImporter);

tileMap = contentManager.load(tmxFileName, TileMap.class);
TextureImporterSettings settings = new TextureImporterSettings(GL12.GL_CLAMP_TO_EDGE, GL12.GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);

tileMap = contentManager.load(tmxFileName, TileMap.class, settings);
tileMapView = new TileMapView();
spriteBatch = new SpriteBatch();
}
Expand Down

0 comments on commit f639ee9

Please sign in to comment.