From 25dfeb7c12c2a9d827f2767e394f7e5b55b28bc0 Mon Sep 17 00:00:00 2001 From: KOTerra <82443072+KOTerra@users.noreply.github.com> Date: Mon, 1 May 2023 23:21:58 +0300 Subject: [PATCH] #15 fixed texture bleeding --- .../core/src/com/strafergame/Strafer.java | 10 +- .../OrthogonalTiledMapRendererBleeding.java | 181 ++++++++++++++++++ .../com/strafergame/input/InputManager.java | 2 +- .../strafergame/screens/LoadingScreen.java | 6 +- .../src/com/strafergame/DesktopLauncher.java | 4 +- 5 files changed, 190 insertions(+), 13 deletions(-) create mode 100644 StrafeOverdead/core/src/com/strafergame/graphics/OrthogonalTiledMapRendererBleeding.java diff --git a/StrafeOverdead/core/src/com/strafergame/Strafer.java b/StrafeOverdead/core/src/com/strafergame/Strafer.java index ee2f185..9c4236f 100644 --- a/StrafeOverdead/core/src/com/strafergame/Strafer.java +++ b/StrafeOverdead/core/src/com/strafergame/Strafer.java @@ -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; @@ -130,7 +130,7 @@ public class Strafer extends Game { /** * the map renderer */ - public static OrthogonalTiledMapRenderer tiledMapRenderer; + public static OrthogonalTiledMapRendererBleeding tiledMapRenderer; public static GameRenderer gameRenderer; @@ -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")); @@ -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()); diff --git a/StrafeOverdead/core/src/com/strafergame/graphics/OrthogonalTiledMapRendererBleeding.java b/StrafeOverdead/core/src/com/strafergame/graphics/OrthogonalTiledMapRendererBleeding.java new file mode 100644 index 0000000..a286250 --- /dev/null +++ b/StrafeOverdead/core/src/com/strafergame/graphics/OrthogonalTiledMapRendererBleeding.java @@ -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 + } +} \ No newline at end of file diff --git a/StrafeOverdead/core/src/com/strafergame/input/InputManager.java b/StrafeOverdead/core/src/com/strafergame/input/InputManager.java index c617c59..de9b55e 100644 --- a/StrafeOverdead/core/src/com/strafergame/input/InputManager.java +++ b/StrafeOverdead/core/src/com/strafergame/input/InputManager.java @@ -40,7 +40,7 @@ private void decideOnHandler() { inputMultiplexer.addProcessor(keyboardHandler); } if (Controllers.getControllers().notEmpty()) { - + System.out.println("controller"); } } diff --git a/StrafeOverdead/core/src/com/strafergame/screens/LoadingScreen.java b/StrafeOverdead/core/src/com/strafergame/screens/LoadingScreen.java index e6a65da..2a518e6 100644 --- a/StrafeOverdead/core/src/com/strafergame/screens/LoadingScreen.java +++ b/StrafeOverdead/core/src/com/strafergame/screens/LoadingScreen.java @@ -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); diff --git a/StrafeOverdead/desktop/src/com/strafergame/DesktopLauncher.java b/StrafeOverdead/desktop/src/com/strafergame/DesktopLauncher.java index ea0ae01..65cb593 100644 --- a/StrafeOverdead/desktop/src/com/strafergame/DesktopLauncher.java +++ b/StrafeOverdead/desktop/src/com/strafergame/DesktopLauncher.java @@ -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); }