-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
99 lines (83 loc) · 2.93 KB
/
Copy pathUtils.java
File metadata and controls
99 lines (83 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
ahbejarano
Utility methods.
*/
package geetransit.minecraft05.engine;
import java.io.*;
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
import java.nio.*;
import java.nio.charset.StandardCharsets;
import org.lwjgl.system.*;
import org.lwjgl.stb.*;
public class Utils {
public static InputStream loadInputStream(String file) throws Exception {
InputStream in = Utils.class.getResourceAsStream(file);
if (in == null)
throw new Exception("file [" + file + "] does not exist");
return in;
}
// source # https://stackoverflow.com/a/17861016
public static byte[] loadByteArray(String file) throws Exception {
try (
InputStream in = loadInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream();
) {
byte[] buffer = new byte[0xFFFF];
int len;
while ((len = in.read(buffer)) != -1)
out.write(buffer, 0, len);
return out.toByteArray();
}
}
public static String loadResource(String file) throws Exception {
try (
InputStream in = loadInputStream(file);
Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name());
) {
return scanner.useDelimiter("\\A").next();
}
}
public static Stream<String> loadLinesStream(String file) throws Exception {
// source # https://stackoverflow.com/a/30336423
InputStream in = loadInputStream(file);
InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8.name());
return new BufferedReader(isr).lines();
}
public static ByteBuffer loadImage(String fileName, BiConsumer<Integer, Integer> consumer) throws Exception {
ByteBuffer imageBuffer;
ByteBuffer rawBuffer;
// Load Texture file
try (MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer widthBuffer = stack.mallocInt(1);
IntBuffer heightBuffer = stack.mallocInt(1);
IntBuffer channelsBuffer = stack.mallocInt(1);
byte[] array = loadByteArray(fileName);
rawBuffer = MemoryUtil.memAlloc(array.length);
rawBuffer.put(array).flip();
imageBuffer = STBImage.stbi_load_from_memory(rawBuffer, widthBuffer, heightBuffer, channelsBuffer, 4);
if (imageBuffer == null)
throw new Exception("Image file [" + fileName + "] not loaded: " + STBImage.stbi_failure_reason());
// Get width and height of image
consumer.accept(widthBuffer.get(), heightBuffer.get());
}
return imageBuffer;
}
public static ByteBuffer loadImage(String fileName, int[] widthArray, int[] heightArray) throws Exception {
return loadImage(fileName, (width, height) -> { widthArray[0] = width; heightArray[0] = height; });
}
public static void freeImage(ByteBuffer imageBuffer) {
STBImage.stbi_image_free(imageBuffer);
}
public static int[] intListToArray(List<Integer> intList) {
return intList.stream().mapToInt(i -> i).toArray();
}
public static float[] floatListToArray(List<Float> floatList) {
float[] floatArray = new float[floatList.size()];
int i = 0;
for (float f : floatList)
floatArray[i++] = f;
return floatArray;
}
}