From be1de3e8e558a7c6959fc4f63c7edff505d5f151 Mon Sep 17 00:00:00 2001 From: aweigit <2452559168@qq.com> Date: Mon, 22 Jul 2024 21:48:45 +0800 Subject: [PATCH] update interface --- .../audio/org_recompile_mobile_Audio.cpp | 619 +++++++++--------- cpp/sdl2/rg28xx.cpp | 103 ++- cpp/sdl2/{rg35xx.cpp => rg35xxH.cpp} | 97 ++- cpp/sdl2/run.sh | 2 + 4 files changed, 505 insertions(+), 316 deletions(-) rename cpp/sdl2/{rg35xx.cpp => rg35xxH.cpp} (93%) diff --git a/cpp/native/audio/org_recompile_mobile_Audio.cpp b/cpp/native/audio/org_recompile_mobile_Audio.cpp index 37af9d2..5ea59ec 100644 --- a/cpp/native/audio/org_recompile_mobile_Audio.cpp +++ b/cpp/native/audio/org_recompile_mobile_Audio.cpp @@ -1,308 +1,311 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -#include -/* Header for class org_recompile_mobile_Audio */ - -#include -#include -#include -#include - -#define DEFAULT_BGM_FREQ 44100 -#define DEFAULT_BGM_FORMAT AUDIO_S16LSB -#define DEFAULT_BGM_CHAN 2 -#define DEFAULT_BGM_CHUNK 4096 - - -Mix_Music *gBGMMusic = NULL; -Mix_Chunk *wave = NULL; - -int initAudio=0; - -//int level=100; -char name[100]; - -bool isExit=false; - -JavaVM *m_jvm=NULL; // Java虚拟机对象 - -jobject m_jobj=NULL; // Java类对象 - -jmethodID m_onCallBack=NULL; -std::string m_onCallBackMethod = "musicFinish"; - -bool endWith(const char* src, const char* str) -{ - if (strlen(src) < strlen(str)) { - return false; - } - const char* ptr = src+(strlen(src)-strlen(str)); - for (int i = 0; i < strlen(str); i++) { - if (ptr[i] != str[i]) { - return false; - } - } - return true; -} - - - -JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) -{ - JNIEnv* env = NULL; - - if(vm->GetEnv((void**) &env, JNI_VERSION_10) != JNI_OK) { - return -1; - } - - if(env == NULL) - { - return -1; - } - - return JNI_VERSION_10; -} - -bool isAttachedCurrentThread(JNIEnv** env) -{ - if(m_jvm->GetEnv((void**)env, JNI_VERSION_10) != JNI_OK) { - m_jvm->AttachCurrentThread((void**)env, NULL); - return true; - } - - return false; -} - - -void musicFinished() { - //printf("%s music end\n",name); - - Mix_HookMusicFinished(NULL); - - JNIEnv *env = NULL; - bool isAttached = isAttachedCurrentThread(&env); - - env->CallVoidMethod(m_jobj, m_onCallBack); - - if (isAttached) { - m_jvm->DetachCurrentThread(); - } - -} - - - -void quit() -{ - if(isExit) - { - return; - } - - isExit=true; - - if(m_jvm) - { - if(m_jobj!=NULL) - { - JNIEnv *env = NULL; - bool isAttached = isAttachedCurrentThread(&env); - env->DeleteGlobalRef(m_jobj); - if (isAttached) { - m_jvm->DetachCurrentThread(); - } - } - } - - - if(gBGMMusic) - { - Mix_FreeMusic(gBGMMusic); - gBGMMusic=NULL; - } - - if (wave) { - Mix_FreeChunk(wave); - wave = NULL; - } - - - Mix_CloseAudio(); - Mix_Quit(); - SDL_Quit(); - - -} - -void changeBGM(const char* fname,int loop) -{ - - if(endWith(fname,".mid")) - { - if(Mix_PlayingMusic()) { - Mix_HaltMusic(); - } - - if(gBGMMusic!=NULL) - { - Mix_FreeMusic(gBGMMusic); - gBGMMusic=NULL; - } - - gBGMMusic = Mix_LoadMUS(fname); - if(gBGMMusic!=NULL) { - if(loop!=-1) - { - //strcpy(name,fname); - Mix_HookMusicFinished(musicFinished); - } - else - { - Mix_HookMusicFinished(NULL); - } - - if(Mix_PlayMusic(gBGMMusic, loop) < 0){ - Mix_HookMusicFinished(NULL); - return; - } - } - - - } - else if(endWith(fname,".wav")) - { - if(Mix_Playing(0)) { - Mix_HaltChannel(0); - } - - if(wave!=NULL) - { - Mix_FreeChunk(wave); - wave = NULL; - } - - wave = Mix_LoadWAV(fname); - if(wave!=NULL) { - //printf("loop %d\n",loop); - //loops:指示音频是否应重复播放。0表示不重复,-1表示无限循环,其他正数表示重复的次数。 - loop = loop>0 ? loop-1 : loop; - Mix_PlayChannel(0, wave, loop); - } - } - - -} - - - -/* - * Class: org_recompile_mobile_Audio - * Method: _start - * Signature: ([B)V - */ -JNIEXPORT void JNICALL Java_org_recompile_mobile_Audio__1start(JNIEnv *aEnv, jclass cls, jstring aBgm, jint loop){ - - - jboolean isCopy = JNI_FALSE; - //char *file = (char *)(aEnv->GetByteArrayElements(aBgm, &isCopy)); - const char *str = aEnv->GetStringUTFChars(aBgm, 0); - - changeBGM(str,loop); - - //aEnv->ReleaseByteArrayElements(aBgm, (jbyte*)file, JNI_ABORT); - aEnv->ReleaseStringUTFChars(aBgm, str); - -} - -/* - * Class: org_recompile_mobile_Audio - * Method: _stop - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_org_recompile_mobile_Audio__1stop(JNIEnv *aEnv, jclass, jint type){ - - if(type==1)//midi - { - if(Mix_PlayingMusic()) { - Mix_HaltMusic(); - } - } - else if(type==2)//wave - { - if(Mix_Playing(0)) { - Mix_HaltChannel(0); - } - } - - -} - -JNIEXPORT void JNICALL Java_org_recompile_mobile_Audio__1setVol - (JNIEnv *aEnv, jclass , jint vol) -{ - if(initAudio==0) - { - if (SDL_Init(SDL_INIT_AUDIO) == -1){ - return; - } - - //Mix_Init(MIX_INIT_MID); - - if( Mix_OpenAudio(DEFAULT_BGM_FREQ, DEFAULT_BGM_FORMAT, DEFAULT_BGM_CHAN, DEFAULT_BGM_CHUNK) < 0){ - return; - } - - initAudio=1; - } - - //printf("[native] MIX_MAX_VOLUME:%d, cur:%d\n",MIX_MAX_VOLUME,vol* MIX_MAX_VOLUME / 100); - Mix_VolumeMusic(vol* MIX_MAX_VOLUME / 100); - - //Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD")); -} - -JNIEXPORT void JNICALL Java_org_recompile_mobile_Audio__1destroy - (JNIEnv *, jclass) -{ - quit(); -} - -/* - * Class: org_recompile_mobile_PlatformPlayer - * Method: _start - * Signature: (Ljava/lang/String;I)V - */ -JNIEXPORT void JNICALL Java_org_recompile_mobile_PlatformPlayer__1start - (JNIEnv *aEnv, jobject object, jstring aBgm, jint loop) -{ - // 初始化JavaVM对象,全局唯一 - if(m_jvm==NULL) - { - aEnv->GetJavaVM(std::addressof(m_jvm)); - } - - if(m_jobj!=NULL) - { - aEnv->DeleteGlobalRef(m_jobj); - } - - - m_jobj = aEnv->NewGlobalRef(object); - if(m_onCallBack==NULL) - { - jclass m_jclz=aEnv->GetObjectClass(m_jobj); - m_onCallBack = aEnv->GetMethodID(m_jclz, m_onCallBackMethod.c_str(), - "()V"); - } - //不需要释放GetObjectClass返回的cls,因为它通常是一个全局引用或弱全局引用 - //aEnv->DeleteLocalRef(m_jclz); - - const char *str = aEnv->GetStringUTFChars(aBgm, 0); - - changeBGM(str,loop); - - aEnv->ReleaseStringUTFChars(aBgm, str); -} - - - +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +#include +/* Header for class org_recompile_mobile_Audio */ + +#include +#include +#include +#include + +#define DEFAULT_BGM_FREQ 44100 +#define DEFAULT_BGM_FORMAT AUDIO_S16LSB +#define DEFAULT_BGM_CHAN 2 +#define DEFAULT_BGM_CHUNK 4096 + + +Mix_Music *gBGMMusic = NULL; +Mix_Chunk *wave = NULL; + +int initAudio=0; + +int volumLevel=100; +char name[100]; + +bool isExit=false; + +JavaVM *m_jvm=NULL; // Java虚拟机对象 + +jobject m_jobj=NULL; // Java类对象 + +jmethodID m_onCallBack=NULL; +std::string m_onCallBackMethod = "musicFinish"; + +bool endWith(const char* src, const char* str) +{ + if (strlen(src) < strlen(str)) { + return false; + } + const char* ptr = src+(strlen(src)-strlen(str)); + for (int i = 0; i < strlen(str); i++) { + if (ptr[i] != str[i]) { + return false; + } + } + return true; +} + + + +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) +{ + JNIEnv* env = NULL; + + if(vm->GetEnv((void**) &env, JNI_VERSION_10) != JNI_OK) { + return -1; + } + + if(env == NULL) + { + return -1; + } + + return JNI_VERSION_10; +} + +bool isAttachedCurrentThread(JNIEnv** env) +{ + if(m_jvm->GetEnv((void**)env, JNI_VERSION_10) != JNI_OK) { + m_jvm->AttachCurrentThread((void**)env, NULL); + return true; + } + + return false; +} + + +void musicFinished() { + //printf("%s music end\n",name); + + Mix_HookMusicFinished(NULL); + + JNIEnv *env = NULL; + bool isAttached = isAttachedCurrentThread(&env); + + env->CallVoidMethod(m_jobj, m_onCallBack); + + if (isAttached) { + m_jvm->DetachCurrentThread(); + } + +} + + + +void quit() +{ + if(isExit) + { + return; + } + + isExit=true; + + if(m_jvm) + { + if(m_jobj!=NULL) + { + JNIEnv *env = NULL; + bool isAttached = isAttachedCurrentThread(&env); + env->DeleteGlobalRef(m_jobj); + if (isAttached) { + m_jvm->DetachCurrentThread(); + } + } + } + + + if(gBGMMusic) + { + Mix_FreeMusic(gBGMMusic); + gBGMMusic=NULL; + } + + if (wave) { + Mix_FreeChunk(wave); + wave = NULL; + } + + + Mix_CloseAudio(); + Mix_Quit(); + SDL_Quit(); + + +} + +void changeBGM(const char* fname,int loop) +{ + + if(endWith(fname,".mid")) + { + if(Mix_PlayingMusic()) { + Mix_HaltMusic(); + } + + if(gBGMMusic!=NULL) + { + Mix_FreeMusic(gBGMMusic); + gBGMMusic=NULL; + } + + gBGMMusic = Mix_LoadMUS(fname); + if(gBGMMusic!=NULL) { + if(loop!=-1) + { + //strcpy(name,fname); + Mix_HookMusicFinished(musicFinished); + } + else + { + Mix_HookMusicFinished(NULL); + } + + if(Mix_PlayMusic(gBGMMusic, loop) < 0){ + Mix_HookMusicFinished(NULL); + return; + } + } + + + } + else if(endWith(fname,".wav")) + { + if(Mix_Playing(0)) { + Mix_HaltChannel(0); + } + + if(wave!=NULL) + { + Mix_FreeChunk(wave); + wave = NULL; + } + + wave = Mix_LoadWAV(fname); + if(wave!=NULL) { + Mix_VolumeChunk(wave, volumLevel); + + //loops:指示音频是否应重复播放。0表示不重复,-1表示无限循环,其他正数表示重复的次数。 + loop = loop>0 ? loop-1 : loop; + Mix_PlayChannel(0, wave, loop); + } + } + + +} + + + +/* + * Class: org_recompile_mobile_Audio + * Method: _start + * Signature: ([B)V + */ +JNIEXPORT void JNICALL Java_org_recompile_mobile_Audio__1start(JNIEnv *aEnv, jclass cls, jstring aBgm, jint loop){ + + + jboolean isCopy = JNI_FALSE; + //char *file = (char *)(aEnv->GetByteArrayElements(aBgm, &isCopy)); + const char *str = aEnv->GetStringUTFChars(aBgm, 0); + + changeBGM(str,loop); + + //aEnv->ReleaseByteArrayElements(aBgm, (jbyte*)file, JNI_ABORT); + aEnv->ReleaseStringUTFChars(aBgm, str); + +} + +/* + * Class: org_recompile_mobile_Audio + * Method: _stop + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_recompile_mobile_Audio__1stop(JNIEnv *aEnv, jclass, jint type){ + + if(type==1)//midi + { + if(Mix_PlayingMusic()) { + Mix_HaltMusic(); + } + } + else if(type==2)//wave + { + if(Mix_Playing(0)) { + Mix_HaltChannel(0); + } + } + + +} + +JNIEXPORT void JNICALL Java_org_recompile_mobile_Audio__1setVol + (JNIEnv *aEnv, jclass , jint vol) +{ + if(initAudio==0) + { + if (SDL_Init(SDL_INIT_AUDIO) == -1){ + return; + } + + //Mix_Init(MIX_INIT_MID); + + if( Mix_OpenAudio(DEFAULT_BGM_FREQ, DEFAULT_BGM_FORMAT, DEFAULT_BGM_CHAN, DEFAULT_BGM_CHUNK) < 0){ + return; + } + + initAudio=1; + } + + volumLevel=vol; + + //printf("[native] MIX_MAX_VOLUME:%d, cur:%d\n",MIX_MAX_VOLUME,vol* MIX_MAX_VOLUME / 100); + Mix_VolumeMusic(volumLevel); + + //Mix_SetMusicCMD(SDL_getenv("MUSIC_CMD")); +} + +JNIEXPORT void JNICALL Java_org_recompile_mobile_Audio__1destroy + (JNIEnv *, jclass) +{ + quit(); +} + +/* + * Class: org_recompile_mobile_PlatformPlayer + * Method: _start + * Signature: (Ljava/lang/String;I)V + */ +JNIEXPORT void JNICALL Java_org_recompile_mobile_PlatformPlayer__1start + (JNIEnv *aEnv, jobject object, jstring aBgm, jint loop) +{ + // 初始化JavaVM对象,全局唯一 + if(m_jvm==NULL) + { + aEnv->GetJavaVM(std::addressof(m_jvm)); + } + + if(m_jobj!=NULL) + { + aEnv->DeleteGlobalRef(m_jobj); + } + + + m_jobj = aEnv->NewGlobalRef(object); + if(m_onCallBack==NULL) + { + jclass m_jclz=aEnv->GetObjectClass(m_jobj); + m_onCallBack = aEnv->GetMethodID(m_jclz, m_onCallBackMethod.c_str(), + "()V"); + } + //不需要释放GetObjectClass返回的cls,因为它通常是一个全局引用或弱全局引用 + //aEnv->DeleteLocalRef(m_jclz); + + const char *str = aEnv->GetStringUTFChars(aBgm, 0); + + changeBGM(str,loop); + + aEnv->ReleaseStringUTFChars(aBgm, str); +} + + + diff --git a/cpp/sdl2/rg28xx.cpp b/cpp/sdl2/rg28xx.cpp index 81514fe..963a797 100644 --- a/cpp/sdl2/rg28xx.cpp +++ b/cpp/sdl2/rg28xx.cpp @@ -23,6 +23,8 @@ along with FreeJ2ME. If not, see http://www.gnu.org/licenses/ */ +//rg28xx的屏幕方向是竖着的480*640 + #include #include #include @@ -115,8 +117,8 @@ unsigned char joymouseImage[374] = 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0 }; -#define KEYHOLD_TIMER_FIRST 12 -#define KEYHOLD_TIMER 3 +#define KEYHOLD_TIMER_FIRST 20 +#define KEYHOLD_TIMER 5 #define REPEAT_INTERVAL 200 // 毫秒,按键重复间隔 #define SDL_JOYSTICK_BUTTON_MAX 30 @@ -155,6 +157,22 @@ void LimitFrameRate() frameDeadline = tc + v + refreshDelay; } +uint32_t btn2keycode(uint32_t btn) +{ + switch(btn) + { + case M_UP: + return SDLK_UP; + case M_DOWN: + return SDLK_DOWN; + case M_LEFT: + return SDLK_LEFT; + case M_RIGHT: + return SDLK_RIGHT; + } + return 0; +} + void tick() { if(j_btn>=M_UP && j_btn<=M_RIGHT) @@ -171,9 +189,10 @@ void tick() m_timer = KEYHOLD_TIMER; // Trigger! SDL_Event event; - event.type = SDL_JOYBUTTONDOWN; - event.jbutton.button = j_btn; - event.jbutton.state = SDL_PRESSED; + event.type = SDL_KEYDOWN; + event.key.keysym.sym=btn2keycode(j_btn); + + event.key.state = SDL_PRESSED; SDL_PushEvent(&event); @@ -842,6 +861,80 @@ void *startCapturing(void *args) } } break; + + case SDL_KEYDOWN: + case SDL_KEYUP: + { + int key = event.key.keysym.sym; + if(key==SDLK_UP)//上 + { + if(rotate==1) + { + key=SDLK_RIGHT; + } + else if(rotate==2) + { + key=SDLK_LEFT; + } + + if(use_mouse && event.key.state == SDL_PRESSED) + { + updateMouse(key); + } + + } + else if(key==SDLK_DOWN)//下 + { + if(rotate==1) + { + key=SDLK_LEFT; + } + else if(rotate==2) + { + key=SDLK_RIGHT; + } + if(use_mouse && event.key.state == SDL_PRESSED) + { + updateMouse(key); + } + } + else if(key==SDLK_LEFT) //左 + { + if(rotate==1) + { + key=SDLK_UP; + } + else if(rotate==2) + { + key=SDLK_DOWN; + } + if(use_mouse && event.key.state == SDL_PRESSED) + { + updateMouse(key); + } + } + else if(key==SDLK_RIGHT) //右 + { + if(rotate==1) + { + key=SDLK_DOWN; + } + else if(rotate==2) + { + key=SDLK_UP; + } + if(use_mouse && event.key.state == SDL_PRESSED) + { + updateMouse(key); + } + } + + if(!use_mouse) + { + sendKey(key, event.key.state == SDL_PRESSED); + } + } + break; /* default: std::cout<<"未识别的 key type "<=M_UP && j_btn<=M_RIGHT) @@ -171,9 +187,10 @@ void tick() m_timer = KEYHOLD_TIMER; // Trigger! SDL_Event event; - event.type = SDL_JOYBUTTONDOWN; - event.jbutton.button = j_btn; - event.jbutton.state = SDL_PRESSED; + event.type = SDL_KEYDOWN; + event.key.keysym.sym=btn2keycode(j_btn); + + event.key.state = SDL_PRESSED; SDL_PushEvent(&event); @@ -843,6 +860,80 @@ void *startCapturing(void *args) } } break; + + case SDL_KEYDOWN: + case SDL_KEYUP: + { + int key = event.key.keysym.sym; + if(key==SDLK_UP)//上 + { + if(rotate==1) + { + key=SDLK_RIGHT; + } + else if(rotate==2) + { + key=SDLK_LEFT; + } + + if(use_mouse && event.key.state == SDL_PRESSED) + { + updateMouse(key); + } + + } + else if(key==SDLK_DOWN)//下 + { + if(rotate==1) + { + key=SDLK_LEFT; + } + else if(rotate==2) + { + key=SDLK_RIGHT; + } + if(use_mouse && event.key.state == SDL_PRESSED) + { + updateMouse(key); + } + } + else if(key==SDLK_LEFT) //左 + { + if(rotate==1) + { + key=SDLK_UP; + } + else if(rotate==2) + { + key=SDLK_DOWN; + } + if(use_mouse && event.key.state == SDL_PRESSED) + { + updateMouse(key); + } + } + else if(key==SDLK_RIGHT) //右 + { + if(rotate==1) + { + key=SDLK_DOWN; + } + else if(rotate==2) + { + key=SDLK_UP; + } + if(use_mouse && event.key.state == SDL_PRESSED) + { + updateMouse(key); + } + } + + if(!use_mouse) + { + sendKey(key, event.key.state == SDL_PRESSED); + } + } + break; /* default: std::cout<<"未识别的 key type "<