Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Sync from next
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Feb 17, 2021
1 parent c875d39 commit 89e6c28
Show file tree
Hide file tree
Showing 36 changed files with 576 additions and 279 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ easyPluginVersion=0.13.0
group=com.soywiz.korlibs.korgw
version=2.0.0-SNAPSHOT

korimVersion=2.0.6
korimVersion=2.0.7
jnaVersion=5.6.0

# bintray location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import kotlin.coroutines.*

actual fun CreateDefaultGameWindow(): GameWindow = TODO()

class AndroidGameWindow(val activity: KorgwActivity) : GameWindow() {
open class BaseAndroidGameWindow() : GameWindow() {
var coroutineContext: CoroutineContext? = null
}

class AndroidGameWindow(val activity: KorgwActivity) : BaseAndroidGameWindow() {
val androidContext get() = activity

override val ag: AG get() = activity.ag
Expand Down Expand Up @@ -109,18 +113,16 @@ class AndroidGameWindow(val activity: KorgwActivity) : GameWindow() {
return listOf(File(uri.toString()).toVfs())
}

lateinit var coroutineContext: CoroutineContext
override suspend fun loop(entry: suspend GameWindow.() -> Unit) {
this.coroutineContext = kotlin.coroutines.coroutineContext
//println("CONTEXT: ${kotlin.coroutines.coroutineContext[AndroidCoroutineContext.Key]?.context}")
entry(this)
}
}

class AndroidGameWindowNoActivity(override val width: Int, override val height: Int, override val ag: AG) : GameWindow() {
class AndroidGameWindowNoActivity(override val width: Int, override val height: Int, override val ag: AG) : BaseAndroidGameWindow() {

override var title: String = "Senaptec"
var coroutineContext: CoroutineContext? = null

override var icon: Bitmap?
get() = super.icon
Expand Down
50 changes: 2 additions & 48 deletions korgw/src/androidMain/kotlin/com/soywiz/korgw/KorgwActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.soywiz.korio.file.VfsFile
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CompletableDeferred
import androidx.core.app.ActivityCompat.startActivityForResult
import com.soywiz.klock.*

abstract class KorgwActivity : Activity() {
var gameWindow: AndroidGameWindow = AndroidGameWindow(this)
Expand Down Expand Up @@ -65,54 +66,7 @@ abstract class KorgwActivity : Activity() {
//ag = AGOpenglFactory.create(this).create(this, AGConfig())
ag = KorgwActivityAGOpengl()

mGLView = object : GLSurfaceView(this) {
val view = this

init {
println("KorgwActivity: Created GLSurfaceView $this for ${this@KorgwActivity}")

setEGLContextClientVersion(2)
setRenderer(object : GLSurfaceView.Renderer {
override fun onSurfaceCreated(unused: GL10, config: EGLConfig) {
//GLES20.glClearColor(0.0f, 0.4f, 0.7f, 1.0f)
gameWindow.handleContextLost()
}

override fun onDrawFrame(unused: GL10) {
gameWindow.handleInitEventIfRequired()
gameWindow.handleReshapeEventIfRequired(0, 0, view.width, view.height)
gameWindow.frame()
}

override fun onSurfaceChanged(unused: GL10, width: Int, height: Int) {
println("---------------- GLSurfaceView.onSurfaceChanged($width, $height) --------------")
//ag.contextVersion++
//GLES20.glViewport(0, 0, width, height)
//surfaceChanged = true
}
})
}

private val touches = TouchEventHandler()
private val coords = MotionEvent.PointerCoords()

override fun onTouchEvent(ev: MotionEvent): Boolean {
val gameWindow = gameWindow ?: return false

touches.handleEvent(gameWindow, gameWindow.coroutineContext, when (ev.action) {
MotionEvent.ACTION_DOWN -> TouchEvent.Type.START
MotionEvent.ACTION_MOVE -> TouchEvent.Type.MOVE
MotionEvent.ACTION_UP -> TouchEvent.Type.END
else -> TouchEvent.Type.END
}, { currentTouchEvent ->
for (n in 0 until ev.pointerCount) {
ev.getPointerCoords(n, coords)
currentTouchEvent.touch(ev.getPointerId(n), coords.x.toDouble(), coords.y.toDouble())
}
})
return true
}
}
mGLView = KorgwSurfaceView(this, this, gameWindow)

gameWindow.initializeAndroid()
setContentView(mGLView)
Expand Down
76 changes: 76 additions & 0 deletions korgw/src/androidMain/kotlin/com/soywiz/korgw/KorgwSurfaceView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.soywiz.korgw

import android.app.*
import android.content.*
import android.opengl.*
import android.os.*
import android.util.*
import android.view.*
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10
import com.soywiz.klock.*
import com.soywiz.korev.*

class KorgwSurfaceView(val viewOrActivity: Any?, context: Context, val gameWindow: BaseAndroidGameWindow) : GLSurfaceView(context) {
val view = this

init {
println("KorgwActivity: Created GLSurfaceView $this for ${viewOrActivity}")

setEGLContextClientVersion(2)
setRenderer(object : GLSurfaceView.Renderer {
override fun onSurfaceCreated(unused: GL10, config: EGLConfig) {
//GLES20.glClearColor(0.0f, 0.4f, 0.7f, 1.0f)
gameWindow.handleContextLost()
}

override fun onDrawFrame(unused: GL10) {
gameWindow.handleInitEventIfRequired()
gameWindow.handleReshapeEventIfRequired(0, 0, view.width, view.height)
gameWindow.frame()
}

override fun onSurfaceChanged(unused: GL10, width: Int, height: Int) {
println("---------------- GLSurfaceView.onSurfaceChanged($width, $height) --------------")
//ag.contextVersion++
//GLES20.glViewport(0, 0, width, height)
//surfaceChanged = true
}
})
}

private val touches = TouchEventHandler()
private val coords = MotionEvent.PointerCoords()

override fun onTouchEvent(ev: MotionEvent): Boolean {
val gameWindow = gameWindow

val actionMasked = ev.actionMasked
val actionPointerIndex = ev.actionIndex

if (ev.action != MotionEvent.ACTION_MOVE) {
//println("[${DateTime.nowUnixLong()}]onTouchEvent: ${ev.action}, ${MotionEvent.actionToString(ev.action)}, actionMasked=$actionMasked, actionPointerId=$actionPointerId, ev.pointerCount=${ev.pointerCount}")
}

val type = when (actionMasked) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> TouchEvent.Type.START
MotionEvent.ACTION_HOVER_MOVE -> TouchEvent.Type.HOVER
MotionEvent.ACTION_MOVE -> TouchEvent.Type.MOVE
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> TouchEvent.Type.END
else -> TouchEvent.Type.UNKNOWN
}
touches.handleEvent(gameWindow, gameWindow.coroutineContext!!, type) { currentTouchEvent ->
for (n in 0 until ev.pointerCount) {
ev.getPointerCoords(n, coords)
val id = ev.getPointerId(n)
val status = when {
type == TouchEvent.Type.START && actionPointerIndex == n -> Touch.Status.ADD
type == TouchEvent.Type.END && actionPointerIndex == n -> Touch.Status.REMOVE
else -> Touch.Status.KEEP
}
currentTouchEvent.touch(id, coords.x.toDouble(), coords.y.toDouble(), status)
}
}
return true
}
}
10 changes: 10 additions & 0 deletions korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ package com.soywiz.kgl
import com.soywiz.kds.*

abstract class KmlGl : Extra by Extra.Mixin(), IKmlGl {
open val root: KmlGl get() = this
open var info: ContextInfo = ContextInfo()

open class ContextInfo {
var current: () -> Any? = { null }

override fun toString(): String = "current=${current()}"
}

val DEPTH_BUFFER_BIT: Int = 0x0100
val STENCIL_BUFFER_BIT: Int = 0x0400
val COLOR_BUFFER_BIT: Int = 0x4000
Expand Down Expand Up @@ -316,6 +325,7 @@ abstract class KmlGl : Extra by Extra.Mixin(), IKmlGl {
val MAX_RENDERBUFFER_SIZE: Int = 0x84E8
val INVALID_FRAMEBUFFER_OPERATION: Int = 0x0506 // 1286

open fun handleContextLost() = Unit
override fun startFrame(): Unit = Unit
override fun endFrame(): Unit = Unit
open fun beforeDoRender(contextVersion: Int): Unit = Unit
Expand Down
8 changes: 8 additions & 0 deletions korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlExt.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.soywiz.kgl

import com.soywiz.klogger.*
import com.soywiz.kmem.*

class KmlGlException(message: String) : RuntimeException(message)
Expand Down Expand Up @@ -57,3 +58,10 @@ fun KmlGl.getErrorString(error: Int = getError()): String {
else -> "UNKNOWN_ERROR$error"
}
}

fun KmlGl.checkError(message: String) {
val error = getError()
if (error != NO_ERROR) {
Console.error("glGetError after $message")
}
}
48 changes: 37 additions & 11 deletions korgw/src/commonMain/kotlin/com/soywiz/kgl/KmlGlProxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package com.soywiz.kgl

import com.soywiz.klogger.*
import com.soywiz.kmem.*
import com.soywiz.korim.bitmap.*
import com.soywiz.korio.lang.printStackTrace
Expand Down Expand Up @@ -1014,6 +1015,14 @@ open class KmlGlProxy(parent: KmlGl) : KmlGlFastProxy(parent) {
}
}
open class KmlGlFastProxy(var parent: KmlGl) : KmlGl() {
override val root: KmlGl get() = parent.root

override var info: ContextInfo
get() = parent.info
set(value) { parent.info = value }

override fun handleContextLost() = parent.handleContextLost()

override fun beforeDoRender(contextVersion: Int) {
parent.beforeDoRender(contextVersion)
}
Expand Down Expand Up @@ -1456,32 +1465,49 @@ class LogKmlGlProxy(parent: KmlGl, var logBefore: Boolean = false, var logAfter:
if (logAfter) println("after: $name ($params) = $result")
}
}
class CheckErrorsKmlGlProxy(parent: KmlGl, val throwException: Boolean = false) : KmlGlProxy(parent) {
class CheckErrorsKmlGlProxy(parent: KmlGl, val throwException: Boolean = false, val printStackTrace: Boolean = false) : KmlGlProxy(parent) {
init {
//println("CheckErrorsKmlGlProxy")
}
override fun getError(): Int = parent.getError()

var tooManyErrors = false
var errorCount = 0
override fun handleContextLost() {
errorCount = 0
tooManyErrors = false
super.handleContextLost()
}

override fun before(name: String, params: String) {
parent.getError()
super.before(name, params)
}

override fun after(name: String, params: String, result: String): Unit {
do {
val error = parent.getError()
val error = getError()
if (error != NO_ERROR) {
println("glError: $error ${parent.getErrorString(error)} calling $name($params) = $result")
if (throwException) {
throw RuntimeException("glError: $error ${parent.getErrorString(error)} calling $name($params) = $result")
} else {
printStackTrace("glError: $error ${parent.getErrorString(error)} calling $name($params) = $result")
if (errorCount >= 50) {
if (!tooManyErrors) {
tooManyErrors = true
Console.error("Too many OpenGL errors")
}
break
}
errorCount++
val msg = "glError: $error ${parent.getErrorString(error)} calling $name($params) = $result [$info]"
Console.warn(msg)
when {
throwException -> throw RuntimeException(msg)
printStackTrace -> printStackTrace(msg)
}
}
} while (error != NO_ERROR)
}
override fun getError(): Int = parent.getError()
}

fun KmlGl.checked(throwException: Boolean = false) = CheckErrorsKmlGlProxy(this, throwException)
fun KmlGl.checkedIf(checked: Boolean, throwException: Boolean = false) = if (checked) CheckErrorsKmlGlProxy(this, throwException) else this
fun KmlGl.cachedIf(cached: Boolean, throwException: Boolean = false) = if (cached) KmlGlCached(this) else this
fun KmlGl.checked(throwException: Boolean = false, printStackTrace: Boolean = false) = CheckErrorsKmlGlProxy(this, throwException, printStackTrace)
fun KmlGl.checkedIf(checked: Boolean, throwException: Boolean = false, printStackTrace: Boolean = false) = if (checked) CheckErrorsKmlGlProxy(this, throwException, printStackTrace) else this
fun KmlGl.cachedIf(cached: Boolean) = if (cached) KmlGlCached(this) else this
fun KmlGl.logIf(log:Boolean=false) = if (log) LogKmlGlProxy(this) else this

This file was deleted.

5 changes: 3 additions & 2 deletions korgw/src/commonMain/kotlin/com/soywiz/korag/AG.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.soywiz.korag

import com.soywiz.kds.*
import com.soywiz.kgl.KmlGl
import com.soywiz.klogger.*
import com.soywiz.kmem.*
import com.soywiz.korag.shader.*
import com.soywiz.korim.bitmap.*
Expand Down Expand Up @@ -40,8 +41,8 @@ abstract class AG : Extra by Extra.Mixin() {
var contextVersion = 0
abstract val nativeComponent: Any

fun contextLost() {
println("AG.contextLost() : $this")
open fun contextLost() {
Console.info("AG.contextLost()", this)
contextVersion++
}

Expand Down
Loading

0 comments on commit 89e6c28

Please sign in to comment.