Skip to content

Commit fcbc8ee

Browse files
committed
Add configs & some graphics api
1 parent 917ad2f commit fcbc8ee

9 files changed

Lines changed: 193 additions & 3 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.turboscript;
2+
3+
import net.neoforged.fml.ModContainer;
4+
import net.neoforged.neoforge.client.gui.ConfigurationScreen;
5+
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
6+
7+
public class JavaWrapper {
8+
9+
public static void registerExtensionPointConfig(ModContainer container) {
10+
container.registerExtensionPoint(IConfigScreenFactory.class, ConfigurationScreen::new);
11+
}
12+
13+
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.github.turboscript
22

3+
import com.github.turboscript.config.Configs
34
import com.mojang.logging.LogUtils
45
import net.neoforged.api.distmarker.Dist
56
import net.neoforged.bus.api.IEventBus
67
import net.neoforged.fml.ModContainer
78
import net.neoforged.fml.common.Mod
9+
import net.neoforged.fml.config.ModConfig
810
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent
911
import org.slf4j.Logger
1012

@@ -15,17 +17,22 @@ class TurboScript(
1517
) {
1618

1719
init {
20+
NAMESPACE = modContainer.namespace
1821
modEventBus.addListener(this::commonSetup)
22+
23+
modContainer.registerConfig(ModConfig.Type.COMMON, Configs.spec)
24+
JavaWrapper.registerExtensionPointConfig(modContainer)
1925
}
2026

2127
private fun commonSetup(event: FMLCommonSetupEvent) {
22-
LOGGER.info(event.description())
28+
LOGGER.info("TurboScript is running!")
2329
}
2430

2531
companion object {
2632

27-
const val MOD_ID = "turboscript"
28-
val LOGGER: Logger = LogUtils.getLogger()
33+
internal const val MOD_ID = "turboscript"
34+
internal val LOGGER: Logger = LogUtils.getLogger()
35+
internal lateinit var NAMESPACE: String; private set
2936

3037
}
3138
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.turboscript.config
2+
3+
import net.neoforged.neoforge.common.ModConfigSpec
4+
import org.apache.commons.lang3.tuple.Pair
5+
6+
class Configs(builder: ModConfigSpec.Builder) {
7+
8+
companion object {
9+
private val pair: Pair<Configs, ModConfigSpec> =
10+
ModConfigSpec.Builder().configure { Configs(it) }
11+
12+
internal fun get(): Configs = pair.left
13+
internal val spec: ModConfigSpec get() = pair.right
14+
}
15+
16+
internal val renderApi: ModConfigSpec.EnumValue<RenderApi>
17+
18+
init {
19+
builder
20+
.comment("Rendering Settings")
21+
.comment("Rendering settings for the TurboScript mod")
22+
.translation("turboscript.config.rendering")
23+
.push("rendering")
24+
25+
renderApi = builder
26+
.comment("Render API")
27+
.comment("The rendering API to use for the TurboScript mod")
28+
.translation("turboscript.config.rendering.api")
29+
.defineEnum("api", RenderApi.OPENGL)
30+
31+
builder.pop()
32+
}
33+
34+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.turboscript.config
2+
3+
enum class RenderApi {
4+
5+
OPENGL,
6+
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.turboscript.graphics
2+
3+
import com.github.turboscript.config.Configs
4+
import com.github.turboscript.config.RenderApi
5+
import com.github.turboscript.graphics.ogl.TOpenGlRenderSystem
6+
7+
object TRenderSystem {
8+
9+
internal val renderApi: RenderApi
10+
get() = Configs.get().renderApi.get()
11+
12+
internal fun beginRender() {
13+
when (renderApi) {
14+
RenderApi.OPENGL -> TOpenGlRenderSystem.beginRender()
15+
}
16+
}
17+
18+
internal fun endRender() {
19+
when (renderApi) {
20+
RenderApi.OPENGL -> TOpenGlRenderSystem.endRender()
21+
}
22+
}
23+
24+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.github.turboscript.graphics.ogl
2+
3+
import com.mojang.blaze3d.platform.GlStateManager
4+
import org.lwjgl.opengl.GL46.*
5+
import kotlin.properties.ReadWriteProperty
6+
import kotlin.reflect.KProperty
7+
8+
object OpenGlState {
9+
10+
private val blend0 = GlState(false) {
11+
if (it) {
12+
GlStateManager._enableBlend()
13+
GlStateManager._blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
14+
} else {
15+
GlStateManager._disableBlend()
16+
}
17+
}
18+
var blend by blend0
19+
20+
private val depth0 = GlState(false) {
21+
if (it) GlStateManager._enableDepthTest()
22+
else GlStateManager._disableDepthTest()
23+
}
24+
var depth by depth0
25+
26+
private val cull0 = GlState(false) {
27+
if (it) GlStateManager._enableCull()
28+
else GlStateManager._disableCull()
29+
}
30+
var cull by cull0
31+
32+
private val lineSmooth0 = GlState(false) {
33+
if (it) glEnable(GL_LINE_SMOOTH)
34+
else glDisable(GL_LINE_SMOOTH)
35+
}
36+
var lineSmooth by lineSmooth0
37+
38+
private val scissor0 = GlState(false) {
39+
if (it) GlStateManager._enableScissorTest()
40+
else GlStateManager._disableScissorTest()
41+
}
42+
var scissor by scissor0
43+
44+
fun bindTexture(unit: Int, texture: Int) {
45+
GlStateManager._activeTexture(GL_TEXTURE0 + unit)
46+
GlStateManager._bindTexture(texture)
47+
}
48+
49+
private val vertexArray0 = GlState(0) { glBindVertexArray(it) }
50+
var vertexArray by vertexArray0
51+
52+
private val program0 = GlState(0) { glUseProgram(it) }
53+
var program by program0
54+
55+
fun reset() {
56+
blend0.forceSetValue(false)
57+
depth0.forceSetValue(false)
58+
cull0.forceSetValue(false)
59+
lineSmooth0.forceSetValue(false)
60+
vertexArray0.forceSetValue(0)
61+
program0.forceSetValue(0)
62+
scissor0.forceSetValue(false)
63+
}
64+
65+
}
66+
67+
class GlState<T>(valueIn: T, private val action: (T) -> Unit) : ReadWriteProperty<Any?, T> {
68+
69+
private var value = valueIn
70+
71+
override operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value
72+
73+
override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
74+
if (this.value != value) {
75+
this.value = value
76+
action.invoke(value)
77+
}
78+
}
79+
80+
fun forceSetValue(value: T) {
81+
this.value = value
82+
action.invoke(value)
83+
}
84+
85+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.turboscript.graphics.ogl
2+
3+
object TOpenGlRenderSystem {
4+
5+
internal fun beginRender() {
6+
OpenGlState.reset()
7+
}
8+
9+
internal fun endRender() {
10+
}
11+
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"turboscript.config.rendering": "Rendering Configuration",
3+
"turboscript.config.rendering.api": "Rendering API"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"turboscript.config.rendering": "渲染配置",
3+
"turboscript.config.rendering.api": "渲染 API"
4+
}

0 commit comments

Comments
 (0)