Skip to content

Commit

Permalink
Update to korlibs 6.0.0-alpha4 and split korge into korge and korge-c…
Browse files Browse the repository at this point in the history
…ore (#2243)

* Update to korlibs 6.0.0-alpha4

* Split korge into korge-core and korge

* Add KorgeCore function to start Korge without the views system
  • Loading branch information
soywiz authored Jun 18, 2024
1 parent 3e48c4f commit 200c6a6
Show file tree
Hide file tree
Showing 258 changed files with 509 additions and 319 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ kotlinx-coroutines = "1.9.0-RC"
kotlinx-serialization = "1.7.0"
kotlinx-atomicfu = "0.24.0"

korlibs = "6.0.0-alpha3"
korlibs = "6.0.0-alpha4"
#korlibs = "999.0.0.999"

kotlinx-benchmark = "0.4.7"
Expand Down
31 changes: 31 additions & 0 deletions korge-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import korlibs.applyProjectProperties

description = "Multiplatform Game Engine written in Kotlin"

project.extensions.extraProperties.properties.apply {
applyProjectProperties(
"https://github.com/korlibs/korge",
"MIT License",
"https://raw.githubusercontent.com/korlibs/korge/master/LICENSE"
)
}

dependencies {
commonMainApi(libs.korlibs.audio)
commonMainApi(libs.korlibs.image)
commonMainApi(libs.korlibs.inject)
commonMainApi(libs.korlibs.template)
commonMainApi(libs.korlibs.time)
commonMainApi(libs.kotlinx.atomicfu)
commonMainApi(libs.kotlinx.coroutines.core)
//commonTestApi(project(":korge-test"))
jvmMainApi("org.jetbrains.kotlin:kotlin-reflect")
jvmMainImplementation(libs.jackson.databind)
jvmMainImplementation(libs.jackson.module.kotlin)

//commonTestApi(testFixtures(project(":korma")))

//add("jvmMainApi", project(":korte"))

//add("commonTestApi", "it.krzeminski.vis-assert:vis-assert:0.4.0-beta")
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import korlibs.io.concurrent.atomic.*
import korlibs.io.file.*
import korlibs.io.file.sync.*
import korlibs.io.lang.*
import korlibs.korge.internal.*
import korlibs.math.*
import korlibs.memory.*
import korlibs.platform.*
Expand All @@ -21,7 +22,8 @@ import kotlin.time.*
/**
* <https://www.kernel.org/doc/Documentation/input/gamepad.txt>
*/
internal class LinuxJoyEventAdapter @OptIn(SyncIOAPI::class) constructor(val syncIO: SyncIO = SyncIO) : AutoCloseable {
@KorgeInternal
class LinuxJoyEventAdapter @OptIn(SyncIOAPI::class) constructor(val syncIO: SyncIO = SyncIO) : AutoCloseable {
companion object {
const val JS_EVENT_BUTTON = 0x01 /* button pressed/released */
const val JS_EVENT_AXIS = 0x02 /* joystick moved */
Expand Down Expand Up @@ -93,7 +95,8 @@ internal class LinuxJoyEventAdapter @OptIn(SyncIOAPI::class) constructor(val syn
readers.clear()
}

internal class X11JoystickReader(val info: DeviceInfo, val platformSyncIO: SyncIO) : AutoCloseable {
@KorgeInternal
class X11JoystickReader(val info: DeviceInfo, val platformSyncIO: SyncIO) : AutoCloseable {
val index: Int = info.id

private val buttonsPressure = FloatArray(GameButton.MAX)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import korlibs.graphics.shader.Attribute
import korlibs.graphics.shader.Uniform
import korlibs.graphics.shader.VariableWithOffset
import korlibs.graphics.shader.VertexLayout
import korlibs.korge.internal.*
import korlibs.logger.Logger

internal fun lazyMetalShaderBufferInputLayouts(
@KorgeInternal
fun lazyMetalShaderBufferInputLayouts(
vertexLayouts: List<VertexLayout>,
uniforms: List<Uniform>
) = lazy {
Expand All @@ -17,7 +19,8 @@ internal fun lazyMetalShaderBufferInputLayouts(
)
}

internal class MetalShaderBufferInputLayouts(
@KorgeInternal
class MetalShaderBufferInputLayouts(
vertexLayouts: List<VertexLayout>,
private val inputBuffers: List<List<VariableWithOffset>>
) : List<List<VariableWithOffset>> by inputBuffers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package korlibs.graphics.metal.shader
import korlibs.graphics.shader.*
import korlibs.graphics.shader.gl.GlobalsProgramVisitor
import korlibs.io.util.Indenter
import korlibs.korge.internal.*

internal const val vertexMainFunctionName = "vertexMain"
internal const val fragmentMainFunctionName = "fragmentMain"
private const val vertexInputStructureName = "VertexInput"
internal const val vertexInputStructureDeclarationName = "vertexInput"

internal class MetalShaderGenerator(
@KorgeInternal
class MetalShaderGenerator(
private val vertexShader: VertexShader,
private val fragmentShader: FragmentShader,
private val bufferLayouts: MetalShaderBufferInputLayouts
Expand Down Expand Up @@ -43,6 +45,7 @@ internal class MetalShaderGenerator(
fragmentBodyGenerator
)

@KorgeInternal
data class Result(
val result: String,
val inputBuffers: MetalShaderBufferInputLayouts
Expand Down
8 changes: 8 additions & 0 deletions korge-core/src/korlibs/graphics/metal/shader/Shader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package korlibs.graphics.metal.shader

import korlibs.graphics.shader.*
import korlibs.korge.internal.*

@KorgeInternal
fun Pair<VertexShader, FragmentShader>.toNewMetalShaderStringResult(bufferInputsLayout: MetalShaderBufferInputLayouts): MetalShaderGenerator.Result = let { (vertexShader, fragmentShader) -> MetalShaderGenerator(vertexShader, fragmentShader, bufferInputsLayout) }
.generateResult()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions korge-core/src/korlibs/korge/KorgeCore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package korlibs.korge

import korlibs.io.async.*
import korlibs.render.*
import kotlinx.coroutines.*

/**
* ```kotlin
* suspend fun main() = KorgeCore {
* val stopWatch = Stopwatch().start()
* onRenderEvent {
* ag.clear(ag.mainFrameBuffer, color = Colors.RED.interpolateWith((stopWatch.elapsed.seconds % 1.0).toRatio(), Colors.WHITE))
* }
* }
* ```
*/
suspend fun KorgeCore(
config: GameWindowCreationConfig = GameWindowCreationConfig.DEFAULT,
gameWindow: GameWindow = CreateDefaultGameWindow(config),
block: suspend GameWindow.() -> Unit
) {
//withContext(PreferSyncIo(preferSyncIo = true)) {
withContext(PreferSyncIo(preferSyncIo = null)) {
gameWindow.loop {
block()
}
}
}

/*
suspefun test() {
KorgeCore {
val gameWindow = this
onRenderEvent {
gameWindow.ag.clear(gameWindow.ag.mainFrameBuffer, color = Colors.RED)
}
}
}
*/
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package korlibs.korge

import korlibs.platform.Platform
import korlibs.event.*
import korlibs.inject.Injector
import korlibs.io.lang.Environment
import korlibs.inject.*
import korlibs.io.lang.*
import korlibs.korge.internal.*
import korlibs.logger.*
import kotlin.jvm.JvmStatic
import kotlin.native.concurrent.ThreadLocal
import kotlin.reflect.KClass
import korlibs.platform.*
import kotlin.jvm.*
import kotlin.reflect.*

class ReloadClassContext(val injector: Injector, val refreshedClasses: Set<String>, val rootFolders: List<String>)

internal open class KorgeReloadInternalImpl {
@KorgeInternal
open class KorgeReloadInternalImpl {
open fun <T : Any> getReloadedClass(clazz: KClass<T>, context: ReloadClassContext): KClass<T> = clazz
open fun transferKeepProperties(old: Any, new: Any) = Unit
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package korlibs.korge.bitmapfont

import korlibs.korge.render.*
import korlibs.korge.view.*
import korlibs.image.color.*
import korlibs.image.font.*
import korlibs.korge.blend.*
import korlibs.math.geom.*

fun BitmapFont.drawText(
Expand Down
131 changes: 131 additions & 0 deletions korge-core/src/korlibs/korge/blend/BlendMode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package korlibs.korge.blend

import korlibs.graphics.*
import korlibs.image.color.*

/**
* Determines how pixels should be blended. The most common blend modes are: [NORMAL] (normal mix) and [ADD] (additive blending) along with [MULTIPLY] and others.
*
* [https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/blendFuncSeparate](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/blendFuncSeparate)
*
* ```kotlin
* // color(RGB) = (sourceColor * srcRGB) + (destinationColor * dstRGB)
* // color(A) = (sourceAlpha * srcAlpha) + (destinationAlpha * dstAlpha)
* ```
*/
data class BlendMode(
val factors: AGBlending,
val name: String? = null,
) {
val __hashCode: Int = factors.hashCode() + name.hashCode() * 7
override fun hashCode(): Int = __hashCode
override fun equals(other: Any?): Boolean = (this === other) || (other is BlendMode && this.factors == other.factors && name == other.name)
override fun toString(): String = name ?: super.toString()

fun apply(src: RGBAf, dst: RGBAf, out: RGBAf = RGBAf()): RGBAf {
return factors.apply(src, dst, out)
}

fun apply(src: RGBA, dst: RGBA): RGBA {
return factors.apply(src, dst)
}

companion object {
/** Mixes the source and destination colors using the source alpha value */
val NORMAL = BlendMode(name = "NORMAL", factors = AGBlending.NORMAL_PRE)
/** Not an actual blending. It is used to indicate that the next non-inherit BlendMode from its ancestors will be used. */
val INHERIT = NORMAL.copy(name = "INHERIT")
/** Doesn't blend at all. Just replaces the colors. */
val NONE = BlendMode(name = "NONE", factors = AGBlending(AGBlendFactor.ONE, AGBlendFactor.ZERO)) // REPLACE
/** Additive mixing for lighting effects */
val ADD = BlendMode(name = "ADD", factors = AGBlending.ADD_PRE)

// Unchecked
val MULTIPLY = BlendMode(name = "MULTIPLY", factors = AGBlending(AGBlendFactor.DESTINATION_COLOR, AGBlendFactor.ONE_MINUS_SOURCE_ALPHA))
val SCREEN = BlendMode(name = "SCREEN", factors = AGBlending(AGBlendFactor.ONE, AGBlendFactor.ONE_MINUS_SOURCE_COLOR))

val ERASE = BlendMode(name = "ERASE", factors = AGBlending(AGBlendFactor.ZERO, AGBlendFactor.ONE_MINUS_SOURCE_ALPHA))
val MASK = BlendMode(name = "MASK", factors = AGBlending(AGBlendFactor.ZERO, AGBlendFactor.SOURCE_ALPHA))
val BELOW = BlendMode(name = "BELOW", factors = AGBlending(AGBlendFactor.ONE_MINUS_DESTINATION_ALPHA, AGBlendFactor.DESTINATION_ALPHA))
val SUBTRACT = BlendMode(
name = "SUBTRACT", factors = AGBlending(
AGBlendFactor.SOURCE_ALPHA, AGBlendFactor.DESTINATION_ALPHA,
AGBlendFactor.ONE, AGBlendFactor.ONE,
AGBlendEquation.REVERSE_SUBTRACT
)
)
val INVERT = BlendMode(
name = "INVERT",
factors = AGBlending(
AGBlendFactor.ONE_MINUS_DESTINATION_COLOR, AGBlendFactor.ZERO,
AGBlendFactor.ONE, AGBlendFactor.ONE,
)
)

// Unimplemented
val LIGHTEN = BlendMode(
name = "LIGHTEN",
factors = AGBlending(
AGBlendFactor.SOURCE_ALPHA, AGBlendFactor.DESTINATION_ALPHA,
AGBlendFactor.ONE, AGBlendFactor.ONE
)
)
val DARKEN = BlendMode(
name = "DARKEN",
factors = AGBlending(
AGBlendFactor.SOURCE_ALPHA, AGBlendFactor.DESTINATION_ALPHA,
AGBlendFactor.ONE, AGBlendFactor.ONE
)
)
val DIFFERENCE = BlendMode(
name = "DIFFERENCE",
factors = AGBlending(
AGBlendFactor.SOURCE_ALPHA, AGBlendFactor.DESTINATION_ALPHA,
AGBlendFactor.ONE, AGBlendFactor.ONE
)
)
val ALPHA = BlendMode(
name = "ALPHA",
factors = AGBlending(
AGBlendFactor.SOURCE_ALPHA, AGBlendFactor.DESTINATION_ALPHA,
AGBlendFactor.ONE, AGBlendFactor.ONE
)
)
val HARDLIGHT = BlendMode(
name = "HARDLIGHT",
factors = AGBlending(
AGBlendFactor.SOURCE_ALPHA, AGBlendFactor.DESTINATION_ALPHA,
AGBlendFactor.ONE, AGBlendFactor.ONE
)
)

val OVERLAY: BlendMode get() = NORMAL
val REPLACE: BlendMode get() = NONE

val BY_ORDINAL: Array<BlendMode> = arrayOf(INHERIT, NONE, NORMAL, ADD, MULTIPLY, SCREEN, ERASE, MASK, BELOW, SUBTRACT, INVERT, LIGHTEN, DARKEN, DIFFERENCE, ALPHA, HARDLIGHT)
val BY_NAME: Map<String, BlendMode> = BY_ORDINAL.associateBy { it.name!! }
val STANDARD_LIST: List<BlendMode> = BY_ORDINAL.toList()

val TO_ORDINAL: Map<BlendMode, Int> = BY_ORDINAL.indices.associateBy { BY_ORDINAL[it] }

operator fun get(ordinal: Int): BlendMode = BY_ORDINAL.getOrElse(ordinal) { INHERIT }
operator fun get(name: String): BlendMode = BY_NAME[name.uppercase()] ?: INHERIT


// https://community.khronos.org/t/blending-mode/34770/4
//multiply a * b
//screen 1 - (1 - a) * (1 - b)
//darken min(a, b)
//lighten max(a, b)
//difference abs(a - b)
//negation 1 - abs(1 - a - b)
//exclusion a + b - 2 * a * b
//overlay a < .5 ? (2 * a * b) : (1 - 2 * (1 - a) * (1 - b))
//hard light b < .5 ? (2 * a * b) : (1 - 2 * (1 - a) * (1 - b))
//soft light b < .5 ? (2 * a * b + a * a * (1 - 2 * b)) : (sqrt(a) * (2 * b - 1) + (2 * a) * (1 - b))
//dodge a / (1 - b)
//burn 1 - (1 - a) / b
}
}

val BlendMode.ordinal: Int get() = BlendMode.TO_ORDINAL.getOrElse(this) { -1 }
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Input : Extra by Extra.Mixin() {
fun getTouch(id: Int) = touches.firstOrNull { it.id == id } ?: dummyTouch

@KorgeInternal
internal fun updateTouches(touchEvent: TouchEvent) {
fun updateTouches(touchEvent: TouchEvent) {
touch.copyFrom(touchEvent)
}

Expand Down Expand Up @@ -172,7 +172,7 @@ class Input : Extra by Extra.Mixin() {
}

@KorgeInternal
internal fun triggerOldKeyEvent(e: KeyEvent) {
fun triggerOldKeyEvent(e: KeyEvent) {
when (e.type) {
KeyEvent.Type.DOWN -> {
setKey(e.keyCode, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package korlibs.korge.internal

import korlibs.math.geom.*

@PublishedApi internal object DefaultViewport {
object DefaultViewport {
const val WIDTH = 1280
const val HEIGHT = 720
val SIZE = Size(WIDTH, HEIGHT)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package korlibs.korge.render
import korlibs.datastructure.*
import korlibs.graphics.*
import korlibs.io.lang.*
import korlibs.korge.internal.*

// @TODO: This is pretty generic, we could expose it elsewhere
class AgAutoFreeManager(
Expand Down Expand Up @@ -40,7 +41,8 @@ class AgAutoFreeManager(
}
}

internal fun gc() {
@KorgeInternal
fun gc() {
// Delete elements that didn't survive the last GC
for (entry in availableInLastGC) {
if (!cachedCloseables.contains(entry.closeable)) {
Expand Down
Loading

0 comments on commit 200c6a6

Please sign in to comment.