Skip to content

Commit

Permalink
Add renderer/surface view files
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Nov 20, 2023
1 parent 469ae28 commit a87397a
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ else()
endif()

if(ANDROID)
target_link_libraries(Alber PRIVATE log)
target_link_libraries(Alber PRIVATE EGL log)
endif()

if(ENABLE_LTO OR ENABLE_USER_BUILD)
Expand Down
31 changes: 28 additions & 3 deletions src/jni_driver.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
#include <jni.h>
#include <stdexcept>
#include <android/log.h>
#include <EGL/egl.h>
#include "renderer_gl/renderer_gl.hpp"
#include "emulator.hpp"

std::unique_ptr<Emulator> emulator;
std::unique_ptr<Emulator> emulator = nullptr;
RendererGL* renderer = nullptr;

extern "C" JNIEXPORT void JNICALL Java_com_panda3ds_pandroid_AlberDriver_Initialize(JNIEnv* env, jobject obj) {
__android_log_print(ANDROID_LOG_INFO, "Panda3DS", "Initializing Alber Driver");
emulator = std::make_unique<Emulator>();
__android_log_print(ANDROID_LOG_INFO, "Panda3DS", "Done");
if (emulator->getRendererType() != RendererType::OpenGL) {
throw std::runtime_error("Renderer is not OpenGL");
}
renderer = static_cast<RendererGL*>(emulator->getRenderer());
__android_log_print(ANDROID_LOG_INFO, "AlberDriver", "OpenGL ES Before %d.%d", GLVersion.major, GLVersion.minor);
if (!gladLoadGLES2Loader(reinterpret_cast<GLADloadproc>(eglGetProcAddress))) {
throw std::runtime_error("OpenGL ES init failed");
}
__android_log_print(ANDROID_LOG_INFO, "AlberDriver", "OpenGL ES %d.%d", GLVersion.major, GLVersion.minor);
emulator->initGraphicsContext(nullptr);
// girlboss it for now :3
emulator->loadROM("/data/data/com.panda3ds.pandroid/files/rom.3ds");
}

extern "C" JNIEXPORT void JNICALL Java_com_panda3ds_pandroid_AlberDriver_RunFrame(JNIEnv* env, jobject obj, jint fbo) {
renderer->setFBO(fbo);
renderer->resetStateManager();
emulator->runFrame();
}

extern "C" JNIEXPORT void JNICALL Java_com_panda3ds_pandroid_AlberDriver_Finalize(JNIEnv* env, jobject obj) {
emulator = nullptr;
renderer = nullptr;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

public class AlberDriver {

AlberDriver() {
super();
}

public static native void Initialize();
public static native void RunFrame(int fbo);
public static native void Finalize();

static {
System.loadLibrary("Alber");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.view.WindowInsets;
import android.view.View;

public class MainActivity extends AppCompatActivity {

PandaGlSurfaceView glView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AlberDriver.Initialize();
glView = new PandaGlSurfaceView(this);
setContentView(glView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.panda3ds.pandroid;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.GLES32.*;

import android.content.res.Resources;
import android.opengl.GLSurfaceView;
import android.util.DisplayMetrics;
import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class PandaGlRenderer implements GLSurfaceView.Renderer {
int screenWidth, screenHeight;
int screenTexture;
public int screenFbo;

PandaGlRenderer() {
super();
}

@Override
protected void finalize() throws Throwable {
if (screenTexture != 0) {
glDeleteTextures(1, new int[]{screenTexture}, 0);
}
if (screenFbo != 0) {
glDeleteFramebuffers(1, new int[]{screenFbo}, 0);
}
super.finalize();
}

public void onSurfaceCreated(GL10 unused, EGLConfig config) {
Log.i("pandroid", glGetString(GL_EXTENSIONS));
Log.w("pandroid", glGetString(GL_VERSION));
screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

int[] generateBuffer = new int[1];
glGenTextures(1, generateBuffer, 0);
screenTexture = generateBuffer[0];
glBindTexture(GL_TEXTURE_2D, screenTexture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, screenWidth, screenHeight);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);

glGenFramebuffers(1, generateBuffer, 0);
screenFbo = generateBuffer[0];
glBindFramebuffer(GL_FRAMEBUFFER, screenFbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, screenTexture, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

AlberDriver.Initialize();
}

public void onDrawFrame(GL10 unused) {
AlberDriver.RunFrame(screenFbo);
// is this needed? prob not
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, screenFbo);
glBlitFramebuffer(0, 0, 400, 480, 0, 0, screenWidth, screenHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}

public void onSurfaceChanged(GL10 unused, int width, int height) {
glViewport(0, 0, width, height);
screenWidth = width;
screenHeight = height;
glDisable(GL_SCISSOR_TEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.panda3ds.pandroid;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.DisplayMetrics;

import com.panda3ds.pandroid.PandaGlRenderer;

public class PandaGlSurfaceView extends GLSurfaceView {
final PandaGlRenderer renderer;

public PandaGlSurfaceView(Context context) {
super(context);
setEGLContextClientVersion(3);
renderer = new PandaGlRenderer();
setRenderer(renderer);
}
}

0 comments on commit a87397a

Please sign in to comment.