Skip to content

Commit

Permalink
add application getAspectRatioContenEdgeMode
Browse files Browse the repository at this point in the history
  • Loading branch information
irov committed Aug 17, 2023
1 parent 083ab1c commit cbd5af6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
51 changes: 38 additions & 13 deletions src/Engine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Engine/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/Frameworks/PythonFramework/EngineScriptEmbedding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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_<EContenEdgeMode>( _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 );
Expand Down
9 changes: 9 additions & 0 deletions src/Interface/ApplicationInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@

namespace Mengine
{
//////////////////////////////////////////////////////////////////////////
enum EContenEdgeMode
{
ECEM_NONE,
ECEM_HORIZONTAL_CONTENT_EDGE,
ECEM_VERTICAL_CONTENT_EDGE
};
//////////////////////////////////////////////////////////////////////////
class ApplicationInterface
: public ServiceInterface
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit cbd5af6

Please sign in to comment.