Skip to content

Commit

Permalink
Android: Properly handle rotations
Browse files Browse the repository at this point in the history
ConfigurationChanged is not sent on all Android versions because of various bugs in the operating system.
Instead SurfaceChanged is used which is consistent with what SDL does.

Also Window events are not filtered anymore when in the background.
This is required on Android, otherwise rotations are lost.
  • Loading branch information
Ghabry committed Feb 23, 2025
1 parent 4493b1a commit bac4a1f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import android.app.AlertDialog;
import android.content.ClipDescription;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Configuration;
Expand Down Expand Up @@ -62,6 +63,7 @@
import org.easyrpg.player.game_browser.GameBrowserActivity;
import org.easyrpg.player.settings.SettingsManager;
import org.libsdl.app.SDLActivity;
import org.libsdl.app.SDLSurface;

import java.io.File;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -94,6 +96,11 @@ protected String[] getLibraries() {
};
}

@Override
protected SDLSurface createSDLSurface(Context context) {
return new EasyRpgSurface(context, this);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -438,7 +445,6 @@ public void showInputLayout() {
public void updateScreenPosition() {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
int topMargin, leftMargin;

// Determine the multiplier
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.easyrpg.player.player;

import android.content.Context;
import android.view.SurfaceHolder;

import org.libsdl.app.SDLSurface;

public class EasyRpgSurface extends SDLSurface {
private EasyRpgPlayerActivity activity = null;

public EasyRpgSurface(Context context, EasyRpgPlayerActivity activity) {
super(context);

this.activity = activity;
}

@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
// configurationChanged is not always sent because of Android bugs in various versions
// SDL uses the surfaceChanged event to detect rotations instead
super.surfaceChanged(holder, format, width, height);

activity.updateScreenPosition();
}
}
5 changes: 5 additions & 0 deletions src/platform/sdl/sdl2_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,11 @@ void Sdl2Ui::ProcessWindowEvent(SDL_Event &evnt) {
SDL_Event wait_event;

while (SDL_WaitEvent(&wait_event)) {
if (wait_event.type == SDL_WINDOWEVENT && wait_event.window.event != SDL_WINDOWEVENT_FOCUS_LOST) {
// Process size change etc. events
ProcessWindowEvent(wait_event);
}

if (FilterUntilFocus(&wait_event)) {
break;
}
Expand Down
7 changes: 6 additions & 1 deletion src/platform/sdl/sdl3_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Sdl3Ui::~Sdl3Ui() {

#ifdef SUPPORT_AUDIO
audio_.reset();
#endif
#endif

SDL_Quit();
}
Expand Down Expand Up @@ -732,6 +732,11 @@ void Sdl3Ui::ProcessWindowEvent(SDL_Event &evnt) {
SDL_Event wait_event;

while (SDL_WaitEvent(&wait_event)) {
if (wait_event.type >= SDL_EVENT_WINDOW_FIRST && wait_event.type <= SDL_EVENT_WINDOW_LAST && wait_event.type != SDL_EVENT_WINDOW_FOCUS_LOST) {
// Process size change etc. events
ProcessWindowEvent(wait_event);
}

if (FilterUntilFocus(&wait_event)) {
break;
}
Expand Down

0 comments on commit bac4a1f

Please sign in to comment.