Skip to content

Commit

Permalink
Loading and drawing tmx map
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Wolfinger committed Oct 7, 2015
1 parent 667ae4b commit cc70043
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 115 deletions.
44 changes: 19 additions & 25 deletions src/de/dungeonrunner/Application.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
package de.dungeonrunner;

import org.jsfml.graphics.CircleShape;
import org.jsfml.graphics.Color;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;

import tiled.core.Map;

public class Application {

public static void main(String[] args) {
Map map = null;
SFMLTileRenderer renderer = null;

for (String arg : args) {
map = TmxMapLoader.loadMap(arg);
}

if(map != null){
map = TmxMapLoader.loadMap(arg);
}

if (map != null) {
TextureHolder.getInstance().loadTiledTextures(map);
renderer = new SFMLTileRenderer(map);
}

RenderWindow window = new RenderWindow();
window.create(new VideoMode(800, 480, 32), "Test");
window.setFramerateLimit(30);


CircleShape shape = new CircleShape(40.0f);
shape.setPosition(100.0f, 100.0f);
shape.setFillColor(Color.BLUE);

while(window.isOpen()){
for(Event event : window.pollEvents()) {
if(event.type == Event.Type.CLOSED) {
//The user pressed the close button
window.close();
}
}


while (window.isOpen()) {
for (Event event : window.pollEvents()) {
if (event.type == Event.Type.CLOSED) {
// The user pressed the close button
window.close();
}
}

window.clear();
if(renderer != null){
window.draw(renderer);
if (renderer != null) {
window.draw(renderer);
}
window.display();
}
Expand Down
85 changes: 21 additions & 64 deletions src/de/dungeonrunner/SFMLTileRenderer.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,52 @@
package de.dungeonrunner;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map.Entry;

import org.jsfml.graphics.Drawable;
import org.jsfml.graphics.Image;
import org.jsfml.graphics.RectangleShape;
import org.jsfml.graphics.RenderStates;
import org.jsfml.graphics.RenderTarget;
import org.jsfml.graphics.Texture;
import org.jsfml.graphics.TextureCreationException;
import org.jsfml.system.Vector2f;
import org.jsfml.system.Vector2i;

import tiled.core.Map;
import tiled.core.MapLayer;
import tiled.core.Tile;
import tiled.core.TileLayer;
import tiled.core.TileSet;

public class SFMLTileRenderer implements Drawable {

private final Map map;
private final Map mTiledMap;
private RectangleShape mCacheTile = new RectangleShape();

public SFMLTileRenderer(Map map) {
this.map = map;
loadTextures();
mTiledMap = map;
}

public void paintTileLayerTest(RenderTarget target, RenderStates states, TileLayer layer) {
final int tileWidth = map.getTileWidth();
final int tileHeight = map.getTileHeight();

final Vector2i targetSize = target.getSize();
final Rectangle boundsInTiles = layer.getBounds();

TextureHolder textureHolder = TextureHolder.getInstance();

for(int x = 0; x < boundsInTiles.getWidth(); x++){
for(int y = 0; y < boundsInTiles.getHeight(); y++){
//System.out.println(x + "/" + y);
final Tile tile = layer.getTileAt(x, y);
if(tile == null){
continue;
} else {
RectangleShape shape = new RectangleShape();
shape.setSize(new Vector2f(tileWidth, tileHeight));
shape.setTexture(textureHolder.getTileTexture(tile.getId()));
shape.setPosition(x * tileWidth, y * tileHeight);
target.draw(shape);
}
}
}
}

private void loadTextures() {
final int numberTileSets = map.getTileSets().size();
final HashMap<Integer, java.awt.Image> mTileImages = new HashMap<>();
TextureHolder texHolder = TextureHolder.getInstance();
Image tempTileImage = new Image();
Texture tempTexture = new Texture();

for (int i = 0; i < numberTileSets; i++) {
TileSet tileSet = map.getTileSets().get(i);
System.out.println("Iterating tileSet " + tileSet.getName());
final int numberTiles = tileSet.size();
for (int o = 0; o < numberTiles; o++) {
Tile tile = tileSet.getTile(o);
mTileImages.put(tile.getId(), tile.getImage());
}
}

for (Entry<Integer, java.awt.Image> entry : mTileImages.entrySet()) {
try {
tempTileImage.create((BufferedImage) entry.getValue());
tempTexture.loadFromImage(tempTileImage);
texHolder.loadTileTexture(entry.getKey(), tempTexture);
System.out.println("Loaded new texture: " + entry.getKey() + ", " + tempTexture.toString());
} catch (TextureCreationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
final int tileWidth = mTiledMap.getTileWidth();
final int tileHeight = mTiledMap.getTileHeight();
final Rectangle boundsInTiles = layer.getBounds();

TextureHolder textureHolder = TextureHolder.getInstance();

for (int x = 0; x < boundsInTiles.getWidth(); x++) {
for (int y = 0; y < boundsInTiles.getHeight(); y++) {
Tile tile = layer.getTileAt(x, y);
if (tile == null) {
continue;
} else {
mCacheTile.setSize(new Vector2f(tileWidth, tileHeight));
mCacheTile.setTexture(textureHolder.getTileTexture(tile.getId()));
mCacheTile.setPosition(x * tileWidth, y * tileHeight);
target.draw(mCacheTile);
}
}
}
}

@Override
public void draw(RenderTarget target, RenderStates states) {

for (MapLayer layer : map) {
for (MapLayer layer : mTiledMap) {
if (layer instanceof TileLayer) {
paintTileLayerTest(target, states, (TileLayer) layer);
}
Expand Down
47 changes: 37 additions & 10 deletions src/de/dungeonrunner/TextureHolder.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package de.dungeonrunner;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;

import org.jsfml.graphics.Image;
import org.jsfml.graphics.Texture;
import org.jsfml.graphics.TextureCreationException;

import tiled.core.Map;
import tiled.core.Tile;
import tiled.core.TileSet;

public class TextureHolder {

Expand All @@ -16,13 +23,13 @@ public enum TextureID {
private HashMap<TextureID, Texture> mTextures;
private HashMap<Integer, Texture> mTileTextures;

public static TextureHolder getInstance(){
if(mInstance == null){
public static TextureHolder getInstance() {
if (mInstance == null) {
mInstance = new TextureHolder();
}
return mInstance;
}

private TextureHolder() {
mTextures = new HashMap<>();
mTileTextures = new HashMap<>();
Expand All @@ -40,21 +47,41 @@ public boolean loadTexture(TextureID id, String filePath) {
mTextures.put(id, texture);
return true;
}

public boolean loadTileTexture(int textureId, Texture texture){
if(texture == null){
System.out.println("Loading null texture into TextureHolder");

private boolean loadImageTexture(int textureId, java.awt.Image awtImage) {
try {
Image image = new Image();
image.create((BufferedImage) awtImage);
Texture texture = new Texture();
texture.loadFromImage(image);
mTileTextures.put(textureId, texture);
} catch (TextureCreationException e) {
System.err.println("Could not create texture from image");
e.printStackTrace();
return false;
}
mTileTextures.put(textureId, texture);

return true;
}

public void loadTiledTextures(Map tiledMap) {
final int numberTileSets = tiledMap.getTileSets().size();

for (int i = 0; i < numberTileSets; i++) {
TileSet tileSet = tiledMap.getTileSets().get(i);
final int numberTiles = tileSet.size();
for (int o = 0; o < numberTiles; o++) {
Tile tile = tileSet.getTile(o);
loadImageTexture(tile.getId(), tile.getImage());
}
}
}

public Texture getTexture(TextureID id) {
return mTextures.get(id);
}
public Texture getTileTexture(int id){

public Texture getTileTexture(int id) {
return mTileTextures.get(id);
}
}
32 changes: 16 additions & 16 deletions src/de/dungeonrunner/TmxMapLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

public class TmxMapLoader {

public static Map loadMap(String filepath){
String fileToOpen = filepath;
public static Map loadMap(String filepath) {
String fileToOpen = filepath;

if (fileToOpen == null) {
return null;
}
if (fileToOpen == null) {
return null;
}

Map map;
try {
TMXMapReader mapReader = new TMXMapReader();
map = mapReader.readMap(fileToOpen);
} catch (Exception e) {
System.out.println("Error while reading the map:\n" + e.getMessage());
return null;
}
Map map;
try {
TMXMapReader mapReader = new TMXMapReader();
map = mapReader.readMap(fileToOpen);
} catch (Exception e) {
System.out.println("Error while reading the map:\n" + e.getMessage());
return null;
}

System.out.println(map.toString() + " loaded");
return map;
}
System.out.println(map.toString() + " loaded");
return map;
}
}

0 comments on commit cc70043

Please sign in to comment.