[Libgdx] Tiles loaded from text file upside down

Hi, I’m trying to load a tilemap I made from a text file. It gets loaded correctly, but It renders the map upside down. I tried to switch up names in order to get everything to flip, but that didn’t work out well. I also attempted to set my OrthoGraphic camera to ortho and have it flipped, but when I render fonts, their not able to be flipped like textures. I’m hoping for help on fixing this so I don’t have to resort to have hardcoded arrays replace map files. Here’s some code I use to create the map:

private void loadMap(String s) {
        try {
            FileHandle file = Gdx.files.internal(s);
            BufferedReader br = new BufferedReader(file.reader());
             
            int mapWidth = Integer.parseInt(br.readLine());
            int mapHeight = Integer.parseInt(br.readLine());
            
            tiles = new Tile[mapHeight][mapWidth];
            
            String del = "\\s+";
             
            for(int row = 0; row < mapHeight; row++) {
                String line = br.readLine();
                String[] tokens = line.split(del);
                
                for(int col = 0;col < mapWidth; col++) {
                    tiles[row][col] = new Tile(col * size, row * size, size, Integer.parseInt(tokens[col]));
                }
            }                 
            br.close();        
        } catch(Exception e) {e.printStackTrace();}
         
    }

And then it is render as so…

public void render(ShapeRenderer sr, SpriteBatch g) {
    	
    	for(int i = 0; i < tiles.length; i++) {
    		for(int j = 0; j <tiles[i].length; j++) {
    			Tile t = tiles[i][j];
    			t.render(sr, g);
    		}
    	}
    	
    }

Here’s an example of what a basic map file looks like as well

21
5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Hope you can help :smiley:

If changing the coordinate system isn’t practical or desirable, the easiest solution seems like it would be simply to reverse the ‘tiles’ array along the y axis once it’s been read from the text file.

Couldn’t figure how to go about that, but I managed to flip the BitMapFont instead.

font = new BitmapFont(true);

The constructor allows me to flip it. What I did to solve my problem is flip every sprite/font and set the camera to ortho. I might just make a little Texture class that does it for me, but (technically) the problem is solved.

You’re going to flip every single asset in your game because of one small issue with your map loading logic?

I can simply create a Sprite class (or modify the current one if i can) that flips the sprite automatiacally so I don’t have to add a flip statement constantly. I was unable to any sort of flip to the array so this was acceptable.

Upload a picture or something to show me what you’re expecting, and what you’re actually getting when trying to load your map. Your “map” file is pretty vague, I don’t entirely understand your problem.

Flipping your sprite image has nothing to do with a map that is loading “upside down”, so your original question and solution don’t make any sense.

Okay, so my map loading code is shown above still. My Screen’s code is here if you want to see how I handle viewports (I think I did it right) and such http://pastebin.java-gaming.org/9276d0c9e2d10 .
So A map file (the one im using right now looks like this)


21
5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

The 3 and the 2 are supposed to be rendered as black squares and the others white. But my logic loaded them upside down as shown here:

As you may notice, the tiles drawn are at the top of the map, but in the text file their at the bottom. Everything else is not flipped and the camera is not using ortho (shown in source). On a side note, is the way I handle viewports correct for cross platform development? It seems to work.

If your map is loading upside down, maybe the method in which you’re saving differs from how you read it (in order). If you could post your saving code that would help a lot.

Are you rendering to a FrameBuffer? The libgdx frame buffer’s internal texture is flipped if you attempt to draw it as-is, you have to draw it with a negative height to flip it right-way-round.

I don’t use a framebuffer or save it (atleast not through code). I just manually make a text file in a text editor.

Maybe you have everything the way you want it now, but I’ll just say that if the only reason you’re not using the map-flipping solution is that you couldn’t get it to work, you should post your map-flipping code here, as I’m sure we could help you sort it out in short order :slight_smile: In any case, you shouldn’t have to jump through a lot of hoops or use a coordinate system other than the one you prefer (maybe you’re already using your preferred coordinate system) just because of the map-reading issue.

The code is there in the pastebin, just commented. I set the camera to ortho and flipped the sprite.

Hm, I didn’t see any code for flipping the map array in the pastebin, but maybe I’m just missing it. (But, if you have everything the way you want it now, maybe it’s no longer relevant.)

You do know that the Y-origin in libgdx is bottom left? From what I can make from your code, I think you’re assuming the origin is top-left although I’m not sure.

That code renders it at the bottom left. So far, making the camera ortho is the only way I can make the map appear how I wanted.