0
\$\begingroup\$

I will simply leave a clip of it as it's easier than describing the problem. https://i.stack.imgur.com/bBAVC.jpg

This is All the code that i have

class MainScreen : Screen
{
    var map = Map(System.currentTimeMillis(), 32, 32, 24,24)
    lateinit var renderer : OrthogonalTiledMapRenderer
    /** Called when this screen becomes the current screen for a [Game].  */
    override fun show() {
        viewport = ExtendViewport(8f, 8f, 64f, 46f)
        rendererCamera = OrthographicCamera(viewport.worldWidth, viewport.worldHeight)

        renderer = OrthogonalTiledMapRenderer(map, 1/ viewport.minWorldWidth)

        Gdx.input.inputProcessor = InputMultiplexer(InputInheritor())
        viewport.camera.position.set(ceil(viewport.camera.position.x), ceil(viewport.camera.position.y), 0f)
    }

    /** Called when the screen should render itself.
     * @param delta The time in seconds since the last render.
     */
    override fun render(delta: Float) {
        ScreenUtils.clear(Color.DARK_GRAY)

        checkInput(this, delta)

        mainBatch.projectionMatrix = viewport.camera.combined
        viewport.apply()
        renderer.render()
        renderer.setView(viewport.camera as OrthographicCamera)
        
        viewport.camera.update(true)
        uiViewport.camera.update()
    }

and my tiled map generator class

class Map : TiledMap {
    lateinit var noise: OpenSimplexNoise
    var seed = 1

    lateinit var thisLayer : TiledMapTileLayer
    constructor() : super()
    constructor(seed:Long, width: Int, height: Int, tileWidth: Int, tileHeight: Int)
    {
        var layer = GenerateMap(seed, width, height, tileWidth, tileHeight)
        thisLayer = layer
        layers.add(thisLayer)
    }
    companion object {
        fun GenerateMap(seed: Long, width: Int, height: Int, tileWidth: Int, tileHeight: Int): TiledMapTileLayer
        {
            var toReturn = TiledMapTileLayer(width, height, tileWidth, tileHeight)
            var noise = OpenSimplexNoise(seed)
            for (x in 0 until toReturn.width)
                for (y in 0 until toReturn.height)
                {
                    var tile: StaticTiledMapTile
                    var eval = noise.eval(x.toDouble(), y.toDouble())
                    if (sin(eval) >0.15f) tile = StaticTiledMapTile(atlas.findRegion("tileGrass1"))
                    else tile = StaticTiledMapTile(atlas.findRegion("tileSand1"))
                    var cell = TiledMapTileLayer.Cell()
                    cell.tile = tile
                    toReturn.setCell(x, y, cell)
                }
            return toReturn
        }
    }

}

and my atlas cache wrapper:

class AtlasWrapper
{
    var cache = mutableMapOf<String, TextureAtlas.AtlasRegion>()
    lateinit var atlas: TextureAtlas
    constructor(directory: String)
    {
        atlas = TextureAtlas(directory)
    }
    constructor(atlasArg: TextureAtlas)
    {
        atlas = atlasArg
    }
    fun findRegion(region: String) : TextureAtlas.AtlasRegion
    {
        if(cache.containsKey(region)) return cache[region]!!
        val atlasRegion = atlas.findRegion(region)
        cache[region] = atlasRegion
        return atlasRegion
    }
}

the problem is at the tile = Staticmaptile() portion of the code as when i replace it with a normal TextureRegion nothing breaks. Any help would be appreciated

edit: those two tiles are identical in dimensions

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I've solved this problem, as i think, the culprit was that the texture was 128px wide/tall and i set it to 32 pixels in tileWidth of the map constructor, which i guess libGDX doesn't like, i fixed it by changing stuff to as such:

    var map = Map(System.currentTimeMillis(), 16, 16, 32,32)
to 
    var map = Map(System.currentTimeMillis(), 16, 16, 128,128)
    renderer = OrthogonalTiledMapRenderer(map, 1/TILE_SIZE)
to
    renderer = OrthogonalTiledMapRenderer(map, 1/TILE_SIZE * TILE_SIZE/128f)

I first tried to simply set width to 128 but it didn't work so i thought id multiply the tile size by a factor of ratio of a normal tile's pixel size / its actual pixel size and everything now works and it (probably) isn't about textureRegions being broken 🥳🥳

\$\endgroup\$
1
  • \$\begingroup\$ Don't forget to mark this answer as Accepted if it worked for you. \$\endgroup\$
    – DMGregory
    Mar 15, 2022 at 11:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .