Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Foresst: InputStreamFilePairs.kt
* Copyright (C) 2025 mtctx
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.mtctx.foresst.resource

object InputStreamFilePairs {
val <X> Pair<X, *>.inputStream get() = first
val <Y> Pair<*, Y>.file get() = second
}
43 changes: 43 additions & 0 deletions core/src/main/kotlin/dev/mtctx/foresst/resource/Resource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Foresst: Resource.kt
* Copyright (C) 2025 mtctx
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.mtctx.foresst.resource

import java.io.File
import java.io.InputStream
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption

class Resource(resourcePath: String) {

private val inputStream: InputStream =
ResourceLoader.getResourceAsStream(resourcePath)
?: throw IllegalStateException("Resource $resourcePath not found.")

private val tempFile: Path by lazy {
val tempFile = Files.createTempFile("foresst-", ".tmp")
tempFile.toFile().deleteOnExit()
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING)
tempFile
}

val file: File
get() = tempFile.toFile()
}
32 changes: 32 additions & 0 deletions core/src/main/kotlin/dev/mtctx/foresst/resource/ResourceLoader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Foresst: ResourceLoader.kt
* Copyright (C) 2025 mtctx
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.mtctx.foresst.resource

import java.io.InputStream

object ResourceLoader {

fun getResourceAsStream(resourcePath: String): InputStream? {
return ResourceLoader::class.java.classLoader.getResourceAsStream(resourcePath)
}
}

fun import(resourcePath: String): Resource {
return Resource(resourcePath)
}
37 changes: 37 additions & 0 deletions core/src/main/kotlin/dev/mtctx/foresst/resource/conversion.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Foresst: conversion.kt
* Copyright (C) 2025 mtctx, kvxd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.mtctx.foresst.resource

import java.io.InputStream
import java.nio.charset.Charset

fun Resource.asText(charset: String = "UTF-8"): String =
file.readText(Charset.forName(charset))

fun Resource.asByteArray(): ByteArray =
file.readBytes()

fun Resource.asInputStream(): InputStream =
file.inputStream()

fun Resource.asReader(charset: String = "UTF-8") =
file.reader(Charset.forName(charset))

fun Resource.asLines(charset: String = "UTF-8"): Sequence<String> =
file.useLines(Charset.forName(charset)) { it.toList() }.asSequence()
3 changes: 3 additions & 0 deletions graphics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ dependencies {
implementation("org.lwjgl", "lwjgl-openal", classifier = lwjglNatives)
implementation("org.lwjgl", "lwjgl-opengl", classifier = lwjglNatives)
implementation("org.lwjgl", "lwjgl-stb", classifier = lwjglNatives)

implementation("io.github.kotlin-graphics:glm:0.9.9.1-12")

testImplementation(kotlin("test"))
}

Expand Down
86 changes: 86 additions & 0 deletions graphics/src/main/kotlin/dev/mtctx/foresst/graphics/Graphics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Foresst: Graphics.kt
* Copyright (C) 2025 mtctx, kvxd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.mtctx.foresst.graphics

import dev.mtctx.foresst.graphics.gl.GLShader
import dev.mtctx.foresst.graphics.gl.GLShaderType
import dev.mtctx.foresst.graphics.renderers.VoxelRenderer
import dev.mtctx.foresst.resource.Resource
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.glfw.GLFWErrorCallback
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL46.*


// TERRIBLE CODE AHEAD; WATCH YOUR STEP
object Graphics {

lateinit var window: Window
lateinit var shader: GLShader
private val renderers = mutableListOf<Renderer>()

fun run() {
init()
render()
}

fun init() {
GLFWErrorCallback.createPrint(System.err).set()
check(glfwInit()) { "Unable to initialize GLFW" }
window = Window()
GL.createCapabilities()

val fragment = Resource("frag.fs")
val vertex = Resource("vert.vs")

shader = GLShader(
listOf(
GLShader.ShaderModule(
fragment,
GLShaderType.FRAGMENT
),
GLShader.ShaderModule(
vertex,
GLShaderType.VERTEX
)
)
)

renderers.add(VoxelRenderer(shader))
}

fun render() {
while (!glfwWindowShouldClose(window.handle)) {
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)

val deltaTime = glfwGetTime().toFloat()

renderers.forEach {
it.update(deltaTime)
it.render()
}

glfwSwapBuffers(window.handle)
glfwPollEvents()
}

renderers.forEach(Renderer::close)
}

}
27 changes: 27 additions & 0 deletions graphics/src/main/kotlin/dev/mtctx/foresst/graphics/Renderable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Foresst: Renderable.kt
* Copyright (C) 2025 mtctx
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.mtctx.foresst.graphics

import java.io.Closeable

interface Renderable: Closeable {

fun render()

}
26 changes: 26 additions & 0 deletions graphics/src/main/kotlin/dev/mtctx/foresst/graphics/Renderer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Foresst: Renderer.kt
* Copyright (C) 2025 mtctx, kvxd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.mtctx.foresst.graphics

interface Renderer : AutoCloseable {

fun render()
fun update(deltaTime: Float) {}

}
63 changes: 63 additions & 0 deletions graphics/src/main/kotlin/dev/mtctx/foresst/graphics/Window.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Foresst: Window.kt
* Copyright (C) 2025 mtctx, kvxd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.mtctx.foresst.graphics

import java.io.Closeable

import org.lwjgl.glfw.Callbacks
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.opengl.GL46.*
import org.lwjgl.system.MemoryUtil

class Window(
var width: Int = 800,
var height: Int = 600,
var handle: Long = -1
) : Closeable {

init {
glfwDefaultWindowHints()
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)

handle = glfwCreateWindow(width, height, "Keaengine", MemoryUtil.NULL, MemoryUtil.NULL)
if (handle == MemoryUtil.NULL) throw RuntimeException("Failed to create the GLFW window")

glfwMakeContextCurrent(handle)
glfwSwapInterval(1)

glfwShowWindow(handle)

glfwSetFramebufferSizeCallback(handle) { _: Long, newWidth: Int, newHeight: Int ->
this.width = newWidth
this.height = newHeight
glViewport(0, 0, width, height)
}
}

override fun close() {
Callbacks.glfwFreeCallbacks(handle)
glfwDestroyWindow(handle)
}

}
Loading
Loading