Skip to content

Commit e645ef2

Browse files
committed
ref: typealias LambdaResource
1 parent a030ec7 commit e645ef2

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaAtlas.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import com.lambda.graphics.texture.TextureOwner.upload
2424
import com.lambda.http.Method
2525
import com.lambda.http.request
2626
import com.lambda.threading.runGameScheduled
27-
import com.lambda.util.LambdaResource
2827
import com.lambda.util.math.Vec2d
28+
import com.lambda.util.stream
2929
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap
3030
import it.unimi.dsi.fastutil.objects.Object2DoubleArrayMap
3131
import it.unimi.dsi.fastutil.objects.Object2IntArrayMap
@@ -162,9 +162,7 @@ object LambdaAtlas : Loadable {
162162
characters: Int = 2048 // How many characters from that font should be used for the generation
163163
) {
164164
val font = fontCache.computeIfAbsent(this) {
165-
val resource = LambdaResource("fonts/$fontName.ttf")
166-
167-
Font.createFont(Font.TRUETYPE_FONT, resource.stream).deriveFont(64.0f)
165+
Font.createFont(Font.TRUETYPE_FONT, "fonts/$fontName.ttf".stream).deriveFont(64.0f)
168166
}
169167

170168
val textureSize = characters * 2

common/src/main/kotlin/com/lambda/graphics/shader/Shader.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class Shader(fragmentPath: String, vertexPath: String) {
3434
private val uniformCache: Object2IntMap<String> = Object2IntOpenHashMap()
3535

3636
private val id = createShaderProgram(
37-
loadShader(ShaderType.VERTEX_SHADER, LambdaResource("shaders/vertex/$vertexPath.vert")),
38-
loadShader(ShaderType.FRAGMENT_SHADER, LambdaResource("shaders/fragment/$fragmentPath.frag"))
37+
loadShader(ShaderType.VERTEX_SHADER, "shaders/vertex/$vertexPath.vert"),
38+
loadShader(ShaderType.FRAGMENT_SHADER, "shaders/fragment/$fragmentPath.frag")
3939
)
4040

4141
constructor(path: String) : this(path, path)

common/src/main/kotlin/com/lambda/graphics/shader/ShaderUtils.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.graphics.shader
1919

2020
import com.google.common.collect.ImmutableList
2121
import com.lambda.util.LambdaResource
22+
import com.lambda.util.stream
2223
import com.mojang.blaze3d.platform.GlStateManager
2324
import org.apache.commons.io.IOUtils
2425
import org.joml.Matrix4f
@@ -42,7 +43,7 @@ object ShaderUtils {
4243
error?.let { err ->
4344
val builder = StringBuilder()
4445
.append("Failed to compile ${type.name} shader").appendLine()
45-
.append("Path: ${resource.path}").appendLine()
46+
.append("Path: $resource").appendLine()
4647
.append("Compiler output:").appendLine()
4748
.append(err)
4849

common/src/main/kotlin/com/lambda/graphics/texture/TextureOwner.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.graphics.texture
1919

2020
import com.lambda.util.LambdaResource
21+
import com.lambda.util.readImage
2122
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
2223
import java.awt.image.BufferedImage
2324

@@ -42,5 +43,5 @@ object TextureOwner {
4243
* @param path Lambda resource path containing the image data
4344
*/
4445
fun Any.upload(path: String, mipmaps: Int = 1) =
45-
Texture(LambdaResource.readImage(path), levels = mipmaps).also { textureMap[this@upload] = it }
46+
Texture(path.readImage(), levels = mipmaps).also { textureMap[this@upload] = it }
4647
}

common/src/main/kotlin/com/lambda/util/LambdaResource.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717

1818
package com.lambda.util
1919

20+
import com.lambda.Lambda
21+
import java.awt.image.BufferedImage
22+
import java.io.FileNotFoundException
2023
import java.io.InputStream
2124
import javax.imageio.ImageIO
2225

23-
class LambdaResource(val path: String) {
24-
val stream: InputStream?
25-
get() = javaClass.getResourceAsStream("/assets/lambda/$path")
26+
typealias LambdaResource = String
2627

27-
companion object {
28-
fun readImage(path: String) = ImageIO.read(LambdaResource(path).stream)
29-
}
30-
}
28+
val LambdaResource.stream: InputStream
29+
get() = Lambda::class.java.getResourceAsStream("/assets/lambda/$this")
30+
?: throw FileNotFoundException("File \"/assets/lambda/$this\" not found")
31+
32+
fun LambdaResource.readImage(): BufferedImage = ImageIO.read(this.stream)

0 commit comments

Comments
 (0)