- Fix MainActivity.java crash on start.

- Separate MainActivity.java into two Classes: MainActivity.java and GameActivity
- Implement post callback in GLRenderer.
This commit is contained in:
gabriel 2023-11-25 17:51:17 -04:00
commit 367701b325
10 changed files with 123 additions and 53 deletions

View file

@ -2,6 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>
<uses-feature
android:required="true"
android:glEsVersion="0x0030000"/>
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
@ -13,14 +19,16 @@
android:theme="@style/Theme.Pandroid" android:theme="@style/Theme.Pandroid"
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".MainActivity" android:name=".app.MainActivity"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".app.GameActivity"
android:configChanges="screenSize|screenLayout|orientation|density|uiMode">
</activity>
</application> </application>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>
</manifest> </manifest>

View file

@ -0,0 +1,6 @@
package com.panda3ds.pandroid;
public class C {
public static final String EXTRA_PATH = "path";
public static final String LOG_TAG = "Alber";
}

View file

@ -0,0 +1,6 @@
package com.panda3ds.pandroid.app;
import androidx.appcompat.app.AppCompatActivity;
public class BaseActivity extends AppCompatActivity {
}

View file

@ -0,0 +1,39 @@
package com.panda3ds.pandroid.app;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.C;
import com.panda3ds.pandroid.app.BaseActivity;
import com.panda3ds.pandroid.view.PandaGlSurfaceView;
public class GameActivity extends BaseActivity {
private PandaGlSurfaceView pandaSurface;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pandaSurface = new PandaGlSurfaceView(this);;
setContentView(pandaSurface);
Intent intent = getIntent();
if(!intent.hasExtra(C.EXTRA_PATH)){
Toast.makeText(this, "INVALID ROM PATH", Toast.LENGTH_LONG).show();
finish();
return;
}
pandaSurface.getRenderer().postDrawEvent(()->{
String path = intent.getStringExtra(C.EXTRA_PATH);
Log.i(C.LOG_TAG,"Try load ROM: "+path);
AlberDriver.LoadRom(path);
});
}
}

View file

@ -1,24 +1,20 @@
package com.panda3ds.pandroid; package com.panda3ds.pandroid.app;
import androidx.appcompat.app.AppCompatActivity;
import static android.provider.Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION; import static android.provider.Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.WindowInsets;
import android.view.View;
import android.os.Environment; import android.os.Environment;
import android.widget.Toast; import android.widget.Toast;
import android.widget.FrameLayout;
import com.panda3ds.pandroid.PathUtils;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import androidx.appcompat.app.AppCompatActivity;
import com.panda3ds.pandroid.C;
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.utils.PathUtils;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
PandaGlSurfaceView glView;
private static final int PICK_3DS_ROM = 2; private static final int PICK_3DS_ROM = 2;
private void openFile() { private void openFile() {
@ -31,22 +27,18 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (!Environment.isExternalStorageManager()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Intent intent = new Intent(ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); if (!Environment.isExternalStorageManager()) {
startActivity(intent); Intent intent = new Intent(ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
} startActivity(intent);
glView = new PandaGlSurfaceView(this);
setContentView(glView);
FloatingActionButton fab = new FloatingActionButton(this);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openFile();
} }
}
setContentView(R.layout.activity_main);
findViewById(R.id.load_rom).setOnClickListener(v->{
openFile();
}); });
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(200, 200);
addContentView(fab, params);
} }
@Override @Override
@ -55,13 +47,10 @@ public class MainActivity extends AppCompatActivity {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
String path = PathUtils.getPath(getApplicationContext(), data.getData()); String path = PathUtils.getPath(getApplicationContext(), data.getData());
Toast.makeText(getApplicationContext(), "pandroid opening " + path, Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), "pandroid opening " + path, Toast.LENGTH_LONG).show();
glView.queueEvent(new Runnable() { startActivity(new Intent(this, GameActivity.class)
@Override .putExtra(C.EXTRA_PATH, path));
public void run() {
AlberDriver.LoadRom(path);
}
});
} }
super.onActivityResult(requestCode, resultCode, data);
} }
} }
} }

View file

@ -1,4 +1,4 @@
package com.panda3ds.pandroid; package com.panda3ds.pandroid.utils;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.Context; import android.content.Context;

View file

