add fps limiting option

This commit is contained in:
Chloe committed 2025-01-19 04:27:23 -07:00
1 parent 327e9b4d39
commit 53cd339213
8 files changed
+274 -12

No files matched your search

@@ -115,9 +115,9 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
@RequiresApi(Build.VERSION_CODES.R)
fun updateRefreshRate() {
val maxRefreshRate = display.supportedModes.maxBy { it.refreshRate }
val maxRefreshRate = display.supportedModes.maxBy { it.refreshRate }.refreshRate
holder.surface.setFrameRate(
maxRefreshRate.refreshRate,
maxRefreshRate,
Surface.FRAME_RATE_COMPATIBILITY_DEFAULT
)
}
@@ -5,6 +5,9 @@ import android.os.Build
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10
private const val NANOSECONDS_PER_SECOND = 1000000000L
private const val NANOSECONDS_PER_MICROSECOND = 1000000L
@Suppress("unused", "KotlinJniMissingFunction")
class Cocos2dxRenderer(private var handler: Cocos2dxGLSurfaceView) : GLSurfaceView.Renderer {
companion object {
@@ -62,6 +65,11 @@ class Cocos2dxRenderer(private var handler: Cocos2dxGLSurfaceView) : GLSurfaceVi
private var screenHeight = 0
var sendResizeEvents = false
var setFrameRate = false
private var mAnimationInterval: Long? = null
fun limitFrameRate(rate: Int) {
mAnimationInterval = NANOSECONDS_PER_SECOND/rate
}
fun setScreenWidthAndHeight(surfaceWidth: Int, surfaceHeight: Int) {
screenWidth = surfaceWidth
@@ -80,7 +88,23 @@ class Cocos2dxRenderer(private var handler: Cocos2dxGLSurfaceView) : GLSurfaceVi
}
override fun onDrawFrame(gl: GL10?) {
nativeRender()
val animationInterval = mAnimationInterval
if (animationInterval == null) {
nativeRender()
} else {
nativeRender()
var now = System.nanoTime()
var interval = now - this.lastTickInNanoSeconds
if (interval < animationInterval) {
try {
Thread.sleep((animationInterval - interval) / NANOSECONDS_PER_MICROSECOND)
} catch (e: Exception) {}
}
lastTickInNanoSeconds = System.nanoTime()
}
}
fun handleActionDown(id: Int, x: Float, y: Float) {