`
kingj
  • 浏览: 421700 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

libgdx系列之-加载tileMap地图

阅读更多

Android libgdx tiled 使用tiledMapEditor编辑二维地图
2011-12-20 18:04

package com.badlogic.gdx.graphics.g2d.tiled;

文件TileAtlas:  

         当 libgdx使用tiled Map时,由于原方法获取图块是通过配置的图片packfile来获取的,用于我找不到生成图片packfile的工具,所以打算修改这个方法,直接不需要packfile就可以操作(一个方法优化可以少生成一个packfile文件),以下是原始方法:

  public TileAtlas (TiledMap map, FileHandle inputDir) {
        // TODO: Create a constructor that doesn't take a tmx map,
        for (TileSet set : map.tileSets) {
            FileHandle packfile = getRelativeFileHandle(inputDir, removeExtension(set.imageName) + " packfile");
            TextureAtlas textureAtlas = new TextureAtlas(packfile, packfile.parent(), false);
            List<AtlasRegion> atlasRegions = textureAtlas.findRegions(removeExtension(removePath(set.imageName)));

            for (AtlasRegion reg : atlasRegions) {
                regionsMap.put(reg.index + set.firstgid, reg);
                if (!textures.contains(reg.getTexture())) {
                    textures.add(reg.getTexture());
                }
            }
        }
    }

修改为:通过算数来算出值,而不是通过packfile文件读取,package file读取可能会产生很多问题。

public TileAtlas (TiledMap map, FileHandle inputDir) { 
        for (TileSet set : map.tileSets) {  
            if(set.imageName==null){
                continue;
            }
            FileHandle imageFile = getRelativeFileHandle(inputDir, (set.imageName) ); 
            List<AtlasRegion> atlasRegions=new ArrayList<AtlasRegion>();
            Texture texture = new Texture(imageFile);
            for(int j=0;j<texture.getHeight()/set.tileHeight;j++){
                int wn=texture.getWidth()/set.tileWidth;
                for(int i=0;i<wn;i++){
                    AtlasRegion atlasRegion= new AtlasRegion(texture,i*set.tileWidth,j*set.tileHeight,set.tileWidth,set.tileHeight);
                    atlasRegion.index=j*wn+i;
                    atlasRegions.add(atlasRegion);
                }
            }
            for (AtlasRegion reg : atlasRegions) {
                regionsMap.put(reg.index + set.firstgid, reg);
                if (!textures.contains(reg.getTexture())) {
                    textures.add(reg.getTexture());
                }
            }
        }
    }

 

 

我这里有一个demo,截图如下


 

代码如下:

 

package com.zx;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
import com.badlogic.gdx.graphics.g2d.tiled.TileSet;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;

public class JavaGame implements ApplicationListener {

    Stage stage; 
    float width; 
    float height; 
    private TiledMap map; 
    private TileAtlas atlas; 
    private TileMapRenderer tileMapRenderer;

    Vector3 camDirection = new Vector3(1, 1, 0); 
    Vector2 maxCamPosition = new Vector2(0, 0);

    Image image;

    @Override 
    public void create() { 
        FileHandle mapHandle = Gdx.files.internal("map/ff.tmx");
        System.out.println("MapHandle is -->"+mapHandle);
        map = TiledLoader.createMap(mapHandle); 
        
        System.out.println("Map is -->"+map);
        FileHandle packages=Gdx.files.internal("map");
        
        System.out.println("Package fileHandle-->"+packages);
        atlas = new CustomerTiledAlisa(map, packages); 
        tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10); 
        
        System.out.println("TileMapRenderer--->"+tileMapRenderer);
        
        maxCamPosition.set(tileMapRenderer.getMapWidthUnits(), tileMapRenderer .getMapHeightUnits()); 
        
        width = Gdx.graphics.getWidth(); 
        height = Gdx.graphics.getHeight(); 
        stage = new Stage(width, height, true);
        
        System.out.printf("Stage->(%f,%f)",width,height);
        
        Label label = new Label("FPS:", new LabelStyle(new BitmapFont(Gdx.files 
                .internal("zh-cn.fnt"), 
                Gdx.files.internal("zh-cn.png"), false), Color.BLACK), 
                "fpsLabel"); 
        
        System.out.println("Label--->"+label.toString());
        stage.addActor(label); 
        Gdx.input.setInputProcessor(stage); 
    }

    @Override 
    public void dispose() { 
        // TODO Auto-generated method stub

    }

    @Override 
    public void pause() { 
        // TODO Auto-generated method stub

    }

    @Override 
    public void render() { 
    	System.out.println("start to render map");
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
        OrthographicCamera c = (OrthographicCamera) stage.getCamera(); 
        
        System.out.println("获取相机->"+c.toString());
        ((Label) stage.findActor("fpsLabel")).setText("FPS: " 
                + Gdx.graphics.getFramesPerSecond()); 
        stage.act(Gdx.graphics.getDeltaTime()); 
        
        System.out.println("延迟时间-->"+Gdx.graphics.getDeltaTime());
        System.out.println("Render(height,width)"+tileMapRenderer.getMapHeightUnits()+","+tileMapRenderer.getMapWidthUnits()+",Map->"+tileMapRenderer.getMap().tileSets);
        tileMapRenderer.render(c); 
        
        
        stage.draw(); 
    }

    @Override 
    public void resize(int width, int height) { 
        // TODO Auto-generated method stub

    }

    @Override 
    public void resume() { 
        // TODO Auto-generated method stub

    }

    private static class CustomerTiledAlisa extends TileAtlas{
    	private static FileHandle getRelativeFileHandle (FileHandle path, String relativePath) {
    		if (relativePath.trim().length() == 0) {
    			return path;
    		}

    		FileHandle child = path;

    		StringTokenizer tokenizer = new StringTokenizer(relativePath, "\\/");
    		while (tokenizer.hasMoreTokens()) {
    			String token = tokenizer.nextToken();
    			if (token.equals("..")) {
    				child = child.parent();
    			} else {
    				child = child.child(token);
    			}
    		}

    		return child;
    	}
    	
    	
    	public CustomerTiledAlisa (TiledMap map, FileHandle inputDir) { 
            for (TileSet set : map.tileSets) {  
                if(set.imageName==null){
                    continue;
                }
                FileHandle imageFile = getRelativeFileHandle(inputDir, (set.imageName) ); 
                List<AtlasRegion> atlasRegions=new ArrayList<AtlasRegion>();
                Texture texture = new Texture(imageFile);
                for(int j=0;j<texture.getHeight()/set.tileHeight;j++){
                    int wn=texture.getWidth()/set.tileWidth;
                    for(int i=0;i<wn;i++){
                        AtlasRegion atlasRegion= new AtlasRegion(texture,i*set.tileWidth,j*set.tileHeight,set.tileWidth,set.tileHeight);
                        atlasRegion.index=j*wn+i;
                        atlasRegions.add(atlasRegion);
                    }
                }
                for (AtlasRegion reg : atlasRegions) {
                    regionsMap.put(reg.index + set.firstgid, reg);
                    if (!textures.contains(reg.getTexture())) {
                        textures.add(reg.getTexture());
                    }
                }
                
                System.out.println("textuare is ->"+textures);
            }
        }
    }
}
 
  • 大小: 15 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics