This is a ZipFile-based FileHandle implementation for LibGDX.
Created
May 3, 2013 21:28
-
-
Save MobiDevelop/5514357 to your computer and use it in GitHub Desktop.
A ZipFile-based FileHandle implementation for LibGDX.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobidevelop.gdx.examples; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
import com.badlogic.gdx.Files.FileType; | |
import com.badlogic.gdx.files.FileHandle; | |
import com.badlogic.gdx.utils.GdxRuntimeException; | |
public class ArchiveFileHandle extends FileHandle { | |
final ZipFile archive; | |
final ZipEntry archiveEntry; | |
public ArchiveFileHandle (ZipFile archive, File file) { | |
super(file, FileType.Classpath); | |
this.archive = archive; | |
archiveEntry = this.archive.getEntry(file.getPath()); | |
} | |
public ArchiveFileHandle (ZipFile archive, String fileName) { | |
super(fileName.replace('\\', '/'), FileType.Classpath); | |
this.archive = archive; | |
this.archiveEntry = archive.getEntry(fileName.replace('\\', '/')); | |
} | |
@Override | |
public FileHandle child (String name) { | |
name = name.replace('\\', '/'); | |
if (file.getPath().length() == 0) return new ArchiveFileHandle(archive, new File(name)); | |
return new ArchiveFileHandle(archive, new File(file, name)); | |
} | |
@Override | |
public FileHandle sibling (String name) { | |
name = name.replace('\\', '/'); | |
if (file.getPath().length() == 0) throw new GdxRuntimeException("Cannot get the sibling of the root."); | |
return new ArchiveFileHandle(archive, new File(file.getParent(), name)); | |
} | |
@Override | |
public FileHandle parent () { | |
File parent = file.getParentFile(); | |
if (parent == null) { | |
if (type == FileType.Absolute) | |
parent = new File("/"); | |
else | |
parent = new File(""); | |
} | |
return new ArchiveFileHandle(archive, parent); | |
} | |
@Override | |
public InputStream read () { | |
try { | |
return archive.getInputStream(archiveEntry); | |
} catch (IOException e) { | |
throw new GdxRuntimeException("File not found: " + file + " (Archive)"); | |
} | |
} | |
@Override | |
public boolean exists() { | |
return archiveEntry != null; | |
} | |
@Override | |
public long length () { | |
return archiveEntry.getSize(); | |
} | |
@Override | |
public long lastModified () { | |
return archiveEntry.getTime(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobidevelop.gdx.examples; | |
import java.util.zip.ZipFile; | |
import com.badlogic.gdx.assets.loaders.FileHandleResolver; | |
import com.badlogic.gdx.files.FileHandle; | |
public class ArchiveFileHandleResolver implements FileHandleResolver { | |
private final ZipFile archive; | |
public ArchiveFileHandleResolver (ZipFile archive) { | |
this.archive = archive; | |
} | |
@Override | |
public FileHandle resolve (String fileName) { | |
return new ArchiveFileHandle(archive, fileName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobidevelop.gdx.examples; | |
import java.io.IOException; | |
import java.util.zip.ZipException; | |
import java.util.zip.ZipFile; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.assets.AssetManager; | |
import com.badlogic.gdx.graphics.GL10; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.g2d.BitmapFont; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.maps.tiled.TiledMap; | |
import com.badlogic.gdx.maps.tiled.TiledMapRenderer; | |
import com.badlogic.gdx.maps.tiled.TmxMapLoader; | |
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; | |
import com.badlogic.gdx.tests.utils.GdxTest; | |
import com.badlogic.gdx.tests.utils.OrthoCamController; | |
public class ArchiveFileHandleResolverTest extends GdxTest { | |
private TiledMap map; | |
private TiledMapRenderer renderer; | |
private OrthographicCamera camera; | |
private OrthoCamController cameraController; | |
private AssetManager assetManager; | |
private BitmapFont font; | |
private SpriteBatch batch; | |
@Override | |
public void create() { | |
float w = Gdx.graphics.getWidth(); | |
float h = Gdx.graphics.getHeight(); | |
camera = new OrthographicCamera(); | |
camera.setToOrtho(false, (w / h) * 10, 10); | |
camera.zoom = 2; | |
camera.update(); | |
cameraController = new OrthoCamController(camera); | |
Gdx.input.setInputProcessor(cameraController); | |
font = new BitmapFont(); | |
batch = new SpriteBatch(); | |
try { | |
ZipFile archive = new ZipFile(Gdx.files.internal("test.zip").file()); | |
ArchiveFileHandleResolver resolver = new ArchiveFileHandleResolver(archive); | |
assetManager = new AssetManager(resolver); | |
assetManager.setLoader(TiledMap.class, new TmxMapLoader(resolver)); | |
assetManager.load("tmx/level1.tmx", TiledMap.class); | |
assetManager.finishLoading(); | |
map = assetManager.get("tmx/level1.tmx"); | |
renderer = new OrthogonalTiledMapRenderer(map, 1f / 16f); | |
} catch (ZipException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void render() { | |
Gdx.gl.glClearColor(100f / 255f, 100f / 255f, 250f / 255f, 1f); | |
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); | |
camera.update(); | |
renderer.setView(camera); | |
renderer.render(); | |
batch.begin(); | |
font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20); | |
batch.end(); | |
} | |
@Override | |
public boolean needsGL20 () { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think data.zip doesn't exist, "test.zip" is used in the example above.