From 73b6854266aba96ebdcf8a9417aafaf55e7e0b42 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Mon, 9 Oct 2023 05:44:38 -0400 Subject: [PATCH] Map loaded updated --- src/engine/util/MapLoader.java | 37 ++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/engine/util/MapLoader.java b/src/engine/util/MapLoader.java index ce187965..779080bd 100644 --- a/src/engine/util/MapLoader.java +++ b/src/engine/util/MapLoader.java @@ -103,31 +103,38 @@ public enum MapLoader { if (Files.isRegularFile(filePath)) { File imageFile = filePath.toFile(); + BufferedImage heightmapImage; + try { - BufferedImage heightmapImage = ImageIO.read(imageFile); + heightmapImage = ImageIO.read(imageFile); + } catch (IOException e) { + throw new RuntimeException(e); + } - // Generate pixel data for this heightmap. RPG channels are all the same - // in this greyscale TGA heightmap. We will choose red. + int fileName = Integer.parseInt(imageFile.getName().substring(0, imageFile.getName().lastIndexOf("."))); + boolean singleBandRaster = heightmapImage.getRaster().getNumBands() == 1; + int color; - short[][] colorData = new short[heightmapImage.getWidth()][heightmapImage.getHeight()]; + // Generate pixel data for this heightmap. - for (int y = 0; y < heightmapImage.getHeight(); y++) - for (int x = 0; x < heightmapImage.getWidth(); x++) { + short[][] colorData = new short[heightmapImage.getWidth()][heightmapImage.getHeight()]; - Color color = new Color(heightmapImage.getRGB(x, y)); - colorData[x][y] = (short) color.getRed(); - } + for (int y = 0; y < heightmapImage.getHeight(); y++) + for (int x = 0; x < heightmapImage.getWidth(); x++) { - // Insert color data into lookup table + if (singleBandRaster) + color = heightmapImage.getRaster().getSample(x, y, 0); + else + color = new Color(heightmapImage.getRGB(x, y)).getRed(); - int heightMapID = Integer.parseInt(imageFile.getName().substring(0, imageFile.getName().lastIndexOf("."))); - Terrain._heightmap_pixel_cache.put(heightMapID, colorData); + colorData[x][y] = (short) color; + } - } catch (IOException e) { - Logger.error(e); - } + // Add pixel for this TGA image into the collection + Terrain._heightmap_pixel_cache.put(fileName, colorData); } + }); // Try with resources block } catch (IOException e) {