I have a CHIP8 emulator written in C ++, and I am making a port for android, the desktop version uses SDL and the pixels are drawn with SDL_Texture
, however I do not have this feature within my reach, looking this source code I noticed that it uses opengl, but some of these functions and features not available in OpenGL ES 2.0
Here's my code:
#include <GLES2/gl2.h>
#include <android/log.h>
#include <jni.h>
#include "CHIP8.hpp"
CHIP8 chip8;
extern "C"{
//OpenGL
JNIEXPORT void JNICALL Java_com_samuelives_chip8_emulator_EmulatorRenderer_initializeGL(JNIEnv *env, jobject instance);
JNIEXPORT void JNICALL Java_com_samuelives_chip8_emulator_EmulatorRenderer_resizeGL(JNIEnv *env, jobject instance, jint w, jint h);
JNIEXPORT void JNICALL Java_com_samuelives_chip8_emulator_EmulatorRenderer_drawGL(JNIEnv *env, jobject instance);
//Emulator
JNIEXPORT jboolean JNICALL Java_com_samuelives_chip8_emulator_Emulator_loadGame(JNIEnv *env, jobject instance, jstring gamePath);
JNIEXPORT void JNICALL Java_com_samuelives_chip8_emulator_Emulator_executeEmulator(JNIEnv *env, jobject instance);
JNIEXPORT jboolean JNICALL Java_com_samuelives_chip8_emulator_Emulator_hasDraw(JNIEnv *env, jobject instance);
}
/* OpenGL */
JNIEXPORT void JNICALL Java_com_samuelives_chip8_emulator_EmulatorRenderer_initializeGL(JNIEnv *env, jobject instance)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
JNIEXPORT void JNICALL Java_com_samuelives_chip8_emulator_EmulatorRenderer_resizeGL(JNIEnv *env, jobject instance, jint w, jint h)
{
glViewport(0, 0, w, h);
}
JNIEXPORT void JNICALL Java_com_samuelives_chip8_emulator_EmulatorRenderer_drawGL(JNIEnv *env, jobject instance)
{
chip8.hasDraw = false;
}
/* Emulator */
JNIEXPORT jboolean JNICALL Java_com_samuelives_chip8_emulator_Emulator_loadGame(JNIEnv *env, jobject instance, jstring gamePath)
{
jboolean ok = JNI_TRUE;
const char *path = env->GetStringUTFChars(gamePath, 0);
if(!chip8.load(path))
ok = JNI_FALSE;
env->ReleaseStringUTFChars(gamePath, path);
return ok;
}
JNIEXPORT void JNICALL Java_com_samuelives_chip8_emulator_Emulator_executeEmulator(JNIEnv *env, jobject instance)
{
chip8.exec();
}
JNIEXPORT jboolean JNICALL Java_com_samuelives_chip8_emulator_Emulator_hasDraw(JNIEnv *env, jobject instance)
{
if(chip8.hasDraw)
return JNI_TRUE;
return JNI_FALSE;
}
Renderer
package com.samuelives.chip8.emulator;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class EmulatorRenderer implements GLSurfaceView.Renderer{
static{
System.loadLibrary("CHIP8");
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
initializeGL();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
resizeGL(width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
drawGL();
}
private native void initializeGL();
private native void resizeGL(int w, int h);
private native void drawGL();
}
Surface
package com.samuelives.chip8.emulator;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
public class EmulatorSurface extends GLSurfaceView {
private EmulatorRenderer mRenderer;
public EmulatorSurface(Context context, AttributeSet attrs) {
super(context, attrs);
this.mRenderer = new EmulatorRenderer();
setEGLContextClientVersion(2);
setRenderer(mRenderer);
setRenderMode(RENDERMODE_WHEN_DIRTY);
}
}
chip8 has an array of bytes ( std::array<std::uint8_t, 2048> gfx;
) that contains the already extracted pixels, so I would like to draw them on my surfaces.