Skip to content

Commit

Permalink
#15 fixed texture bleeding
Browse files Browse the repository at this point in the history
  • Loading branch information
KOTerra committed May 1, 2023
1 parent a28cc51 commit 25dfeb7
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 13 deletions.
10 changes: 5 additions & 5 deletions StrafeOverdead/core/src/com/strafergame/Strafer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.utils.I18NBundle;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.strafergame.graphics.OrthogonalTiledMapRendererBleeding;
import com.strafergame.graphics.WorldCamera;
import com.strafergame.input.InputManager;
import com.strafergame.screens.GameScreen;
Expand Down Expand Up @@ -130,7 +130,7 @@ public class Strafer extends Game {
/**
* the map renderer
*/
public static OrthogonalTiledMapRenderer tiledMapRenderer;
public static OrthogonalTiledMapRendererBleeding tiledMapRenderer;

public static GameRenderer gameRenderer;

Expand All @@ -142,8 +142,7 @@ public class Strafer extends Game {
@Override
public void create() {
try {// TODO
i18n = I18NBundle.createBundle(Gdx.files.internal(
"assets/i18n/ui/bundle"),
i18n = I18NBundle.createBundle(Gdx.files.internal("assets/i18n/ui/bundle"),
new Locale(Settings.getPreferences().getString("LANGUAGE")), "utf-8");
// i18n = I18NBundle.createBundle(Gdx.files.internal("assets/i18n/ui/bundle"),
// new Locale("ro", "utf-8"));
Expand All @@ -161,7 +160,8 @@ public void create() {
worldCamera.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2, 0);
extendViewport = new ExtendViewport(WORLD_WIDTH, WORLD_HEIGHT, worldCamera);

tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap, SCALE_FACTOR, spriteBatch);
tiledMapRenderer = new OrthogonalTiledMapRendererBleeding(tiledMap, SCALE_FACTOR);

tiledMapRenderer.setView(worldCamera);

uiCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.strafergame.graphics;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

import static com.badlogic.gdx.graphics.g2d.Batch.*;

// https://www.badlogicgames.com/forum/viewtopic.php?t=16368#p74103
// better solution: render to framebuffer and scale that one?
public class OrthogonalTiledMapRendererBleeding extends OrthogonalTiledMapRenderer {
public OrthogonalTiledMapRendererBleeding(TiledMap map, float unitScale) {
super(map, unitScale);
}

@Override
public void renderTileLayer(TiledMapTileLayer layer) {
final Color batchColor = batch.getColor();
final float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b,
batchColor.a * layer.getOpacity());

final int layerWidth = layer.getWidth();
final int layerHeight = layer.getHeight();

final float layerTileWidth = layer.getTileWidth() * unitScale;
final float layerTileHeight = layer.getTileHeight() * unitScale;

final float layerOffsetX = layer.getRenderOffsetX() * unitScale;
// offset in tiled is y down, so we flip it
final float layerOffsetY = -layer.getRenderOffsetY() * unitScale;

final int col1 = Math.max(0, (int) ((viewBounds.x - layerOffsetX) / layerTileWidth));
final int col2 = Math.min(layerWidth,
(int) ((viewBounds.x + viewBounds.width + layerTileWidth - layerOffsetX) / layerTileWidth));

final int row1 = Math.max(0, (int) ((viewBounds.y - layerOffsetY) / layerTileHeight));
final int row2 = Math.min(layerHeight,
(int) ((viewBounds.y + viewBounds.height + layerTileHeight - layerOffsetY) / layerTileHeight));

float y = row2 * layerTileHeight + layerOffsetY;
float xStart = col1 * layerTileWidth + layerOffsetX;
final float[] vertices = this.vertices;

for (int row = row2; row >= row1; row--) {
float x = xStart;
for (int col = col1; col < col2; col++) {
final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
if (cell == null) {
x += layerTileWidth;
continue;
}
final TiledMapTile tile = cell.getTile();

if (tile != null) {
final boolean flipX = cell.getFlipHorizontally();
final boolean flipY = cell.getFlipVertically();
final int rotations = cell.getRotation();

TextureRegion region = tile.getTextureRegion();
fixBleeding(region);

float x1 = x + tile.getOffsetX() * unitScale;
float y1 = y + tile.getOffsetY() * unitScale;
float x2 = x1 + region.getRegionWidth() * unitScale;
float y2 = y1 + region.getRegionHeight() * unitScale;

float u1 = region.getU();
float v1 = region.getV2();
float u2 = region.getU2();
float v2 = region.getV();

vertices[X1] = x1;
vertices[Y1] = y1;
vertices[C1] = color;
vertices[U1] = u1;
vertices[V1] = v1;

vertices[X2] = x1;
vertices[Y2] = y2;
vertices[C2] = color;
vertices[U2] = u1;
vertices[V2] = v2;

vertices[X3] = x2;
vertices[Y3] = y2;
vertices[C3] = color;
vertices[U3] = u2;
vertices[V3] = v2;

vertices[X4] = x2;
vertices[Y4] = y1;
vertices[C4] = color;
vertices[U4] = u2;
vertices[V4] = v1;

if (flipX) {
float temp = vertices[U1];
vertices[U1] = vertices[U3];
vertices[U3] = temp;
temp = vertices[U2];
vertices[U2] = vertices[U4];
vertices[U4] = temp;
}
if (flipY) {
float temp = vertices[V1];
vertices[V1] = vertices[V3];
vertices[V3] = temp;
temp = vertices[V2];
vertices[V2] = vertices[V4];
vertices[V4] = temp;
}
if (rotations != 0) {
switch (rotations) {
case TiledMapTileLayer.Cell.ROTATE_90: {
float tempV = vertices[V1];
vertices[V1] = vertices[V2];
vertices[V2] = vertices[V3];
vertices[V3] = vertices[V4];
vertices[V4] = tempV;

float tempU = vertices[U1];
vertices[U1] = vertices[U2];
vertices[U2] = vertices[U3];
vertices[U3] = vertices[U4];
vertices[U4] = tempU;
break;
}
case TiledMapTileLayer.Cell.ROTATE_180: {
float tempU = vertices[U1];
vertices[U1] = vertices[U3];
vertices[U3] = tempU;
tempU = vertices[U2];
vertices[U2] = vertices[U4];
vertices[U4] = tempU;
float tempV = vertices[V1];
vertices[V1] = vertices[V3];
vertices[V3] = tempV;
tempV = vertices[V2];
vertices[V2] = vertices[V4];
vertices[V4] = tempV;
break;
}
case TiledMapTileLayer.Cell.ROTATE_270: {
float tempV = vertices[V1];
vertices[V1] = vertices[V4];
vertices[V4] = vertices[V3];
vertices[V3] = vertices[V2];
vertices[V2] = tempV;

float tempU = vertices[U1];
vertices[U1] = vertices[U4];
vertices[U4] = vertices[U3];
vertices[U3] = vertices[U2];
vertices[U2] = tempU;
break;
}
}
}
batch.draw(region.getTexture(), vertices, 0, NUM_VERTICES);
}
x += layerTileWidth;
}
y -= layerTileHeight;
}
}

public static void fixBleeding(TextureRegion region) {
float fix = 0.01f;
float x = region.getRegionX();
float y = region.getRegionY();
float width = region.getRegionWidth();
float height = region.getRegionHeight();
float invTexWidth = 1f / region.getTexture().getWidth();
float invTexHeight = 1f / region.getTexture().getHeight();
region.setRegion((x + fix) * invTexWidth, (y + fix) * invTexHeight, (x + width - fix) * invTexWidth,
(y + height - fix) * invTexHeight); // Trims Region
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void decideOnHandler() {
inputMultiplexer.addProcessor(keyboardHandler);
}
if (Controllers.getControllers().notEmpty()) {

System.out.println("controller");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,8 @@ private void queueAssetsToLoad() {
Strafer.assetManager.load("images/back.png", Texture.class);
Strafer.assetManager.load("ui/backgrounds/banner.png", Texture.class);

TmxMapLoader.Parameters tmxParams = new TmxMapLoader.Parameters();
tmxParams.textureMinFilter = Texture.TextureFilter.Linear;
tmxParams.textureMagFilter = Texture.TextureFilter.Nearest;
tmxParams.generateMipMaps = true;
Strafer.assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
Strafer.assetManager.load("maps/test/map.tmx", TiledMap.class,tmxParams);
Strafer.assetManager.load("maps/test/map.tmx", TiledMap.class);

Strafer.assetManager.setLoader(TextureAtlas.class, new TextureAtlasLoader(new InternalFileHandleResolver()));
Strafer.assetManager.load("spritesheets/player/player.atlas", TextureAtlas.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public static void main(String[] arg) {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setForegroundFPS(144);
config.useVsync(true);
config.setWindowedMode(1280, 720);
//config.setWindowedMode(1280, 720);
config.setTitle("Strafe Overdead");
// config.setDecorated(false);
config.setResizable(true);

// config.setFullscreenMode(Lwjgl3ApplicationConfiguration.getDisplayMode());
config.setFullscreenMode(Lwjgl3ApplicationConfiguration.getDisplayMode());
new Lwjgl3Application(new Strafer(), config);

}
Expand Down

0 comments on commit 25dfeb7

Please sign in to comment.