Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ elseif(APPLE)
platform/ios/RenderViewImpl-ios.h
platform/ios/StdC-ios.h
platform/ios/InputView-ios.h
platform/ios/AxmolAppController.h
platform/ios/AxmolViewController.h
)
set(_AX_PLATFORM_SPECIFIC_SRC
${_AX_PLATFORM_SPECIFIC_SRC}
Expand All @@ -97,6 +99,8 @@ elseif(APPLE)
platform/ios/RenderViewImpl-ios.mm
platform/ios/Image-ios.mm
platform/ios/InputView-ios.mm
platform/ios/AxmolAppController.mm
platform/ios/AxmolViewController.mm
)

if(AX_USE_GL)
Expand Down Expand Up @@ -162,6 +166,11 @@ elseif(EMSCRIPTEN)
endif()
endif()

# Add desktop-specific sources for platforms where GLFW is available
if((WIN32 AND NOT WINRT) OR LINUX OR MACOSX OR WASM)
list(APPEND _AX_PLATFORM_SPECIFIC_SRC "platform/desktop/Device-desktop.cpp")
endif()

set(_AX_PLATFORM_HEADER
${_AX_PLATFORM_SPECIFIC_HEADER}
platform/Application.h
Expand Down
103 changes: 102 additions & 1 deletion core/platform/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,112 @@ class AX_DLL Device
int& height,
bool& hasPremultipliedAlpha);

#pragma region Orientation control support
enum class Orientation
{
Unknown,
Portrait, // Portrait (upright)
ReversePortrait, // Portrait upside down
SensorPortrait, // Portrait + reverse portrait (auto-rotate with sensor)

Landscape, // Landscape (left)
ReverseLandscape, // Landscape (right)
SensorLandscape, // Landscape + reverse landscape (auto-rotate with sensor)

Sensor, // All orientations except upside down (auto-rotate with sensor)
FullSensor // All orientations including upside down (auto-rotate with sensor)
};

enum class OrientationMask
{
Portrait = 1 << 0,
ReversePortrait = 1 << 1, // Portrait upside down
Landscape = 1 << 2, // Landscape (left)
ReverseLandscape = 1 << 3, // Landscape (right)
All = Portrait | ReversePortrait | Landscape | ReverseLandscape
};

/**
* @brief Sets the preferred screen orientation for the application.
*
* This method attempts to switch the screen orientation to the specified value.
* If the orientation is supported by the platform (as declared in Info.plist or AndroidManifest),
* the system will rotate and lock to that orientation.
* Use Orientation::Auto to unlock and allow automatic rotation.
*
* @param orientation The desired screen orientation. Use Orientation::Auto to reset.
* @since axmol-2.9.0
*/
static void setPreferredOrientation(Orientation orientation);

/**
* @brief Gets the currently preferred (locked) screen orientation.
*
* Returns the orientation that was last set via setPreferredOrientation().
* If Orientation::Auto is returned, the application is currently allowing automatic rotation.
*
* @return The current preferred orientation.
* @since axmol-2.9.0
*/
static Orientation getPreferredOrientation();

/**
* @brief Gets the set of orientations supported by the platform.
*
* This reflects the maximum orientation capabilities declared in Info.plist (iOS)
* or AndroidManifest.xml (Android). The application can only rotate within this set.
*
* @return A bitmask representing supported orientations.
* @since axmol-2.9.0
*/
static OrientationMask getSupportedOrientations();

/**
* @brief Gets the current screen orientation as rendered by the system.
*
* This reflects the actual orientation currently applied to the screen,
* which may differ from the preferred orientation if Orientation::Sensor is set.
*
* @return The current screen orientation.
* @since axmol-2.9.0
*/
static Orientation getCurrentOrientation();

/**
* @brief Returns the device's physical orientation (hardware posture).
*
* Unlike getCurrentOrientation(), which reflects the UI's current interface
* orientation, this method reports the device's actual physical posture as
* detected by sensors (e.g., accelerometer). It may differ from the UI
* orientation when rotation is locked or when the app restricts supported
* orientations.
*
* Platform notes:
* - iOS: Maps from UIDeviceOrientation. FaceUp/FaceDown are treated as Unknown.
* You should ensure orientation notifications are active internally
* (beginGeneratingDeviceOrientationNotifications) if needed.
* - Android: Maps from display rotation and/or sensor readings to the closest
* Orientation value. Sensor-based "flat" states map to Unknown.
*
* Typical usage:
* - Use getCurrentOrientation() for layout, rendering, and UI decisions.
* - Use getPhysicalOrientation() for gameplay/input mechanics that depend on
* how the user is holding the device, independent of UI rotation.
*
* @return Orientation The device's physical orientation.
* @since axmol-2.9.0
*/
static Orientation getPhysicalOrientation();

#pragma endregion Orientation control support

private:
AX_DISALLOW_IMPLICIT_CONSTRUCTORS(Device);
};

AX_ENABLE_BITMASK_OPS(Device::OrientationMask)

// end group
/// @}

}
} // namespace ax
31 changes: 31 additions & 0 deletions core/platform/android/Device-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,37 @@ void Device::selectionChanged()
JniHelper::callStaticVoidMethod(deviceHelperClassName, "selectionChanged");
}

static Device::Orientation s_preferredOrientation = Device::Orientation::Sensor;

void Device::setPreferredOrientation(Device::Orientation orientation)
{
s_preferredOrientation = orientation;
JniHelper::callStaticVoidMethod(deviceHelperClassName, "setPreferredOrientation", static_cast<jint>(orientation));
}

Device::Orientation Device::getPreferredOrientation()
{
return s_preferredOrientation;
}

Device::OrientationMask Device::getSupportedOrientations()
{
jint mask = JniHelper::callStaticIntMethod(deviceHelperClassName, "getSupportedOrientations");
return static_cast<Device::OrientationMask>(mask);
}

Device::Orientation Device::getCurrentOrientation()
{
jint orientation = JniHelper::callStaticIntMethod(deviceHelperClassName, "getCurrentOrientation");
return static_cast<Device::Orientation>(orientation);
}

Device::Orientation Device::getPhysicalOrientation()
{
jint orientation = JniHelper::callStaticIntMethod(deviceHelperClassName, "getPhysicalOrientation");
return static_cast<Device::Orientation>(orientation);
}

}

// this method is called by BitmapHelper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,8 @@ public void enableAccel() {
}

public void setInterval(float interval) {
// Honeycomb version is 11
if(android.os.Build.VERSION.SDK_INT < 11) {
this.mSensorManager.registerListener(this, this.mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
} else {
//convert seconds to microseconds
this.mSensorManager.registerListener(this, this.mAccelerometer, (int)(interval*1000000));
}
//convert seconds to microseconds
this.mSensorManager.registerListener(this, this.mAccelerometer, (int)(interval*1000000));
}

public void disable() {
Expand Down
Loading
Loading