@ -1,4 +1,4 @@
package com.panda3ds.pandroid; package com.panda3ds.pandroid.view;
import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL10;
@ -10,11 +10,16 @@ import android.opengl.GLSurfaceView;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import com.panda3ds.pandroid.AlberDriver;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.util.ArrayList;
public class PandaGlRenderer implements GLSurfaceView.Renderer { public class PandaGlRenderer implements GLSurfaceView.Renderer {
private final ArrayList<Runnable> events = new ArrayList<>();
int screenWidth, screenHeight; int screenWidth, screenHeight;
int screenTexture; int screenTexture;
public int screenFbo; public int screenFbo;
@ -23,6 +28,7 @@ public class PandaGlRenderer implements GLSurfaceView.Renderer {
super(); super();
} }
@Override @Override
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
if (screenTexture != 0) { if (screenTexture != 0) {
@ -33,7 +39,7 @@ public class PandaGlRenderer implements GLSurfaceView.Renderer {
} }
super.finalize(); super.finalize();
} }
public void onSurfaceCreated(GL10 unused, EGLConfig config) { public void onSurfaceCreated(GL10 unused, EGLConfig config) {
Log.i("pandroid", glGetString(GL_EXTENSIONS)); Log.i("pandroid", glGetString(GL_EXTENSIONS));
Log.w("pandroid", glGetString(GL_VERSION)); Log.w("pandroid", glGetString(GL_VERSION));
@ -61,6 +67,10 @@ public class PandaGlRenderer implements GLSurfaceView.Renderer {
AlberDriver.Initialize(); AlberDriver.Initialize();
} }
public void postDrawEvent(Runnable callback){
events.add(callback);
}
public void onDrawFrame(GL10 unused) { public void onDrawFrame(GL10 unused) {
if (AlberDriver.HasRomLoaded()) { if (AlberDriver.HasRomLoaded()) {
AlberDriver.RunFrame(screenFbo); AlberDriver.RunFrame(screenFbo);
@ -68,6 +78,13 @@ public class PandaGlRenderer implements GLSurfaceView.Renderer {
glBindFramebuffer(GL_READ_FRAMEBUFFER, screenFbo); glBindFramebuffer(GL_READ_FRAMEBUFFER, screenFbo);
glBlitFramebuffer(0, 0, 400, 480, 0, 0, screenWidth, screenHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR); glBlitFramebuffer(0, 0, 400, 480, 0, 0, screenWidth, screenHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);
} }
int count = events.size();
while (count > 0){
events.get(0).run();
events.remove(0);
count--;
}
} }
public void onSurfaceChanged(GL10 unused, int width, int height) { public void onSurfaceChanged(GL10 unused, int width, int height) {
@ -76,4 +93,4 @@ public class PandaGlRenderer implements GLSurfaceView.Renderer {
screenHeight = height; screenHeight = height;
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);
} }
} }

View file

@ -1,11 +1,7 @@
package com.panda3ds.pandroid; package com.panda3ds.pandroid.view;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView;
import android.util.DisplayMetrics;
import com.panda3ds.pandroid.PandaGlRenderer;
public class PandaGlSurfaceView extends GLSurfaceView { public class PandaGlSurfaceView extends GLSurfaceView {
final PandaGlRenderer renderer; final PandaGlRenderer renderer;
@ -16,4 +12,8 @@ public class PandaGlSurfaceView extends GLSurfaceView {
renderer = new PandaGlRenderer(); renderer = new PandaGlRenderer();
setRenderer(renderer); setRenderer(renderer);
} }
public PandaGlRenderer getRenderer() {
return renderer;
}
} }

View file

@ -1,18 +1,22 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".app.MainActivity">
<TextView <LinearLayout
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:text="Hello World!" android:gravity="bottom|end"
app:layout_constraintBottom_toBottomOf="parent" android:padding="17dp">
app:layout_constraintEnd_toEndOf="parent" <Button
app:layout_constraintStart_toStartOf="parent" android:id="@+id/load_rom"
app:layout_constraintTop_toTopOf="parent" /> android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="@string/load_rom"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </FrameLayout>

View file

@ -1,3 +1,4 @@
<resources> <resources>
<string name="app_name">pandroid</string> <string name="app_name">pandroid</string>
<string name="load_rom">Load ROM</string>
</resources> </resources>