Yet Another ASCII MSX1 Emulator その2

涼しい3連休のおかげで、今までの成果物をまとめてAndroidに移植しました。今回は、ソースコードのみのリリースですので、Android版のビルド方法です。

http://www.geocities.jp/parallel_computer_inc/android.html

f:id:tanam:20130923231350p:image:w360

例によってSDLのサイトから、android-project.zipをダウンロードします。

http://www.libsdl.org/tmp/android-project.zip

SDLサウンドのためだけに使うので、GLES2を無効(#define SDL_VIDEO_RENDER_OGL_ES2 0)とします。あと、1箇所コンパイルエラーになるので追記(#define SDL_ATOMIC_DISABLED 1)します。

SDL_config_android.h


(省略)

/* Enable OpenGL ES */
#define SDL_VIDEO_OPENGL_ES 1
#define SDL_VIDEO_RENDER_OGL_ES 1
#define SDL_VIDEO_RENDER_OGL_ES2    0

(省略)

SDL_spinlock.c


(省略)

#include "SDL_stdinc.h"

#include "SDL_atomic.h"
#include "SDL_mutex.h"
#include "SDL_timer.h"
#define SDL_ATOMIC_DISABLED 1

(省略)

以下のソースが肝なのですが、C++からJNIを使ってJavaサウンドを鳴らします。

SDL_android.cpp

/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2011 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_stdinc.h"

#include "SDL_android.h"

extern "C" {
#include "../../events/SDL_events_c.h"
#include "../../video/android/SDL_androidkeyboard.h"
#include "../../video/android/SDL_androidtouch.h"
#include "../../video/android/SDL_androidvideo.h"

/* Impelemented in audio/android/SDL_androidaudio.c */
extern void Android_RunAudioThread();
} // C

/*******************************************************************************
 This file links the Java side of Android with libsdl
*******************************************************************************/
#include <jni.h>
#include <android/log.h>


/*******************************************************************************
                               Globals
*******************************************************************************/
///static JNIEnv* mEnv = NULL;
static JNIEnv* mAudioEnv = NULL;
static JavaVM* s_javaVM = NULL;

// Main activity
static jclass mActivityClass;

// method signatures
static jmethodID midCreateGLContext;
static jmethodID midFlipBuffers;
static jmethodID midAudioInit;
static jmethodID midAudioWriteShortBuffer;
static jmethodID midAudioWriteByteBuffer;
static jmethodID midAudioQuit;

// Accelerometer data storage
static float fLastAccelerometer[3];


/*******************************************************************************
                 Functions called by JNI
*******************************************************************************/

// Library init
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    __android_log_print(ANDROID_LOG_INFO, "Qt", "JNI_OnLoad()");
    JNIEnv* env;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4) != JNI_OK) {
        __android_log_print(ANDROID_LOG_INFO, "SDL", "Can't get the enviroument");
        return -1;
    }

    s_javaVM = vm;
    // search for our class
    jclass clazz=env->FindClass("org/kde/necessitas/origo/QtActivity");
    if (!clazz)
    {
        __android_log_print(ANDROID_LOG_INFO, "SDL", "Can't find QtActivity class");
        return -1;
    }
    mActivityClass = (jclass)env->NewGlobalRef(clazz);

///    return JNI_VERSION_1_4;
/// }
///
// Called before SDL_main() to initialize JNI bindings
/// extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
///{
///    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
///
///    mEnv = env;
///    mActivityClass = cls;
///
///    midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
///                                "createGLContext","(II)Z");
///    midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
///                                "flipBuffers","()V");
///    midAudioInit = mEnv->GetStaticMethodID(mActivityClass,
///                                "audioInit", "(IZZI)Ljava/lang/Object;");
    midAudioInit = env->GetStaticMethodID(mActivityClass, "audioInit", "(IZZI)Ljava/lang/Object;");
///    midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
///                                "audioWriteShortBuffer", "([S)V");
    midAudioWriteShortBuffer = env->GetStaticMethodID(mActivityClass, "audioWriteShortBuffer", "([S)V");
///    midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
///                                "audioWriteByteBuffer", "([B)V");
    midAudioWriteByteBuffer = env->GetStaticMethodID(mActivityClass, "audioWriteByteBuffer", "([B)V");
///    midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
///                                "audioQuit", "()V");
     midAudioQuit = env->GetStaticMethodID(mActivityClass, "audioQuit", "()V");
///    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
///       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
///        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
///    }
     if(!midAudioInit || !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
         __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
     }
     __android_log_print(ANDROID_LOG_INFO, "Qt", "JNI_VERSION_1_4");
     return JNI_VERSION_1_4;
}

// Resize
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
                                    JNIEnv* env, jclass jcls,
                                    jint width, jint height, jint format)
{
    Android_SetScreenResolution(width, height, format);
}

// Keydown
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
                                    JNIEnv* env, jclass jcls, jint keycode)
{
    Android_OnKeyDown(keycode);
}

// Keyup
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
                                    JNIEnv* env, jclass jcls, jint keycode)
{
    Android_OnKeyUp(keycode);
}

// Touch
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
                                    JNIEnv* env, jclass jcls,
                                    jint action, jfloat x, jfloat y, jfloat p)
{
    Android_OnTouch(action, x, y, p);
}

// Accelerometer
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
                                    JNIEnv* env, jclass jcls,
                                    jfloat x, jfloat y, jfloat z)
{
    fLastAccelerometer[0] = x;
    fLastAccelerometer[1] = y;
    fLastAccelerometer[2] = z;   
}

// Quit
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
                                    JNIEnv* env, jclass cls)
{    
    // Inject a SDL_QUIT event
    SDL_SendQuit();
}

extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
                                    JNIEnv* env, jclass cls)
{
    /// This is the audio thread, with a different environment
    mAudioEnv = env;

    Android_RunAudioThread();
}

extern "C" void Java_org_kde_necessitas_origo_QtActivity_nativeRunAudioThread(
                                    JNIEnv* env, jclass cls)
{
    __android_log_print(ANDROID_LOG_VERBOSE, "Qt", "Android_RunAudioThread()");
    /* This is the audio thread, with a different environment */
    mAudioEnv = env;

    Android_RunAudioThread();
}


/*******************************************************************************
             Functions called by SDL into Java
*******************************************************************************/
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
{
///    if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
///        return SDL_TRUE;
///    } else {
        return SDL_FALSE;
///    }
}

extern "C" void Android_JNI_SwapWindow()
{
///    mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers);
}

extern "C" void Android_JNI_SetActivityTitle(const char *title)
{
///    jmethodID mid;
///
///    mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
///    if (mid) {
///        mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
///    }
}

extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
{
    int i;
    for (i = 0; i < 3; ++i) {
        values[i] = fLastAccelerometer[i];
    }
}

//
// Audio support
//
static jboolean audioBuffer16Bit = JNI_FALSE;
static jboolean audioBufferStereo = JNI_FALSE;
static jobject audioBuffer = NULL;
static void* audioBufferPinned = NULL;

extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
{
    int audioBufferFrames;

    __android_log_print(ANDROID_LOG_VERBOSE, "Qt", "SDL audio: opening device");
    audioBuffer16Bit = is16Bit;
    audioBufferStereo = channelCount > 1;

///    audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
    JNIEnv* env;
    if (s_javaVM->AttachCurrentThread(&env, NULL) < 0)
    {
        __android_log_print(ANDROID_LOG_VERBOSE, "Qt", "AttachCurrentThread failed");
        return 0;
    }
    audioBuffer = env->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);

    if (audioBuffer == NULL) {
        __android_log_print(ANDROID_LOG_WARN, "Qt", "SDL audio: didn't get back a good audio buffer!");
        return 0;
    }
///    audioBuffer = mEnv->NewGlobalRef(audioBuffer);
    audioBuffer = env->NewGlobalRef(audioBuffer);

    jboolean isCopy = JNI_FALSE;
    if (audioBuffer16Bit) {
///        audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
///        audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
        audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
        audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
    } else {
///        audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
///        audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
        audioBufferPinned = env->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
        audioBufferFrames = env->GetArrayLength((jbyteArray)audioBuffer);
    }
    if (audioBufferStereo) {
        audioBufferFrames /= 2;
    }

    s_javaVM->DetachCurrentThread();
    return audioBufferFrames;
}

extern "C" void * Android_JNI_GetAudioBuffer()
{
    __android_log_print(ANDROID_LOG_VERBOSE, "Qt", " Android_JNI_GetAudioBuffer()");
    return audioBufferPinned;
}

extern "C" void Android_JNI_WriteAudioBuffer()
{
    if (audioBuffer16Bit) {
        mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
    } else {
__android_log_print(ANDROID_LOG_VERBOSE, "Qt", "Android_JNI_WriteAudioBuffer()");
        mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
    }

    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
}

extern "C" void Android_JNI_CloseAudioDevice()
{
    __android_log_print(ANDROID_LOG_VERBOSE, "Qt", "Android_JNI_CloseAudioDevice()");
    JNIEnv* env;
    if (s_javaVM->AttachCurrentThread(&env, NULL) < 0)
    {
        __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "AttachCurrentThread failed");
        return;
    }
///    mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit);
    env->CallStaticVoidMethod(mActivityClass, midAudioQuit);

    if (audioBuffer) {
///        mEnv->DeleteGlobalRef(audioBuffer);
        env->DeleteGlobalRef(audioBuffer);
        audioBuffer = NULL;
        audioBufferPinned = NULL;
    }
    s_javaVM->DetachCurrentThread();
}

まずは、以下からsource.zipをダウンロードして、以下のフォルダ構成になります。

http://homepage3.nifty.com/takeda-toshiya/common/index.html

+source ←和製MESSを解凍して、以下のファイルのみ差し替える
 GAMEGEAR.pro
 MSX1.pro
 PC6001.pro
+include
 windows.h
 tchar.h
+src 
 config.cpp
 config.h
 emu.cpp ← Win32版をバックアップしてemu.cpp_qtをリネーム
 emu.h ← Win32版をバックアップしてemu.h_qtをリネーム
 fileio.cpp
 qt_input.cpp
 qt_screen.cpp
 qt_sound.cpp
 qtmain.cpp
 winmain.cpp
 +SDL ← android-project.zipからコピー
 +res 
  gamegear.rc
  gamegear.ico ← Win32版で使います
  msx1.rc
  msx1.ico ← Win32版で使います
  pc6001.rc
  pc6001.ico ← Win32版で使います
  resource.h
 +vm
  315-5124.cpp
  315-5124.h
  datarec.cpp
  datarec.h
  i8255.cpp
  i8255.h
  sn76489an.cpp
  sn76489an.h
  vm.h
  +fmgen
   file.cpp
  +gamegear ← yaGG本体
  +msx1 ← yaMSX1本体
  +pc6001 ← yaP6本体

準備が出来たら、プロジェクトファイル(GAMEGEAR.pro など)をQt Creatorで開きます。「Necessitas Qt 4.8.2 for Android armv5」でプロジェクトを構成します。以下のサイトを参考にビルド および デプロイしてみてください。

http://vivi.dyndns.org/tech/Qt/Necessitas.html

不明な点はTwitterなどでお問い合わせください。