option to start rendering on main thread, #29

This commit is contained in:
Bixilon 2021-11-29 14:55:03 +01:00
parent d6240abc20
commit ecd3d1e93e
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 56 additions and 20 deletions

View File

@ -26,6 +26,7 @@ import de.bixilon.minosoft.data.registries.versions.Versions
import de.bixilon.minosoft.gui.eros.Eros
import de.bixilon.minosoft.gui.eros.crash.ErosCrashReport.Companion.crash
import de.bixilon.minosoft.gui.eros.util.JavaFXInitializer
import de.bixilon.minosoft.gui.rendering.Rendering
import de.bixilon.minosoft.modding.event.events.FinishInitializingEvent
import de.bixilon.minosoft.modding.event.events.connection.play.PlayConnectionStateChangeEvent
import de.bixilon.minosoft.modding.event.events.connection.status.ServerStatusReceiveEvent
@ -56,6 +57,9 @@ object Minosoft {
val LANGUAGE_MANAGER = MultiLanguageManager()
val START_UP_LATCH = CountUpAndDownLatch(1)
val RENDERING_LATCH = CountUpAndDownLatch(Int.MAX_VALUE shr 1)
var rendering: Rendering? = null
@Deprecated("Will be singleton interface")
lateinit var config: Configuration
@ -65,6 +69,7 @@ object Minosoft {
private set
@JvmStatic
fun main(args: Array<String>) {
CommandLineArguments.parse(args)
@ -139,6 +144,11 @@ object Minosoft {
GlobalEventMaster.fireEvent(FinishInitializingEvent())
RunConfiguration.AUTO_CONNECT_TO?.let { autoConnect(it) }
while (true) {
RENDERING_LATCH.waitForChange()
rendering?.start() ?: continue
}
}
private fun autoConnect(address: ServerAddress, version: Version, account: Account) {

View File

@ -17,6 +17,7 @@ import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.gui.rendering.modding.events.WindowCloseEvent
import de.bixilon.minosoft.gui.rendering.sound.AudioPlayer
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.terminal.RunConfiguration
import de.bixilon.minosoft.util.CountUpAndDownLatch
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
@ -24,14 +25,30 @@ import de.bixilon.minosoft.util.logging.LogMessageType
import org.lwjgl.Version
class Rendering(private val connection: PlayConnection) {
private var latch: CountUpAndDownLatch? = null
val renderWindow: RenderWindow = RenderWindow(connection, this)
val audioPlayer: AudioPlayer = AudioPlayer(connection, this)
fun init(latch: CountUpAndDownLatch) {
Log.log(LogMessageType.RENDERING_GENERAL, LogLevels.INFO) { "Hello LWJGL ${Version.getVersion()}!" }
latch.inc()
startRenderThread(latch)
this.latch = latch
if (RunConfiguration.OPEN_Gl_ON_FIRST_THREAD) {
Minosoft.rendering = this
Minosoft.RENDERING_LATCH.dec()
return
}
start()
}
fun start() {
val latch = this.latch ?: throw IllegalStateException("Rendering not initialized yet!")
startAudioPlayerThread(latch)
if (RunConfiguration.OPEN_Gl_ON_FIRST_THREAD) {
startRenderWindow(latch)
} else {
startRenderWindowThread(latch)
}
}
private fun startAudioPlayerThread(latch: CountUpAndDownLatch) {
@ -55,27 +72,29 @@ class Rendering(private val connection: PlayConnection) {
}, "Audio#${connection.connectionId}").start()
}
private fun startRenderThread(latch: CountUpAndDownLatch) {
Thread({
private fun startRenderWindowThread(latch: CountUpAndDownLatch) {
Thread({ startRenderWindow(latch) }, "Rendering#${connection.connectionId}").start()
}
private fun startRenderWindow(latch: CountUpAndDownLatch) {
try {
CONTEXT_MAP[Thread.currentThread()] = renderWindow
renderWindow.init(latch)
renderWindow.startLoop()
} catch (exception: Throwable) {
CONTEXT_MAP.remove(Thread.currentThread())
exception.printStackTrace()
try {
CONTEXT_MAP[Thread.currentThread()] = renderWindow
renderWindow.init(latch)
renderWindow.startLoop()
} catch (exception: Throwable) {
CONTEXT_MAP.remove(Thread.currentThread())
exception.printStackTrace()
try {
renderWindow.window.destroy()
connection.fireEvent(WindowCloseEvent(window = renderWindow.window))
} catch (ignored: Throwable) {
}
if (connection.protocolState.connected) {
connection.disconnect()
}
connection.disconnect()
connection.error = exception
renderWindow.window.destroy()
connection.fireEvent(WindowCloseEvent(window = renderWindow.window))
} catch (ignored: Throwable) {
}
}, "Rendering#${connection.connectionId}").start()
if (connection.protocolState.connected) {
connection.disconnect()
}
connection.disconnect()
connection.error = exception
}
}
companion object {

View File

@ -63,6 +63,10 @@ object CommandLineArguments {
.setDefault(null)
.action(Arguments.store())
.help("Automatically connects to a specific server. Full format: hostname.whatever<:port><,version><,account>\nPort is by default ${ProtocolDefinition.DEFAULT_PORT}, version is automatic and account is the current selected one")
addArgument("--opengl_on_first_thread")
.action(Arguments.storeTrue())
.help("Forces OpenGL to use the main thread. Can not be disabled on MacOS. Defaults to false")
}
fun parse(args: Array<String>) {
@ -100,5 +104,7 @@ object CommandLineArguments {
}
RunConfiguration.AUTO_CONNECT_TO = namespace.getString("connect")
RunConfiguration.OPEN_Gl_ON_FIRST_THREAD = RunConfiguration.OPEN_Gl_ON_FIRST_THREAD || namespace.getBoolean("opengl_on_first_thread")
}
}

View File

@ -59,4 +59,5 @@ object RunConfiguration {
var VERSION_STRING = "Minosoft ${StaticConfiguration.VERSION}"
var SKIP_RENDERERS: List<ResourceLocation> = listOf()
var OPEN_Gl_ON_FIRST_THREAD = OSUtil.OS == OSUtil.OSs.MAC
}