diff --git a/src/Engine/Application.cpp b/src/Engine/Application.cpp index 9811deba5f..df23397103 100644 --- a/src/Engine/Application.cpp +++ b/src/Engine/Application.cpp @@ -798,8 +798,9 @@ namespace Mengine m_currentWindowResolution = windowResolution; + float renderViewportAspect; Viewport renderViewport; - this->calcRenderViewport_( &renderViewport ); + this->calcRenderViewport_( &renderViewportAspect, &renderViewport ); LOGGER_INFO( "system", "set render viewport [%f %f - %f %f]" , renderViewport.begin.x @@ -2050,7 +2051,7 @@ namespace Mengine return m_currentWindowResolution; } ////////////////////////////////////////////////////////////////////////// - void Application::calcRenderViewport_( Viewport * const _viewport ) + void Application::calcRenderViewport_( float * const _aspect, Viewport * const _viewport ) const { float windowWidth = m_currentWindowResolution.getWidthF(); float windowHeight = m_currentWindowResolution.getHeightF(); @@ -2060,6 +2061,8 @@ namespace Mengine if( fixedDisplayResolution == false ) { + *_aspect = windowAspect; + _viewport->begin.x = 0.f; _viewport->begin.y = 0.f; _viewport->end.x = windowWidth; @@ -2076,21 +2079,20 @@ namespace Mengine bestAspect = contentAspect; } - float oneDivWidth = 1.f / windowWidth; - float oneDivHeight = 1.f / windowHeight; - float dw = 1.f; - float dh = windowWidth / bestAspect * oneDivHeight; + float dh = windowAspect / bestAspect; if( dh > 1.f ) { - dw = windowHeight * bestAspect * oneDivWidth; + dw = bestAspect / windowAspect; dh = 1.f; } float areaWidth = MENGINE_CEILF( dw * windowWidth ); float areaHeight = MENGINE_CEILF( dh * windowHeight ); + *_aspect = bestAspect; + _viewport->begin.x = MENGINE_CEILF( (windowWidth - areaWidth) * 0.5f ); _viewport->begin.y = MENGINE_CEILF( (windowHeight - areaHeight) * 0.5f ); _viewport->end.x = _viewport->begin.x + areaWidth; @@ -2193,8 +2195,9 @@ namespace Mengine return; } + float renderViewportAspect; Viewport renderViewport; - this->calcRenderViewport_( &renderViewport ); + this->calcRenderViewport_( &renderViewportAspect, &renderViewport ); LOGGER_INFO( "system", "set render viewport [%f %f - %f %f]" , renderViewport.begin.x @@ -2445,15 +2448,12 @@ namespace Mengine return; } - float oneDivWidth = 1.f / contentWidth; - float oneDivHeight = 1.f / contentHeight; - float dw = 1.f; - float dh = contentWidth / windowAspect * oneDivHeight; + float dh = contentAspect / windowAspect; if( dh < 1.f ) { - dw = contentHeight * windowAspect * oneDivWidth; + dw = windowAspect / contentAspect; dh = 1.f; } @@ -2468,6 +2468,31 @@ namespace Mengine _viewport->end.y = _viewport->begin.y + areaHeight; } ////////////////////////////////////////////////////////////////////////// + EContenEdgeMode Application::getAspectRatioContenEdgeMode() const + { + if( m_fixedContentResolution == false ) + { + return ECEM_NONE; + } + + if( m_fixedViewportResolution == true ) + { + return ECEM_NONE; + } + + float contentAspect = m_contentResolution.getAspectRatio(); + float windowAspect = m_currentWindowResolution.getAspectRatio(); + + float dh = contentAspect / windowAspect; + + if( dh < 1.f ) + { + return ECEM_HORIZONTAL_CONTENT_EDGE; + } + + return ECEM_VERTICAL_CONTENT_EDGE; + } + ////////////////////////////////////////////////////////////////////////// bool Application::getAllowFullscreenSwitchShortcut() const { return false; diff --git a/src/Engine/Application.h b/src/Engine/Application.h index a586f302c0..6af67186a3 100644 --- a/src/Engine/Application.h +++ b/src/Engine/Application.h @@ -87,8 +87,10 @@ namespace Mengine void getGameViewport( float * const _aspect, Viewport * const _viewport ) const override; + EContenEdgeMode getAspectRatioContenEdgeMode() const override; + protected: - void calcRenderViewport_( Viewport * const _viewport ); + void calcRenderViewport_( float * const _aspect, Viewport * const _viewport ) const; public: bool render() override; diff --git a/src/Frameworks/PythonFramework/EngineScriptEmbedding.cpp b/src/Frameworks/PythonFramework/EngineScriptEmbedding.cpp index 4407cba435..e0db8de446 100644 --- a/src/Frameworks/PythonFramework/EngineScriptEmbedding.cpp +++ b/src/Frameworks/PythonFramework/EngineScriptEmbedding.cpp @@ -4021,6 +4021,14 @@ namespace Mengine return viewport; } ////////////////////////////////////////////////////////////////////////// + EContenEdgeMode s_getAspectRatioContenEdgeMode() + { + EContenEdgeMode mode = APPLICATION_SERVICE() + ->getAspectRatioContenEdgeMode(); + + return mode; + } + ////////////////////////////////////////////////////////////////////////// bool s_hasGameParam( const ConstString & _paramName ) { const ConfigInterfacePtr & config = CONFIG_SERVICE() @@ -4445,6 +4453,14 @@ namespace Mengine pybind::def_functor( _kernel, "getGameAspect", nodeScriptMethod, &EngineScriptMethod::s_getGameAspect ); pybind::def_functor( _kernel, "getGameViewport", nodeScriptMethod, &EngineScriptMethod::s_getGameViewport ); + pybind::enum_( _kernel, "EContenEdgeMode" ) + .def( "ECEM_NONE", ECEM_NONE ) + .def( "ECEM_HORIZONTAL_CONTENT_EDGE", ECEM_HORIZONTAL_CONTENT_EDGE ) + .def( "ECEM_VERTICAL_CONTENT_EDGE", ECEM_VERTICAL_CONTENT_EDGE ) + ; + + pybind::def_functor( _kernel, "getAspectRatioContenEdgeMode", nodeScriptMethod, &EngineScriptMethod::s_getAspectRatioContenEdgeMode ); + pybind::def_functor( _kernel, "hasGameParam", nodeScriptMethod, &EngineScriptMethod::s_hasGameParam ); pybind::def_functor_kernel( _kernel, "getGameParamString", nodeScriptMethod, &EngineScriptMethod::s_getGameParamString ); pybind::def_functor_kernel( _kernel, "getGameParamUnicode", nodeScriptMethod, &EngineScriptMethod::s_getGameParamUnicode ); diff --git a/src/Interface/ApplicationInterface.h b/src/Interface/ApplicationInterface.h index c1734bd08c..edeae8b5b8 100644 --- a/src/Interface/ApplicationInterface.h +++ b/src/Interface/ApplicationInterface.h @@ -24,6 +24,13 @@ namespace Mengine { + ////////////////////////////////////////////////////////////////////////// + enum EContenEdgeMode + { + ECEM_NONE, + ECEM_HORIZONTAL_CONTENT_EDGE, + ECEM_VERTICAL_CONTENT_EDGE + }; ////////////////////////////////////////////////////////////////////////// class ApplicationInterface : public ServiceInterface @@ -96,6 +103,8 @@ namespace Mengine virtual const Resolution & getContentResolution() const = 0; virtual void getGameViewport( float * const _aspect, Viewport * const _viewport ) const = 0; + virtual EContenEdgeMode getAspectRatioContenEdgeMode() const = 0; + public: virtual bool createRenderWindow() = 0;