Skip to content

Commit

Permalink
Prepare for interchangeable SurfaceViews
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Apr 7, 2023
1 parent bfe8edf commit dc06308
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.simongellis.vvb.emulator

import androidx.annotation.ColorInt
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class AnaglyphRenderer(emulator: Emulator, settings: Settings) : Renderer {
private var _pointer = 0L
Expand All @@ -21,15 +19,15 @@ class AnaglyphRenderer(emulator: Emulator, settings: Settings) : Renderer {
}
}

override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
override fun onSurfaceCreated() {
nativeOnSurfaceCreated()
}

override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
override fun onSurfaceChanged(width: Int, height: Int) {
nativeOnSurfaceChanged(width, height)
}

override fun onDrawFrame(gl: GL10?) {
override fun onDrawFrame() {
nativeOnDrawFrame()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.simongellis.vvb.emulator

import androidx.annotation.ColorInt
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class CardboardRenderer(emulator: Emulator, settings: Settings) : Renderer {
private var _pointer = 0L
Expand All @@ -21,15 +19,15 @@ class CardboardRenderer(emulator: Emulator, settings: Settings) : Renderer {
}
}

override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
override fun onSurfaceCreated() {
nativeOnSurfaceCreated()
}

override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
override fun onSurfaceChanged(width: Int, height: Int) {
nativeOnSurfaceChanged(width, height)
}

override fun onDrawFrame(gl: GL10?) {
override fun onDrawFrame() {
nativeOnDrawFrame()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.simongellis.vvb.emulator

import androidx.annotation.ColorInt
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class LeiaRenderer(emulator: Emulator, settings: Settings) : Renderer {
private var _pointer = 0L
Expand All @@ -21,15 +19,15 @@ class LeiaRenderer(emulator: Emulator, settings: Settings) : Renderer {
}
}

override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
override fun onSurfaceCreated() {
nativeOnSurfaceCreated()
}

override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
override fun onSurfaceChanged(width: Int, height: Int) {
nativeOnSurfaceChanged(width, height)
}

override fun onDrawFrame(gl: GL10?) {
override fun onDrawFrame() {
nativeOnDrawFrame()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.simongellis.vvb.emulator

import androidx.annotation.ColorInt
import androidx.annotation.IntRange
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class MonoRenderer(emulator: Emulator, settings: Settings): Renderer {
private var _pointer = 0L
Expand All @@ -22,15 +20,15 @@ class MonoRenderer(emulator: Emulator, settings: Settings): Renderer {
}
}

override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
override fun onSurfaceCreated() {
nativeOnSurfaceCreated()
}

override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
override fun onSurfaceChanged(width: Int, height: Int) {
nativeOnSurfaceChanged(width, height)
}

override fun onDrawFrame(gl: GL10?) {
override fun onDrawFrame() {
nativeOnDrawFrame()
}

Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/simongellis/vvb/emulator/Renderer.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.simongellis.vvb.emulator

import android.opengl.GLSurfaceView

interface Renderer : GLSurfaceView.Renderer {
interface Renderer {
fun onSurfaceCreated()
fun onSurfaceChanged(width: Int, height: Int)
fun onDrawFrame()
fun destroy()
fun onResume() {}
fun onModeChanged(enable3d: Boolean) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.simongellis.vvb.emulator

import androidx.annotation.ColorInt
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class StereoRenderer(emulator: Emulator, settings: Settings): Renderer {
private var _pointer = 0L
Expand All @@ -21,15 +19,15 @@ class StereoRenderer(emulator: Emulator, settings: Settings): Renderer {
}
}

override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
override fun onSurfaceCreated() {
nativeOnSurfaceCreated()
}

override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
override fun onSurfaceChanged(width: Int, height: Int) {
nativeOnSurfaceChanged(width, height)
}

override fun onDrawFrame(gl: GL10?) {
override fun onDrawFrame() {
nativeOnDrawFrame()
}

Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/com/simongellis/vvb/game/GLSurfaceViewAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.simongellis.vvb.game

import android.content.Context
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class GLSurfaceViewAdapter: GLSurfaceView, SurfaceViewAdapter {
constructor(context: Context): super(context)
constructor(context: Context, attrs: AttributeSet?): super(context, attrs)

override fun setRenderer(renderer: com.simongellis.vvb.emulator.Renderer) {
setEGLContextClientVersion(2)
setRenderer(RendererAdapter(renderer))
renderMode = RENDERMODE_CONTINUOUSLY
}

class RendererAdapter(private val inner: com.simongellis.vvb.emulator.Renderer): Renderer {
override fun onSurfaceCreated(gl: GL10?, p1: EGLConfig?) {
inner.onSurfaceCreated()
}

override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
inner.onSurfaceChanged(width, height)
}

override fun onDrawFrame(gl: GL10?) {
inner.onDrawFrame()
}
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/com/simongellis/vvb/game/GameView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.simongellis.vvb.game
import android.content.Context
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
Expand Down Expand Up @@ -36,6 +35,9 @@ class GameView : ConstraintLayout, BacklightModeListener {

val requestedOrientation: Int

private val _surfaceView: SurfaceViewAdapter
get() = _binding.surfaceView as SurfaceViewAdapter

constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
Expand All @@ -61,9 +63,7 @@ class GameView : ConstraintLayout, BacklightModeListener {
guidePercent = _preferences.horizontalOffset
}

surfaceView.setEGLContextClientVersion(2)
surfaceView.setRenderer(_renderer)
surfaceView.renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY
_surfaceView.setRenderer(_renderer)

gamepadView.setPreferences(_preferences)

Expand All @@ -85,12 +85,12 @@ class GameView : ConstraintLayout, BacklightModeListener {
}

fun onPause() {
_binding.surfaceView.onPause()
_surfaceView.onPause()
checkShouldToggle3D(false)
}

fun onResume() {
_binding.surfaceView.onResume()
_surfaceView.onResume()
_renderer.onResume()
checkShouldToggle3D(true)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.simongellis.vvb.game

import com.simongellis.vvb.emulator.Renderer

interface SurfaceViewAdapter {
fun setRenderer(renderer: Renderer)
fun onPause()
fun onResume()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.simongellis.vvb.game

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.simongellis.vvb.R
import com.simongellis.vvb.databinding.WrapperSurfaceViewAdapterBinding
import com.simongellis.vvb.emulator.Renderer

class WrapperSurfaceViewAdapter : LinearLayout, SurfaceViewAdapter {
constructor(context: Context): super(context)
constructor(context: Context, attrs: AttributeSet?): super(context, attrs)

init {
val layoutInflater = LayoutInflater.from(context)
WrapperSurfaceViewAdapterBinding.inflate(layoutInflater, this, true)
}

@Suppress("TypeParameterFindViewById")
private val inner: SurfaceViewAdapter
get() = findViewById(R.id.inner_surface_view) as SurfaceViewAdapter

override fun setRenderer(renderer: Renderer) {
inner.setRenderer(renderer)
}

override fun onPause() {
inner.onPause()
}

override fun onResume() {
inner.onResume()
}

}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/game_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
app:layout_constraintGuide_percent="0.3"
android:orientation="vertical" />

<android.opengl.GLSurfaceView
<com.simongellis.vvb.game.WrapperSurfaceViewAdapter
android:id="@+id/surface_view"
app:layout_constraintStart_toStartOf="@id/start_guideline"
app:layout_constraintWidth_default="percent"
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/wrapper_surface_view_adapter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.simongellis.vvb.game.GLSurfaceViewAdapter
android:id="@+id/inner_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

0 comments on commit dc06308

Please sign in to comment.